Fixed a bug when joining multiple tables with the same fields and where on the same field.

This commit is contained in:
Vladislav Fursov 2016-08-09 14:28:43 +09:00
parent 613c065569
commit e8c14bd1b3
2 changed files with 82 additions and 13 deletions

View File

@ -538,6 +538,12 @@ func TestJoins(t *testing.T) {
if len(users4) != 0 { if len(users4) != 0 {
t.Errorf("should find no user when searching with unexisting credit card") 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) { func TestJoinsWithSelect(t *testing.T) {

View File

@ -495,7 +495,12 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
} }
func (scope *Scope) primaryCondition(value interface{}) string { 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) { 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: case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, sql.NullInt64:
return scope.primaryCondition(scope.AddToVars(value)) return scope.primaryCondition(scope.AddToVars(value))
case []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []string, []interface{}: 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} clause["args"] = []interface{}{value}
case map[string]interface{}: case map[string]interface{}:
var sqls []string var sqls []string
for key, value := range value { for key, value := range value {
if value != nil { 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 { } 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 ") return strings.Join(sqls, " AND ")
case interface{}: case interface{}:
var sqls []string var sqls []string
for _, field := range scope.New(value).Fields() { for _, field := range scope.New(value).Fields() {
if !field.IsIgnored && !field.IsBlank { 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 ") return strings.Join(sqls, " AND ")
@ -566,20 +592,42 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string
case string: case string:
// is number // is number
if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) { 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) 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) { } else if regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS|IN) ").MatchString(value) {
str = fmt.Sprintf(" NOT (%v) ", value) str = fmt.Sprintf(" NOT (%v) ", value)
notEqualSQL = fmt.Sprintf("NOT (%v)", value) notEqualSQL = fmt.Sprintf("NOT (%v)", value)
} else { } else {
str = fmt.Sprintf("(%v NOT IN (?))", scope.Quote(value)) formatStr := "(%v NOT IN (?))"
notEqualSQL = fmt.Sprintf("(%v <> ?)", scope.Quote(value)) 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: 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: case []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []string:
if reflect.ValueOf(value).Len() > 0 { 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} clause["args"] = []interface{}{value}
} }
return "" return ""
@ -587,9 +635,19 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string
var sqls []string var sqls []string
for key, value := range value { for key, value := range value {
if value != nil { 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 { } 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 ") return strings.Join(sqls, " AND ")
@ -597,7 +655,12 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string
var sqls []string var sqls []string
for _, field := range scope.New(value).Fields() { for _, field := range scope.New(value).Fields() {
if !field.IsBlank { 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 ") return strings.Join(sqls, " AND ")