Skip to content
Merged
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
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module github.com/survivorbat/gorm-case

go 1.23.0

toolchain go1.24.1
go 1.24.1

require (
github.com/ing-bank/gormtestutil v0.0.0
github.com/stretchr/testify v1.8.0
gorm.io/driver/sqlite v1.4.3
gorm.io/gorm v1.25.12
gorm.io/gorm v1.30.0
)

require (
Expand All @@ -18,6 +16,6 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/text v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand All @@ -35,5 +35,5 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU=
gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
27 changes: 13 additions & 14 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func (d *gormCase) queryCallback(db *gorm.DB) {
func (d *gormCase) handleExpression(db *gorm.DB, cond clause.Expression) clause.Expression { //nolint:gocognit,cyclop,ireturn // accepted for enabling linter
switch cond := cond.(type) {
case clause.Eq:
if d.conditionalTag {
columnName, ok := cond.Column.(string)
if !ok {
return nil
}
column, ok := cond.Column.(clause.Column)
if !ok {
return nil
}

if d.conditionalTag {
table := db.Statement.Schema.Table + "."
columnName = strings.TrimPrefix(columnName, table)
columnName := strings.TrimPrefix(column.Name, table)
value := db.Statement.Schema.FieldsByDBName[columnName].Tag.Get(tagName)

// Ignore if there's no valid tag value
Expand All @@ -59,18 +59,17 @@ func (d *gormCase) handleExpression(db *gorm.DB, cond clause.Expression) clause.
return nil
}

condition := fmt.Sprintf("UPPER(%s) = UPPER(?)", cond.Column)
condition := fmt.Sprintf("UPPER(%s) = UPPER(?)", column.Name)

return db.Session(&gorm.Session{NewDB: true}).Where(condition, value).Statement.Clauses["WHERE"].Expression
case clause.IN:
column, ok := cond.Column.(clause.Column)
if !ok {
return nil
}
if d.conditionalTag {
columnName, ok := cond.Column.(string)
if !ok {
return nil
}

table := db.Statement.Schema.Table + "."
columnName = strings.TrimPrefix(columnName, table)
columnName := strings.TrimPrefix(column.Name, table)
value := db.Statement.Schema.FieldsByDBName[columnName].Tag.Get(tagName)

// Ignore if there's no valid tag value
Expand All @@ -92,7 +91,7 @@ func (d *gormCase) handleExpression(db *gorm.DB, cond clause.Expression) clause.

caseCounter++

condition := fmt.Sprintf("UPPER(%s) = UPPER(?)", cond.Column)
condition := fmt.Sprintf("UPPER(%s) = UPPER(?)", column.Name)

if useOr {
query = query.Or(condition, value)
Expand Down
Loading