From e8c14bd1b3688d3901a11257da0ac61df5b3bd4f Mon Sep 17 00:00:00 2001 From: Vladislav Fursov Date: Tue, 9 Aug 2016 14:28:43 +0900 Subject: [PATCH] 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 ")