forked from mirror/gorm
273 lines
8.0 KiB
Go
273 lines
8.0 KiB
Go
package callbacks
|
|
|
|
import (
|
|
"reflect"
|
|
"sort"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"gorm.io/gorm/schema"
|
|
)
|
|
|
|
func SetupUpdateReflectValue(db *gorm.DB) {
|
|
if db.Error == nil && db.Statement.Schema != nil {
|
|
if !db.Statement.ReflectValue.CanAddr() || db.Statement.Model != db.Statement.Dest {
|
|
db.Statement.ReflectValue = reflect.ValueOf(db.Statement.Model)
|
|
for db.Statement.ReflectValue.Kind() == reflect.Ptr {
|
|
db.Statement.ReflectValue = db.Statement.ReflectValue.Elem()
|
|
}
|
|
|
|
if dest, ok := db.Statement.Dest.(map[string]interface{}); ok {
|
|
for _, rel := range db.Statement.Schema.Relationships.BelongsTo {
|
|
if _, ok := dest[rel.Name]; ok {
|
|
rel.Field.Set(db.Statement.ReflectValue, dest[rel.Name])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func BeforeUpdate(db *gorm.DB) {
|
|
if db.Error == nil && db.Statement.Schema != nil && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeUpdate) {
|
|
tx := db.Session(&gorm.Session{})
|
|
callMethod := func(value interface{}) bool {
|
|
var ok bool
|
|
if db.Statement.Schema.BeforeSave {
|
|
if i, ok := value.(gorm.BeforeSaveInterface); ok {
|
|
ok = true
|
|
db.AddError(i.BeforeSave(tx))
|
|
}
|
|
}
|
|
|
|
if db.Statement.Schema.BeforeUpdate {
|
|
if i, ok := value.(gorm.BeforeUpdateInterface); ok {
|
|
ok = true
|
|
db.AddError(i.BeforeUpdate(tx))
|
|
}
|
|
}
|
|
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())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func Update(db *gorm.DB) {
|
|
if db.Error == nil {
|
|
if db.Statement.Schema != nil && !db.Statement.Unscoped {
|
|
for _, c := range db.Statement.Schema.UpdateClauses {
|
|
db.Statement.AddClause(c)
|
|
}
|
|
}
|
|
|
|
if db.Statement.SQL.String() == "" {
|
|
db.Statement.AddClauseIfNotExists(clause.Update{})
|
|
if set := ConvertToAssignments(db.Statement); len(set) != 0 {
|
|
db.Statement.AddClause(set)
|
|
} else {
|
|
return
|
|
}
|
|
db.Statement.Build("UPDATE", "SET", "WHERE")
|
|
}
|
|
|
|
if _, ok := db.Statement.Clauses["WHERE"]; !ok {
|
|
db.AddError(gorm.ErrMissingWhereClause)
|
|
return
|
|
}
|
|
|
|
if !db.DryRun {
|
|
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
|
|
|
if err == nil {
|
|
db.RowsAffected, _ = result.RowsAffected()
|
|
} else {
|
|
db.AddError(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func AfterUpdate(db *gorm.DB) {
|
|
if db.Error == nil && db.Statement.Schema != nil && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterUpdate) {
|
|
tx := db.Session(&gorm.Session{})
|
|
callMethod := func(value interface{}) bool {
|
|
var ok bool
|
|
if db.Statement.Schema.AfterSave {
|
|
if i, ok := value.(gorm.AfterSaveInterface); ok {
|
|
ok = true
|
|
db.AddError(i.AfterSave(tx))
|
|
}
|
|
}
|
|
|
|
if db.Statement.Schema.AfterUpdate {
|
|
if i, ok := value.(gorm.AfterUpdateInterface); ok {
|
|
ok = true
|
|
db.AddError(i.AfterUpdate(tx))
|
|
}
|
|
}
|
|
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())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ConvertToAssignments convert to update assignments
|
|
func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
|
|
var (
|
|
selectColumns, restricted = SelectAndOmitColumns(stmt, false, true)
|
|
assignValue func(field *schema.Field, value interface{})
|
|
)
|
|
|
|
switch stmt.ReflectValue.Kind() {
|
|
case reflect.Slice, reflect.Array:
|
|
assignValue = func(field *schema.Field, value interface{}) {
|
|
for i := 0; i < stmt.ReflectValue.Len(); i++ {
|
|
field.Set(stmt.ReflectValue.Index(i), value)
|
|
}
|
|
}
|
|
case reflect.Struct:
|
|
assignValue = func(field *schema.Field, value interface{}) {
|
|
if stmt.ReflectValue.CanAddr() {
|
|
field.Set(stmt.ReflectValue, value)
|
|
}
|
|
}
|
|
default:
|
|
assignValue = func(field *schema.Field, value interface{}) {
|
|
}
|
|
}
|
|
|
|
updatingValue := reflect.ValueOf(stmt.Dest)
|
|
for updatingValue.Kind() == reflect.Ptr {
|
|
updatingValue = updatingValue.Elem()
|
|
}
|
|
|
|
switch value := updatingValue.Interface().(type) {
|
|
case map[string]interface{}:
|
|
set = make([]clause.Assignment, 0, len(value))
|
|
|
|
var keys []string
|
|
for k, _ := range value {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
if stmt.Schema != nil {
|
|
if field := stmt.Schema.LookUpField(k); field != nil {
|
|
if field.DBName != "" {
|
|
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) {
|
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: value[k]})
|
|
assignValue(field, value[k])
|
|
}
|
|
} else if v, ok := selectColumns[field.Name]; (ok && v) || (!ok && !restricted) {
|
|
assignValue(field, value[k])
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
|
|
if v, ok := selectColumns[k]; (ok && v) || (!ok && !restricted) {
|
|
set = append(set, clause.Assignment{Column: clause.Column{Name: k}, Value: value[k]})
|
|
}
|
|
}
|
|
|
|
if !stmt.DisableUpdateTime && stmt.Schema != nil {
|
|
for _, field := range stmt.Schema.FieldsByDBName {
|
|
if field.AutoUpdateTime > 0 && value[field.Name] == nil && value[field.DBName] == nil {
|
|
now := stmt.DB.NowFunc()
|
|
assignValue(field, now)
|
|
|
|
if field.AutoUpdateTime == schema.UnixNanosecond {
|
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.UnixNano()})
|
|
} else if field.DataType == schema.Time {
|
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now})
|
|
} else {
|
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.Unix()})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
default:
|
|
switch updatingValue.Kind() {
|
|
case reflect.Struct:
|
|
set = make([]clause.Assignment, 0, len(stmt.Schema.FieldsByDBName))
|
|
for _, field := range stmt.Schema.FieldsByDBName {
|
|
if !field.PrimaryKey || (!updatingValue.CanAddr() || stmt.Dest != stmt.Model) {
|
|
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) {
|
|
value, isZero := field.ValueOf(updatingValue)
|
|
if !stmt.DisableUpdateTime {
|
|
if field.AutoUpdateTime > 0 {
|
|
if field.AutoUpdateTime == schema.UnixNanosecond {
|
|
value = stmt.DB.NowFunc().UnixNano()
|
|
} else if field.DataType == schema.Time {
|
|
value = stmt.DB.NowFunc()
|
|
} else {
|
|
value = stmt.DB.NowFunc().Unix()
|
|
}
|
|
isZero = false
|
|
}
|
|
}
|
|
|
|
if ok || !isZero {
|
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: value})
|
|
assignValue(field, value)
|
|
}
|
|
}
|
|
} else {
|
|
if value, isZero := field.ValueOf(updatingValue); !isZero {
|
|
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.Eq{Column: field.DBName, Value: value}}})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if !updatingValue.CanAddr() || stmt.Dest != stmt.Model {
|
|
switch stmt.ReflectValue.Kind() {
|
|
case reflect.Slice, reflect.Array:
|
|
var priamryKeyExprs []clause.Expression
|
|
for i := 0; i < stmt.ReflectValue.Len(); i++ {
|
|
var exprs = make([]clause.Expression, len(stmt.Schema.PrimaryFields))
|
|
var notZero bool
|
|
for idx, field := range stmt.Schema.PrimaryFields {
|
|
value, isZero := field.ValueOf(stmt.ReflectValue.Index(i))
|
|
exprs[idx] = clause.Eq{Column: field.DBName, Value: value}
|
|
notZero = notZero || !isZero
|
|
}
|
|
if notZero {
|
|
priamryKeyExprs = append(priamryKeyExprs, clause.And(exprs...))
|
|
}
|
|
}
|
|
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.Or(priamryKeyExprs...)}})
|
|
case reflect.Struct:
|
|
for _, field := range stmt.Schema.PrimaryFields {
|
|
if value, isZero := field.ValueOf(stmt.ReflectValue); !isZero {
|
|
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.Eq{Column: field.DBName, Value: value}}})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|