forked from mirror/gorm
fix(Joins): args with select and omit (#5790)
* fix(Joins): args with select and omit * chore: gofumpt style
This commit is contained in:
parent
9d82aa5673
commit
b2f42528a4
|
@ -117,13 +117,21 @@ func BuildQuerySQL(db *gorm.DB) {
|
|||
} else if relation, ok := db.Statement.Schema.Relationships.Relations[join.Name]; ok {
|
||||
tableAliasName := relation.Name
|
||||
|
||||
columnStmt := gorm.Statement{
|
||||
Table: tableAliasName, DB: db, Schema: relation.FieldSchema,
|
||||
Selects: join.Selects, Omits: join.Omits,
|
||||
}
|
||||
|
||||
selectColumns, restricted := columnStmt.SelectAndOmitColumns(false, false)
|
||||
for _, s := range relation.FieldSchema.DBNames {
|
||||
if v, ok := selectColumns[s]; (ok && v) || (!ok && !restricted) {
|
||||
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{
|
||||
Table: tableAliasName,
|
||||
Name: s,
|
||||
Alias: tableAliasName + "__" + s,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
exprs := make([]clause.Expression, len(relation.References))
|
||||
for idx, ref := range relation.References {
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
)
|
||||
|
||||
// Model specify the model you would like to run db operations
|
||||
//
|
||||
// // update all users's name to `hello`
|
||||
// db.Model(&User{}).Update("name", "hello")
|
||||
// // if user's primary key is non-blank, will use it as condition, then will only update the user's name to `hello`
|
||||
|
@ -179,6 +180,7 @@ func (db *DB) Or(query interface{}, args ...interface{}) (tx *DB) {
|
|||
}
|
||||
|
||||
// Joins specify Joins conditions
|
||||
//
|
||||
// db.Joins("Account").Find(&user)
|
||||
// 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{}))
|
||||
|
@ -187,10 +189,12 @@ func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
|
|||
|
||||
if len(args) == 1 {
|
||||
if db, ok := args[0].(*DB); ok {
|
||||
j := join{Name: query, Conds: args, Selects: db.Statement.Selects, Omits: db.Statement.Omits}
|
||||
if where, ok := db.Statement.Clauses["WHERE"].Expression.(clause.Where); ok {
|
||||
tx.Statement.Joins = append(tx.Statement.Joins, join{Name: query, Conds: args, On: &where})
|
||||
return
|
||||
j.On = &where
|
||||
}
|
||||
tx.Statement.Joins = append(tx.Statement.Joins, j)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -219,6 +223,7 @@ func (db *DB) Having(query interface{}, args ...interface{}) (tx *DB) {
|
|||
}
|
||||
|
||||
// Order specify order when retrieve records from database
|
||||
//
|
||||
// db.Order("name DESC")
|
||||
// db.Order(clause.OrderByColumn{Column: clause.Column{Name: "name"}, Desc: true})
|
||||
func (db *DB) Order(value interface{}) (tx *DB) {
|
||||
|
@ -256,6 +261,7 @@ func (db *DB) Offset(offset int) (tx *DB) {
|
|||
}
|
||||
|
||||
// Scopes pass current database connection to arguments `func(DB) DB`, which could be used to add conditions dynamically
|
||||
//
|
||||
// func AmountGreaterThan1000(db *gorm.DB) *gorm.DB {
|
||||
// return db.Where("amount > ?", 1000)
|
||||
// }
|
||||
|
@ -274,6 +280,7 @@ func (db *DB) Scopes(funcs ...func(*DB) *DB) (tx *DB) {
|
|||
}
|
||||
|
||||
// Preload preload associations with given conditions
|
||||
//
|
||||
// db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
|
||||
func (db *DB) Preload(query string, args ...interface{}) (tx *DB) {
|
||||
tx = db.getInstance()
|
||||
|
|
|
@ -52,6 +52,8 @@ type join struct {
|
|||
Name string
|
||||
Conds []interface{}
|
||||
On *clause.Where
|
||||
Selects []string
|
||||
Omits []string
|
||||
}
|
||||
|
||||
// StatementModifier statement modifier interface
|
||||
|
@ -544,6 +546,7 @@ func (stmt *Statement) clone() *Statement {
|
|||
}
|
||||
|
||||
// SetColumn set column's value
|
||||
//
|
||||
// stmt.SetColumn("Name", "jinzhu") // Hooks Method
|
||||
// stmt.SetColumn("Name", "jinzhu", true) // Callbacks Method
|
||||
func (stmt *Statement) SetColumn(name string, value interface{}, fromCallbacks ...bool) {
|
||||
|
|
|
@ -260,3 +260,46 @@ func TestJoinWithSameColumnName(t *testing.T) {
|
|||
t.Fatalf("wrong pet name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoinArgsWithDB(t *testing.T) {
|
||||
user := *GetUser("joins-args-db", Config{Pets: 2})
|
||||
DB.Save(&user)
|
||||
|
||||
// test where
|
||||
var user1 User
|
||||
onQuery := DB.Where(&Pet{Name: "joins-args-db_pet_2"})
|
||||
if err := DB.Joins("NamedPet", onQuery).Where("users.name = ?", user.Name).First(&user1).Error; err != nil {
|
||||
t.Fatalf("Failed to load with joins on, got error: %v", err)
|
||||
}
|
||||
|
||||
AssertEqual(t, user1.NamedPet.Name, "joins-args-db_pet_2")
|
||||
|
||||
// test where and omit
|
||||
onQuery2 := DB.Where(&Pet{Name: "joins-args-db_pet_2"}).Omit("Name")
|
||||
var user2 User
|
||||
if err := DB.Joins("NamedPet", onQuery2).Where("users.name = ?", user.Name).First(&user2).Error; err != nil {
|
||||
t.Fatalf("Failed to load with joins on, got error: %v", err)
|
||||
}
|
||||
AssertEqual(t, user2.NamedPet.ID, user1.NamedPet.ID)
|
||||
AssertEqual(t, user2.NamedPet.Name, "")
|
||||
|
||||
// test where and select
|
||||
onQuery3 := DB.Where(&Pet{Name: "joins-args-db_pet_2"}).Select("Name")
|
||||
var user3 User
|
||||
if err := DB.Joins("NamedPet", onQuery3).Where("users.name = ?", user.Name).First(&user3).Error; err != nil {
|
||||
t.Fatalf("Failed to load with joins on, got error: %v", err)
|
||||
}
|
||||
AssertEqual(t, user3.NamedPet.ID, 0)
|
||||
AssertEqual(t, user3.NamedPet.Name, "joins-args-db_pet_2")
|
||||
|
||||
// test select
|
||||
onQuery4 := DB.Select("ID")
|
||||
var user4 User
|
||||
if err := DB.Joins("NamedPet", onQuery4).Where("users.name = ?", user.Name).First(&user4).Error; err != nil {
|
||||
t.Fatalf("Failed to load with joins on, got error: %v", err)
|
||||
}
|
||||
if user4.NamedPet.ID == 0 {
|
||||
t.Fatal("Pet ID can not be empty")
|
||||
}
|
||||
AssertEqual(t, user4.NamedPet.Name, "")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue