Append unused argument to gorm statement

This commit is contained in:
Jinzhu 2021-11-23 14:52:00 +08:00
parent ff26c82306
commit 4bb28e1217
3 changed files with 10 additions and 1 deletions

View File

@ -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})
}
}
}

View File

@ -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]}}
}

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" {
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 {