Add unused argument (#4871)

* Append unused argument to gorm statement
This commit is contained in:
Jinzhu 2021-11-23 17:11:52 +08:00 committed by GitHub
parent cff7845e58
commit b8f33a42a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 2 deletions

View File

@ -6,6 +6,8 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Check out code into the Go module directory - name: Check out code into the Go module directory
uses: actions/checkout@v1 uses: actions/checkout@v2
- name: golangci-lint - name: golangci-lint
uses: reviewdog/action-golangci-lint@v2 uses: reviewdog/action-golangci-lint@v2
with:
golangci_lint_flags: '-E cyclop,unconvert,misspell,unparam,ineffassign,gocritic,prealloc,exportloopref,gosec'

View File

@ -67,6 +67,12 @@ func (expr Expr) Build(builder Builder) {
builder.WriteByte(v) builder.WriteByte(v)
} }
} }
if idx < len(expr.Vars) {
for _, v := range expr.Vars[idx:] {
builder.AddVar(builder, sql.NamedArg{Value: v})
}
}
} }
// NamedExpr raw expression for named expr // NamedExpr raw expression for named expr

View File

@ -284,6 +284,11 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
return []clause.Expression{clause.NamedExpr{SQL: s, Vars: args}} return []clause.Expression{clause.NamedExpr{SQL: s, Vars: args}}
} }
if strings.Contains(strings.TrimSpace(s), " ") {
// looks like a where condition
return []clause.Expression{clause.Expr{SQL: s, Vars: args}}
}
if len(args) == 1 { if len(args) == 1 {
return []clause.Expression{clause.Eq{Column: s, Value: args[0]}} return []clause.Expression{clause.Eq{Column: s, Value: args[0]}}
} }

View File

@ -4,11 +4,13 @@ go 1.14
require ( require (
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/jackc/pgx/v4 v4.14.0 // indirect
github.com/jinzhu/now v1.1.3 github.com/jinzhu/now v1.1.3
github.com/lib/pq v1.10.4 github.com/lib/pq v1.10.4
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
gorm.io/driver/mysql v1.2.0 gorm.io/driver/mysql v1.2.0
gorm.io/driver/postgres v1.2.2 gorm.io/driver/postgres v1.2.2
gorm.io/driver/sqlite v1.2.4 gorm.io/driver/sqlite v1.2.6
gorm.io/driver/sqlserver v1.2.1 gorm.io/driver/sqlserver v1.2.1
gorm.io/gorm v1.22.3 gorm.io/gorm v1.22.3
) )

View File

@ -44,6 +44,10 @@ func TestPostgres(t *testing.T) {
if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu" { if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu" {
t.Errorf("No error should happen, but got %v", err) t.Errorf("No error should happen, but got %v", err)
} }
if err := DB.Where("id = $1", harumph.ID).First(&Harumph{}).Error; err != nil || harumph.Name != "jinzhu" {
t.Errorf("No error should happen, but got %v", err)
}
} }
type Post struct { type Post struct {