Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (d Dialector) Initialize(db *gorm.DB) (err error) {
callback.Create().Replace("gorm:create", Create)
callback.Delete().Replace("gorm:delete", Delete)
callback.Update().Replace("gorm:update", Update)
callback.Query().Before("gorm:query").Register("oracle:before_query", BeforeQuery)

maps.Copy(db.ClauseBuilders, OracleClauseBuilders())

Expand Down
68 changes: 68 additions & 0 deletions oracle/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
** Copyright (c) 2025 Oracle and/or its affiliates.
**
** The Universal Permissive License (UPL), Version 1.0
**
** Subject to the condition set forth below, permission is hereby granted to any
** person obtaining a copy of this software, associated documentation and/or data
** (collectively the "Software"), free of charge and under any and all copyright
** rights in the Software, and any and all patent rights owned or freely
** licensable by each licensor hereunder covering either (i) the unmodified
** Software as contributed to or provided by such licensor, or (ii) the Larger
** Works (as defined below), to deal in both
**
** (a) the Software, and
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
** one is included with the Software (each a "Larger Work" to which the Software
** is contributed by such licensors),
**
** without restriction, including without limitation the rights to copy, create
** derivative works of, display, perform, and distribute the Software and make,
** use, sell, offer for sale, import, export, have made, and have sold the
** Software and the Larger Work(s), and to sublicense the foregoing rights on
** either these or other terms.
**
** This license is subject to the following condition:
** The above copyright notice and either this complete permission notice or at
** a minimum a reference to the UPL must be included in all copies or
** substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
*/

package oracle

import (
"gorm.io/gorm"
"regexp"
"strings"
)

// Identifies the table name alias provided as
// "\"users\" \"u\"". Gorm already handles
//
// the other formats like "users u", "users AS u" etc.
var tableRegexp = regexp.MustCompile(`^"([^"]+)"\s+"([^"]+)"$`)

func BeforeQuery(db *gorm.DB) {
if db == nil || db.Statement == nil || db.Statement.TableExpr == nil {
return
}
name := db.Statement.TableExpr.SQL
if strings.Contains(name, " ") || strings.Contains(name, "`") {
if results := tableRegexp.FindStringSubmatch(name); len(results) == 3 {
if results[2] != "" {
db.Statement.Table = results[2]
} else {
db.Statement.Table = results[1]
}
}
}
return
}
3 changes: 1 addition & 2 deletions tests/generics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,14 @@ func TestGenericsCreateInBatches(t *testing.T) {
}

func TestGenericsExecAndUpdate(t *testing.T) {
t.Skip()
ctx := context.Background()

name := "GenericsExec"
if err := gorm.G[User](DB).Exec(ctx, "INSERT INTO \"users\"(\"name\") VALUES(?)", name); err != nil {
t.Fatalf("Exec insert failed: %v", err)
}

u, err := gorm.G[User](DB).Table("\"users\" u").Where("u.name = ?", name).First(ctx)
u, err := gorm.G[User](DB).Table("\"users\" \"u\"").Where("\"u\".\"name\" = ?", name).First(ctx)
if err != nil {
t.Fatalf("failed to find user, got error: %v", err)
} else if u.Name != name || u.ID == 0 {
Expand Down
4 changes: 2 additions & 2 deletions tests/passed-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ TestSupportedDialectorWithErrDuplicatedKey
TestSupportedDialectorWithErrForeignKeyViolated
#TestGenericsCreate
TestGenericsCreateInBatches
#TestGenericsExecAndUpdate
TestGenericsExecAndUpdate
TestGenericsRow
TestGenericsDelete
TestGenericsFindInBatches
Expand Down Expand Up @@ -222,7 +222,7 @@ TestPreloadManyToManyCallbacks
TestPreloadWithAssociations
TestNestedPreload
TestNestedPreloadForSlice
#TestPreloadWithConds
TestPreloadWithConds
TestNestedPreloadWithConds
TestPreloadEmptyData
TestPreloadGoroutine
Expand Down
11 changes: 5 additions & 6 deletions tests/preload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func TestNestedPreloadForSlice(t *testing.T) {
}

func TestPreloadWithConds(t *testing.T) {
t.Skip()
users := []User{
*GetUser("slice_nested_preload_1", Config{Account: true}),
*GetUser("slice_nested_preload_2", Config{Account: false}),
Expand All @@ -165,7 +164,7 @@ func TestPreloadWithConds(t *testing.T) {
}

var users2 []User
DB.Preload("Account", clause.Eq{Column: "account_number", Value: users[0].Account.AccountNumber}).Find(&users2, "id IN ?", userIDs)
DB.Preload("Account", clause.Eq{Column: "account_number", Value: users[0].Account.AccountNumber}).Find(&users2, "\"id\" IN ?", userIDs)
sort.Slice(users2, func(i, j int) bool {
return users2[i].ID < users2[j].ID
})
Expand All @@ -180,8 +179,8 @@ func TestPreloadWithConds(t *testing.T) {

var users3 []User
if err := DB.Preload("Account", func(tx *gorm.DB) *gorm.DB {
return tx.Table("accounts a").Select("a.*")
}).Find(&users3, "id IN ?", userIDs).Error; err != nil {
return tx.Table("\"accounts\" \"a\"").Select("\"a\".*")
}).Find(&users3, "\"id\" IN ?", userIDs).Error; err != nil {
t.Errorf("failed to query, got error %v", err)
}
sort.Slice(users3, func(i, j int) bool {
Expand All @@ -195,13 +194,13 @@ func TestPreloadWithConds(t *testing.T) {
var user4 User
DB.Delete(&users3[0].Account)

if err := DB.Preload(clause.Associations).Take(&user4, "id = ?", users3[0].ID).Error; err != nil || user4.Account.ID != 0 {
if err := DB.Preload(clause.Associations).Take(&user4, "\"id\" = ?", users3[0].ID).Error; err != nil || user4.Account.ID != 0 {
t.Errorf("failed to query, got error %v, account: %#v", err, user4.Account)
}

if err := DB.Preload(clause.Associations, func(tx *gorm.DB) *gorm.DB {
return tx.Unscoped()
}).Take(&user4, "id = ?", users3[0].ID).Error; err != nil || user4.Account.ID == 0 {
}).Take(&user4, "\"id\" = ?", users3[0].ID).Error; err != nil || user4.Account.ID == 0 {
t.Errorf("failed to query, got error %v, account: %#v", err, user4.Account)
}
}
Expand Down
Loading