Skip to content

Commit b1c789f

Browse files
authored
Merge pull request #5 from bramca/master
fix faulty or clause
2 parents e755bd2 + 8646a39 commit b1c789f

File tree

8 files changed

+96
-133
lines changed

8 files changed

+96
-133
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.23' ]
10+
go-version: [ '1.24' ]
1111
steps:
1212
- uses: actions/checkout@v3
1313

@@ -21,7 +21,7 @@ jobs:
2121
run: go test -json > TestResults-${{ matrix.go-version }}.json
2222

2323
- name: Upload Go test results for ${{ matrix.go-version }}
24-
uses: actions/upload-artifact@v3
24+
uses: actions/upload-artifact@v4
2525
with:
2626
name: Go-results-${{ matrix.go-version }}
2727
path: TestResults-${{ matrix.go-version }}.json

.golangci.yaml

Lines changed: 53 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,58 @@
1-
linters-settings:
2-
gocritic:
3-
disabled-checks:
4-
- "paramTypeCombine"
5-
enabled-tags:
6-
- "performance"
7-
- "style"
8-
- "diagnostic"
1+
version: "2"
92
linters:
3+
default: all
4+
disable:
5+
- depguard
6+
- exhaustruct
7+
- funlen
8+
- gochecknoglobals
9+
- godot
10+
- godox
11+
- lll
12+
- testpackage
13+
- varnamelen
14+
- wrapcheck
15+
- wsl
16+
settings:
17+
gocritic:
18+
disabled-checks:
19+
- paramTypeCombine
20+
- unnamedResult
21+
enabled-tags:
22+
- performance
23+
- style
24+
- diagnostic
25+
govet:
26+
disable:
27+
- fieldalignment
28+
enable-all: true
29+
nlreturn:
30+
block-size: 3
31+
exclusions:
32+
generated: lax
33+
presets:
34+
- comments
35+
- common-false-positives
36+
- legacy
37+
- std-error-handling
38+
rules:
39+
- linters:
40+
- err113
41+
- goconst
42+
path: (.+)_test.go
43+
paths:
44+
- third_party$
45+
- builtin$
46+
- examples$
47+
formatters:
1048
enable:
11-
- asasalint
12-
- asciicheck
13-
- bidichk
14-
- bodyclose
15-
- containedctx
16-
- contextcheck
17-
- cyclop
18-
- decorder
19-
- dogsled
20-
- dupword
21-
- durationcheck
22-
- errcheck
23-
- errname
24-
- errorlint
25-
- execinquery
26-
- exhaustive
27-
- exportloopref
28-
- forbidigo
29-
- ginkgolinter
30-
- gocheckcompilerdirectives
31-
- gochecknoinits
32-
- gocognit
33-
- goconst
34-
- gocritic
35-
- gocyclo
36-
- goerr113
49+
- gci
3750
- gofmt
3851
- gofumpt
39-
- goheader
4052
- goimports
41-
- gomoddirectives
42-
- gomodguard
43-
- goprintffuncname
44-
- gosec
45-
- gosimple
46-
- gosmopolitan
47-
- govet
48-
- grouper
49-
- ifshort
50-
- importas
51-
- ineffassign
52-
- interfacebloat
53-
- interfacer
54-
- ireturn
55-
- loggercheck
56-
- maintidx
57-
- makezero
58-
- mirror
59-
- misspell
60-
- musttag
61-
- nakedret
62-
- nestif
63-
- nilerr
64-
- nilnil
65-
- nlreturn
66-
- nonamedreturns
67-
- nosprintfhostport
68-
- paralleltest
69-
- prealloc
70-
- predeclared
71-
- promlinter
72-
- reassign
73-
- rowserrcheck
74-
- sqlclosecheck
75-
- staticcheck
76-
- tenv
77-
- thelper
78-
- tparallel
79-
- typecheck
80-
- unconvert
81-
- unparam
82-
- unused
83-
- usestdlibvars
84-
- wastedassign
85-
- whitespace
86-
- zerologlint
53+
exclusions:
54+
generated: lax
55+
paths:
56+
- third_party$
57+
- builtin$
58+
- examples$

examples_test.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

go.mod

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

3-
go 1.23.1
3+
go 1.24.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
9-
gorm.io/driver/sqlite v1.4.3
10-
gorm.io/gorm v1.25.12
9+
gorm.io/gorm v1.26.0
1110
)
1211

1312
require (
@@ -17,6 +16,7 @@ require (
1716
github.com/kr/text v0.2.0 // indirect
1817
github.com/mattn/go-sqlite3 v1.14.15 // indirect
1918
github.com/pmezard/go-difflib v1.0.0 // indirect
20-
golang.org/x/text v0.19.0 // indirect
19+
golang.org/x/text v0.24.0 // indirect
2120
gopkg.in/yaml.v3 v3.0.1 // indirect
21+
gorm.io/driver/sqlite v1.4.3 // indirect
2222
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
2626
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2727
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
2828
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=
29+
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
30+
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
3131
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3232
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
3333
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
@@ -37,5 +37,5 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3737
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU=
3838
gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
3939
gorm.io/gorm v1.24.0/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=
40+
gorm.io/gorm v1.26.0 h1:9lqQVPG5aNNS6AyHdRiwScAVnXHg/L/Srzx55G5fOgs=
41+
gorm.io/gorm v1.26.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=

plugin_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/ing-bank/gormtestutil"
77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
func TestDeepGorm_Name_ReturnsExpectedName(t *testing.T) {
@@ -29,6 +30,6 @@ func TestDeepGorm_Initialize_RegistersCallback(t *testing.T) {
2930
err := plugin.Initialize(db)
3031

3132
// Assert
32-
assert.NoError(t, err)
33+
require.NoError(t, err)
3334
assert.NotNil(t, db.Callback().Query().Get("gormlike:query"))
3435
}

query.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
const tagName = "gormlike"
1212

13+
//nolint:gocognit,cyclop // is a complex, recursive function
1314
func (d *gormLike) replaceExpressions(db *gorm.DB, expressions []clause.Expression) []clause.Expression {
1415
for index, cond := range expressions {
1516
switch cond := cond.(type) {
@@ -49,8 +50,8 @@ func (d *gormLike) replaceExpressions(db *gorm.DB, expressions []clause.Expressi
4950
continue
5051
}
5152

52-
// If there are no % AND there aren't ony replaceable characters, just skip it because it's a normal query
53-
if !strings.Contains(value, "%") && !(d.replaceCharacter != "" && strings.Contains(value, d.replaceCharacter)) {
53+
// If there are no % AND there aren't only replaceable characters, just skip it because it's a normal query
54+
if !strings.Contains(value, "%") && (d.replaceCharacter == "" || !strings.Contains(value, d.replaceCharacter)) {
5455
continue
5556
}
5657

@@ -89,14 +90,14 @@ func (d *gormLike) replaceExpressions(db *gorm.DB, expressions []clause.Expressi
8990
query := db.Session(&gorm.Session{NewDB: true})
9091

9192
for _, value := range cond.Values {
92-
value, ok := value.(string)
93-
if !ok {
93+
value, valueOk := value.(string)
94+
if !valueOk {
9495
continue
9596
}
9697

9798
condition := fmt.Sprintf("%s = ?", cond.Column)
9899

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

@@ -115,14 +116,22 @@ func (d *gormLike) replaceExpressions(db *gorm.DB, expressions []clause.Expressi
115116
continue
116117
}
117118

119+
whereExpression, ok := query.Statement.Clauses["WHERE"].Expression.(clause.Where)
120+
121+
if ok {
122+
expressions[index] = whereExpression
123+
}
124+
118125
// This feels a bit like a dirty hack
119126
// but otherwise the generated query would not be correct in case of an AND condition between multiple OR conditions
120127
// e.g. without this -> x = .. OR x = .. AND y = .. OR y = .. (no brackets around the OR conditions mess up the query)
121128
// 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
129+
if len(whereExpression.Exprs) > 1 {
130+
var newExpression clause.OrConditions
131+
newExpression.Exprs = whereExpression.Exprs
124132

125-
expressions[index] = newExpression
133+
expressions[index] = newExpression
134+
}
126135
}
127136
}
128137

0 commit comments

Comments
 (0)