gorm/model_struct.go

641 lines
24 KiB
Go
Raw Normal View History

2015-02-15 18:01:09 +03:00
package gorm
import (
"database/sql"
2016-01-03 10:22:35 +03:00
"errors"
2015-02-15 18:01:09 +03:00
"go/ast"
"reflect"
2015-02-16 11:35:26 +03:00
"strings"
"sync"
2015-02-15 18:01:09 +03:00
"time"
2015-12-03 10:18:42 +03:00
"github.com/jinzhu/inflection"
2015-02-15 18:01:09 +03:00
)
2016-03-07 09:54:20 +03:00
// DefaultTableNameHandler default table name handler
2015-05-27 07:19:48 +03:00
var DefaultTableNameHandler = func(db *DB, defaultTableName string) string {
return defaultTableName
}
var modelStructsMap sync.Map
2016-03-07 09:54:20 +03:00
// ModelStruct model definition
2015-02-16 07:04:46 +03:00
type ModelStruct struct {
2015-05-27 07:19:48 +03:00
PrimaryFields []*StructField
StructFields []*StructField
ModelType reflect.Type
2015-05-27 07:19:48 +03:00
defaultTableName string
l sync.Mutex
2015-05-27 07:19:48 +03:00
}
// TableName returns model's table name
func (s *ModelStruct) TableName(db *DB) string {
s.l.Lock()
defer s.l.Unlock()
if s.defaultTableName == "" && db != nil && s.ModelType != nil {
// Set default table name
if tabler, ok := reflect.New(s.ModelType).Interface().(tabler); ok {
s.defaultTableName = tabler.TableName()
} else {
tableName := ToTableName(s.ModelType.Name())
if db == nil || !db.parent.singularTable {
tableName = inflection.Plural(tableName)
}
s.defaultTableName = tableName
}
}
2015-05-27 07:19:48 +03:00
return DefaultTableNameHandler(db, s.defaultTableName)
2015-02-16 07:04:46 +03:00
}
2016-03-07 09:54:20 +03:00
// StructField model field's struct definition
2015-02-15 18:01:09 +03:00
type StructField struct {
2015-02-18 05:19:34 +03:00
DBName string
Name string
Names []string
IsPrimaryKey bool
IsNormal bool
IsIgnored bool
IsScanner bool
HasDefaultValue bool
Tag reflect.StructTag
2016-01-03 05:00:18 +03:00
TagSettings map[string]string
2015-02-18 05:19:34 +03:00
Struct reflect.StructField
IsForeignKey bool
Relationship *Relationship
tagSettingsLock sync.RWMutex
}
// TagSettingsSet Sets a tag in the tag settings map
func (s *StructField) TagSettingsSet(key, val string) {
s.tagSettingsLock.Lock()
defer s.tagSettingsLock.Unlock()
s.TagSettings[key] = val
}
// TagSettingsGet returns a tag from the tag settings
func (s *StructField) TagSettingsGet(key string) (string, bool) {
s.tagSettingsLock.RLock()
defer s.tagSettingsLock.RUnlock()
val, ok := s.TagSettings[key]
return val, ok
}
// TagSettingsDelete deletes a tag
func (s *StructField) TagSettingsDelete(key string) {
s.tagSettingsLock.Lock()
defer s.tagSettingsLock.Unlock()
delete(s.TagSettings, key)
2015-02-16 07:04:46 +03:00
}
2015-02-17 18:18:12 +03:00
func (structField *StructField) clone() *StructField {
2016-05-09 17:32:33 +03:00
clone := &StructField{
2015-02-18 05:19:34 +03:00
DBName: structField.DBName,
Name: structField.Name,
Names: structField.Names,
IsPrimaryKey: structField.IsPrimaryKey,
IsNormal: structField.IsNormal,
IsIgnored: structField.IsIgnored,
IsScanner: structField.IsScanner,
HasDefaultValue: structField.HasDefaultValue,
Tag: structField.Tag,
2016-05-09 17:32:33 +03:00
TagSettings: map[string]string{},
2015-02-18 05:19:34 +03:00
Struct: structField.Struct,
IsForeignKey: structField.IsForeignKey,
2017-08-11 13:03:32 +03:00
}
if structField.Relationship != nil {
relationship := *structField.Relationship
clone.Relationship = &relationship
2015-02-17 18:18:12 +03:00
}
2016-05-09 17:32:33 +03:00
// copy the struct field tagSettings, they should be read-locked while they are copied
structField.tagSettingsLock.Lock()
defer structField.tagSettingsLock.Unlock()
2016-05-09 17:32:33 +03:00
for key, value := range structField.TagSettings {
clone.TagSettings[key] = value
}
return clone
2015-02-17 18:18:12 +03:00
}
2016-01-03 10:22:35 +03:00
// Relationship described the relationship between models
2015-02-16 07:04:46 +03:00
type Relationship struct {
Kind string
PolymorphicType string
PolymorphicDBName string
PolymorphicValue string
ForeignFieldNames []string
ForeignDBNames []string
AssociationForeignFieldNames []string
AssociationForeignDBNames []string
JoinTableHandler JoinTableHandlerInterface
2015-02-15 18:01:09 +03:00
}
2016-01-03 06:11:30 +03:00
func getForeignField(column string, fields []*StructField) *StructField {
for _, field := range fields {
if field.Name == column || field.DBName == column || field.DBName == ToColumnName(column) {
2016-01-03 06:11:30 +03:00
return field
}
}
return nil
}
2016-03-07 16:09:05 +03:00
// GetModelStruct get value's model struct, relationships based on struct and tag definition
2015-02-28 10:54:38 +03:00
func (scope *Scope) GetModelStruct() *ModelStruct {
2015-02-16 11:35:26 +03:00
var modelStruct ModelStruct
2016-01-03 04:46:07 +03:00
// Scope value can't be nil
if scope.Value == nil {
2015-02-17 12:40:21 +03:00
return &modelStruct
}
2016-01-03 04:46:07 +03:00
reflectType := reflect.ValueOf(scope.Value).Type()
for reflectType.Kind() == reflect.Slice || reflectType.Kind() == reflect.Ptr {
reflectType = reflectType.Elem()
2015-02-15 18:01:09 +03:00
}
2015-02-17 13:37:47 +03:00
2016-01-03 04:46:07 +03:00
// Scope value need to be a struct
if reflectType.Kind() != reflect.Struct {
return &modelStruct
2015-02-18 03:47:44 +03:00
}
2016-01-03 04:46:07 +03:00
// Get Cached model struct
if value, ok := modelStructsMap.Load(reflectType); ok && value != nil {
return value.(*ModelStruct)
2015-02-17 18:18:12 +03:00
}
2016-01-03 04:46:07 +03:00
modelStruct.ModelType = reflectType
2015-02-16 11:35:26 +03:00
2015-02-18 06:37:08 +03:00
// Get all fields
2016-01-03 04:46:07 +03:00
for i := 0; i < reflectType.NumField(); i++ {
if fieldStruct := reflectType.Field(i); ast.IsExported(fieldStruct.Name) {
2015-02-17 17:55:14 +03:00
field := &StructField{
2016-01-03 05:00:18 +03:00
Struct: fieldStruct,
Name: fieldStruct.Name,
Names: []string{fieldStruct.Name},
Tag: fieldStruct.Tag,
TagSettings: parseTagSetting(fieldStruct.Tag),
2015-02-15 18:01:09 +03:00
}
2016-01-03 10:22:35 +03:00
// is ignored field
if _, ok := field.TagSettingsGet("-"); ok {
2015-02-17 17:55:14 +03:00
field.IsIgnored = true
} else {
if _, ok := field.TagSettingsGet("PRIMARY_KEY"); ok {
2016-01-03 06:11:30 +03:00
field.IsPrimaryKey = true
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
2015-03-19 10:02:15 +03:00
}
2015-02-15 18:01:09 +03:00
if _, ok := field.TagSettingsGet("DEFAULT"); ok {
2016-01-03 06:11:30 +03:00
field.HasDefaultValue = true
2015-03-19 10:02:15 +03:00
}
2015-02-15 18:01:09 +03:00
if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok && !field.IsPrimaryKey {
field.HasDefaultValue = true
}
2016-01-04 13:40:06 +03:00
indirectType := fieldStruct.Type
for indirectType.Kind() == reflect.Ptr {
indirectType = indirectType.Elem()
}
fieldValue := reflect.New(indirectType).Interface()
2016-01-03 06:11:30 +03:00
if _, isScanner := fieldValue.(sql.Scanner); isScanner {
// is scanner
field.IsScanner, field.IsNormal = true, true
if indirectType.Kind() == reflect.Struct {
for i := 0; i < indirectType.NumField(); i++ {
for key, value := range parseTagSetting(indirectType.Field(i).Tag) {
if _, ok := field.TagSettingsGet(key); !ok {
field.TagSettingsSet(key, value)
2017-02-06 03:43:49 +03:00
}
}
}
}
2016-01-03 06:11:30 +03:00
} else if _, isTime := fieldValue.(*time.Time); isTime {
// is time
2015-03-19 10:02:15 +03:00
field.IsNormal = true
} else if _, ok := field.TagSettingsGet("EMBEDDED"); ok || fieldStruct.Anonymous {
2016-01-03 10:22:35 +03:00
// is embedded struct
2017-01-09 13:30:21 +03:00
for _, subField := range scope.New(fieldValue).GetModelStruct().StructFields {
2016-01-03 06:11:30 +03:00
subField = subField.clone()
subField.Names = append([]string{fieldStruct.Name}, subField.Names...)
if prefix, ok := field.TagSettingsGet("EMBEDDED_PREFIX"); ok {
2016-09-05 17:26:57 +03:00
subField.DBName = prefix + subField.DBName
}
2017-01-09 13:30:21 +03:00
2016-01-03 06:11:30 +03:00
if subField.IsPrimaryKey {
if _, ok := subField.TagSettingsGet("PRIMARY_KEY"); ok {
2017-01-09 13:30:21 +03:00
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
} else {
subField.IsPrimaryKey = false
}
2015-02-18 07:30:09 +03:00
}
if subField.Relationship != nil && subField.Relationship.JoinTableHandler != nil {
if joinTableHandler, ok := subField.Relationship.JoinTableHandler.(*JoinTableHandler); ok {
2017-08-11 13:03:32 +03:00
newJoinTableHandler := &JoinTableHandler{}
newJoinTableHandler.Setup(subField.Relationship, joinTableHandler.TableName, reflectType, joinTableHandler.Destination.ModelType)
subField.Relationship.JoinTableHandler = newJoinTableHandler
}
}
2016-01-03 06:11:30 +03:00
modelStruct.StructFields = append(modelStruct.StructFields, subField)
2015-02-18 07:30:09 +03:00
}
2016-01-03 06:11:30 +03:00
continue
} else {
2016-01-03 10:22:35 +03:00
// build relationships
2015-03-19 10:02:15 +03:00
switch indirectType.Kind() {
case reflect.Slice:
2016-01-03 06:11:30 +03:00
defer func(field *StructField) {
var (
relationship = &Relationship{}
toScope = scope.New(reflect.New(field.Struct.Type).Interface())
2016-01-03 09:04:59 +03:00
foreignKeys []string
associationForeignKeys []string
2016-01-03 06:11:30 +03:00
elemType = field.Struct.Type
)
if foreignKey, _ := field.TagSettingsGet("FOREIGNKEY"); foreignKey != "" {
2018-02-10 15:57:39 +03:00
foreignKeys = strings.Split(foreignKey, ",")
2016-01-03 09:04:59 +03:00
}
if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_FOREIGNKEY"); foreignKey != "" {
2018-02-10 15:57:39 +03:00
associationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
2018-02-10 15:57:39 +03:00
associationForeignKeys = strings.Split(foreignKey, ",")
2016-01-03 09:04:59 +03:00
}
2016-01-03 06:11:30 +03:00
for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
}
2015-02-18 06:37:08 +03:00
2016-01-03 06:11:30 +03:00
if elemType.Kind() == reflect.Struct {
if many2many, _ := field.TagSettingsGet("MANY2MANY"); many2many != "" {
2016-01-03 06:11:30 +03:00
relationship.Kind = "many_to_many"
2015-07-30 09:26:48 +03:00
2018-02-10 15:57:39 +03:00
{ // Foreign Keys for Source
joinTableDBNames := []string{}
if foreignKey, _ := field.TagSettingsGet("JOINTABLE_FOREIGNKEY"); foreignKey != "" {
2018-02-10 15:57:39 +03:00
joinTableDBNames = strings.Split(foreignKey, ",")
2016-01-03 06:11:30 +03:00
}
2015-03-19 10:02:15 +03:00
2018-02-10 15:57:39 +03:00
// if no foreign keys defined with tag
if len(foreignKeys) == 0 {
for _, field := range modelStruct.PrimaryFields {
foreignKeys = append(foreignKeys, field.DBName)
}
2016-01-03 06:11:30 +03:00
}
2015-07-30 09:26:48 +03:00
2018-02-10 15:57:39 +03:00
for idx, foreignKey := range foreignKeys {
if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
// source foreign keys (db names)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.DBName)
// setup join table foreign keys for source
if len(joinTableDBNames) > idx {
// if defined join table's foreign key
relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBNames[idx])
} else {
defaultJointableForeignKey := ToColumnName(reflectType.Name()) + "_" + foreignField.DBName
2018-02-10 15:57:39 +03:00
relationship.ForeignDBNames = append(relationship.ForeignDBNames, defaultJointableForeignKey)
}
}
2016-01-03 06:11:30 +03:00
}
2015-07-30 09:26:48 +03:00
}
2018-02-10 15:57:39 +03:00
{ // Foreign Keys for Association (Destination)
associationJoinTableDBNames := []string{}
if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_JOINTABLE_FOREIGNKEY"); foreignKey != "" {
2018-02-10 15:57:39 +03:00
associationJoinTableDBNames = strings.Split(foreignKey, ",")
}
2018-02-10 15:57:39 +03:00
// if no association foreign keys defined with tag
if len(associationForeignKeys) == 0 {
for _, field := range toScope.PrimaryFields() {
associationForeignKeys = append(associationForeignKeys, field.DBName)
}
2018-02-10 15:57:39 +03:00
}
2018-02-10 15:57:39 +03:00
for idx, name := range associationForeignKeys {
if field, ok := toScope.FieldByName(name); ok {
// association foreign keys (db names)
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.DBName)
// setup join table foreign keys for association
if len(associationJoinTableDBNames) > idx {
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationJoinTableDBNames[idx])
} else {
// join table foreign keys for association
joinTableDBName := ToColumnName(elemType.Name()) + "_" + field.DBName
2018-02-10 15:57:39 +03:00
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
}
}
2016-01-03 06:11:30 +03:00
}
2015-07-30 09:26:48 +03:00
}
2015-03-19 10:02:15 +03:00
2016-01-03 06:11:30 +03:00
joinTableHandler := JoinTableHandler{}
joinTableHandler.Setup(relationship, ToTableName(many2many), reflectType, elemType)
2016-01-03 06:11:30 +03:00
relationship.JoinTableHandler = &joinTableHandler
field.Relationship = relationship
} else {
2016-01-03 10:22:35 +03:00
// User has many comments, associationType is User, comment use UserID as foreign key
var associationType = reflectType.Name()
var toFields = toScope.GetStructFields()
2016-01-03 06:11:30 +03:00
relationship.Kind = "has_many"
2015-07-30 12:58:49 +03:00
if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" {
2016-01-03 10:22:35 +03:00
// Dog has many toys, tag polymorphic is Owner, then associationType is Owner
// Toy use OwnerID, OwnerType ('dogs') as foreign key
if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
associationType = polymorphic
relationship.PolymorphicType = polymorphicType.Name
relationship.PolymorphicDBName = polymorphicType.DBName
// if Dog has multiple set of toys set name of the set (instead of default 'dogs')
if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok {
relationship.PolymorphicValue = value
} else {
relationship.PolymorphicValue = scope.TableName()
}
2016-01-03 10:22:35 +03:00
polymorphicType.IsForeignKey = true
2016-01-03 09:16:58 +03:00
}
}
2016-01-03 10:22:35 +03:00
// if no foreign keys defined with tag
2016-01-03 06:11:30 +03:00
if len(foreignKeys) == 0 {
2016-01-03 10:22:35 +03:00
// if no association foreign keys defined with tag
2016-01-03 09:04:59 +03:00
if len(associationForeignKeys) == 0 {
for _, field := range modelStruct.PrimaryFields {
2016-01-03 10:22:35 +03:00
foreignKeys = append(foreignKeys, associationType+field.Name)
2016-01-03 09:04:59 +03:00
associationForeignKeys = append(associationForeignKeys, field.Name)
}
} else {
2016-01-03 10:22:35 +03:00
// generate foreign keys from defined association foreign keys
for _, scopeFieldName := range associationForeignKeys {
if foreignField := getForeignField(scopeFieldName, modelStruct.StructFields); foreignField != nil {
foreignKeys = append(foreignKeys, associationType+foreignField.Name)
2016-01-03 09:04:59 +03:00
associationForeignKeys = append(associationForeignKeys, foreignField.Name)
}
}
2015-07-30 12:58:49 +03:00
}
2016-01-03 10:22:35 +03:00
} else {
// generate association foreign keys from foreign keys
if len(associationForeignKeys) == 0 {
for _, foreignKey := range foreignKeys {
if strings.HasPrefix(foreignKey, associationType) {
2016-03-07 18:51:04 +03:00
associationForeignKey := strings.TrimPrefix(foreignKey, associationType)
if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
associationForeignKeys = append(associationForeignKeys, associationForeignKey)
}
2016-01-03 10:22:35 +03:00
}
}
if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
associationForeignKeys = []string{scope.PrimaryKey()}
}
2016-01-03 10:22:35 +03:00
} else if len(foreignKeys) != len(associationForeignKeys) {
scope.Err(errors.New("invalid foreign keys, should have same length"))
return
}
2015-07-30 09:26:48 +03:00
}
2016-01-03 06:11:30 +03:00
2016-01-03 09:04:59 +03:00
for idx, foreignKey := range foreignKeys {
2016-01-03 10:22:35 +03:00
if foreignField := getForeignField(foreignKey, toFields); foreignField != nil {
2016-01-03 09:04:59 +03:00
if associationField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); associationField != nil {
// source foreign keys
foreignField.IsForeignKey = true
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
// association foreign keys
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
}
2015-07-30 12:58:49 +03:00
}
2015-07-30 09:26:48 +03:00
}
2016-01-03 06:11:30 +03:00
if len(relationship.ForeignFieldNames) != 0 {
field.Relationship = relationship
}
2015-03-19 10:02:15 +03:00
}
2016-01-03 06:11:30 +03:00
} else {
field.IsNormal = true
2015-02-18 07:30:09 +03:00
}
2016-01-03 06:11:30 +03:00
}(field)
2015-03-19 10:02:15 +03:00
case reflect.Struct:
2016-01-03 06:11:30 +03:00
defer func(field *StructField) {
var (
2016-01-03 10:22:35 +03:00
// user has one profile, associationType is User, profile use UserID as foreign key
// user belongs to profile, associationType is Profile, user use ProfileID as foreign key
associationType = reflectType.Name()
2016-01-03 09:04:59 +03:00
relationship = &Relationship{}
toScope = scope.New(reflect.New(field.Struct.Type).Interface())
2016-01-03 10:22:35 +03:00
toFields = toScope.GetStructFields()
2016-01-03 09:04:59 +03:00
tagForeignKeys []string
tagAssociationForeignKeys []string
2016-01-03 06:11:30 +03:00
)
if foreignKey, _ := field.TagSettingsGet("FOREIGNKEY"); foreignKey != "" {
2018-02-10 15:57:39 +03:00
tagForeignKeys = strings.Split(foreignKey, ",")
2015-02-25 05:56:05 +03:00
}
2016-01-03 06:11:30 +03:00
if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_FOREIGNKEY"); foreignKey != "" {
2018-02-10 15:57:39 +03:00
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
2018-02-10 15:57:39 +03:00
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
2016-01-03 09:04:59 +03:00
}
if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" {
2016-01-03 10:22:35 +03:00
// Cat has one toy, tag polymorphic is Owner, then associationType is Owner
// Toy use OwnerID, OwnerType ('cats') as foreign key
if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
associationType = polymorphic
relationship.PolymorphicType = polymorphicType.Name
relationship.PolymorphicDBName = polymorphicType.DBName
// if Cat has several different types of toys set name for each (instead of default 'cats')
if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok {
relationship.PolymorphicValue = value
} else {
relationship.PolymorphicValue = scope.TableName()
}
2016-01-03 10:22:35 +03:00
polymorphicType.IsForeignKey = true
2016-01-03 09:16:58 +03:00
}
}
2016-01-03 09:04:59 +03:00
// Has One
{
var foreignKeys = tagForeignKeys
var associationForeignKeys = tagAssociationForeignKeys
2016-01-03 10:22:35 +03:00
// if no foreign keys defined with tag
2016-01-03 09:04:59 +03:00
if len(foreignKeys) == 0 {
2016-01-03 10:22:35 +03:00
// if no association foreign keys defined with tag
2016-01-03 09:04:59 +03:00
if len(associationForeignKeys) == 0 {
for _, primaryField := range modelStruct.PrimaryFields {
2016-01-03 10:22:35 +03:00
foreignKeys = append(foreignKeys, associationType+primaryField.Name)
2016-01-03 09:04:59 +03:00
associationForeignKeys = append(associationForeignKeys, primaryField.Name)
}
} else {
2016-01-03 10:22:35 +03:00
// generate foreign keys form association foreign keys
2016-01-03 09:04:59 +03:00
for _, associationForeignKey := range tagAssociationForeignKeys {
if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
2016-01-03 10:22:35 +03:00
foreignKeys = append(foreignKeys, associationType+foreignField.Name)
2016-01-03 09:04:59 +03:00
associationForeignKeys = append(associationForeignKeys, foreignField.Name)
}
}
}
2016-01-03 10:22:35 +03:00
} else {
// generate association foreign keys from foreign keys
if len(associationForeignKeys) == 0 {
for _, foreignKey := range foreignKeys {
if strings.HasPrefix(foreignKey, associationType) {
2016-03-07 18:51:04 +03:00
associationForeignKey := strings.TrimPrefix(foreignKey, associationType)
if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
associationForeignKeys = append(associationForeignKeys, associationForeignKey)
}
2016-01-03 10:22:35 +03:00
}
}
if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
associationForeignKeys = []string{scope.PrimaryKey()}
}
2016-01-03 10:22:35 +03:00
} else if len(foreignKeys) != len(associationForeignKeys) {
scope.Err(errors.New("invalid foreign keys, should have same length"))
return
}
2016-01-03 09:04:59 +03:00
}
for idx, foreignKey := range foreignKeys {
2016-01-03 10:22:35 +03:00
if foreignField := getForeignField(foreignKey, toFields); foreignField != nil {
2016-01-03 09:04:59 +03:00
if scopeField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); scopeField != nil {
foreignField.IsForeignKey = true
// source foreign keys
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scopeField.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scopeField.DBName)
// association foreign keys
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
}
}
2015-07-30 09:26:48 +03:00
}
2015-02-18 07:30:09 +03:00
}
2015-03-19 10:02:15 +03:00
2015-07-30 09:26:48 +03:00
if len(relationship.ForeignFieldNames) != 0 {
relationship.Kind = "has_one"
2015-02-18 07:30:09 +03:00
field.Relationship = relationship
2015-03-19 10:02:15 +03:00
} else {
2016-01-03 09:04:59 +03:00
var foreignKeys = tagForeignKeys
var associationForeignKeys = tagAssociationForeignKeys
2016-01-03 10:22:35 +03:00
2015-07-30 13:56:05 +03:00
if len(foreignKeys) == 0 {
2016-01-03 10:22:35 +03:00
// generate foreign keys & association foreign keys
2016-01-03 09:04:59 +03:00
if len(associationForeignKeys) == 0 {
2016-01-03 10:22:35 +03:00
for _, primaryField := range toScope.PrimaryFields() {
foreignKeys = append(foreignKeys, field.Name+primaryField.Name)
associationForeignKeys = append(associationForeignKeys, primaryField.Name)
2016-01-03 09:04:59 +03:00
}
} else {
2016-01-03 10:22:35 +03:00
// generate foreign keys with association foreign keys
2016-01-03 09:04:59 +03:00
for _, associationForeignKey := range associationForeignKeys {
2016-01-03 10:22:35 +03:00
if foreignField := getForeignField(associationForeignKey, toFields); foreignField != nil {
2016-01-03 09:04:59 +03:00
foreignKeys = append(foreignKeys, field.Name+foreignField.Name)
associationForeignKeys = append(associationForeignKeys, foreignField.Name)
}
}
2015-07-30 09:26:48 +03:00
}
2016-01-03 10:22:35 +03:00
} else {
// generate foreign keys & association foreign keys
if len(associationForeignKeys) == 0 {
for _, foreignKey := range foreignKeys {
if strings.HasPrefix(foreignKey, field.Name) {
associationForeignKey := strings.TrimPrefix(foreignKey, field.Name)
if foreignField := getForeignField(associationForeignKey, toFields); foreignField != nil {
associationForeignKeys = append(associationForeignKeys, associationForeignKey)
}
2016-01-03 10:22:35 +03:00
}
}
if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
associationForeignKeys = []string{toScope.PrimaryKey()}
}
2016-01-03 10:22:35 +03:00
} else if len(foreignKeys) != len(associationForeignKeys) {
scope.Err(errors.New("invalid foreign keys, should have same length"))
return
}
2016-01-03 06:11:30 +03:00
}
2016-01-03 09:04:59 +03:00
for idx, foreignKey := range foreignKeys {
2016-01-03 06:11:30 +03:00
if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
2016-01-03 10:22:35 +03:00
if associationField := getForeignField(associationForeignKeys[idx], toFields); associationField != nil {
foreignField.IsForeignKey = true
2016-01-03 09:04:59 +03:00
// association foreign keys
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
// source foreign keys
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
}
2015-07-30 09:26:48 +03:00
}
}
if len(relationship.ForeignFieldNames) != 0 {
relationship.Kind = "belongs_to"
2015-03-19 10:02:15 +03:00
field.Relationship = relationship
}
2015-02-18 07:30:09 +03:00
}
2016-01-03 06:11:30 +03:00
}(field)
2015-03-19 10:02:15 +03:00
default:
field.IsNormal = true
2015-02-15 18:01:09 +03:00
}
}
2016-01-03 06:11:30 +03:00
}
2015-02-15 18:01:09 +03:00
2016-01-03 06:11:30 +03:00
// Even it is ignored, also possible to decode db value into the field
if value, ok := field.TagSettingsGet("COLUMN"); ok {
2016-01-03 06:11:30 +03:00
field.DBName = value
} else {
field.DBName = ToColumnName(fieldStruct.Name)
2015-02-17 12:40:21 +03:00
}
2016-01-03 06:11:30 +03:00
2015-03-19 10:02:15 +03:00
modelStruct.StructFields = append(modelStruct.StructFields, field)
2015-02-16 12:10:13 +03:00
}
2016-01-03 06:11:30 +03:00
}
2015-02-15 18:01:09 +03:00
2016-01-03 06:11:30 +03:00
if len(modelStruct.PrimaryFields) == 0 {
if field := getForeignField("id", modelStruct.StructFields); field != nil {
field.IsPrimaryKey = true
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
}
}
2015-02-18 03:47:44 +03:00
modelStructsMap.Store(reflectType, &modelStruct)
2015-02-16 11:35:26 +03:00
return &modelStruct
}
2015-02-15 18:01:09 +03:00
2016-03-07 09:54:20 +03:00
// GetStructFields get model's field structs
2015-02-16 11:35:26 +03:00
func (scope *Scope) GetStructFields() (fields []*StructField) {
return scope.GetModelStruct().StructFields
2015-02-15 18:01:09 +03:00
}
2015-02-24 17:06:35 +03:00
2016-01-03 04:52:27 +03:00
func parseTagSetting(tags reflect.StructTag) map[string]string {
2015-02-24 17:06:35 +03:00
setting := map[string]string{}
2016-01-03 04:52:27 +03:00
for _, str := range []string{tags.Get("sql"), tags.Get("gorm")} {
tags := strings.Split(str, ";")
for _, value := range tags {
v := strings.Split(value, ":")
k := strings.TrimSpace(strings.ToUpper(v[0]))
if len(v) >= 2 {
setting[k] = strings.Join(v[1:], ":")
} else {
setting[k] = k
}
2015-02-24 17:06:35 +03:00
}
}
return setting
}