Skip to content

Commit baadd6e

Browse files
Merge pull request #26 from oracle-samples/prefix-fix
Fixes the alias names not being identified when quoted.
2 parents 50c04f5 + ef5eb5a commit baadd6e

File tree

4 files changed

+80
-8
lines changed

4 files changed

+80
-8
lines changed

oracle/oracle.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func (d Dialector) Initialize(db *gorm.DB) (err error) {
102102
callback.Create().Replace("gorm:create", Create)
103103
callback.Delete().Replace("gorm:delete", Delete)
104104
callback.Update().Replace("gorm:update", Update)
105+
callback.Query().Before("gorm:query").Register("oracle:before_query", BeforeQuery)
105106

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

oracle/query.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
** Copyright (c) 2025 Oracle and/or its affiliates.
3+
**
4+
** The Universal Permissive License (UPL), Version 1.0
5+
**
6+
** Subject to the condition set forth below, permission is hereby granted to any
7+
** person obtaining a copy of this software, associated documentation and/or data
8+
** (collectively the "Software"), free of charge and under any and all copyright
9+
** rights in the Software, and any and all patent rights owned or freely
10+
** licensable by each licensor hereunder covering either (i) the unmodified
11+
** Software as contributed to or provided by such licensor, or (ii) the Larger
12+
** Works (as defined below), to deal in both
13+
**
14+
** (a) the Software, and
15+
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
** one is included with the Software (each a "Larger Work" to which the Software
17+
** is contributed by such licensors),
18+
**
19+
** without restriction, including without limitation the rights to copy, create
20+
** derivative works of, display, perform, and distribute the Software and make,
21+
** use, sell, offer for sale, import, export, have made, and have sold the
22+
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
** either these or other terms.
24+
**
25+
** This license is subject to the following condition:
26+
** The above copyright notice and either this complete permission notice or at
27+
** a minimum a reference to the UPL must be included in all copies or
28+
** substantial portions of the Software.
29+
**
30+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
** SOFTWARE.
37+
*/
38+
39+
package oracle
40+
41+
import (
42+
"gorm.io/gorm"
43+
"regexp"
44+
"strings"
45+
)
46+
47+
// Identifies the table name alias provided as
48+
// "\"users\" \"u\"" and "\"users\" u". Gorm already handles
49+
// the other formats like "users u", "users AS u" etc.
50+
var tableRegexp = regexp.MustCompile(`^"(\w+)"\s+"?(\w+)"?$`)
51+
52+
func BeforeQuery(db *gorm.DB) {
53+
if db == nil || db.Statement == nil || db.Statement.TableExpr == nil {
54+
return
55+
}
56+
name := db.Statement.TableExpr.SQL
57+
if strings.Contains(name, " ") || strings.Contains(name, "`") {
58+
if results := tableRegexp.FindStringSubmatch(name); len(results) == 3 {
59+
if results[2] != "" {
60+
db.Statement.Table = results[2]
61+
} else {
62+
db.Statement.Table = results[1]
63+
}
64+
}
65+
}
66+
return
67+
}

tests/generics_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,17 @@ func TestGenericsCreateInBatches(t *testing.T) {
151151
}
152152

153153
func TestGenericsExecAndUpdate(t *testing.T) {
154-
t.Skip()
155154
ctx := context.Background()
156155

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

162-
u, err := gorm.G[User](DB).Table("\"users\" u").Where("u.name = ?", name).First(ctx)
161+
// todo: uncomment the below line, once the alias quoting issue is resolved.
162+
// Gorm issue track: https://github.com/oracle-samples/gorm-oracle/issues/36
163+
// u, err := gorm.G[User](DB).Table("\"users\" u").Where("u.\"name\" = ?", name).First(ctx)
164+
u, err := gorm.G[User](DB).Table("users").Where("\"name\" = ?", name).First(ctx)
163165
if err != nil {
164166
t.Fatalf("failed to find user, got error: %v", err)
165167
} else if u.Name != name || u.ID == 0 {

tests/preload_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ func TestNestedPreloadForSlice(t *testing.T) {
148148
}
149149

150150
func TestPreloadWithConds(t *testing.T) {
151-
t.Skip()
152151
users := []User{
153152
*GetUser("slice_nested_preload_1", Config{Account: true}),
154153
*GetUser("slice_nested_preload_2", Config{Account: false}),
@@ -165,7 +164,7 @@ func TestPreloadWithConds(t *testing.T) {
165164
}
166165

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

181180
var users3 []User
182181
if err := DB.Preload("Account", func(tx *gorm.DB) *gorm.DB {
183-
return tx.Table("accounts a").Select("a.*")
184-
}).Find(&users3, "id IN ?", userIDs).Error; err != nil {
182+
// todo: uncomment the below line, once the alias quoting issue is resolved.
183+
// Gorm issue track: https://github.com/oracle-samples/gorm-oracle/issues/36
184+
// return tx.Table("\"accounts\" a").Select("a.*")
185+
return tx.Table("accounts").Select("*")
186+
}).Find(&users3, "\"id\" IN ?", userIDs).Error; err != nil {
185187
t.Errorf("failed to query, got error %v", err)
186188
}
187189
sort.Slice(users3, func(i, j int) bool {
@@ -195,13 +197,13 @@ func TestPreloadWithConds(t *testing.T) {
195197
var user4 User
196198
DB.Delete(&users3[0].Account)
197199

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

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

0 commit comments

Comments
 (0)