2020-02-02 03:35:01 +03:00
|
|
|
package callbacks
|
|
|
|
|
2020-02-02 09:40:44 +03:00
|
|
|
import (
|
2020-09-03 15:59:41 +03:00
|
|
|
"fmt"
|
2020-02-19 07:53:46 +03:00
|
|
|
"reflect"
|
2023-03-10 11:35:26 +03:00
|
|
|
"strings"
|
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"
|
2021-10-28 02:24:38 +03:00
|
|
|
"gorm.io/gorm/utils"
|
2020-02-02 09:40:44 +03:00
|
|
|
)
|
2020-02-02 03:35:01 +03:00
|
|
|
|
2022-03-22 17:42:36 +03:00
|
|
|
// BeforeCreate before create hooks
|
2020-02-02 03:35:01 +03:00
|
|
|
func BeforeCreate(db *gorm.DB) {
|
2020-11-17 12:49:43 +03:00
|
|
|
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (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 {
|
2020-08-27 10:03:57 +03:00
|
|
|
if i, ok := value.(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 {
|
2020-08-27 10:03:57 +03:00
|
|
|
if i, ok := value.(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
|
|
|
}
|
|
|
|
|
2022-03-22 17:42:36 +03:00
|
|
|
// Create create hook
|
2020-03-12 08:05:22 +03:00
|
|
|
func Create(config *Config) func(db *gorm.DB) {
|
2021-10-28 02:24:38 +03:00
|
|
|
supportReturning := utils.Contains(config.CreateClauses, "RETURNING")
|
2020-05-29 02:35:45 +03:00
|
|
|
|
2021-06-10 05:21:28 +03:00
|
|
|
return func(db *gorm.DB) {
|
|
|
|
if db.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2021-10-26 17:36:37 +03:00
|
|
|
if db.Statement.Schema != nil {
|
|
|
|
if !db.Statement.Unscoped {
|
|
|
|
for _, c := range db.Statement.Schema.CreateClauses {
|
|
|
|
db.Statement.AddClause(c)
|
|
|
|
}
|
2021-06-10 05:21:28 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 02:24:38 +03:00
|
|
|
if supportReturning && len(db.Statement.Schema.FieldsWithDefaultDBValue) > 0 {
|
2021-10-26 17:36:37 +03:00
|
|
|
if _, ok := db.Statement.Clauses["RETURNING"]; !ok {
|
|
|
|
fromColumns := make([]clause.Column, 0, len(db.Statement.Schema.FieldsWithDefaultDBValue))
|
|
|
|
for _, field := range db.Statement.Schema.FieldsWithDefaultDBValue {
|
|
|
|
fromColumns = append(fromColumns, clause.Column{Name: field.DBName})
|
2020-03-12 08:05:22 +03:00
|
|
|
}
|
2021-10-26 17:36:37 +03:00
|
|
|
db.Statement.AddClause(clause.Returning{Columns: fromColumns})
|
2020-03-12 08:05:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 05:40:03 +03:00
|
|
|
|
2021-12-08 08:58:06 +03:00
|
|
|
if db.Statement.SQL.Len() == 0 {
|
2021-10-26 17:36:37 +03:00
|
|
|
db.Statement.SQL.Grow(180)
|
2020-08-06 11:25:26 +03:00
|
|
|
db.Statement.AddClauseIfNotExists(clause.Insert{})
|
2020-05-31 18:55:56 +03:00
|
|
|
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
|
2020-05-29 02:35:45 +03:00
|
|
|
|
2021-04-28 12:19:30 +03:00
|
|
|
db.Statement.Build(db.Statement.BuildClauses...)
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2021-11-04 08:45:44 +03:00
|
|
|
isDryRun := !db.DryRun && db.Error == nil
|
|
|
|
if !isDryRun {
|
|
|
|
return
|
|
|
|
}
|
2021-10-28 02:24:38 +03:00
|
|
|
|
2021-11-04 08:45:44 +03:00
|
|
|
ok, mode := hasReturning(db, supportReturning)
|
|
|
|
if ok {
|
|
|
|
if c, ok := db.Statement.Clauses["ON CONFLICT"]; ok {
|
|
|
|
if onConflict, _ := c.Expression.(clause.OnConflict); onConflict.DoNothing {
|
|
|
|
mode |= gorm.ScanOnConflictDoNothing
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2021-11-04 08:45:44 +03:00
|
|
|
}
|
2020-03-12 08:05:22 +03:00
|
|
|
|
2021-11-04 08:45:44 +03:00
|
|
|
rows, err := db.Statement.ConnPool.QueryContext(
|
|
|
|
db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
|
|
|
|
)
|
|
|
|
if db.AddError(err) == nil {
|
2022-03-29 13:48:06 +03:00
|
|
|
defer func() {
|
|
|
|
db.AddError(rows.Close())
|
|
|
|
}()
|
2021-11-04 08:45:44 +03:00
|
|
|
gorm.Scan(rows, db, mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := db.Statement.ConnPool.ExecContext(
|
|
|
|
db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
db.AddError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db.RowsAffected, _ = result.RowsAffected()
|
2023-11-15 16:32:56 +03:00
|
|
|
if db.RowsAffected == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
pkField *schema.Field
|
|
|
|
pkFieldName = "@id"
|
|
|
|
)
|
2024-03-19 06:50:28 +03:00
|
|
|
|
|
|
|
insertID, err := result.LastInsertId()
|
|
|
|
insertOk := err == nil && insertID > 0
|
|
|
|
|
|
|
|
if !insertOk {
|
|
|
|
if !supportReturning {
|
|
|
|
db.AddError(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:32:56 +03:00
|
|
|
if db.Statement.Schema != nil {
|
|
|
|
if db.Statement.Schema.PrioritizedPrimaryField == nil || !db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pkField = db.Statement.Schema.PrioritizedPrimaryField
|
|
|
|
pkFieldName = db.Statement.Schema.PrioritizedPrimaryField.DBName
|
|
|
|
}
|
|
|
|
|
|
|
|
// append @id column with value for auto-increment primary key
|
|
|
|
// the @id value is correct, when: 1. without setting auto-increment primary key, 2. database AutoIncrementIncrement = 1
|
|
|
|
switch values := db.Statement.Dest.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
values[pkFieldName] = insertID
|
|
|
|
case *map[string]interface{}:
|
|
|
|
(*values)[pkFieldName] = insertID
|
|
|
|
case []map[string]interface{}, *[]map[string]interface{}:
|
|
|
|
mapValues, ok := values.([]map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
if v, ok := values.(*[]map[string]interface{}); ok {
|
|
|
|
if *v != nil {
|
|
|
|
mapValues = *v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 06:50:28 +03:00
|
|
|
|
|
|
|
if config.LastInsertIDReversed {
|
|
|
|
insertID -= int64(len(mapValues)-1) * schema.DefaultAutoIncrementIncrement
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:32:56 +03:00
|
|
|
for _, mapValue := range mapValues {
|
|
|
|
if mapValue != nil {
|
|
|
|
mapValue[pkFieldName] = insertID
|
|
|
|
}
|
|
|
|
insertID += schema.DefaultAutoIncrementIncrement
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if pkField == nil {
|
2021-11-04 08:45:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch db.Statement.ReflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
if config.LastInsertIDReversed {
|
|
|
|
for i := db.Statement.ReflectValue.Len() - 1; i >= 0; i-- {
|
|
|
|
rv := db.Statement.ReflectValue.Index(i)
|
|
|
|
if reflect.Indirect(rv).Kind() != reflect.Struct {
|
|
|
|
break
|
|
|
|
}
|
2020-07-22 06:28:00 +03:00
|
|
|
|
2023-11-15 16:32:56 +03:00
|
|
|
_, isZero := pkField.ValueOf(db.Statement.Context, rv)
|
2021-11-04 08:45:44 +03:00
|
|
|
if isZero {
|
2023-11-15 16:32:56 +03:00
|
|
|
db.AddError(pkField.Set(db.Statement.Context, rv, insertID))
|
|
|
|
insertID -= pkField.AutoIncrementIncrement
|
2020-06-01 16:26:23 +03:00
|
|
|
}
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2021-11-04 08:45:44 +03:00
|
|
|
} else {
|
|
|
|
for i := 0; i < db.Statement.ReflectValue.Len(); i++ {
|
|
|
|
rv := db.Statement.ReflectValue.Index(i)
|
|
|
|
if reflect.Indirect(rv).Kind() != reflect.Struct {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:32:56 +03:00
|
|
|
if _, isZero := pkField.ValueOf(db.Statement.Context, rv); isZero {
|
|
|
|
db.AddError(pkField.Set(db.Statement.Context, rv, insertID))
|
|
|
|
insertID += pkField.AutoIncrementIncrement
|
2021-11-04 08:45:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
2023-11-15 16:32:56 +03:00
|
|
|
_, isZero := pkField.ValueOf(db.Statement.Context, db.Statement.ReflectValue)
|
2021-11-04 08:45:44 +03:00
|
|
|
if isZero {
|
2023-11-15 16:32:56 +03:00
|
|
|
db.AddError(pkField.Set(db.Statement.Context, db.Statement.ReflectValue, insertID))
|
2020-06-01 16:26:23 +03:00
|
|
|
}
|
2020-06-01 17:31:50 +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
|
|
|
}
|
|
|
|
|
2022-03-22 17:42:36 +03:00
|
|
|
// AfterCreate after create hooks
|
2020-02-02 03:35:01 +03:00
|
|
|
func AfterCreate(db *gorm.DB) {
|
2020-11-17 12:49:43 +03:00
|
|
|
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (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) {
|
2022-03-22 17:42:36 +03:00
|
|
|
if db.Statement.Schema.AfterCreate {
|
|
|
|
if i, ok := value.(AfterCreateInterface); ok {
|
2020-06-05 15:24:15 +03:00
|
|
|
called = true
|
2022-03-22 17:42:36 +03:00
|
|
|
db.AddError(i.AfterCreate(tx))
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 17:42:36 +03:00
|
|
|
if db.Statement.Schema.AfterSave {
|
|
|
|
if i, ok := value.(AfterSaveInterface); ok {
|
2020-06-05 15:24:15 +03:00
|
|
|
called = true
|
2022-03-22 17:42:36 +03:00
|
|
|
db.AddError(i.AfterSave(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-06-09 19:02:14 +03:00
|
|
|
func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
|
2021-09-17 10:29:49 +03:00
|
|
|
curTime := stmt.DB.NowFunc()
|
|
|
|
|
2020-02-19 07:53:46 +03:00
|
|
|
switch value := stmt.Dest.(type) {
|
|
|
|
case map[string]interface{}:
|
2020-06-09 19:02:14 +03:00
|
|
|
values = ConvertMapToValuesForCreate(stmt, value)
|
2020-08-27 11:27:59 +03:00
|
|
|
case *map[string]interface{}:
|
|
|
|
values = ConvertMapToValuesForCreate(stmt, *value)
|
2020-02-19 07:53:46 +03:00
|
|
|
case []map[string]interface{}:
|
2020-06-09 19:02:14 +03:00
|
|
|
values = ConvertSliceOfMapToValuesForCreate(stmt, value)
|
2020-08-27 11:27:59 +03:00
|
|
|
case *[]map[string]interface{}:
|
|
|
|
values = ConvertSliceOfMapToValuesForCreate(stmt, *value)
|
2020-02-19 07:53:46 +03:00
|
|
|
default:
|
|
|
|
var (
|
2020-06-30 11:53:54 +03:00
|
|
|
selectColumns, restricted = stmt.SelectAndOmitColumns(true, false)
|
2021-06-18 10:38:20 +03:00
|
|
|
_, updateTrackTime = stmt.Get("gorm:update_track_time")
|
2020-06-09 07:00:43 +03:00
|
|
|
isZero bool
|
2020-02-19 07:53:46 +03:00
|
|
|
)
|
2021-06-18 10:38:20 +03:00
|
|
|
stmt.Settings.Delete("gorm:update_track_time")
|
|
|
|
|
2020-06-09 19:02:14 +03:00
|
|
|
values = clause.Values{Columns: make([]clause.Column, 0, len(stmt.Schema.DBNames))}
|
2020-02-19 07:53:46 +03:00
|
|
|
|
|
|
|
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-12-15 06:18:29 +03:00
|
|
|
if v, ok := selectColumns[db]; (ok && v) || (!ok && (!restricted || field.AutoCreateTime > 0 || field.AutoUpdateTime > 0)) {
|
2020-06-09 05:17:24 +03:00
|
|
|
values.Columns = append(values.Columns, clause.Column{Name: db})
|
2020-02-20 05:13:26 +03:00
|
|
|
}
|
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:
|
2021-11-29 04:33:20 +03:00
|
|
|
rValLen := stmt.ReflectValue.Len()
|
|
|
|
if rValLen == 0 {
|
2020-09-03 06:32:30 +03:00
|
|
|
stmt.AddError(gorm.ErrEmptySlice)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 04:28:19 +03:00
|
|
|
stmt.SQL.Grow(rValLen * 18)
|
|
|
|
stmt.Vars = make([]interface{}, 0, rValLen*len(values.Columns))
|
|
|
|
values.Values = make([][]interface{}, rValLen)
|
|
|
|
|
2021-11-29 04:33:20 +03:00
|
|
|
defaultValueFieldsHavingValue := map[*schema.Field][]interface{}{}
|
|
|
|
for i := 0; i < rValLen; i++ {
|
2020-02-23 14:41:29 +03:00
|
|
|
rv := reflect.Indirect(stmt.ReflectValue.Index(i))
|
2020-09-03 15:59:41 +03:00
|
|
|
if !rv.IsValid() {
|
|
|
|
stmt.AddError(fmt.Errorf("slice data #%v is invalid: %w", i, gorm.ErrInvalidData))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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]
|
2022-02-16 10:30:43 +03:00
|
|
|
if values.Values[i][idx], isZero = field.ValueOf(stmt.Context, rv); isZero {
|
2020-02-19 07:53:46 +03:00
|
|
|
if field.DefaultValueInterface != nil {
|
|
|
|
values.Values[i][idx] = field.DefaultValueInterface
|
2022-03-23 12:24:25 +03:00
|
|
|
stmt.AddError(field.Set(stmt.Context, rv, field.DefaultValueInterface))
|
2020-02-19 07:53:46 +03:00
|
|
|
} else if field.AutoCreateTime > 0 || field.AutoUpdateTime > 0 {
|
2022-03-23 12:24:25 +03:00
|
|
|
stmt.AddError(field.Set(stmt.Context, rv, curTime))
|
2022-02-16 10:30:43 +03:00
|
|
|
values.Values[i][idx], _ = field.ValueOf(stmt.Context, rv)
|
2020-02-20 05:13:26 +03:00
|
|
|
}
|
2021-06-18 10:38:20 +03:00
|
|
|
} else if field.AutoUpdateTime > 0 && updateTrackTime {
|
2022-03-23 12:24:25 +03:00
|
|
|
stmt.AddError(field.Set(stmt.Context, rv, curTime))
|
2022-02-16 10:30:43 +03:00
|
|
|
values.Values[i][idx], _ = field.ValueOf(stmt.Context, 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) {
|
2022-02-16 10:30:43 +03:00
|
|
|
if rvOfvalue, isZero := field.ValueOf(stmt.Context, rv); !isZero {
|
2020-06-20 05:51:36 +03:00
|
|
|
if len(defaultValueFieldsHavingValue[field]) == 0 {
|
2021-11-29 04:33:20 +03:00
|
|
|
defaultValueFieldsHavingValue[field] = make([]interface{}, rValLen)
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
2021-11-29 04:33:20 +03:00
|
|
|
defaultValueFieldsHavingValue[field][i] = rvOfvalue
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 05:13:26 +03:00
|
|
|
|
2024-03-18 08:48:42 +03:00
|
|
|
for _, field := range stmt.Schema.FieldsWithDefaultDBValue {
|
|
|
|
if vs, ok := defaultValueFieldsHavingValue[field]; ok {
|
|
|
|
values.Columns = append(values.Columns, clause.Column{Name: field.DBName})
|
|
|
|
for idx := range values.Values {
|
|
|
|
if vs[idx] == nil {
|
|
|
|
values.Values[idx] = append(values.Values[idx], stmt.Dialector.DefaultValueOf(field))
|
|
|
|
} else {
|
|
|
|
values.Values[idx] = append(values.Values[idx], vs[idx])
|
|
|
|
}
|
2020-02-20 05:13:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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]
|
2022-02-16 10:30:43 +03:00
|
|
|
if values.Values[0][idx], isZero = field.ValueOf(stmt.Context, stmt.ReflectValue); isZero {
|
2020-02-19 07:53:46 +03:00
|
|
|
if field.DefaultValueInterface != nil {
|
|
|
|
values.Values[0][idx] = field.DefaultValueInterface
|
2022-03-23 12:24:25 +03:00
|
|
|
stmt.AddError(field.Set(stmt.Context, stmt.ReflectValue, field.DefaultValueInterface))
|
2020-02-19 07:53:46 +03:00
|
|
|
} else if field.AutoCreateTime > 0 || field.AutoUpdateTime > 0 {
|
2022-03-23 12:24:25 +03:00
|
|
|
stmt.AddError(field.Set(stmt.Context, stmt.ReflectValue, curTime))
|
2022-02-16 10:30:43 +03:00
|
|
|
values.Values[0][idx], _ = field.ValueOf(stmt.Context, stmt.ReflectValue)
|
2020-02-20 05:13:26 +03:00
|
|
|
}
|
2021-06-18 10:38:20 +03:00
|
|
|
} else if field.AutoUpdateTime > 0 && updateTrackTime {
|
2022-03-23 12:24:25 +03:00
|
|
|
stmt.AddError(field.Set(stmt.Context, stmt.ReflectValue, curTime))
|
2022-02-16 10:30:43 +03:00
|
|
|
values.Values[0][idx], _ = field.ValueOf(stmt.Context, 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) {
|
2022-02-16 10:30:43 +03:00
|
|
|
if rvOfvalue, isZero := field.ValueOf(stmt.Context, stmt.ReflectValue); !isZero {
|
2020-06-05 16:23:20 +03:00
|
|
|
values.Columns = append(values.Columns, clause.Column{Name: field.DBName})
|
2021-11-29 04:33:20 +03:00
|
|
|
values.Values[0] = append(values.Values[0], rvOfvalue)
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 13:00:36 +03:00
|
|
|
default:
|
|
|
|
stmt.AddError(gorm.ErrInvalidData)
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
2020-06-09 19:02:14 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 15:22:08 +03:00
|
|
|
if c, ok := stmt.Clauses["ON CONFLICT"]; ok {
|
|
|
|
if onConflict, _ := c.Expression.(clause.OnConflict); onConflict.UpdateAll {
|
2021-07-13 16:29:31 +03:00
|
|
|
if stmt.Schema != nil && len(values.Columns) >= 1 {
|
2021-07-13 13:04:42 +03:00
|
|
|
selectColumns, restricted := stmt.SelectAndOmitColumns(true, true)
|
|
|
|
|
2020-11-16 15:22:08 +03:00
|
|
|
columns := make([]string, 0, len(values.Columns)-1)
|
|
|
|
for _, column := range values.Columns {
|
|
|
|
if field := stmt.Schema.LookUpField(column.Name); field != nil {
|
2021-07-13 13:04:42 +03:00
|
|
|
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) {
|
2023-03-10 11:35:26 +03:00
|
|
|
if !field.PrimaryKey && (!field.HasDefaultValue || field.DefaultValueInterface != nil ||
|
|
|
|
strings.EqualFold(field.DefaultValue, "NULL")) && field.AutoCreateTime == 0 {
|
2021-09-17 10:29:49 +03:00
|
|
|
if field.AutoUpdateTime > 0 {
|
|
|
|
assignment := clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: curTime}
|
|
|
|
switch field.AutoUpdateTime {
|
|
|
|
case schema.UnixNanosecond:
|
|
|
|
assignment.Value = curTime.UnixNano()
|
|
|
|
case schema.UnixMillisecond:
|
2024-03-18 08:44:55 +03:00
|
|
|
assignment.Value = curTime.UnixMilli()
|
2021-09-17 10:29:49 +03:00
|
|
|
case schema.UnixSecond:
|
|
|
|
assignment.Value = curTime.Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
onConflict.DoUpdates = append(onConflict.DoUpdates, assignment)
|
|
|
|
} else {
|
|
|
|
columns = append(columns, column.Name)
|
|
|
|
}
|
2021-07-13 13:04:42 +03:00
|
|
|
}
|
2020-11-16 15:22:08 +03:00
|
|
|
}
|
2020-06-09 19:02:14 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-20 05:13:26 +03:00
|
|
|
|
2021-09-17 10:29:49 +03:00
|
|
|
onConflict.DoUpdates = append(onConflict.DoUpdates, clause.AssignmentColumns(columns)...)
|
2021-11-29 06:02:32 +03:00
|
|
|
if len(onConflict.DoUpdates) == 0 {
|
|
|
|
onConflict.DoNothing = true
|
|
|
|
}
|
2020-11-16 15:22:08 +03:00
|
|
|
|
2021-03-18 06:44:04 +03:00
|
|
|
// use primary fields as default OnConflict columns
|
|
|
|
if len(onConflict.Columns) == 0 {
|
|
|
|
for _, field := range stmt.Schema.PrimaryFields {
|
|
|
|
onConflict.Columns = append(onConflict.Columns, clause.Column{Name: field.DBName})
|
|
|
|
}
|
2020-11-16 15:22:08 +03:00
|
|
|
}
|
|
|
|
stmt.AddClause(onConflict)
|
2020-06-09 19:02:14 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
2020-06-09 19:02:14 +03:00
|
|
|
|
|
|
|
return values
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|