improve time maintaining feature for package gdb

This commit is contained in:
John Guo
2021-08-07 11:01:15 +08:00
parent cd3593182a
commit 9cd944fd77
4 changed files with 11 additions and 15 deletions

View File

@ -455,9 +455,8 @@ func (c *Core) formatOnDuplicate(columns []string, option DoInsertOption) string
}
} else {
for _, column := range columns {
// If it's SAVE operation,
// do not automatically update the creating time.
if c.isSoftCreatedFilledName(column) {
// If it's SAVE operation, do not automatically update the creating time.
if c.isSoftCreatedFieldName(column) {
continue
}
if len(onDuplicateStr) > 0 {
@ -679,8 +678,8 @@ func (c *Core) HasTable(name string) (bool, error) {
return false, nil
}
// isSoftCreatedFilledName checks and returns whether given filed name is an automatic-filled created time.
func (c *Core) isSoftCreatedFilledName(fieldName string) bool {
// isSoftCreatedFieldName checks and returns whether given filed name is an automatic-filled created time.
func (c *Core) isSoftCreatedFieldName(fieldName string) bool {
if fieldName == "" {
return false
}

View File

@ -217,7 +217,6 @@ func (m *Model) doInsertWithOption(insertOption int) (result sql.Result, err err
nowString = gtime.Now().String()
fieldNameCreate = m.getSoftFieldNameCreated()
fieldNameUpdate = m.getSoftFieldNameUpdated()
fieldNameDelete = m.getSoftFieldNameDeleted()
)
newData, err := m.filterDataForInsertOrUpdate(m.data)
if err != nil {
@ -286,7 +285,6 @@ func (m *Model) doInsertWithOption(insertOption int) (result sql.Result, err err
// Automatic handling for creating/updating time.
if !m.unscoped && (fieldNameCreate != "" || fieldNameUpdate != "") {
for k, v := range list {
gutil.MapDelete(v, fieldNameCreate, fieldNameUpdate, fieldNameDelete)
if fieldNameCreate != "" {
v[fieldNameCreate] = nowString
}

View File

@ -15,7 +15,6 @@ import (
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
)
// Update does "UPDATE ... " statement for the model.
@ -43,9 +42,7 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
}
var (
updateData = m.data
fieldNameCreate = m.getSoftFieldNameCreated()
fieldNameUpdate = m.getSoftFieldNameUpdated()
fieldNameDelete = m.getSoftFieldNameDeleted()
conditionWhere, conditionExtra, conditionArgs = m.formatCondition(false, false)
)
// Automatically update the record updating time.
@ -61,7 +58,6 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
switch refKind {
case reflect.Map, reflect.Struct:
dataMap := ConvertDataForTableRecord(m.data)
gutil.MapDelete(dataMap, fieldNameCreate, fieldNameUpdate, fieldNameDelete)
if fieldNameUpdate != "" {
dataMap[fieldNameUpdate] = gtime.Now().String()
}

View File

@ -663,6 +663,9 @@ CREATE TABLE %s (
}
defer dropTable(table)
//db.SetDebug(true)
//defer db.SetDebug(false)
type Entity struct {
Id uint64 `orm:"id,primary" json:"id"`
Name string `orm:"name" json:"name"`
@ -679,7 +682,7 @@ CREATE TABLE %s (
UpdateAt: nil,
DeleteAt: nil,
}
r, err := db.Model(table).Data(dataInsert).Insert()
r, err := db.Model(table).Data(dataInsert).OmitEmpty().Insert()
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
@ -702,7 +705,7 @@ CREATE TABLE %s (
UpdateAt: nil,
DeleteAt: nil,
}
r, err = db.Model(table).Data(dataSave).Save()
r, err = db.Model(table).Data(dataSave).OmitEmpty().Save()
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
@ -726,7 +729,7 @@ CREATE TABLE %s (
UpdateAt: nil,
DeleteAt: nil,
}
r, err = db.Model(table).Data(dataUpdate).WherePri(1).Update()
r, err = db.Model(table).Data(dataUpdate).WherePri(1).OmitEmpty().Update()
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
@ -747,7 +750,7 @@ CREATE TABLE %s (
UpdateAt: nil,
DeleteAt: nil,
}
r, err = db.Model(table).Data(dataReplace).Replace()
r, err = db.Model(table).Data(dataReplace).OmitEmpty().Replace()
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)