Fix Where with empty struct, close #3966

This commit is contained in:
Jinzhu 2021-01-19 16:37:49 +08:00
parent 6095dbf939
commit 9790103e68
1 changed files with 21 additions and 9 deletions

View File

@ -116,7 +116,9 @@ func (db *DB) First(dest interface{}, conds ...interface{}) (tx *DB) {
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey}, Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
}) })
if len(conds) > 0 { if len(conds) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondition(conds[0], conds[1:]...)}) if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: exprs})
}
} }
tx.Statement.RaiseErrorOnNotFound = true tx.Statement.RaiseErrorOnNotFound = true
tx.Statement.Dest = dest tx.Statement.Dest = dest
@ -128,7 +130,9 @@ func (db *DB) First(dest interface{}, conds ...interface{}) (tx *DB) {
func (db *DB) Take(dest interface{}, conds ...interface{}) (tx *DB) { func (db *DB) Take(dest interface{}, conds ...interface{}) (tx *DB) {
tx = db.Limit(1) tx = db.Limit(1)
if len(conds) > 0 { if len(conds) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondition(conds[0], conds[1:]...)}) if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: exprs})
}
} }
tx.Statement.RaiseErrorOnNotFound = true tx.Statement.RaiseErrorOnNotFound = true
tx.Statement.Dest = dest tx.Statement.Dest = dest
@ -143,7 +147,9 @@ func (db *DB) Last(dest interface{}, conds ...interface{}) (tx *DB) {
Desc: true, Desc: true,
}) })
if len(conds) > 0 { if len(conds) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondition(conds[0], conds[1:]...)}) if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: exprs})
}
} }
tx.Statement.RaiseErrorOnNotFound = true tx.Statement.RaiseErrorOnNotFound = true
tx.Statement.Dest = dest tx.Statement.Dest = dest
@ -155,7 +161,9 @@ func (db *DB) Last(dest interface{}, conds ...interface{}) (tx *DB) {
func (db *DB) Find(dest interface{}, conds ...interface{}) (tx *DB) { func (db *DB) Find(dest interface{}, conds ...interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
if len(conds) > 0 { if len(conds) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondition(conds[0], conds[1:]...)}) if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: exprs})
}
} }
tx.Statement.Dest = dest tx.Statement.Dest = dest
tx.callbacks.Query().Execute(tx) tx.callbacks.Query().Execute(tx)
@ -221,8 +229,9 @@ func (tx *DB) assignInterfacesToValue(values ...interface{}) {
} }
} }
case clause.Expression, map[string]string, map[interface{}]interface{}, map[string]interface{}: case clause.Expression, map[string]string, map[interface{}]interface{}, map[string]interface{}:
exprs := tx.Statement.BuildCondition(value) if exprs := tx.Statement.BuildCondition(value); len(exprs) > 0 {
tx.assignInterfacesToValue(exprs) tx.assignInterfacesToValue(exprs)
}
default: default:
if s, err := schema.Parse(value, tx.cacheStore, tx.NamingStrategy); err == nil { if s, err := schema.Parse(value, tx.cacheStore, tx.NamingStrategy); err == nil {
reflectValue := reflect.Indirect(reflect.ValueOf(value)) reflectValue := reflect.Indirect(reflect.ValueOf(value))
@ -239,8 +248,9 @@ func (tx *DB) assignInterfacesToValue(values ...interface{}) {
} }
} }
} else if len(values) > 0 { } else if len(values) > 0 {
exprs := tx.Statement.BuildCondition(values[0], values[1:]...) if exprs := tx.Statement.BuildCondition(values[0], values[1:]...); len(exprs) > 0 {
tx.assignInterfacesToValue(exprs) tx.assignInterfacesToValue(exprs)
}
return return
} }
} }
@ -352,7 +362,9 @@ func (db *DB) UpdateColumns(values interface{}) (tx *DB) {
func (db *DB) Delete(value interface{}, conds ...interface{}) (tx *DB) { func (db *DB) Delete(value interface{}, conds ...interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
if len(conds) > 0 { if len(conds) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondition(conds[0], conds[1:]...)}) if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: exprs})
}
} }
tx.Statement.Dest = value tx.Statement.Dest = value
tx.callbacks.Delete().Execute(tx) tx.callbacks.Delete().Execute(tx)