From e8c14bd1b3688d3901a11257da0ac61df5b3bd4f Mon Sep 17 00:00:00 2001 From: Vladislav Fursov Date: Tue, 9 Aug 2016 14:28:43 +0900 Subject: [PATCH 1/5] Fixed a bug when joining multiple tables with the same fields and where on the same field. --- main_test.go | 6 ++++ scope.go | 89 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 82 insertions(+), 13 deletions(-) diff --git a/main_test.go b/main_test.go index 315dd95e..e08eb6e3 100644 --- a/main_test.go +++ b/main_test.go @@ -538,6 +538,12 @@ func TestJoins(t *testing.T) { if len(users4) != 0 { t.Errorf("should find no user when searching with unexisting credit card") } + + var users5 []User + db5 := DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins("join credit_cards on credit_cards.user_id = users.id AND credit_cards.number = ?", "411111111111").Where(User{Id:1}).Where(Email{Id:1}).Not(Email{Id:10}).First(&users5) + if db5.Error != nil { + t.Errorf("Should not raise error for join where identical fields in different tables. Error: %s", db5.Error.Error()) + } } func TestJoinsWithSelect(t *testing.T) { diff --git a/scope.go b/scope.go index 974ff035..a8690bc8 100644 --- a/scope.go +++ b/scope.go @@ -495,7 +495,12 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) { } func (scope *Scope) primaryCondition(value interface{}) string { - return fmt.Sprintf("(%v = %v)", scope.Quote(scope.PrimaryKey()), value) + format := "(%v = %v)" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v = %%v)", scope.New(value).QuotedTableName()) + } + + return fmt.Sprintf(format, scope.Quote(scope.PrimaryKey()), value) } func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str string) { @@ -510,23 +515,44 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, sql.NullInt64: return scope.primaryCondition(scope.AddToVars(value)) case []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []string, []interface{}: - str = fmt.Sprintf("(%v IN (?))", scope.Quote(scope.PrimaryKey())) + format := "(%v IN (?))" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v IN (?))", scope.New(value).QuotedTableName()) + } + + str = fmt.Sprintf(format, scope.Quote(scope.PrimaryKey())) clause["args"] = []interface{}{value} case map[string]interface{}: var sqls []string for key, value := range value { if value != nil { - sqls = append(sqls, fmt.Sprintf("(%v = %v)", scope.Quote(key), scope.AddToVars(value))) + format := "(%v = %v)" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v = %%v)", scope.New(value).QuotedTableName()) + } + + sqls = append(sqls, fmt.Sprintf(format, scope.Quote(key), scope.AddToVars(value))) } else { - sqls = append(sqls, fmt.Sprintf("(%v IS NULL)", scope.Quote(key))) + format := "(%v IS NULL)" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v IS NULL)", scope.New(value).QuotedTableName()) + } + + sqls = append(sqls, fmt.Sprintf(format, scope.Quote(key))) } } return strings.Join(sqls, " AND ") case interface{}: var sqls []string + for _, field := range scope.New(value).Fields() { if !field.IsIgnored && !field.IsBlank { - sqls = append(sqls, fmt.Sprintf("(%v = %v)", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) + format := "(%v = %v)" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v = %%v)", scope.New(value).QuotedTableName()) + } + + sqls = append(sqls, fmt.Sprintf(format, scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) } } return strings.Join(sqls, " AND ") @@ -566,20 +592,42 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string case string: // is number if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) { + format := "(%v <> %v)" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v <> %%v)", scope.New(value).QuotedTableName()) + } + id, _ := strconv.Atoi(value) - return fmt.Sprintf("(%v <> %v)", scope.Quote(primaryKey), id) + return fmt.Sprintf(format, scope.Quote(primaryKey), id) } else if regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS|IN) ").MatchString(value) { str = fmt.Sprintf(" NOT (%v) ", value) notEqualSQL = fmt.Sprintf("NOT (%v)", value) } else { - str = fmt.Sprintf("(%v NOT IN (?))", scope.Quote(value)) - notEqualSQL = fmt.Sprintf("(%v <> ?)", scope.Quote(value)) + formatStr := "(%v NOT IN (?))" + formatNotEqualSQL := "(%v <> ?)" + if len(scope.Search.joinConditions) > 0 { + formatStr = fmt.Sprintf("(%v.%%v NOT IN (?))", scope.New(value).QuotedTableName()) + formatNotEqualSQL = fmt.Sprintf("(%v.%%v <> ?)", scope.New(value).QuotedTableName()) + } + + str = fmt.Sprintf(formatStr, scope.Quote(value)) + notEqualSQL = fmt.Sprintf(formatNotEqualSQL, scope.Quote(value)) } case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, sql.NullInt64: - return fmt.Sprintf("(%v <> %v)", scope.Quote(primaryKey), value) + format := "(%v <> %v)" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v <> %%v)", scope.New(value).QuotedTableName()) + } + + return fmt.Sprintf(format, scope.Quote(primaryKey), value) case []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []string: if reflect.ValueOf(value).Len() > 0 { - str = fmt.Sprintf("(%v NOT IN (?))", scope.Quote(primaryKey)) + format := "(%v NOT IN (?))" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v NOT IN (?))", scope.New(value).QuotedTableName()) + } + + str = fmt.Sprintf(format, scope.Quote(primaryKey)) clause["args"] = []interface{}{value} } return "" @@ -587,9 +635,19 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string var sqls []string for key, value := range value { if value != nil { - sqls = append(sqls, fmt.Sprintf("(%v <> %v)", scope.Quote(key), scope.AddToVars(value))) + format := "(%v <> %v)" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v <> %%v)", scope.New(value).QuotedTableName()) + } + + sqls = append(sqls, fmt.Sprintf(format, scope.Quote(key), scope.AddToVars(value))) } else { - sqls = append(sqls, fmt.Sprintf("(%v IS NOT NULL)", scope.Quote(key))) + format := "(%v IS NOT NULL)" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v IS NOT NULL)", scope.New(value).QuotedTableName()) + } + + sqls = append(sqls, fmt.Sprintf(format, scope.Quote(key))) } } return strings.Join(sqls, " AND ") @@ -597,7 +655,12 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string var sqls []string for _, field := range scope.New(value).Fields() { if !field.IsBlank { - sqls = append(sqls, fmt.Sprintf("(%v <> %v)", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) + format := "(%v <> %v)" + if len(scope.Search.joinConditions) > 0 { + format = fmt.Sprintf("(%v.%%v <> %%v)", scope.New(value).QuotedTableName()) + } + + sqls = append(sqls, fmt.Sprintf(format, scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) } } return strings.Join(sqls, " AND ") From 9cdda4e8ee9d7e00c682a9bf6f6677868870e2e5 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 11 Jul 2016 21:37:44 +0800 Subject: [PATCH 2/5] Expose current database name API --- dialect.go | 3 +++ dialect_common.go | 8 ++++---- dialect_mysql.go | 4 ++-- dialect_postgres.go | 2 +- dialect_sqlite3.go | 2 +- dialects/mssql/mssql.go | 6 +++--- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/dialect.go b/dialect.go index 96bf4a2c..facde0d0 100644 --- a/dialect.go +++ b/dialect.go @@ -43,6 +43,9 @@ type Dialect interface { // BuildForeignKeyName returns a foreign key name for the given table, field and reference BuildForeignKeyName(tableName, field, dest string) string + + // CurrentDatabase return current database name + CurrentDatabase() string } var dialectsMap = map[string]Dialect{} diff --git a/dialect_common.go b/dialect_common.go index 8e66110f..5b5682c5 100644 --- a/dialect_common.go +++ b/dialect_common.go @@ -93,7 +93,7 @@ func (commonDialect) DataTypeOf(field *StructField) string { func (s commonDialect) HasIndex(tableName string, indexName string) bool { var count int - s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = ? AND table_name = ? AND index_name = ?", s.currentDatabase(), tableName, indexName).Scan(&count) + s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = ? AND table_name = ? AND index_name = ?", s.CurrentDatabase(), tableName, indexName).Scan(&count) return count > 0 } @@ -108,17 +108,17 @@ func (s commonDialect) HasForeignKey(tableName string, foreignKeyName string) bo func (s commonDialect) HasTable(tableName string) bool { var count int - s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = ? AND table_name = ?", s.currentDatabase(), tableName).Scan(&count) + s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = ? AND table_name = ?", s.CurrentDatabase(), tableName).Scan(&count) return count > 0 } func (s commonDialect) HasColumn(tableName string, columnName string) bool { var count int - s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = ? AND table_name = ? AND column_name = ?", s.currentDatabase(), tableName, columnName).Scan(&count) + s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = ? AND table_name = ? AND column_name = ?", s.CurrentDatabase(), tableName, columnName).Scan(&count) return count > 0 } -func (s commonDialect) currentDatabase() (name string) { +func (s commonDialect) CurrentDatabase() (name string) { s.db.QueryRow("SELECT DATABASE()").Scan(&name) return } diff --git a/dialect_mysql.go b/dialect_mysql.go index 0ddcea4d..11b894b3 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -114,11 +114,11 @@ func (s mysql) RemoveIndex(tableName string, indexName string) error { func (s mysql) HasForeignKey(tableName string, foreignKeyName string) bool { var count int - s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA=? AND TABLE_NAME=? AND CONSTRAINT_NAME=? AND CONSTRAINT_TYPE='FOREIGN KEY'", s.currentDatabase(), tableName, foreignKeyName).Scan(&count) + s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA=? AND TABLE_NAME=? AND CONSTRAINT_NAME=? AND CONSTRAINT_TYPE='FOREIGN KEY'", s.CurrentDatabase(), tableName, foreignKeyName).Scan(&count) return count > 0 } -func (s mysql) currentDatabase() (name string) { +func (s mysql) CurrentDatabase() (name string) { s.db.QueryRow("SELECT DATABASE()").Scan(&name) return } diff --git a/dialect_postgres.go b/dialect_postgres.go index 0c17d28e..5a6114c0 100644 --- a/dialect_postgres.go +++ b/dialect_postgres.go @@ -107,7 +107,7 @@ func (s postgres) HasColumn(tableName string, columnName string) bool { return count > 0 } -func (s postgres) currentDatabase() (name string) { +func (s postgres) CurrentDatabase() (name string) { s.db.QueryRow("SELECT CURRENT_DATABASE()").Scan(&name) return } diff --git a/dialect_sqlite3.go b/dialect_sqlite3.go index 79adf6d2..2abcefa5 100644 --- a/dialect_sqlite3.go +++ b/dialect_sqlite3.go @@ -89,7 +89,7 @@ func (s sqlite3) HasColumn(tableName string, columnName string) bool { return count > 0 } -func (s sqlite3) currentDatabase() (name string) { +func (s sqlite3) CurrentDatabase() (name string) { var ( ifaces = make([]interface{}, 3) pointers = make([]*string, 3) diff --git a/dialects/mssql/mssql.go b/dialects/mssql/mssql.go index fcdd0ed8..a7bca6b8 100644 --- a/dialects/mssql/mssql.go +++ b/dialects/mssql/mssql.go @@ -113,17 +113,17 @@ func (s mssql) HasForeignKey(tableName string, foreignKeyName string) bool { func (s mssql) HasTable(tableName string) bool { var count int - s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = ? AND table_catalog = ?", tableName, s.currentDatabase()).Scan(&count) + s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = ? AND table_catalog = ?", tableName, s.CurrentDatabase()).Scan(&count) return count > 0 } func (s mssql) HasColumn(tableName string, columnName string) bool { var count int - s.db.QueryRow("SELECT count(*) FROM information_schema.columns WHERE table_catalog = ? AND table_name = ? AND column_name = ?", s.currentDatabase(), tableName, columnName).Scan(&count) + s.db.QueryRow("SELECT count(*) FROM information_schema.columns WHERE table_catalog = ? AND table_name = ? AND column_name = ?", s.CurrentDatabase(), tableName, columnName).Scan(&count) return count > 0 } -func (s mssql) currentDatabase() (name string) { +func (s mssql) CurrentDatabase() (name string) { s.db.QueryRow("SELECT DB_NAME() AS [Current Database]").Scan(&name) return } From 084968cc0add5eec52a1f5d90ee0a8738d0ac83f Mon Sep 17 00:00:00 2001 From: Nick Sarbicki Date: Sat, 16 Jul 2016 14:14:46 +0100 Subject: [PATCH 3/5] Fixing go get error Get this on install: ../../jinzhu/gorm/utils.go:137: syntax error: unexpected range, expecting { ../../jinzhu/gorm/utils.go:147: non-declaration statement outside function body ../../jinzhu/gorm/utils.go:148: syntax error: unexpected } --- utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils.go b/utils.go index dc69e804..ba1f08ab 100644 --- a/utils.go +++ b/utils.go @@ -134,7 +134,7 @@ func toQueryMarks(primaryValues [][]interface{}) string { for _, primaryValue := range primaryValues { var marks []string - for range primaryValue { + for _,_ = range primaryValue { marks = append(marks, "?") } From ccb35db93430a796bcc909b5e917be5079338c35 Mon Sep 17 00:00:00 2001 From: zardak Date: Sun, 7 Aug 2016 13:00:06 +0300 Subject: [PATCH 4/5] Fix failing sqlite3 tests due to db connection not being closed --- main_test.go | 17 +++++++++-------- query_test.go | 2 ++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/main_test.go b/main_test.go index e08eb6e3..1344c65b 100644 --- a/main_test.go +++ b/main_test.go @@ -32,14 +32,6 @@ func init() { panic(fmt.Sprintf("No error should happen when connecting to test database, but got err=%+v", err)) } - // DB.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)}) - // DB.SetLogger(log.New(os.Stdout, "\r\n", 0)) - if os.Getenv("DEBUG") == "true" { - DB.LogMode(true) - } - - DB.DB().SetMaxIdleConns(10) - runMigration() } @@ -72,6 +64,15 @@ func OpenTestConnection() (db *gorm.DB, err error) { fmt.Println("testing sqlite3...") db, err = gorm.Open("sqlite3", filepath.Join(os.TempDir(), "gorm.db")) } + + // db.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)}) + // db.SetLogger(log.New(os.Stdout, "\r\n", 0)) + if os.Getenv("DEBUG") == "true" { + db.LogMode(true) + } + + db.DB().SetMaxIdleConns(10) + return } diff --git a/query_test.go b/query_test.go index 62f86ce9..1a500465 100644 --- a/query_test.go +++ b/query_test.go @@ -628,6 +628,8 @@ func TestSelectWithVariables(t *testing.T) { t.Errorf("Should only contains one column") } } + + rows.Close() } func TestSelectWithArrayInput(t *testing.T) { From fde205f758c6b41ff0f62d0631c02551b3f74530 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 13 Aug 2016 21:23:18 +0800 Subject: [PATCH 5/5] Refactor joining multiple tables with the same fields --- scope.go | 95 ++++++++++---------------------------------------------- 1 file changed, 17 insertions(+), 78 deletions(-) diff --git a/scope.go b/scope.go index a8690bc8..2b9b21e7 100644 --- a/scope.go +++ b/scope.go @@ -495,12 +495,7 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) { } func (scope *Scope) primaryCondition(value interface{}) string { - format := "(%v = %v)" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v = %%v)", scope.New(value).QuotedTableName()) - } - - return fmt.Sprintf(format, scope.Quote(scope.PrimaryKey()), value) + return fmt.Sprintf("(%v.%v = %v)", scope.QuotedTableName(), scope.Quote(scope.PrimaryKey()), value) } func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str string) { @@ -515,44 +510,24 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, sql.NullInt64: return scope.primaryCondition(scope.AddToVars(value)) case []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []string, []interface{}: - format := "(%v IN (?))" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v IN (?))", scope.New(value).QuotedTableName()) - } - - str = fmt.Sprintf(format, scope.Quote(scope.PrimaryKey())) + str = fmt.Sprintf("(%v.%v IN (?))", scope.QuotedTableName(), scope.Quote(scope.PrimaryKey())) clause["args"] = []interface{}{value} case map[string]interface{}: var sqls []string for key, value := range value { if value != nil { - format := "(%v = %v)" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v = %%v)", scope.New(value).QuotedTableName()) - } - - sqls = append(sqls, fmt.Sprintf(format, scope.Quote(key), scope.AddToVars(value))) + sqls = append(sqls, fmt.Sprintf("(%v.%v = %v)", scope.QuotedTableName(), scope.Quote(key), scope.AddToVars(value))) } else { - format := "(%v IS NULL)" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v IS NULL)", scope.New(value).QuotedTableName()) - } - - sqls = append(sqls, fmt.Sprintf(format, scope.Quote(key))) + sqls = append(sqls, fmt.Sprintf("(%v.%v IS NULL)", scope.QuotedTableName(), scope.Quote(key))) } } return strings.Join(sqls, " AND ") case interface{}: var sqls []string - - for _, field := range scope.New(value).Fields() { + newScope := scope.New(value) + for _, field := range newScope.Fields() { if !field.IsIgnored && !field.IsBlank { - format := "(%v = %v)" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v = %%v)", scope.New(value).QuotedTableName()) - } - - sqls = append(sqls, fmt.Sprintf(format, scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) + sqls = append(sqls, fmt.Sprintf("(%v.%v = %v)", newScope.QuotedTableName(), scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) } } return strings.Join(sqls, " AND ") @@ -592,42 +567,20 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string case string: // is number if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) { - format := "(%v <> %v)" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v <> %%v)", scope.New(value).QuotedTableName()) - } - id, _ := strconv.Atoi(value) - return fmt.Sprintf(format, scope.Quote(primaryKey), id) + return fmt.Sprintf("(%v <> %v)", scope.Quote(primaryKey), id) } else if regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS|IN) ").MatchString(value) { str = fmt.Sprintf(" NOT (%v) ", value) notEqualSQL = fmt.Sprintf("NOT (%v)", value) } else { - formatStr := "(%v NOT IN (?))" - formatNotEqualSQL := "(%v <> ?)" - if len(scope.Search.joinConditions) > 0 { - formatStr = fmt.Sprintf("(%v.%%v NOT IN (?))", scope.New(value).QuotedTableName()) - formatNotEqualSQL = fmt.Sprintf("(%v.%%v <> ?)", scope.New(value).QuotedTableName()) - } - - str = fmt.Sprintf(formatStr, scope.Quote(value)) - notEqualSQL = fmt.Sprintf(formatNotEqualSQL, scope.Quote(value)) + str = fmt.Sprintf("(%v.%v NOT IN (?))", scope.QuotedTableName(), scope.Quote(value)) + notEqualSQL = fmt.Sprintf("(%v.%v <> ?)", scope.QuotedTableName(), scope.Quote(value)) } case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, sql.NullInt64: - format := "(%v <> %v)" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v <> %%v)", scope.New(value).QuotedTableName()) - } - - return fmt.Sprintf(format, scope.Quote(primaryKey), value) + return fmt.Sprintf("(%v.%v <> %v)", scope.QuotedTableName(), scope.Quote(primaryKey), value) case []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []string: if reflect.ValueOf(value).Len() > 0 { - format := "(%v NOT IN (?))" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v NOT IN (?))", scope.New(value).QuotedTableName()) - } - - str = fmt.Sprintf(format, scope.Quote(primaryKey)) + str = fmt.Sprintf("(%v.%v NOT IN (?))", scope.QuotedTableName(), scope.Quote(primaryKey)) clause["args"] = []interface{}{value} } return "" @@ -635,32 +588,18 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string var sqls []string for key, value := range value { if value != nil { - format := "(%v <> %v)" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v <> %%v)", scope.New(value).QuotedTableName()) - } - - sqls = append(sqls, fmt.Sprintf(format, scope.Quote(key), scope.AddToVars(value))) + sqls = append(sqls, fmt.Sprintf("(%v.%v <> %v)", scope.QuotedTableName(), scope.Quote(key), scope.AddToVars(value))) } else { - format := "(%v IS NOT NULL)" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v IS NOT NULL)", scope.New(value).QuotedTableName()) - } - - sqls = append(sqls, fmt.Sprintf(format, scope.Quote(key))) + sqls = append(sqls, fmt.Sprintf("(%v.%v IS NOT NULL)", scope.QuotedTableName(), scope.Quote(key))) } } return strings.Join(sqls, " AND ") case interface{}: var sqls []string - for _, field := range scope.New(value).Fields() { + var newScope = scope.New(value) + for _, field := range newScope.Fields() { if !field.IsBlank { - format := "(%v <> %v)" - if len(scope.Search.joinConditions) > 0 { - format = fmt.Sprintf("(%v.%%v <> %%v)", scope.New(value).QuotedTableName()) - } - - sqls = append(sqls, fmt.Sprintf(format, scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) + sqls = append(sqls, fmt.Sprintf("(%v.%v <> %v)", newScope.QuotedTableName(), scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface()))) } } return strings.Join(sqls, " AND ")