2020-02-02 03:35:01 +03:00
|
|
|
package callbacks
|
|
|
|
|
2020-02-02 09:40:44 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
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) {
|
|
|
|
// before save
|
|
|
|
// before create
|
|
|
|
|
|
|
|
// assign timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
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-19 07:53:46 +03:00
|
|
|
values, _ := ConvertToCreateValues(db.Statement)
|
|
|
|
db.Statement.AddClause(values)
|
2020-02-03 05:40:03 +03:00
|
|
|
|
2020-02-04 03:56:15 +03:00
|
|
|
db.Statement.Build("INSERT", "VALUES", "ON_CONFLICT")
|
2020-02-03 05:40:03 +03:00
|
|
|
result, err := db.DB.ExecContext(db.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
2020-02-19 07:53:46 +03:00
|
|
|
|
|
|
|
fmt.Printf("%+v\n", values)
|
2020-02-03 05:40:03 +03:00
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println(result)
|
2020-02-02 09:40:44 +03:00
|
|
|
fmt.Println(db.Statement.SQL.String(), db.Statement.Vars)
|
2020-02-02 03:35:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func SaveAfterAssociations(db *gorm.DB) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func AfterCreate(db *gorm.DB) {
|
|
|
|
// after save
|
|
|
|
// after create
|
|
|
|
}
|
2020-02-19 07:53:46 +03:00
|
|
|
|
|
|
|
// ConvertToCreateValues convert to create values
|
|
|
|
func ConvertToCreateValues(stmt *gorm.Statement) (clause.Values, []map[string]interface{}) {
|
|
|
|
switch value := stmt.Dest.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
return ConvertMapToValues(stmt, value), nil
|
|
|
|
case []map[string]interface{}:
|
|
|
|
return ConvertSliceOfMapToValues(stmt, value), nil
|
|
|
|
default:
|
|
|
|
var (
|
|
|
|
values = clause.Values{}
|
|
|
|
selectColumns, restricted = SelectAndOmitColumns(stmt)
|
|
|
|
curTime = stmt.DB.NowFunc()
|
|
|
|
isZero = false
|
|
|
|
returnningValues []map[string]interface{}
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, db := range stmt.Schema.DBNames {
|
|
|
|
if v, ok := selectColumns[db]; (ok && v) || (!ok && !restricted) {
|
|
|
|
values.Columns = append(values.Columns, clause.Column{Name: db})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reflectValue := reflect.Indirect(reflect.ValueOf(stmt.Dest))
|
|
|
|
switch reflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
values.Values = make([][]interface{}, reflectValue.Len())
|
|
|
|
for i := 0; i < reflectValue.Len(); i++ {
|
|
|
|
rv := reflect.Indirect(reflectValue.Index(i))
|
|
|
|
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)
|
|
|
|
} else if field.HasDefaultValue {
|
|
|
|
if len(returnningValues) == 0 {
|
|
|
|
returnningValues = make([]map[string]interface{}, reflectValue.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
if returnningValues[i] == nil {
|
|
|
|
returnningValues[i] = map[string]interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
returnningValues[i][column.Name] = field.ReflectValueOf(reflectValue).Addr().Interface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
values.Values = [][]interface{}{make([]interface{}, len(values.Columns))}
|
|
|
|
for idx, column := range values.Columns {
|
|
|
|
field := stmt.Schema.FieldsByDBName[column.Name]
|
|
|
|
if values.Values[0][idx], _ = field.ValueOf(reflectValue); isZero {
|
|
|
|
if field.DefaultValueInterface != nil {
|
|
|
|
values.Values[0][idx] = field.DefaultValueInterface
|
|
|
|
field.Set(reflectValue, field.DefaultValueInterface)
|
|
|
|
} else if field.AutoCreateTime > 0 || field.AutoUpdateTime > 0 {
|
|
|
|
field.Set(reflectValue, curTime)
|
|
|
|
values.Values[0][idx], _ = field.ValueOf(reflectValue)
|
|
|
|
} else if field.HasDefaultValue {
|
|
|
|
if len(returnningValues) == 0 {
|
|
|
|
returnningValues = make([]map[string]interface{}, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
values.Values[0][idx] = clause.Expr{SQL: "DEFAULT"}
|
|
|
|
returnningValues[0][column.Name] = field.ReflectValueOf(reflectValue).Addr().Interface()
|
|
|
|
} else if field.PrimaryKey {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values, returnningValues
|
|
|
|
}
|
|
|
|
}
|