2015-02-15 18:01:09 +03:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2016-01-03 10:22:35 +03:00
|
|
|
"errors"
|
2015-02-16 11:35:26 +03:00
|
|
|
"fmt"
|
2015-02-15 18:01:09 +03:00
|
|
|
"go/ast"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2015-02-16 11:35:26 +03:00
|
|
|
"strings"
|
2015-10-01 02:09:00 +03:00
|
|
|
"sync"
|
2015-02-15 18:01:09 +03:00
|
|
|
"time"
|
2015-08-01 06:20:08 +03:00
|
|
|
|
2015-12-03 10:18:42 +03:00
|
|
|
"github.com/jinzhu/inflection"
|
2015-02-15 18:01:09 +03:00
|
|
|
)
|
|
|
|
|
2015-05-27 07:19:48 +03:00
|
|
|
var DefaultTableNameHandler = func(db *DB, defaultTableName string) string {
|
|
|
|
return defaultTableName
|
|
|
|
}
|
|
|
|
|
2015-10-01 02:09:00 +03:00
|
|
|
type safeModelStructsMap struct {
|
|
|
|
m map[reflect.Type]*ModelStruct
|
|
|
|
l *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *safeModelStructsMap) Set(key reflect.Type, value *ModelStruct) {
|
|
|
|
s.l.Lock()
|
|
|
|
defer s.l.Unlock()
|
|
|
|
s.m[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *safeModelStructsMap) Get(key reflect.Type) *ModelStruct {
|
|
|
|
s.l.RLock()
|
|
|
|
defer s.l.RUnlock()
|
|
|
|
return s.m[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
func newModelStructsMap() *safeModelStructsMap {
|
|
|
|
return &safeModelStructsMap{l: new(sync.RWMutex), m: make(map[reflect.Type]*ModelStruct)}
|
|
|
|
}
|
|
|
|
|
|
|
|
var modelStructsMap = newModelStructsMap()
|
|
|
|
|
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
|
|
|
|
defaultTableName string
|
|
|
|
}
|
|
|
|
|
2015-12-23 13:42:03 +03:00
|
|
|
func (s *ModelStruct) TableName(db *DB) string {
|
2015-05-27 07:19:48 +03:00
|
|
|
return DefaultTableNameHandler(db, s.defaultTableName)
|
2015-02-16 07:04:46 +03:00
|
|
|
}
|
|
|
|
|
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
|
2015-02-16 07:04:46 +03:00
|
|
|
}
|
|
|
|
|
2015-02-17 18:18:12 +03:00
|
|
|
func (structField *StructField) clone() *StructField {
|
|
|
|
return &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,
|
|
|
|
Struct: structField.Struct,
|
|
|
|
IsForeignKey: structField.IsForeignKey,
|
|
|
|
Relationship: structField.Relationship,
|
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 {
|
2015-12-31 05:55:12 +03:00
|
|
|
Kind string
|
|
|
|
PolymorphicType string
|
|
|
|
PolymorphicDBName 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 {
|
2016-01-04 03:26:02 +03:00
|
|
|
if field.Name == column || field.DBName == column || field.DBName == ToDBName(column) {
|
2016-01-03 06:11:30 +03:00
|
|
|
return field
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-03 10:22:35 +03:00
|
|
|
// GetModelStruct generate 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 := modelStructsMap.Get(reflectType); value != nil {
|
2015-03-11 13:33:50 +03:00
|
|
|
return value
|
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
|
|
|
|
2016-01-03 04:46:07 +03:00
|
|
|
// Set default table name
|
|
|
|
if tabler, ok := reflect.New(reflectType).Interface().(tabler); ok {
|
2015-05-27 07:19:48 +03:00
|
|
|
modelStruct.defaultTableName = tabler.TableName()
|
2015-02-16 11:35:26 +03:00
|
|
|
} else {
|
2016-01-03 04:46:07 +03:00
|
|
|
tableName := ToDBName(reflectType.Name())
|
2015-02-16 11:35:26 +03:00
|
|
|
if scope.db == nil || !scope.db.parent.singularTable {
|
2016-01-03 04:46:07 +03:00
|
|
|
tableName = inflection.Plural(tableName)
|
2015-02-16 11:35:26 +03:00
|
|
|
}
|
2016-01-03 04:46:07 +03:00
|
|
|
modelStruct.defaultTableName = tableName
|
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
|
2015-02-17 17:55:14 +03:00
|
|
|
if fieldStruct.Tag.Get("sql") == "-" {
|
|
|
|
field.IsIgnored = true
|
2015-12-11 06:45:22 +03:00
|
|
|
} else {
|
2016-01-03 06:11:30 +03:00
|
|
|
if _, ok := field.TagSettings["PRIMARY_KEY"]; ok {
|
|
|
|
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
|
|
|
|
2016-01-03 06:11:30 +03:00
|
|
|
if _, ok := field.TagSettings["DEFAULT"]; ok {
|
|
|
|
field.HasDefaultValue = true
|
2015-03-19 10:02:15 +03:00
|
|
|
}
|
2015-02-15 18:01:09 +03:00
|
|
|
|
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
|
|
|
|
} else if _, isTime := fieldValue.(*time.Time); isTime {
|
|
|
|
// is time
|
2015-03-19 10:02:15 +03:00
|
|
|
field.IsNormal = true
|
2016-01-03 06:11:30 +03:00
|
|
|
} else if _, ok := field.TagSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
2016-01-03 10:22:35 +03:00
|
|
|
// is embedded struct
|
2016-01-03 06:11:30 +03:00
|
|
|
for _, subField := range scope.New(fieldValue).GetStructFields() {
|
|
|
|
subField = subField.clone()
|
|
|
|
subField.Names = append([]string{fieldStruct.Name}, subField.Names...)
|
|
|
|
if subField.IsPrimaryKey {
|
|
|
|
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
|
2015-02-18 07:30:09 +03:00
|
|
|
}
|
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
|
|
|
|
)
|
|
|
|
|
2016-01-03 09:04:59 +03:00
|
|
|
if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
|
2016-01-03 12:20:24 +03:00
|
|
|
foreignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ",")
|
2016-01-03 09:04:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
|
2016-01-03 12:20:24 +03:00
|
|
|
associationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ",")
|
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.TagSettings["MANY2MANY"]; many2many != "" {
|
|
|
|
relationship.Kind = "many_to_many"
|
2015-07-30 09:26:48 +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
|
|
|
for _, field := range modelStruct.PrimaryFields {
|
2016-01-03 06:11:30 +03:00
|
|
|
foreignKeys = append(foreignKeys, field.DBName)
|
|
|
|
}
|
2015-07-30 09:26:48 +03:00
|
|
|
}
|
2015-03-19 10:02:15 +03:00
|
|
|
|
2016-01-03 06:11:30 +03:00
|
|
|
for _, foreignKey := range foreignKeys {
|
2016-01-03 10:22:35 +03:00
|
|
|
if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
|
2016-01-03 06:11:30 +03:00
|
|
|
// source foreign keys (db names)
|
2016-01-03 10:22:35 +03:00
|
|
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.DBName)
|
2016-01-03 06:11:30 +03:00
|
|
|
// join table foreign keys for source
|
2016-01-03 10:22:35 +03:00
|
|
|
joinTableDBName := ToDBName(reflectType.Name()) + "_" + foreignField.DBName
|
2016-01-03 06:11:30 +03:00
|
|
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBName)
|
|
|
|
}
|
2015-07-30 09:26:48 +03:00
|
|
|
}
|
|
|
|
|
2016-01-03 10:22:35 +03:00
|
|
|
// if no association foreign keys defined with tag
|
2016-01-03 06:11:30 +03:00
|
|
|
if len(associationForeignKeys) == 0 {
|
|
|
|
for _, field := range toScope.PrimaryFields() {
|
|
|
|
associationForeignKeys = append(associationForeignKeys, field.DBName)
|
|
|
|
}
|
2015-07-30 09:26:48 +03:00
|
|
|
}
|
|
|
|
|
2016-01-03 06:11:30 +03:00
|
|
|
for _, name := range associationForeignKeys {
|
|
|
|
if field, ok := toScope.FieldByName(name); ok {
|
|
|
|
// association foreign keys (db names)
|
|
|
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.DBName)
|
|
|
|
// join table foreign keys for association
|
|
|
|
joinTableDBName := ToDBName(elemType.Name()) + "_" + field.DBName
|
|
|
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
|
|
|
|
}
|
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, many2many, reflectType, elemType)
|
|
|
|
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
|
|
|
|
2016-01-03 09:16:58 +03:00
|
|
|
if polymorphic := field.TagSettings["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
|
|
|
|
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) {
|
|
|
|
associationForeignKeys = append(associationForeignKeys, strings.TrimPrefix(foreignKey, associationType))
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 07:21: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
|
|
|
)
|
|
|
|
|
2016-01-03 09:04:59 +03:00
|
|
|
if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
|
2016-01-03 12:20:24 +03:00
|
|
|
tagForeignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ",")
|
2015-02-25 05:56:05 +03:00
|
|
|
}
|
2016-01-03 06:11:30 +03:00
|
|
|
|
2016-01-03 09:04:59 +03:00
|
|
|
if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
|
2016-01-03 12:20:24 +03:00
|
|
|
tagAssociationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ",")
|
2016-01-03 09:04:59 +03:00
|
|
|
}
|
|
|
|
|
2016-01-03 09:16:58 +03:00
|
|
|
if polymorphic := field.TagSettings["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
|
|
|
|
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) {
|
|
|
|
associationForeignKeys = append(associationForeignKeys, strings.TrimPrefix(foreignKey, associationType))
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 07:21: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 {
|
2015-07-30 17:59:25 +03:00
|
|
|
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) {
|
|
|
|
associationForeignKeys = append(associationForeignKeys, strings.TrimPrefix(foreignKey, field.Name))
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 07:21: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 {
|
2015-07-30 17:59:25 +03:00
|
|
|
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.TagSettings["COLUMN"]; ok {
|
|
|
|
field.DBName = value
|
|
|
|
} else {
|
|
|
|
field.DBName = ToDBName(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
|
|
|
|
2016-01-03 06:11:30 +03:00
|
|
|
modelStructsMap.Set(reflectType, &modelStruct)
|
2015-09-11 02:35:57 +03:00
|
|
|
|
2015-02-16 11:35:26 +03:00
|
|
|
return &modelStruct
|
|
|
|
}
|
2015-02-15 18:01:09 +03:00
|
|
|
|
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
|
|
|
|
2015-03-11 12:05:58 +03:00
|
|
|
func (scope *Scope) generateSqlTag(field *StructField) string {
|
2015-02-24 17:06:35 +03:00
|
|
|
var sqlType string
|
|
|
|
structType := field.Struct.Type
|
|
|
|
if structType.Kind() == reflect.Ptr {
|
|
|
|
structType = structType.Elem()
|
|
|
|
}
|
|
|
|
reflectValue := reflect.Indirect(reflect.New(structType))
|
|
|
|
|
2016-01-03 05:00:18 +03:00
|
|
|
if value, ok := field.TagSettings["TYPE"]; ok {
|
2015-02-24 17:06:35 +03:00
|
|
|
sqlType = value
|
|
|
|
}
|
|
|
|
|
2016-01-03 05:00:18 +03:00
|
|
|
additionalType := field.TagSettings["NOT NULL"] + " " + field.TagSettings["UNIQUE"]
|
|
|
|
if value, ok := field.TagSettings["DEFAULT"]; ok {
|
2015-03-20 12:21:13 +03:00
|
|
|
additionalType = additionalType + " DEFAULT " + value
|
2015-02-24 17:06:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if field.IsScanner {
|
|
|
|
var getScannerValue func(reflect.Value)
|
|
|
|
getScannerValue = func(value reflect.Value) {
|
|
|
|
reflectValue = value
|
|
|
|
if _, isScanner := reflect.New(reflectValue.Type()).Interface().(sql.Scanner); isScanner && reflectValue.Kind() == reflect.Struct {
|
|
|
|
getScannerValue(reflectValue.Field(0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getScannerValue(reflectValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sqlType == "" {
|
|
|
|
var size = 255
|
|
|
|
|
2016-01-03 05:00:18 +03:00
|
|
|
if value, ok := field.TagSettings["SIZE"]; ok {
|
2015-02-24 17:06:35 +03:00
|
|
|
size, _ = strconv.Atoi(value)
|
|
|
|
}
|
|
|
|
|
2016-01-03 05:00:18 +03:00
|
|
|
v, autoIncrease := field.TagSettings["AUTO_INCREMENT"]
|
2015-02-24 17:06:35 +03:00
|
|
|
if field.IsPrimaryKey {
|
2015-03-11 12:05:58 +03:00
|
|
|
autoIncrease = true
|
2015-02-24 17:06:35 +03:00
|
|
|
}
|
2015-11-22 04:35:18 +03:00
|
|
|
if v == "FALSE" {
|
2015-11-21 12:15:47 +03:00
|
|
|
autoIncrease = false
|
|
|
|
}
|
2015-03-11 12:05:58 +03:00
|
|
|
|
|
|
|
sqlType = scope.Dialect().SqlTag(reflectValue, size, autoIncrease)
|
2015-02-24 17:06:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(additionalType) == "" {
|
2015-03-11 12:05:58 +03:00
|
|
|
return sqlType
|
2015-02-24 17:06:35 +03:00
|
|
|
} else {
|
2015-03-11 12:05:58 +03:00
|
|
|
return fmt.Sprintf("%v %v", sqlType, additionalType)
|
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
|
|
|
|
}
|