Skip to content

Commit e755bd2

Browse files
authored
Merge pull request #4 from bramca/master
Update gorm version and fix wildcards on non strings
2 parents 7e2f9c9 + 7e5c169 commit e755bd2

File tree

5 files changed

+205
-90
lines changed

5 files changed

+205
-90
lines changed

.github/workflows/test.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
go-version: [ '1.18', '1.19' ]
10+
go-version: [ '1.23' ]
1111
steps:
1212
- uses: actions/checkout@v3
1313

@@ -24,4 +24,4 @@ jobs:
2424
uses: actions/upload-artifact@v3
2525
with:
2626
name: Go-results-${{ matrix.go-version }}
27-
path: TestResults-${{ matrix.go-version }}.json
27+
path: TestResults-${{ matrix.go-version }}.json

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module github.com/survivorbat/gorm-like
22

3-
go 1.20
3+
go 1.23.1
44

55
require (
66
github.com/google/uuid v1.3.0
77
github.com/ing-bank/gormtestutil v0.0.0
88
github.com/stretchr/testify v1.8.0
99
gorm.io/driver/sqlite v1.4.3
10-
gorm.io/gorm v1.24.2
10+
gorm.io/gorm v1.25.12
1111
)
1212

1313
require (
@@ -17,5 +17,6 @@ require (
1717
github.com/kr/text v0.2.0 // indirect
1818
github.com/mattn/go-sqlite3 v1.14.15 // indirect
1919
github.com/pmezard/go-difflib v1.0.0 // indirect
20+
golang.org/x/text v0.19.0 // indirect
2021
gopkg.in/yaml.v3 v3.0.1 // indirect
2122
)

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,30 @@ github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/
1212
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
1313
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
1414
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
15+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
1516
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1617
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1718
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
1819
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
1920
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2021
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2122
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
23+
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
2224
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2325
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
2426
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2527
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
2628
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
29+
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
30+
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
2731
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2832
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
33+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
2934
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3035
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3136
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3237
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU=
3338
gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
3439
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
35-
gorm.io/gorm v1.24.2 h1:9wR6CFD+G8nOusLdvkZelOEhpJVwwHzpQOUM+REd6U0=
36-
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
40+
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
41+
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=

query.go

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,17 @@ import (
1010

1111
const tagName = "gormlike"
1212

13-
//nolint:gocognit,cyclop // Acceptable
14-
func (d *gormLike) queryCallback(db *gorm.DB) {
15-
// If we only want to like queries that are explicitly set to true, we back out early if anything's amiss
16-
settingValue, settingOk := db.Get(tagName)
17-
if d.conditionalSetting && !settingOk {
18-
return
19-
}
20-
21-
if settingOk {
22-
if boolValue, _ := settingValue.(bool); !boolValue {
23-
return
24-
}
25-
}
26-
27-
exp, settingOk := db.Statement.Clauses["WHERE"].Expression.(clause.Where)
28-
if !settingOk {
29-
return
30-
}
31-
32-
for index, cond := range exp.Exprs {
13+
func (d *gormLike) replaceExpressions(db *gorm.DB, expressions []clause.Expression) []clause.Expression {
14+
for index, cond := range expressions {
3315
switch cond := cond.(type) {
16+
case clause.AndConditions:
17+
// Recursively go through the expressions of AndConditions
18+
cond.Exprs = d.replaceExpressions(db, cond.Exprs)
19+
expressions[index] = cond
20+
case clause.OrConditions:
21+
// Recursively go through the expressions of OrConditions
22+
cond.Exprs = d.replaceExpressions(db, cond.Exprs)
23+
expressions[index] = cond
3424
case clause.Eq:
3525
columnName, columnOk := cond.Column.(string)
3626
if !columnOk {
@@ -64,13 +54,13 @@ func (d *gormLike) queryCallback(db *gorm.DB) {
6454
continue
6555
}
6656

67-
condition := fmt.Sprintf("%s LIKE ?", cond.Column)
57+
condition := fmt.Sprintf("CAST(%s as varchar) LIKE ?", cond.Column)
6858

6959
if d.replaceCharacter != "" {
7060
value = strings.ReplaceAll(value, d.replaceCharacter, "%")
7161
}
7262

73-
exp.Exprs[index] = db.Session(&gorm.Session{NewDB: true}).Where(condition, value).Statement.Clauses["WHERE"].Expression
63+
expressions[index] = db.Session(&gorm.Session{NewDB: true}).Where(condition, value).Statement.Clauses["WHERE"].Expression
7464
case clause.IN:
7565
columnName, columnOk := cond.Column.(string)
7666
if !columnOk {
@@ -95,7 +85,6 @@ func (d *gormLike) queryCallback(db *gorm.DB) {
9585
}
9686

9787
var likeCounter int
98-
var useOr bool
9988

10089
query := db.Session(&gorm.Session{NewDB: true})
10190

@@ -109,7 +98,7 @@ func (d *gormLike) queryCallback(db *gorm.DB) {
10998

11099
// If there are no % AND there aren't ony replaceable characters, just skip it because it's a normal query
111100
if strings.Contains(value, "%") || (d.replaceCharacter != "" && strings.Contains(value, d.replaceCharacter)) {
112-
condition = fmt.Sprintf("%s LIKE ?", cond.Column)
101+
condition = fmt.Sprintf("CAST(%s as varchar) LIKE ?", cond.Column)
113102

114103
if d.replaceCharacter != "" {
115104
value = strings.ReplaceAll(value, d.replaceCharacter, "%")
@@ -118,22 +107,45 @@ func (d *gormLike) queryCallback(db *gorm.DB) {
118107
likeCounter++
119108
}
120109

121-
if useOr {
122-
query = query.Or(condition, value)
123-
124-
continue
125-
}
126-
127-
query = query.Where(condition, value)
128-
useOr = true
110+
query = query.Or(condition, value)
129111
}
130112

131113
// Don't alter the query if it isn't necessary
132114
if likeCounter == 0 {
133115
continue
134116
}
135117

136-
exp.Exprs[index] = db.Session(&gorm.Session{NewDB: true}).Where(query).Statement.Clauses["WHERE"].Expression
118+
// This feels a bit like a dirty hack
119+
// but otherwise the generated query would not be correct in case of an AND condition between multiple OR conditions
120+
// e.g. without this -> x = .. OR x = .. AND y = .. OR y = .. (no brackets around the OR conditions mess up the query)
121+
// e.g. with this -> (x = .. OR x = ..) AND (y = .. OR y = ..)
122+
var newExpression clause.OrConditions
123+
newExpression.Exprs = query.Statement.Clauses["WHERE"].Expression.(clause.Where).Exprs
124+
125+
expressions[index] = newExpression
126+
}
127+
}
128+
129+
return expressions
130+
}
131+
132+
func (d *gormLike) queryCallback(db *gorm.DB) {
133+
// If we only want to like queries that are explicitly set to true, we back out early if anything's amiss
134+
settingValue, settingOk := db.Get(tagName)
135+
if d.conditionalSetting && !settingOk {
136+
return
137+
}
138+
139+
if settingOk {
140+
if boolValue, _ := settingValue.(bool); !boolValue {
141+
return
137142
}
138143
}
144+
145+
exp, settingOk := db.Statement.Clauses["WHERE"].Expression.(clause.Where)
146+
if !settingOk {
147+
return
148+
}
149+
150+
exp.Exprs = d.replaceExpressions(db, exp.Exprs)
139151
}

0 commit comments

Comments
 (0)