From 9b318bb57f2152e22d3ae4cd10093e5589d65e04 Mon Sep 17 00:00:00 2001 From: wln32 <49137144+wln32@users.noreply.github.com> Date: Thu, 12 Sep 2024 15:38:18 +0800 Subject: [PATCH] perf(database/gdb): performance improvement for struct scanning when with feature disabled (#3677) --- database/gdb/gdb_model_soft_time.go | 24 +++++++++++++++++++----- database/gdb/gdb_model_with.go | 6 ++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/database/gdb/gdb_model_soft_time.go b/database/gdb/gdb_model_soft_time.go index bee3da23f..aa6d50fea 100644 --- a/database/gdb/gdb_model_soft_time.go +++ b/database/gdb/gdb_model_soft_time.go @@ -15,12 +15,11 @@ import ( "github.com/gogf/gf/v2/errors/gcode" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/internal/intlog" + "github.com/gogf/gf/v2/internal/utils" "github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/text/gregex" "github.com/gogf/gf/v2/text/gstr" - "github.com/gogf/gf/v2/util/gconv" - "github.com/gogf/gf/v2/util/gutil" ) // SoftTimeType custom defines the soft time field type. @@ -206,9 +205,7 @@ func (m *softTimeMaintainer) getSoftFieldNameAndType( return nil, nil } for _, checkFiledName := range checkFiledNames { - fieldName, _ = gutil.MapPossibleItemByKey( - gconv.Map(fieldsMap), checkFiledName, - ) + fieldName = searchFieldNameFromMap(fieldsMap, checkFiledName) if fieldName != "" { fieldType, _ = m.db.CheckLocalTypeForField( ctx, fieldsMap[fieldName].Type, nil, @@ -238,6 +235,23 @@ func (m *softTimeMaintainer) getSoftFieldNameAndType( return } +func searchFieldNameFromMap(fieldsMap map[string]*TableField, key string) string { + if len(fieldsMap) == 0 { + return "" + } + _, ok := fieldsMap[key] + if ok { + return key + } + key = utils.RemoveSymbols(key) + for k := range fieldsMap { + if strings.EqualFold(utils.RemoveSymbols(k), key) { + return k + } + } + return "" +} + // GetWhereConditionForDelete retrieves and returns the condition string for soft deleting. // It supports multiple tables string like: // "user u, user_detail ud" diff --git a/database/gdb/gdb_model_with.go b/database/gdb/gdb_model_with.go index e259d1186..74c18caee 100644 --- a/database/gdb/gdb_model_with.go +++ b/database/gdb/gdb_model_with.go @@ -67,6 +67,9 @@ func (m *Model) WithAll() *Model { // doWithScanStruct handles model association operations feature for single struct. func (m *Model) doWithScanStruct(pointer interface{}) error { + if len(m.withArray) == 0 && m.withAll == false { + return nil + } var ( err error allowedTypeStrArray = make([]string, 0) @@ -183,6 +186,9 @@ func (m *Model) doWithScanStruct(pointer interface{}) error { // doWithScanStructs handles model association operations feature for struct slice. // Also see doWithScanStruct. func (m *Model) doWithScanStructs(pointer interface{}) error { + if len(m.withArray) == 0 && m.withAll == false { + return nil + } if v, ok := pointer.(reflect.Value); ok { pointer = v.Interface() }