gorm/callbacks/create.go

204 lines
6.2 KiB
Go
Raw Normal View History

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
"github.com/jinzhu/gorm"
2020-02-03 05:40:03 +03:00
"github.com/jinzhu/gorm/clause"
2020-02-02 09:40:44 +03:00
)
2020-02-02 03:35:01 +03:00
func BeforeCreate(db *gorm.DB) {
2020-02-23 16:22:35 +03:00
if db.Statement.Schema != nil && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeCreate) {
callMethod := func(value interface{}) bool {
var ok bool
if db.Statement.Schema.BeforeSave {
if i, ok := value.(gorm.BeforeSaveInterface); ok {
ok = true
i.BeforeSave(db)
}
}
if db.Statement.Schema.BeforeCreate {
if i, ok := value.(gorm.BeforeCreateInterface); ok {
ok = true
i.BeforeCreate(db)
}
}
return ok
}
if ok := callMethod(db.Statement.Dest); !ok {
switch db.Statement.ReflectValue.Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i <= db.Statement.ReflectValue.Len(); i++ {
callMethod(db.Statement.ReflectValue.Index(i).Interface())
}
case reflect.Struct:
callMethod(db.Statement.ReflectValue.Interface())
}
}
}
2020-02-02 03:35:01 +03:00
}
func SaveBeforeAssociations(db *gorm.DB) {
}
func Create(db *gorm.DB) {
2020-02-03 05:40:03 +03:00
db.Statement.AddClauseIfNotExists(clause.Insert{
2020-02-19 07:53:46 +03:00
Table: clause.Table{Name: db.Statement.Table},
2020-02-03 05:40:03 +03:00
})
2020-02-23 14:41:29 +03:00
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
2020-02-03 05:40:03 +03:00
2020-02-04 03:56:15 +03:00
db.Statement.Build("INSERT", "VALUES", "ON_CONFLICT")
2020-03-09 08:10:48 +03:00
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
2020-02-19 07:53:46 +03:00
2020-02-23 14:41:29 +03:00
if err == nil {
if db.Statement.Schema != nil {
if insertID, err := result.LastInsertId(); err == nil {
switch db.Statement.ReflectValue.Kind() {
case reflect.Slice, reflect.Array:
for i := db.Statement.ReflectValue.Len() - 1; i >= 0; i-- {
db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.ReflectValue.Index(i), insertID)
insertID--
}
case reflect.Struct:
db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.ReflectValue, insertID)
}
}
}
db.RowsAffected, _ = result.RowsAffected()
} else {
db.AddError(err)
}
2020-02-02 03:35:01 +03:00
}
func SaveAfterAssociations(db *gorm.DB) {
}
func AfterCreate(db *gorm.DB) {
2020-02-23 16:22:35 +03:00
if db.Statement.Schema != nil && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterCreate) {
callMethod := func(value interface{}) bool {
var ok bool
if db.Statement.Schema.AfterSave {
if i, ok := value.(gorm.AfterSaveInterface); ok {
ok = true
i.AfterSave(db)
}
}
if db.Statement.Schema.AfterCreate {
if i, ok := value.(gorm.AfterCreateInterface); ok {
ok = true
i.AfterCreate(db)
}
}
return ok
}
if ok := callMethod(db.Statement.Dest); !ok {
switch db.Statement.ReflectValue.Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i <= db.Statement.ReflectValue.Len(); i++ {
callMethod(db.Statement.ReflectValue.Index(i).Interface())
}
case reflect.Struct:
callMethod(db.Statement.ReflectValue.Interface())
}
}
}
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-02-23 14:41:29 +03:00
return ConvertMapToValues(stmt, value)
2020-02-19 07:53:46 +03:00
case []map[string]interface{}:
2020-02-23 14:41:29 +03:00
return ConvertSliceOfMapToValues(stmt, value)
2020-02-19 07:53:46 +03:00
default:
var (
values = clause.Values{}
selectColumns, restricted = SelectAndOmitColumns(stmt)
curTime = stmt.DB.NowFunc()
isZero = false
)
for _, db := range stmt.Schema.DBNames {
2020-02-20 05:13:26 +03:00
if stmt.Schema.FieldsWithDefaultDBValue[db] == nil {
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-02-20 05:13:26 +03:00
for db, field := range stmt.Schema.FieldsWithDefaultDBValue {
if v, ok := selectColumns[db]; (ok && v) || (!ok && !restricted) {
if v, isZero := field.ValueOf(rv); !isZero {
if len(defaultValueFieldsHavingValue[db]) == 0 {
2020-02-23 14:41:29 +03:00
defaultValueFieldsHavingValue[db] = make([]interface{}, stmt.ReflectValue.Len())
2020-02-19 07:53:46 +03:00
}
2020-02-20 05:13:26 +03:00
defaultValueFieldsHavingValue[db][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-02-20 05:13:26 +03:00
for db, field := range stmt.Schema.FieldsWithDefaultDBValue {
if v, ok := selectColumns[db]; (ok && v) || (!ok && !restricted) {
2020-02-23 14:41:29 +03:00
if v, isZero := field.ValueOf(stmt.ReflectValue); !isZero {
2020-02-20 05:13:26 +03:00
values.Columns = append(values.Columns, clause.Column{Name: db})
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
}
}