From b8f33a42a469f5a4ab64bb8937ef7c8e5524af7e Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 23 Nov 2021 17:11:52 +0800 Subject: [PATCH] Add unused argument (#4871) * Append unused argument to gorm statement --- .github/workflows/reviewdog.yml | 4 +++- clause/expression.go | 6 ++++++ statement.go | 5 +++++ tests/go.mod | 4 +++- tests/postgres_test.go | 4 ++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index d55a4699..abfd57f3 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -6,6 +6,8 @@ jobs: runs-on: ubuntu-latest steps: - name: Check out code into the Go module directory - uses: actions/checkout@v1 + uses: actions/checkout@v2 - name: golangci-lint uses: reviewdog/action-golangci-lint@v2 + with: + golangci_lint_flags: '-E cyclop,unconvert,misspell,unparam,ineffassign,gocritic,prealloc,exportloopref,gosec' diff --git a/clause/expression.go b/clause/expression.go index e914b7b3..d0498306 100644 --- a/clause/expression.go +++ b/clause/expression.go @@ -67,6 +67,12 @@ func (expr Expr) Build(builder Builder) { 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 diff --git a/statement.go b/statement.go index 1bd6c2b2..453e485e 100644 --- a/statement.go +++ b/statement.go @@ -284,6 +284,11 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) [] 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 { return []clause.Expression{clause.Eq{Column: s, Value: args[0]}} } diff --git a/tests/go.mod b/tests/go.mod index 6502c179..7e5ea8a5 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -4,11 +4,13 @@ go 1.14 require ( 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/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/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/gorm v1.22.3 ) diff --git a/tests/postgres_test.go b/tests/postgres_test.go index 94077d1d..85671864 100644 --- a/tests/postgres_test.go +++ b/tests/postgres_test.go @@ -44,6 +44,10 @@ func TestPostgres(t *testing.T) { if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu" { 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 {