From 67de7a8af82adeb86399846986d4c213d09f31b4 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 19 Aug 2025 14:35:49 +0800 Subject: [PATCH] performance improve for schema --- schema/schema.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index be8d8f07..9419846b 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -94,9 +94,6 @@ func (schema *Schema) LookUpField(name string) *Field { // ID string // is selected by LookUpFieldByBindName([]string{"ID"}, "ID") // } func (schema *Schema) LookUpFieldByBindName(bindNames []string, name string) *Field { - if len(bindNames) == 0 { - return nil - } for i := len(bindNames) - 1; i >= 0; i-- { find := strings.Join(bindNames[:i], ".") + "." + name if field, ok := schema.FieldsByBindName[find]; ok { @@ -133,13 +130,11 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest) } - value := reflect.ValueOf(dest) - if value.Kind() == reflect.Ptr && value.IsNil() { - value = reflect.New(value.Type().Elem()) + modelType := reflect.ValueOf(dest).Type() + if modelType.Kind() == reflect.Ptr { + modelType = modelType.Elem() } - modelType := reflect.Indirect(value).Type() - if modelType.Kind() != reflect.Struct { if modelType.Kind() == reflect.Interface { modelType = reflect.Indirect(reflect.ValueOf(dest)).Elem().Type() @@ -174,7 +169,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam var tableName string modelValue := reflect.New(modelType) - if specialTableName != "" && specialTableName != tableName { + if specialTableName != "" { tableName = specialTableName } else if en, ok := namer.(embeddedNamer); ok { tableName = en.Table @@ -190,6 +185,8 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam Name: modelType.Name(), ModelType: modelType, Table: tableName, + DBNames: make([]string, 0, 10), + Fields: make([]*Field, 0, 10), FieldsByName: make(map[string]*Field, 10), FieldsByBindName: make(map[string]*Field, 10), FieldsByDBName: make(map[string]*Field, 10), @@ -236,8 +233,9 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam schema.FieldsByBindName[bindName] = field if v != nil && v.PrimaryKey { + // remove the existing primary key field for idx, f := range schema.PrimaryFields { - if f == v { + if f.DBName == v.DBName { schema.PrimaryFields = append(schema.PrimaryFields[0:idx], schema.PrimaryFields[idx+1:]...) } }