performance improve for schema

This commit is contained in:
Jinzhu 2025-08-19 14:35:49 +08:00
parent 725aa5b5ff
commit 67de7a8af8

View File

@ -94,9 +94,6 @@ func (schema *Schema) LookUpField(name string) *Field {
// ID string // is selected by LookUpFieldByBindName([]string{"ID"}, "ID") // ID string // is selected by LookUpFieldByBindName([]string{"ID"}, "ID")
// } // }
func (schema *Schema) LookUpFieldByBindName(bindNames []string, name string) *Field { func (schema *Schema) LookUpFieldByBindName(bindNames []string, name string) *Field {
if len(bindNames) == 0 {
return nil
}
for i := len(bindNames) - 1; i >= 0; i-- { for i := len(bindNames) - 1; i >= 0; i-- {
find := strings.Join(bindNames[:i], ".") + "." + name find := strings.Join(bindNames[:i], ".") + "." + name
if field, ok := schema.FieldsByBindName[find]; ok { 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) return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest)
} }
value := reflect.ValueOf(dest) modelType := reflect.ValueOf(dest).Type()
if value.Kind() == reflect.Ptr && value.IsNil() { if modelType.Kind() == reflect.Ptr {
value = reflect.New(value.Type().Elem()) modelType = modelType.Elem()
} }
modelType := reflect.Indirect(value).Type()
if modelType.Kind() != reflect.Struct { if modelType.Kind() != reflect.Struct {
if modelType.Kind() == reflect.Interface { if modelType.Kind() == reflect.Interface {
modelType = reflect.Indirect(reflect.ValueOf(dest)).Elem().Type() modelType = reflect.Indirect(reflect.ValueOf(dest)).Elem().Type()
@ -174,7 +169,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
var tableName string var tableName string
modelValue := reflect.New(modelType) modelValue := reflect.New(modelType)
if specialTableName != "" && specialTableName != tableName { if specialTableName != "" {
tableName = specialTableName tableName = specialTableName
} else if en, ok := namer.(embeddedNamer); ok { } else if en, ok := namer.(embeddedNamer); ok {
tableName = en.Table tableName = en.Table
@ -190,6 +185,8 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
Name: modelType.Name(), Name: modelType.Name(),
ModelType: modelType, ModelType: modelType,
Table: tableName, Table: tableName,
DBNames: make([]string, 0, 10),
Fields: make([]*Field, 0, 10),
FieldsByName: make(map[string]*Field, 10), FieldsByName: make(map[string]*Field, 10),
FieldsByBindName: make(map[string]*Field, 10), FieldsByBindName: make(map[string]*Field, 10),
FieldsByDBName: 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 schema.FieldsByBindName[bindName] = field
if v != nil && v.PrimaryKey { if v != nil && v.PrimaryKey {
// remove the existing primary key field
for idx, f := range schema.PrimaryFields { 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:]...) schema.PrimaryFields = append(schema.PrimaryFields[0:idx], schema.PrimaryFields[idx+1:]...)
} }
} }