Skip to content

Commit

Permalink
feat: support inner join (#5583)
Browse files Browse the repository at this point in the history
* feat: support inner join

* test: mixed inner join and left join

* chore: code comment

* Update statement.go

Co-authored-by: Jinzhu <wosmvp@gmail.com>
  • Loading branch information
a631807682 and jinzhu committed Dec 24, 2022
1 parent 775fa70 commit 1935eb0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion callbacks/query.go
Expand Up @@ -185,7 +185,7 @@ func BuildQuerySQL(db *gorm.DB) {
}

fromClause.Joins = append(fromClause.Joins, clause.Join{
Type: clause.LeftJoin,
Type: join.JoinType,
Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName},
ON: clause.Where{Exprs: exprs},
})
Expand Down
12 changes: 11 additions & 1 deletion chainable_api.go
Expand Up @@ -235,6 +235,16 @@ func (db *DB) Or(query interface{}, args ...interface{}) (tx *DB) {
// db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Find(&user)
// db.Joins("Account", DB.Select("id").Where("user_id = users.id AND name = ?", "someName").Model(&Account{}))
func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
return joins(db, clause.LeftJoin, query, args...)
}

// InnerJoins specify inner joins conditions
// db.InnerJoins("Account").Find(&user)
func (db *DB) InnerJoins(query string, args ...interface{}) (tx *DB) {
return joins(db, clause.InnerJoin, query, args...)
}

func joins(db *DB, joinType clause.JoinType, query string, args ...interface{}) (tx *DB) {
tx = db.getInstance()

if len(args) == 1 {
Expand All @@ -248,7 +258,7 @@ func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
}
}

tx.Statement.Joins = append(tx.Statement.Joins, join{Name: query, Conds: args})
tx.Statement.Joins = append(tx.Statement.Joins, join{Name: query, Conds: args, JoinType: joinType})
return
}

Expand Down
11 changes: 6 additions & 5 deletions statement.go
Expand Up @@ -49,11 +49,12 @@ type Statement struct {
}

type join struct {
Name string
Conds []interface{}
On *clause.Where
Selects []string
Omits []string
Name string
Conds []interface{}
On *clause.Where
Selects []string
Omits []string
JoinType clause.JoinType
}

// StatementModifier statement modifier interface
Expand Down
22 changes: 22 additions & 0 deletions tests/joins_test.go
Expand Up @@ -230,6 +230,28 @@ func TestJoinWithSoftDeleted(t *testing.T) {
}
}

func TestInnerJoins(t *testing.T) {
user := *GetUser("inner-joins-1", Config{Company: true, Manager: true, Account: true, NamedPet: false})

DB.Create(&user)

var user2 User
var err error
err = DB.InnerJoins("Company").InnerJoins("Manager").InnerJoins("Account").First(&user2, "users.name = ?", user.Name).Error
AssertEqual(t, err, nil)
CheckUser(t, user2, user)

// inner join and NamedPet is nil
err = DB.InnerJoins("NamedPet").InnerJoins("Company").InnerJoins("Manager").InnerJoins("Account").First(&user2, "users.name = ?", user.Name).Error
AssertEqual(t, err, gorm.ErrRecordNotFound)

// mixed inner join and left join
var user3 User
err = DB.Joins("NamedPet").InnerJoins("Company").InnerJoins("Manager").InnerJoins("Account").First(&user3, "users.name = ?", user.Name).Error
AssertEqual(t, err, nil)
CheckUser(t, user3, user)
}

func TestJoinWithSameColumnName(t *testing.T) {
user := GetUser("TestJoinWithSameColumnName", Config{
Languages: 1,
Expand Down

0 comments on commit 1935eb0

Please sign in to comment.