Skip to content

Commit

Permalink
fix: Disable automatic handling for creating/updating time if it has …
Browse files Browse the repository at this point in the history
…been specified. Fixes #3613

Signed-off-by: oninowang <[email protected]>
  • Loading branch information
oninowang committed May 28, 2024
1 parent 457f7ab commit 95e856f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
24 changes: 15 additions & 9 deletions database/gdb/gdb_model_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,27 @@ func (m *Model) doInsertWithOption(ctx context.Context, insertOption InsertOptio
if !m.unscoped && (fieldNameCreate != "" || fieldNameUpdate != "") {
for k, v := range list {
if fieldNameCreate != "" {
fieldCreateValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeCreate, false)
if fieldCreateValue != nil {
v[fieldNameCreate] = fieldCreateValue
if _, ok := v[fieldNameCreate]; !ok {
fieldCreateValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeCreate, false)
if fieldCreateValue != nil {
v[fieldNameCreate] = fieldCreateValue
}
}
}
if fieldNameUpdate != "" {
fieldUpdateValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeUpdate, false)
if fieldUpdateValue != nil {
v[fieldNameUpdate] = fieldUpdateValue
if _, ok := v[fieldNameUpdate]; !ok {
fieldUpdateValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeUpdate, false)
if fieldUpdateValue != nil {
v[fieldNameUpdate] = fieldUpdateValue
}
}
}
if fieldNameDelete != "" {
fieldDeleteValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeDelete, true)
if fieldDeleteValue != nil {
v[fieldNameDelete] = fieldDeleteValue
if _, ok := v[fieldNameDelete]; !ok {
fieldDeleteValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeDelete, true)
if fieldDeleteValue != nil {
v[fieldNameDelete] = fieldDeleteValue
}
}
}
list[k] = v
Expand Down
14 changes: 7 additions & 7 deletions database/gdb/gdb_model_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
var dataMap = anyValueToMapBeforeToRecord(m.data)
// Automatically update the record updating time.
if fieldNameUpdate != "" {
dataValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeUpdate, false)
dataMap[fieldNameUpdate] = dataValue
if _, ok := dataMap[fieldNameUpdate]; !ok {
dataValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeUpdate, false)
dataMap[fieldNameUpdate] = dataValue
}
}
updateData = dataMap

default:
updates := gconv.String(m.data)
// Automatically update the record updating time.
if fieldNameUpdate != "" {
if fieldNameUpdate != "" && !gstr.Contains(updates, fieldNameUpdate) {
dataValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeUpdate, false)
if fieldNameUpdate != "" && !gstr.Contains(updates, fieldNameUpdate) {
updates += fmt.Sprintf(`,%s=?`, fieldNameUpdate)
conditionArgs = append([]interface{}{dataValue}, conditionArgs...)
}
updates += fmt.Sprintf(`,%s=?`, fieldNameUpdate)
conditionArgs = append([]interface{}{dataValue}, conditionArgs...)
}
updateData = updates
}
Expand Down

0 comments on commit 95e856f

Please sign in to comment.