optimization callbacks/create.go Create func if...else logic
This commit is contained in:
parent
c27b80ab28
commit
5dbcc21152
@ -33,9 +33,14 @@ func BeforeCreate(db *gorm.DB) {
|
|||||||
func Create(config *Config) func(db *gorm.DB) {
|
func Create(config *Config) func(db *gorm.DB) {
|
||||||
if config.WithReturning {
|
if config.WithReturning {
|
||||||
return CreateWithReturning
|
return CreateWithReturning
|
||||||
} else {
|
}
|
||||||
|
|
||||||
return func(db *gorm.DB) {
|
return func(db *gorm.DB) {
|
||||||
if db.Error == nil {
|
if db.Error != nil {
|
||||||
|
// maybe record logger TODO
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if db.Statement.Schema != nil && !db.Statement.Unscoped {
|
if db.Statement.Schema != nil && !db.Statement.Unscoped {
|
||||||
for _, c := range db.Statement.Schema.CreateClauses {
|
for _, c := range db.Statement.Schema.CreateClauses {
|
||||||
db.Statement.AddClause(c)
|
db.Statement.AddClause(c)
|
||||||
@ -53,10 +58,15 @@ func Create(config *Config) func(db *gorm.DB) {
|
|||||||
if !db.DryRun && db.Error == nil {
|
if !db.DryRun && db.Error == nil {
|
||||||
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
||||||
|
|
||||||
if err == nil {
|
if err != nil {
|
||||||
db.RowsAffected, _ = result.RowsAffected()
|
db.AddError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db.RowsAffected, _ = result.RowsAffected()
|
||||||
|
if !(db.RowsAffected > 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if db.RowsAffected > 0 {
|
|
||||||
if db.Statement.Schema != nil && db.Statement.Schema.PrioritizedPrimaryField != nil && db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
|
if db.Statement.Schema != nil && db.Statement.Schema.PrioritizedPrimaryField != nil && db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
|
||||||
if insertID, err := result.LastInsertId(); err == nil && insertID > 0 {
|
if insertID, err := result.LastInsertId(); err == nil && insertID > 0 {
|
||||||
switch db.Statement.ReflectValue.Kind() {
|
switch db.Statement.ReflectValue.Kind() {
|
||||||
@ -96,12 +106,7 @@ func Create(config *Config) func(db *gorm.DB) {
|
|||||||
db.AddError(err)
|
db.AddError(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
db.AddError(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user