// Package schema provides the Schema struct and related functions for parsing and managing database schemas in GORM.
package schema import ( ) type callbackType string const ( callbackTypeBeforeCreate callbackType = "BeforeCreate" callbackTypeBeforeUpdate callbackType = "BeforeUpdate" callbackTypeAfterCreate callbackType = "AfterCreate" callbackTypeAfterUpdate callbackType = "AfterUpdate" callbackTypeBeforeSave callbackType = "BeforeSave" callbackTypeAfterSave callbackType = "AfterSave" callbackTypeBeforeDelete callbackType = "BeforeDelete" callbackTypeAfterDelete callbackType = "AfterDelete" callbackTypeAfterFind callbackType = "AfterFind" ) // ErrUnsupportedDataType unsupported data type var ErrUnsupportedDataType = errors.New("unsupported data type") type Schema struct { Name string ModelType reflect.Type Table string PrioritizedPrimaryField *Field DBNames []string PrimaryFields []*Field PrimaryFieldDBNames []string Fields []*Field FieldsByName map[string]*Field FieldsByBindName map[string]*Field // embedded fields is 'Embed.Field' FieldsByDBName map[string]*Field FieldsWithDefaultDBValue []*Field // fields with default value assigned by database Relationships Relationships CreateClauses []clause.Interface QueryClauses []clause.Interface UpdateClauses []clause.Interface DeleteClauses []clause.Interface BeforeCreate, AfterCreate bool BeforeUpdate, AfterUpdate bool BeforeDelete, AfterDelete bool BeforeSave, AfterSave bool AfterFind bool err error initialized chan struct{} namer Namer cacheStore *sync.Map } func ( *Schema) () string { if .ModelType.Name() == "" { return fmt.Sprintf("%s(%s)", .Name, .Table) } return fmt.Sprintf("%s.%s", .ModelType.PkgPath(), .ModelType.Name()) } func ( *Schema) () reflect.Value { := reflect.MakeSlice(reflect.SliceOf(reflect.PointerTo(.ModelType)), 0, 20) := reflect.New(.Type()) .Elem().Set() return } func ( *Schema) ( string) *Field { if , := .FieldsByDBName[]; { return } if , := .FieldsByName[]; { return } // Lookup field using namer-driven ColumnName if .namer == nil { return nil } := .namer.ColumnName(.Table, ) if , := .FieldsByDBName[]; { return } return nil } // LookUpFieldByBindName looks for the closest field in the embedded struct. // // type Struct struct { // Embedded struct { // ID string // is selected by LookUpFieldByBindName([]string{"Embedded", "ID"}, "ID") // } // ID string // is selected by LookUpFieldByBindName([]string{"ID"}, "ID") // } func ( *Schema) ( []string, string) *Field { for := len() - 1; >= 0; -- { := strings.Join([:], ".") + "." + if , := .FieldsByBindName[]; { return } } return nil } type Tabler interface { TableName() string } type TablerWithNamer interface { TableName(Namer) string } var callbackTypes = []callbackType{ callbackTypeBeforeCreate, callbackTypeAfterCreate, callbackTypeBeforeUpdate, callbackTypeAfterUpdate, callbackTypeBeforeSave, callbackTypeAfterSave, callbackTypeBeforeDelete, callbackTypeAfterDelete, callbackTypeAfterFind, } // Parse get data type from dialector func ( interface{}, *sync.Map, Namer) (*Schema, error) { return ParseWithSpecialTableName(, , , "") } // ParseWithSpecialTableName get data type from dialector with extra schema table func ( interface{}, *sync.Map, Namer, string) (*Schema, error) { if == nil { return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, ) } := reflect.ValueOf().Type() if .Kind() == reflect.Ptr { = .Elem() } if .Kind() != reflect.Struct { if .Kind() == reflect.Interface { = reflect.Indirect(reflect.ValueOf()).Elem().Type() } for .Kind() == reflect.Slice || .Kind() == reflect.Array || .Kind() == reflect.Ptr { = .Elem() } if .Kind() != reflect.Struct { if .PkgPath() == "" { return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, ) } return nil, fmt.Errorf("%w: %s.%s", ErrUnsupportedDataType, .PkgPath(), .Name()) } } // Cache the Schema for performance, // Use the modelType or modelType + schemaTable (if it present) as cache key. var interface{} = if != "" { = fmt.Sprintf("%p-%s", , ) } // Load exist schema cache, return if exists if , := .Load(); { := .(*Schema) // Wait for the initialization of other goroutines to complete <-.initialized return , .err } var string := reflect.New() if != "" { = } else if , := .(embeddedNamer); { = .Table } else if , := .Interface().(Tabler); { = .TableName() } else if , := .Interface().(TablerWithNamer); { = .TableName() } else { = .TableName(.Name()) } := &Schema{ Name: .Name(), ModelType: , Table: , 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), Relationships: Relationships{Relations: map[string]*Relationship{}}, cacheStore: , namer: , initialized: make(chan struct{}), } // When the schema initialization is completed, the channel will be closed defer close(.initialized) // Load exist schema cache, return if exists if , := .Load(); { := .(*Schema) // Wait for the initialization of other goroutines to complete <-.initialized return , .err } for := 0; < .NumField(); ++ { if := .Field(); ast.IsExported(.Name) { if := .ParseField(); .EmbeddedSchema != nil { .Fields = append(.Fields, .EmbeddedSchema.Fields...) } else { .Fields = append(.Fields, ) } } } for , := range .Fields { if .DBName == "" && .DataType != "" { .DBName = .ColumnName(.Table, .Name) } := .BindName() if .DBName != "" { // nonexistence or shortest path or first appear prioritized if has permission if , := .FieldsByDBName[.DBName]; ! || ((.Creatable || .Updatable || .Readable) && len(.BindNames) < len(.BindNames)) { if , := .FieldsByDBName[.DBName]; ! { .DBNames = append(.DBNames, .DBName) } .FieldsByDBName[.DBName] = .FieldsByName[.Name] = .FieldsByBindName[] = if != nil && .PrimaryKey { // remove the existing primary key field for , := range .PrimaryFields { if .DBName == .DBName { .PrimaryFields = append(.PrimaryFields[0:], .PrimaryFields[+1:]...) } } } if .PrimaryKey { .PrimaryFields = append(.PrimaryFields, ) } } } if , := .FieldsByName[.Name]; ! || .TagSettings["-"] == "-" { .FieldsByName[.Name] = } if , := .FieldsByBindName[]; ! || .TagSettings["-"] == "-" { .FieldsByBindName[] = } .setupValuerAndSetter() } := .LookUpField("id") if == nil { = .LookUpField("ID") } if != nil { if .PrimaryKey { .PrioritizedPrimaryField = } else if len(.PrimaryFields) == 0 { .PrimaryKey = true .PrioritizedPrimaryField = .PrimaryFields = append(.PrimaryFields, ) } } if .PrioritizedPrimaryField == nil { if len(.PrimaryFields) == 1 { .PrioritizedPrimaryField = .PrimaryFields[0] } else if len(.PrimaryFields) > 1 { // If there are multiple primary keys, the AUTOINCREMENT field is prioritized for , := range .PrimaryFields { if .AutoIncrement { .PrioritizedPrimaryField = break } } } } for , := range .PrimaryFields { .PrimaryFieldDBNames = append(.PrimaryFieldDBNames, .DBName) } , := .cacheStore.Load(embeddedCacheKey) := []*Field{} for , := range .Fields { if .DataType != "" && .HasDefaultValue && .DefaultValueInterface == nil { .FieldsWithDefaultDBValue = append(.FieldsWithDefaultDBValue, ) } if ! { if .DataType == "" && .GORMDataType == "" && (.Creatable || .Updatable || .Readable) { = append(, ) .FieldsByName[.Name] = .FieldsByBindName[.BindName()] = } := reflect.New(.IndirectFieldType).Interface() if , := .(CreateClausesInterface); { .Schema.CreateClauses = append(.Schema.CreateClauses, .CreateClauses()...) } if , := .(QueryClausesInterface); { .Schema.QueryClauses = append(.Schema.QueryClauses, .QueryClauses()...) } if , := .(UpdateClausesInterface); { .Schema.UpdateClauses = append(.Schema.UpdateClauses, .UpdateClauses()...) } if , := .(DeleteClausesInterface); { .Schema.DeleteClauses = append(.Schema.DeleteClauses, .DeleteClauses()...) } } } if := .PrioritizedPrimaryField; != nil { switch .GORMDataType { case Int, Uint: if , := .TagSettings["AUTOINCREMENT"]; ! { if !.HasDefaultValue || .DefaultValueInterface != nil { .FieldsWithDefaultDBValue = append(.FieldsWithDefaultDBValue, ) } .HasDefaultValue = true .AutoIncrement = true } } } // Cache the schema if , := .LoadOrStore(, ); { := .(*Schema) // Wait for the initialization of other goroutines to complete <-.initialized return , .err } defer func() { if .err != nil { logger.Default.Error(context.Background(), .err.Error()) .Delete() } }() for , := range callbackTypes { if := .MethodByName(string()); .IsValid() { switch .Type().String() { case "func(*gorm.DB) error": := path.Dir(reflect.TypeOf().Elem().PkgPath()) if := .Type().In(0).Elem().PkgPath(); == { reflect.Indirect(reflect.ValueOf()).FieldByName(string()).SetBool(true) } else { logger.Default.Warn(context.Background(), "In model %v, the hook function `%v(*gorm.DB) error` has an incorrect parameter type. The expected parameter type is `%v`, but the provided type is `%v`.", , , , ) // PASS } default: logger.Default.Warn(context.Background(), "Model %v don't match %vInterface, should be `%v(*gorm.DB) error`. Please see https://gorm.io/docs/hooks.html", , , ) } } } // parse relationships for , := range { if .parseRelation(); .err != nil { return , .err } } return , .err } func getOrParse( interface{}, *sync.Map, Namer) (*Schema, error) { := reflect.ValueOf().Type() if .Kind() != reflect.Struct { for .Kind() == reflect.Slice || .Kind() == reflect.Array || .Kind() == reflect.Ptr { = .Elem() } if .Kind() != reflect.Struct { if .PkgPath() == "" { return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, ) } return nil, fmt.Errorf("%w: %s.%s", ErrUnsupportedDataType, .PkgPath(), .Name()) } } if , := .Load(); { return .(*Schema), nil } return Parse(, , ) }