diff --git a/chainable_api.go b/chainable_api.go index dca12b08..58b9336f 100644 --- a/chainable_api.go +++ b/chainable_api.go @@ -93,7 +93,7 @@ func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) { } delete(tx.Statement.Clauses, "SELECT") case string: - if (strings.Contains(v, " ?") || strings.Contains(v, "(?")) && len(args) > 0 { + if strings.Count(v, "?") >= len(args) && len(args) > 0 { tx.Statement.AddClause(clause.Select{ Distinct: db.Statement.Distinct, Expression: clause.Expr{SQL: v, Vars: args}, diff --git a/tests/query_test.go b/tests/query_test.go index af8bbf07..f1234d0a 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -612,11 +612,15 @@ func TestSelect(t *testing.T) { t.Fatalf("Build Select with slice, but got %v", r.Statement.SQL.String()) } + // SELECT COALESCE(age,'42') FROM users; r = dryDB.Table("users").Select("COALESCE(age,?)", 42).Find(&User{}) if !regexp.MustCompile(`SELECT COALESCE\(age,.*\) FROM .*users.*`).MatchString(r.Statement.SQL.String()) { t.Fatalf("Build Select with func, but got %v", r.Statement.SQL.String()) } - // SELECT COALESCE(age,'42') FROM users; + + if _, err := DB.Table("users").Select("COALESCE(age,?)", "42").Rows(); err != nil { + t.Fatalf("Failed, got error: %v", err) + } r = dryDB.Select("u.*").Table("users as u").First(&User{}, user.ID) if !regexp.MustCompile(`SELECT u\.\* FROM .*users.*`).MatchString(r.Statement.SQL.String()) {