From 4bb28e1217afa16a6f40aab9514dcff88613c2d6 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 23 Nov 2021 14:52:00 +0800 Subject: [PATCH] Append unused argument to gorm statement --- clause/expression.go | 2 +- statement.go | 5 +++++ tests/postgres_test.go | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/clause/expression.go b/clause/expression.go index 893f6bcb..d0498306 100644 --- a/clause/expression.go +++ b/clause/expression.go @@ -70,7 +70,7 @@ func (expr Expr) Build(builder Builder) { if idx < len(expr.Vars) { for _, v := range expr.Vars[idx:] { - builder.AddVar(builder, v) + builder.AddVar(builder, sql.NamedArg{Value: v}) } } } 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/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 {