From 52287359153b5788d95960c963f74bebcdea88c7 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 24 Sep 2020 15:00:13 +0800 Subject: [PATCH] Don't build IN condition if value implemented Valuer interface, #3517 --- statement.go | 16 +++++++++++----- tests/query_test.go | 5 +++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/statement.go b/statement.go index ee80f8cd..38d35926 100644 --- a/statement.go +++ b/statement.go @@ -299,12 +299,18 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) (c reflectValue := reflect.Indirect(reflect.ValueOf(v[key])) switch reflectValue.Kind() { case reflect.Slice, reflect.Array: - values := make([]interface{}, reflectValue.Len()) - for i := 0; i < reflectValue.Len(); i++ { - values[i] = reflectValue.Index(i).Interface() - } + if _, ok := v[key].(driver.Valuer); ok { + conds = append(conds, clause.Eq{Column: key, Value: v[key]}) + } else if _, ok := v[key].(Valuer); ok { + conds = append(conds, clause.Eq{Column: key, Value: v[key]}) + } else { + values := make([]interface{}, reflectValue.Len()) + for i := 0; i < reflectValue.Len(); i++ { + values[i] = reflectValue.Index(i).Interface() + } - conds = append(conds, clause.IN{Column: key, Values: values}) + conds = append(conds, clause.IN{Column: key, Values: values}) + } default: conds = append(conds, clause.Eq{Column: key, Value: v[key]}) } diff --git a/tests/query_test.go b/tests/query_test.go index d3bcbdbe..9c9ad9f2 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -345,6 +345,11 @@ func TestNot(t *testing.T) { t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) } + result = dryDB.Where(map[string]interface{}{"name": []string{"jinzhu", "jinzhu 2"}}).Find(&User{}) + if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* IN \\(.+,.+\\)").MatchString(result.Statement.SQL.String()) { + t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) + } + result = dryDB.Not("name = ?", "jinzhu").Find(&User{}) if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE NOT.*name.* = .+").MatchString(result.Statement.SQL.String()) { t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())