gorm/schema/schema.go

156 lines
4.2 KiB
Go
Raw Normal View History

2020-01-31 07:22:37 +03:00
package schema
import (
2020-02-02 09:40:44 +03:00
"errors"
"fmt"
2020-01-31 07:22:37 +03:00
"go/ast"
"reflect"
"sync"
"github.com/jinzhu/gorm/logger"
2020-01-31 07:22:37 +03:00
)
2020-02-02 09:40:44 +03:00
// ErrUnsupportedDataType unsupported data type
var ErrUnsupportedDataType = errors.New("unsupported data type")
2020-01-31 07:22:37 +03:00
type Schema struct {
Name string
2020-01-31 07:22:37 +03:00
ModelType reflect.Type
Table string
PrioritizedPrimaryField *Field
PrimaryFields []*Field
Fields []*Field
FieldsByName map[string]*Field
FieldsByDBName map[string]*Field
Relationships Relationships
err error
2020-01-31 09:31:15 +03:00
namer Namer
cacheStore *sync.Map
}
func (schema Schema) String() string {
if schema.ModelType.Name() == "" {
return fmt.Sprintf("%v(%v)", schema.Name, schema.Table)
}
2020-02-01 16:48:06 +03:00
return fmt.Sprintf("%v.%v", schema.ModelType.PkgPath(), schema.ModelType.Name())
}
func (schema Schema) LookUpField(name string) *Field {
if field, ok := schema.FieldsByDBName[name]; ok {
return field
}
if field, ok := schema.FieldsByName[name]; ok {
return field
}
return nil
2020-01-31 07:22:37 +03:00
}
// get data type from dialector
func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) {
2020-01-31 07:22:37 +03:00
modelType := reflect.ValueOf(dest).Type()
for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Ptr {
modelType = modelType.Elem()
}
if modelType.Kind() != reflect.Struct {
if modelType.PkgPath() == "" {
2020-02-02 09:40:44 +03:00
return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest)
}
2020-02-02 09:40:44 +03:00
return nil, fmt.Errorf("%w: %v.%v", ErrUnsupportedDataType, modelType.PkgPath(), modelType.Name())
2020-01-31 07:22:37 +03:00
}
if v, ok := cacheStore.Load(modelType); ok {
return v.(*Schema), nil
2020-01-31 07:22:37 +03:00
}
schema := &Schema{
Name: modelType.Name(),
2020-01-31 07:22:37 +03:00
ModelType: modelType,
2020-01-31 09:31:15 +03:00
Table: namer.TableName(modelType.Name()),
2020-01-31 07:22:37 +03:00
FieldsByName: map[string]*Field{},
FieldsByDBName: map[string]*Field{},
2020-02-01 16:48:06 +03:00
Relationships: Relationships{Relations: map[string]*Relationship{}},
cacheStore: cacheStore,
namer: namer,
2020-01-31 07:22:37 +03:00
}
defer func() {
if schema.err != nil {
logger.Default.Error(schema.err.Error())
cacheStore.Delete(modelType)
2020-01-31 07:22:37 +03:00
}
}()
2020-01-31 07:22:37 +03:00
for i := 0; i < modelType.NumField(); i++ {
if fieldStruct := modelType.Field(i); ast.IsExported(fieldStruct.Name) {
2020-02-01 16:48:06 +03:00
if field := schema.ParseField(fieldStruct); field.EmbeddedSchema != nil {
schema.Fields = append(schema.Fields, field.EmbeddedSchema.Fields...)
2020-02-01 16:48:06 +03:00
} else {
schema.Fields = append(schema.Fields, field)
2020-01-31 09:31:15 +03:00
}
}
2020-01-31 07:22:37 +03:00
}
for _, field := range schema.Fields {
2020-02-02 09:40:44 +03:00
if field.DBName == "" && field.DataType != "" {
field.DBName = namer.ColumnName(schema.Table, field.Name)
2020-01-31 09:31:15 +03:00
}
2020-01-31 07:22:37 +03:00
if field.DBName != "" {
2020-01-31 09:31:15 +03:00
// nonexistence or shortest path or first appear prioritized if has permission
if v, ok := schema.FieldsByDBName[field.DBName]; !ok || (field.Creatable && len(field.BindNames) < len(v.BindNames)) {
2020-01-31 07:22:37 +03:00
schema.FieldsByDBName[field.DBName] = field
schema.FieldsByName[field.Name] = field
2020-02-01 16:48:06 +03:00
if v != nil && v.PrimaryKey {
if schema.PrioritizedPrimaryField == v {
schema.PrioritizedPrimaryField = nil
}
for idx, f := range schema.PrimaryFields {
if f == v {
schema.PrimaryFields = append(schema.PrimaryFields[0:idx], schema.PrimaryFields[idx+1:]...)
} else if schema.PrioritizedPrimaryField == nil {
schema.PrioritizedPrimaryField = f
}
}
}
if field.PrimaryKey {
if schema.PrioritizedPrimaryField == nil {
schema.PrioritizedPrimaryField = field
}
schema.PrimaryFields = append(schema.PrimaryFields, field)
}
2020-01-31 07:22:37 +03:00
}
}
if _, ok := schema.FieldsByName[field.Name]; !ok {
schema.FieldsByName[field.Name] = field
}
}
2020-02-01 16:48:06 +03:00
if f := schema.LookUpField("id"); f != nil {
if f.PrimaryKey {
schema.PrioritizedPrimaryField = f
} else if len(schema.PrimaryFields) == 0 {
f.PrimaryKey = true
schema.PrioritizedPrimaryField = f
schema.PrimaryFields = append(schema.PrimaryFields, f)
2020-01-31 07:22:37 +03:00
}
2020-02-01 16:48:06 +03:00
}
2020-01-31 07:22:37 +03:00
2020-02-01 16:48:06 +03:00
cacheStore.Store(modelType, schema)
2020-02-01 16:48:06 +03:00
// parse relations for unidentified fields
for _, field := range schema.Fields {
if field.DataType == "" && field.Creatable {
if schema.parseRelation(field); schema.err != nil {
return schema, schema.err
}
}
2020-01-31 07:22:37 +03:00
}
return schema, schema.err
2020-01-31 07:22:37 +03:00
}