mirror of https://github.com/go-gorm/gorm.git
Refactor FirstOrCreate, FirstOrInit
This commit is contained in:
parent
0211ac91a2
commit
6a6dfdae72
|
@ -290,7 +290,7 @@ func (db *DB) FirstOrInit(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 tx = queryTx.Find(dest, conds...); queryTx.RowsAffected == 0 {
|
if tx = queryTx.Find(dest, conds...); tx.RowsAffected == 0 {
|
||||||
if c, ok := tx.Statement.Clauses["WHERE"]; ok {
|
if c, ok := tx.Statement.Clauses["WHERE"]; ok {
|
||||||
if where, ok := c.Expression.(clause.Where); ok {
|
if where, ok := c.Expression.(clause.Where); ok {
|
||||||
tx.assignInterfacesToValue(where.Exprs)
|
tx.assignInterfacesToValue(where.Exprs)
|
||||||
|
@ -312,25 +312,26 @@ func (db *DB) FirstOrInit(dest interface{}, conds ...interface{}) (tx *DB) {
|
||||||
|
|
||||||
// FirstOrCreate gets the first matched record or create a new one with given conditions (only works with struct, map conditions)
|
// FirstOrCreate gets the first matched record or create a new one with given conditions (only works with struct, map conditions)
|
||||||
func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) {
|
func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) {
|
||||||
queryTx := db.Limit(1).Order(clause.OrderByColumn{
|
tx = db.getInstance()
|
||||||
|
queryTx := db.Session(&Session{}).Limit(1).Order(clause.OrderByColumn{
|
||||||
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
|
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
|
||||||
})
|
})
|
||||||
if tx = queryTx.Find(dest, conds...); tx.Error == nil {
|
if result := queryTx.Find(dest, conds...); result.Error == nil {
|
||||||
if tx.RowsAffected == 0 {
|
if result.RowsAffected == 0 {
|
||||||
if c, ok := tx.Statement.Clauses["WHERE"]; ok {
|
if c, ok := result.Statement.Clauses["WHERE"]; ok {
|
||||||
if where, ok := c.Expression.(clause.Where); ok {
|
if where, ok := c.Expression.(clause.Where); ok {
|
||||||
tx.assignInterfacesToValue(where.Exprs)
|
result.assignInterfacesToValue(where.Exprs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize with attrs, conds
|
// initialize with attrs, conds
|
||||||
if len(tx.Statement.attrs) > 0 {
|
if len(db.Statement.attrs) > 0 {
|
||||||
tx.assignInterfacesToValue(tx.Statement.attrs...)
|
result.assignInterfacesToValue(db.Statement.attrs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize with attrs, conds
|
// initialize with attrs, conds
|
||||||
if len(tx.Statement.assigns) > 0 {
|
if len(db.Statement.assigns) > 0 {
|
||||||
tx.assignInterfacesToValue(tx.Statement.assigns...)
|
result.assignInterfacesToValue(db.Statement.assigns...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx.Create(dest)
|
return tx.Create(dest)
|
||||||
|
@ -351,8 +352,7 @@ func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) {
|
||||||
|
|
||||||
return tx.Model(dest).Updates(assigns)
|
return tx.Model(dest).Updates(assigns)
|
||||||
} else {
|
} else {
|
||||||
// can not use Find RowsAffected
|
tx.Error = result.Error
|
||||||
tx.RowsAffected = 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tx
|
return tx
|
||||||
|
|
|
@ -7,13 +7,12 @@ require (
|
||||||
github.com/google/uuid v1.3.0
|
github.com/google/uuid v1.3.0
|
||||||
github.com/jinzhu/now v1.1.5
|
github.com/jinzhu/now v1.1.5
|
||||||
github.com/lib/pq v1.10.5
|
github.com/lib/pq v1.10.5
|
||||||
github.com/mattn/go-sqlite3 v1.14.12 // indirect
|
|
||||||
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
|
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
|
||||||
gorm.io/driver/mysql v1.3.3
|
gorm.io/driver/mysql v1.3.3
|
||||||
gorm.io/driver/postgres v1.3.4
|
gorm.io/driver/postgres v1.3.5
|
||||||
gorm.io/driver/sqlite v1.3.1
|
gorm.io/driver/sqlite v1.3.2
|
||||||
gorm.io/driver/sqlserver v1.3.2
|
gorm.io/driver/sqlserver v1.3.2
|
||||||
gorm.io/gorm v1.23.3
|
gorm.io/gorm v1.23.4
|
||||||
)
|
)
|
||||||
|
|
||||||
replace gorm.io/gorm => ../
|
replace gorm.io/gorm => ../
|
||||||
|
|
Loading…
Reference in New Issue