Skip to content

Commit

Permalink
Joins support parameters, close go-gorm#673
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Feb 16, 2016
1 parent 5883c70 commit 1157899
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ db.Table("users").Select("users.name, emails.email").Joins("left join emails on
db.Joins("inner join emails on emails.user_id = users.id").Where("emails.email = ?", "x@example.org").Find(&user)
// find all email addresses for a user
db.Joins("left join users on users.id = emails.user_id").Where("users.name = ?", "jinzhu").Find(&emails)
db.Joins("LEFT JOIN users ON users.id = emails.user_id AND users.name = ?", "jinzhu").Find(&emails)
```
## Transactions
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ func (s *DB) Having(query string, values ...interface{}) *DB {
return s.clone().search.Having(query, values...).db
}

func (s *DB) Joins(query string) *DB {
return s.clone().search.Joins(query).db
func (s *DB) Joins(query string, args ...interface{}) *DB {
return s.clone().search.Joins(query, args...).db
}

func (s *DB) Scopes(funcs ...func(*DB) *DB) *DB {
Expand Down
14 changes: 10 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,16 @@ func TestJoins(t *testing.T) {
}
DB.Save(&user)

var result User
DB.Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins").First(&result)
if result.Name != "joins" || result.Id != user.Id {
t.Errorf("Should find all two emails with Join")
var users1 []User
DB.Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins").Find(&users1)
if len(users1) != 2 {
t.Errorf("should find two users using left join")
}

var users2 []User
DB.Joins("left join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Where("name = ?", "joins").First(&users2)
if len(users2) != 1 {
t.Errorf("should find one users using left join with conditions")
}
}

Expand Down
14 changes: 10 additions & 4 deletions scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ var hasCountRegexp = regexp.MustCompile(`(?i)count\(.+\)`)

func (scope *Scope) selectSql() string {
if len(scope.Search.selects) == 0 {
if scope.Search.joins != "" {
if len(scope.Search.joinConditions) > 0 {
return fmt.Sprintf("%v.*", scope.QuotedTableName())
}
return "*"
Expand Down Expand Up @@ -263,12 +263,11 @@ func (scope *Scope) groupSql() string {
}

func (scope *Scope) havingSql() string {
if scope.Search.havingConditions == nil {
if len(scope.Search.havingConditions) == 0 {
return ""
}

var andConditions []string

for _, clause := range scope.Search.havingConditions {
if sql := scope.buildWhereCondition(clause); sql != "" {
andConditions = append(andConditions, sql)
Expand All @@ -284,7 +283,14 @@ func (scope *Scope) havingSql() string {
}

func (scope *Scope) joinsSql() string {
return scope.Search.joins + " "
var joinConditions []string
for _, clause := range scope.Search.joinConditions {
if sql := scope.buildWhereCondition(clause); sql != "" {
joinConditions = append(joinConditions, strings.TrimSuffix(strings.TrimPrefix(sql, "("), ")"))
}
}

return strings.Join(joinConditions, " ") + " "
}

func (scope *Scope) prepareQuerySql() {
Expand Down
6 changes: 3 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ type search struct {
orConditions []map[string]interface{}
notConditions []map[string]interface{}
havingConditions []map[string]interface{}
joinConditions []map[string]interface{}
initAttrs []interface{}
assignAttrs []interface{}
selects map[string]interface{}
omits []string
orders []string
joins string
preload []searchPreload
offset int
limit int
Expand Down Expand Up @@ -102,8 +102,8 @@ func (s *search) Having(query string, values ...interface{}) *search {
return s
}

func (s *search) Joins(query string) *search {
s.joins = query
func (s *search) Joins(query string, values ...interface{}) *search {
s.joinConditions = append(s.joinConditions, map[string]interface{}{"query": query, "args": values})
return s
}

Expand Down

0 comments on commit 1157899

Please sign in to comment.