2020-02-02 03:35:01 +03:00
|
|
|
package callbacks
|
|
|
|
|
2020-02-02 09:40:44 +03:00
|
|
|
import (
|
2020-02-19 07:53:46 +03:00
|
|
|
"reflect"
|
2020-02-02 09:40:44 +03:00
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
"gorm.io/gorm/schema"
|
2020-02-02 09:40:44 +03:00
|
|
|
)
|
2020-02-02 03:35:01 +03:00
|
|
|
|
|
|
|
func BeforeCreate(db *gorm.DB) {
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Error == nil && db.Statement.Schema != nil && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeCreate) {
|
2020-06-05 16:23:20 +03:00
|
|
|
callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
|
2020-02-23 16:22:35 +03:00
|
|
|
if db.Statement.Schema.BeforeSave {
|
|
|
|
if i, ok := value.(gorm.BeforeSaveInterface); ok {
|
2020-06-05 15:24:15 +03:00
|
|
|
called = true
|
2020-05-31 18:55:56 +03:00
|
|
|
db.AddError(i.BeforeSave(tx))
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if db.Statement.Schema.BeforeCreate {
|
|
|
|
if i, ok := value.(gorm.BeforeCreateInterface); ok {
|
2020-06-05 15:24:15 +03:00
|
|
|
called = true
|
2020-05-31 18:55:56 +03:00
|
|
|
db.AddError(i.BeforeCreate(tx))
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 15:24:15 +03:00
|
|
|
return called
|
2020-06-05 16:23:20 +03:00
|
|
|
})
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
2020-02-02 03:35:01 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 08:05:22 +03:00
|
|
|
func Create(config *Config) func(db *gorm.DB) {
|
|
|
|
if config.WithReturning {
|
|
|
|
return CreateWithReturning
|
|
|
|
} else {
|
|
|
|
return func(db *gorm.DB) {
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Error == nil {
|
|
|
|
if db.Statement.Schema != nil && !db.Statement.Unscoped {
|
|
|
|
for _, c := range db.Statement.Schema.CreateClauses {
|
|
|
|
db.Statement.AddClause(c)
|
|
|
|
}
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Statement.SQL.String() == "" {
|
|
|
|
db.Statement.AddClauseIfNotExists(clause.Insert{
|
|
|
|
Table: clause.Table{Name: db.Statement.Table},
|
|
|
|
})
|
|
|
|
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
db.Statement.Build("INSERT", "VALUES", "ON CONFLICT")
|
|
|
|
}
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2020-06-01 16:26:23 +03:00
|
|
|
if !db.DryRun {
|
|
|
|
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
|
|
|
|
|
|
|
if err == nil {
|
2020-06-05 16:23:20 +03:00
|
|
|
if db.Statement.Schema != nil && db.Statement.Schema.PrioritizedPrimaryField != nil && db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
|
|
|
|
if insertID, err := result.LastInsertId(); err == nil {
|
|
|
|
switch db.Statement.ReflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
if config.LastInsertIDReversed {
|
|
|
|
for i := db.Statement.ReflectValue.Len() - 1; i >= 0; i-- {
|
|
|
|
db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.ReflectValue.Index(i), insertID)
|
|
|
|
insertID--
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := 0; i < db.Statement.ReflectValue.Len(); i++ {
|
|
|
|
db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.ReflectValue.Index(i), insertID)
|
|
|
|
insertID++
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-05-23 13:38:27 +03:00
|
|
|
}
|
2020-06-05 16:23:20 +03:00
|
|
|
case reflect.Struct:
|
|
|
|
db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.ReflectValue, insertID)
|
2020-03-12 08:05:22 +03:00
|
|
|
}
|
2020-06-05 16:23:20 +03:00
|
|
|
} else {
|
|
|
|
db.AddError(err)
|
2020-03-12 08:05:22 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-01 16:26:23 +03:00
|
|
|
db.RowsAffected, _ = result.RowsAffected()
|
|
|
|
} else {
|
|
|
|
db.AddError(err)
|
2020-03-12 08:05:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateWithReturning(db *gorm.DB) {
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Error == nil {
|
|
|
|
if db.Statement.Schema != nil && !db.Statement.Unscoped {
|
|
|
|
for _, c := range db.Statement.Schema.CreateClauses {
|
|
|
|
db.Statement.AddClause(c)
|
|
|
|
}
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
2020-02-03 05:40:03 +03:00
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Statement.SQL.String() == "" {
|
|
|
|
db.Statement.AddClauseIfNotExists(clause.Insert{
|
|
|
|
Table: clause.Table{Name: db.Statement.Table},
|
|
|
|
})
|
|
|
|
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
|
2020-05-29 02:35:45 +03:00
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
db.Statement.Build("INSERT", "VALUES", "ON CONFLICT")
|
|
|
|
}
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
if sch := db.Statement.Schema; sch != nil && len(sch.FieldsWithDefaultDBValue) > 0 {
|
|
|
|
db.Statement.WriteString(" RETURNING ")
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
var (
|
|
|
|
fields = make([]*schema.Field, len(sch.FieldsWithDefaultDBValue))
|
|
|
|
values = make([]interface{}, len(sch.FieldsWithDefaultDBValue))
|
|
|
|
)
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2020-06-05 16:23:20 +03:00
|
|
|
for idx, field := range sch.FieldsWithDefaultDBValue {
|
|
|
|
if idx > 0 {
|
2020-05-31 18:55:56 +03:00
|
|
|
db.Statement.WriteByte(',')
|
|
|
|
}
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
fields[idx] = field
|
2020-06-05 16:23:20 +03:00
|
|
|
db.Statement.WriteQuoted(field.DBName)
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2020-06-01 16:26:23 +03:00
|
|
|
if !db.DryRun {
|
|
|
|
rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2020-06-01 16:26:23 +03:00
|
|
|
if err == nil {
|
|
|
|
defer rows.Close()
|
2020-05-31 18:55:56 +03:00
|
|
|
|
2020-06-01 16:26:23 +03:00
|
|
|
switch db.Statement.ReflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
for rows.Next() {
|
|
|
|
for idx, field := range fields {
|
|
|
|
values[idx] = field.ReflectValueOf(db.Statement.ReflectValue.Index(int(db.RowsAffected))).Addr().Interface()
|
|
|
|
}
|
2020-06-05 16:23:20 +03:00
|
|
|
|
|
|
|
db.RowsAffected++
|
2020-06-01 16:26:23 +03:00
|
|
|
if err := rows.Scan(values...); err != nil {
|
|
|
|
db.AddError(err)
|
|
|
|
}
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-06-01 16:26:23 +03:00
|
|
|
case reflect.Struct:
|
|
|
|
for idx, field := range fields {
|
|
|
|
values[idx] = field.ReflectValueOf(db.Statement.ReflectValue).Addr().Interface()
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2020-06-01 16:26:23 +03:00
|
|
|
if rows.Next() {
|
|
|
|
db.RowsAffected++
|
2020-06-05 16:23:20 +03:00
|
|
|
db.AddError(rows.Scan(values...))
|
2020-06-01 16:26:23 +03:00
|
|
|
}
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-06-05 16:23:20 +03:00
|
|
|
} else {
|
2020-06-01 16:26:23 +03:00
|
|
|
db.AddError(err)
|
|
|
|
}
|
2020-06-01 17:31:50 +03:00
|
|
|
}
|
|
|
|
} else if !db.DryRun {
|
|
|
|
if result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); err == nil {
|
|
|
|
db.RowsAffected, _ = result.RowsAffected()
|
2020-05-31 18:55:56 +03:00
|
|
|
} else {
|
2020-06-01 17:31:50 +03:00
|
|
|
db.AddError(err)
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-03-12 08:05:22 +03:00
|
|
|
}
|
2020-02-23 14:41:29 +03:00
|
|
|
}
|
2020-02-02 03:35:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func AfterCreate(db *gorm.DB) {
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Error == nil && db.Statement.Schema != nil && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterCreate) {
|
2020-06-05 16:23:20 +03:00
|
|
|
callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
|
2020-02-23 16:22:35 +03:00
|
|
|
if db.Statement.Schema.AfterSave {
|
|
|
|
if i, ok := value.(gorm.AfterSaveInterface); ok {
|
2020-06-05 15:24:15 +03:00
|
|
|
called = true
|
2020-05-31 18:55:56 +03:00
|
|
|
db.AddError(i.AfterSave(tx))
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if db.Statement.Schema.AfterCreate {
|
|
|
|
if i, ok := value.(gorm.AfterCreateInterface); ok {
|
2020-06-05 15:24:15 +03:00
|
|
|
called = true
|
2020-05-31 18:55:56 +03:00
|
|
|
db.AddError(i.AfterCreate(tx))
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 15:24:15 +03:00
|
|
|
return called
|
2020-06-05 16:23:20 +03:00
|
|
|
})
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
2020-02-02 03:35:01 +03:00
|
|
|
}
|
2020-02-19 07:53:46 +03:00
|
|
|
|
|
|
|
// ConvertToCreateValues convert to create values
|
2020-02-23 14:41:29 +03:00
|
|
|
func ConvertToCreateValues(stmt *gorm.Statement) clause.Values {
|
2020-02-19 07:53:46 +03:00
|
|
|
switch value := stmt.Dest.(type) {
|
|
|
|
case map[string]interface{}:
|
2020-04-08 03:15:00 +03:00
|
|
|
return ConvertMapToValuesForCreate(stmt, value)
|
2020-02-19 07:53:46 +03:00
|
|
|
case []map[string]interface{}:
|
2020-04-08 03:15:00 +03:00
|
|
|
return ConvertSliceOfMapToValuesForCreate(stmt, value)
|
2020-02-19 07:53:46 +03:00
|
|
|
default:
|
|
|
|
var (
|
|
|
|
values = clause.Values{}
|
2020-04-08 03:15:00 +03:00
|
|
|
selectColumns, restricted = SelectAndOmitColumns(stmt, true, false)
|
2020-02-19 07:53:46 +03:00
|
|
|
curTime = stmt.DB.NowFunc()
|
|
|
|
isZero = false
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, db := range stmt.Schema.DBNames {
|
2020-06-05 16:23:20 +03:00
|
|
|
if field := stmt.Schema.FieldsByDBName[db]; !field.HasDefaultValue || field.DefaultValueInterface != nil {
|
2020-02-20 05:13:26 +03:00
|
|
|
if v, ok := selectColumns[db]; (ok && v) || (!ok && !restricted) {
|
|
|
|
values.Columns = append(values.Columns, clause.Column{Name: db})
|
|
|
|
}
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 14:41:29 +03:00
|
|
|
switch stmt.ReflectValue.Kind() {
|
2020-02-19 07:53:46 +03:00
|
|
|
case reflect.Slice, reflect.Array:
|
2020-02-23 14:41:29 +03:00
|
|
|
values.Values = make([][]interface{}, stmt.ReflectValue.Len())
|
2020-02-20 05:13:26 +03:00
|
|
|
defaultValueFieldsHavingValue := map[string][]interface{}{}
|
2020-02-23 14:41:29 +03:00
|
|
|
for i := 0; i < stmt.ReflectValue.Len(); i++ {
|
|
|
|
rv := reflect.Indirect(stmt.ReflectValue.Index(i))
|
2020-02-19 07:53:46 +03:00
|
|
|
values.Values[i] = make([]interface{}, len(values.Columns))
|
|
|
|
for idx, column := range values.Columns {
|
|
|
|
field := stmt.Schema.FieldsByDBName[column.Name]
|
|
|
|
if values.Values[i][idx], isZero = field.ValueOf(rv); isZero {
|
|
|
|
if field.DefaultValueInterface != nil {
|
|
|
|
values.Values[i][idx] = field.DefaultValueInterface
|
|
|
|
field.Set(rv, field.DefaultValueInterface)
|
|
|
|
} else if field.AutoCreateTime > 0 || field.AutoUpdateTime > 0 {
|
|
|
|
field.Set(rv, curTime)
|
|
|
|
values.Values[i][idx], _ = field.ValueOf(rv)
|
2020-02-20 05:13:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 07:53:46 +03:00
|
|
|
|
2020-06-05 16:23:20 +03:00
|
|
|
for _, field := range stmt.Schema.FieldsWithDefaultDBValue {
|
|
|
|
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) {
|
2020-02-20 05:13:26 +03:00
|
|
|
if v, isZero := field.ValueOf(rv); !isZero {
|
2020-06-05 16:23:20 +03:00
|
|
|
if len(defaultValueFieldsHavingValue[field.DBName]) == 0 {
|
|
|
|
defaultValueFieldsHavingValue[field.DBName] = make([]interface{}, stmt.ReflectValue.Len())
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
2020-06-05 16:23:20 +03:00
|
|
|
defaultValueFieldsHavingValue[field.DBName][i] = v
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 05:13:26 +03:00
|
|
|
|
|
|
|
for db, vs := range defaultValueFieldsHavingValue {
|
|
|
|
values.Columns = append(values.Columns, clause.Column{Name: db})
|
|
|
|
for idx := range values.Values {
|
|
|
|
if vs[idx] == nil {
|
|
|
|
values.Values[idx] = append(values.Values[idx], clause.Expr{SQL: "DEFAULT"})
|
|
|
|
} else {
|
|
|
|
values.Values[idx] = append(values.Values[idx], vs[idx])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 07:53:46 +03:00
|
|
|
case reflect.Struct:
|
|
|
|
values.Values = [][]interface{}{make([]interface{}, len(values.Columns))}
|
|
|
|
for idx, column := range values.Columns {
|
|
|
|
field := stmt.Schema.FieldsByDBName[column.Name]
|
2020-02-23 14:41:29 +03:00
|
|
|
if values.Values[0][idx], isZero = field.ValueOf(stmt.ReflectValue); isZero {
|
2020-02-19 07:53:46 +03:00
|
|
|
if field.DefaultValueInterface != nil {
|
|
|
|
values.Values[0][idx] = field.DefaultValueInterface
|
2020-02-23 14:41:29 +03:00
|
|
|
field.Set(stmt.ReflectValue, field.DefaultValueInterface)
|
2020-02-19 07:53:46 +03:00
|
|
|
} else if field.AutoCreateTime > 0 || field.AutoUpdateTime > 0 {
|
2020-02-23 14:41:29 +03:00
|
|
|
field.Set(stmt.ReflectValue, curTime)
|
|
|
|
values.Values[0][idx], _ = field.ValueOf(stmt.ReflectValue)
|
2020-02-20 05:13:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 07:53:46 +03:00
|
|
|
|
2020-06-05 16:23:20 +03:00
|
|
|
for _, field := range stmt.Schema.FieldsWithDefaultDBValue {
|
|
|
|
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) {
|
2020-02-23 14:41:29 +03:00
|
|
|
if v, isZero := field.ValueOf(stmt.ReflectValue); !isZero {
|
2020-06-05 16:23:20 +03:00
|
|
|
values.Columns = append(values.Columns, clause.Column{Name: field.DBName})
|
2020-02-20 05:13:26 +03:00
|
|
|
values.Values[0] = append(values.Values[0], v)
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 05:13:26 +03:00
|
|
|
|
2020-02-23 14:41:29 +03:00
|
|
|
return values
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
|
|
|
}
|