gorm/model_struct.go

446 lines
15 KiB
Go
Raw Normal View History

2015-02-15 18:01:09 +03:00
package gorm
import (
"database/sql"
2015-02-16 11:35:26 +03:00
"fmt"
2015-02-15 18:01:09 +03:00
"go/ast"
"reflect"
2015-02-16 11:35:26 +03:00
"regexp"
2015-02-15 18:01:09 +03:00
"strconv"
2015-02-16 11:35:26 +03:00
"strings"
2015-02-15 18:01:09 +03:00
"time"
)
2015-03-11 13:33:50 +03:00
var modelStructs = map[reflect.Type]*ModelStruct{}
2015-05-27 07:19:48 +03:00
var DefaultTableNameHandler = func(db *DB, defaultTableName string) string {
return defaultTableName
}
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
}
func (s ModelStruct) TableName(db *DB) string {
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
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
}
}
2015-02-16 07:04:46 +03:00
type Relationship struct {
2015-07-30 09:26:48 +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
}
2015-02-16 11:35:26 +03:00
var pluralMapKeys = []*regexp.Regexp{regexp.MustCompile("ch$"), regexp.MustCompile("ss$"), regexp.MustCompile("sh$"), regexp.MustCompile("day$"), regexp.MustCompile("y$"), regexp.MustCompile("x$"), regexp.MustCompile("([^s])s?$")}
var pluralMapValues = []string{"ches", "sses", "shes", "days", "ies", "xes", "${1}s"}
2015-02-28 10:54:38 +03:00
func (scope *Scope) GetModelStruct() *ModelStruct {
2015-02-16 11:35:26 +03:00
var modelStruct ModelStruct
2015-02-15 18:01:09 +03:00
reflectValue := reflect.Indirect(reflect.ValueOf(scope.Value))
2015-02-17 12:40:21 +03:00
if !reflectValue.IsValid() {
return &modelStruct
}
2015-02-15 18:01:09 +03:00
if reflectValue.Kind() == reflect.Slice {
2015-02-17 09:30:37 +03:00
reflectValue = reflect.Indirect(reflect.New(reflectValue.Type().Elem()))
2015-02-15 18:01:09 +03:00
}
2015-02-17 13:37:47 +03:00
2015-02-17 12:40:21 +03:00
scopeType := reflectValue.Type()
2015-02-18 03:47:44 +03:00
if scopeType.Kind() == reflect.Ptr {
scopeType = scopeType.Elem()
}
2015-03-11 13:33:50 +03:00
if value, ok := modelStructs[scopeType]; ok {
return value
2015-02-17 18:18:12 +03:00
}
2015-02-18 07:30:09 +03:00
modelStruct.ModelType = scopeType
2015-02-17 12:40:21 +03:00
if scopeType.Kind() != reflect.Struct {
return &modelStruct
}
2015-02-16 11:35:26 +03:00
// Set tablename
2015-05-27 07:19:48 +03:00
type tabler interface {
TableName() string
}
if tabler, ok := reflect.New(scopeType).Interface().(interface {
TableName() string
}); ok {
modelStruct.defaultTableName = tabler.TableName()
2015-02-16 11:35:26 +03:00
} else {
2015-04-08 06:36:01 +03:00
name := ToDBName(scopeType.Name())
2015-02-16 11:35:26 +03:00
if scope.db == nil || !scope.db.parent.singularTable {
for index, reg := range pluralMapKeys {
2015-04-08 06:36:01 +03:00
if reg.MatchString(name) {
name = reg.ReplaceAllString(name, pluralMapValues[index])
2015-02-16 11:35:26 +03:00
}
}
}
2015-04-08 06:36:01 +03:00
2015-05-27 07:19:48 +03:00
modelStruct.defaultTableName = name
2015-02-16 11:35:26 +03:00
}
2015-02-18 06:37:08 +03:00
// Get all fields
fields := []*StructField{}
2015-02-17 12:40:21 +03:00
for i := 0; i < scopeType.NumField(); i++ {
2015-02-17 17:55:14 +03:00
if fieldStruct := scopeType.Field(i); ast.IsExported(fieldStruct.Name) {
field := &StructField{
Struct: fieldStruct,
Name: fieldStruct.Name,
Names: []string{fieldStruct.Name},
Tag: fieldStruct.Tag,
2015-02-15 18:01:09 +03:00
}
2015-02-17 17:55:14 +03:00
if fieldStruct.Tag.Get("sql") == "-" {
field.IsIgnored = true
2015-02-15 18:01:09 +03:00
} else {
2015-02-17 17:55:14 +03:00
sqlSettings := parseTagSetting(field.Tag.Get("sql"))
gormSettings := parseTagSetting(field.Tag.Get("gorm"))
if _, ok := gormSettings["PRIMARY_KEY"]; ok {
field.IsPrimaryKey = true
2015-03-11 06:28:30 +03:00
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
2015-02-17 17:55:14 +03:00
}
2015-02-15 18:01:09 +03:00
2015-02-18 05:19:34 +03:00
if _, ok := sqlSettings["DEFAULT"]; ok {
field.HasDefaultValue = true
2015-02-17 17:55:14 +03:00
}
2015-02-15 18:01:09 +03:00
2015-02-17 17:55:14 +03:00
if value, ok := gormSettings["COLUMN"]; ok {
field.DBName = value
} else {
2015-02-18 05:19:34 +03:00
field.DBName = ToDBName(fieldStruct.Name)
2015-02-17 17:55:14 +03:00
}
2015-02-18 06:37:08 +03:00
}
fields = append(fields, field)
}
}
2015-02-15 18:01:09 +03:00
2015-03-19 10:02:15 +03:00
defer func() {
for _, field := range fields {
if !field.IsIgnored {
fieldStruct := field.Struct
fieldType, indirectType := fieldStruct.Type, fieldStruct.Type
if indirectType.Kind() == reflect.Ptr {
indirectType = indirectType.Elem()
}
2015-02-15 18:01:09 +03:00
2015-03-19 10:02:15 +03:00
if _, isScanner := reflect.New(fieldType).Interface().(sql.Scanner); isScanner {
field.IsScanner, field.IsNormal = true, true
}
2015-02-15 18:01:09 +03:00
2015-03-19 10:02:15 +03:00
if _, isTime := reflect.New(indirectType).Interface().(*time.Time); isTime {
field.IsNormal = true
}
2015-02-15 18:01:09 +03:00
2015-03-19 10:02:15 +03:00
if !field.IsNormal {
gormSettings := parseTagSetting(field.Tag.Get("gorm"))
toScope := scope.New(reflect.New(fieldStruct.Type).Interface())
2015-02-28 10:54:38 +03:00
2015-03-19 10:02:15 +03:00
getForeignField := func(column string, fields []*StructField) *StructField {
for _, field := range fields {
if field.Name == column || field.DBName == ToDBName(column) {
return field
}
2015-02-18 07:30:09 +03:00
}
2015-03-19 10:02:15 +03:00
return nil
2015-02-18 07:30:09 +03:00
}
2015-03-19 10:02:15 +03:00
var relationship = &Relationship{}
if polymorphic := gormSettings["POLYMORPHIC"]; polymorphic != "" {
if polymorphicField := getForeignField(polymorphic+"Id", toScope.GetStructFields()); polymorphicField != nil {
if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil {
2015-07-30 09:26:48 +03:00
relationship.ForeignFieldNames = []string{polymorphicField.Name}
relationship.ForeignDBNames = []string{polymorphicField.DBName}
2015-03-19 10:02:15 +03:00
relationship.PolymorphicType = polymorphicType.Name
relationship.PolymorphicDBName = polymorphicType.DBName
polymorphicType.IsForeignKey = true
polymorphicField.IsForeignKey = true
}
2015-02-18 07:30:09 +03:00
}
}
2015-02-15 18:01:09 +03:00
2015-07-30 09:26:48 +03:00
var foreignKeys []string
if foreignKey, ok := gormSettings["FOREIGNKEY"]; ok {
2015-07-30 13:19:49 +03:00
foreignKeys = append(foreignKeys, foreignKey)
2015-07-30 09:26:48 +03:00
}
2015-03-19 10:02:15 +03:00
switch indirectType.Kind() {
case reflect.Slice:
elemType := indirectType.Elem()
if elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
2015-02-18 06:37:08 +03:00
}
2015-03-19 10:02:15 +03:00
if elemType.Kind() == reflect.Struct {
if many2many := gormSettings["MANY2MANY"]; many2many != "" {
relationship.Kind = "many_to_many"
2015-07-30 09:26:48 +03:00
// foreign keys
if len(foreignKeys) == 0 {
for _, field := range scope.PrimaryFields() {
foreignKeys = append(foreignKeys, field.DBName)
}
2015-03-19 10:02:15 +03:00
}
2015-07-30 09:26:48 +03:00
for _, foreignKey := range foreignKeys {
if field, ok := scope.FieldByName(foreignKey); ok {
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, field.DBName)
joinTableDBName := ToDBName(scopeType.Name()) + "_" + field.DBName
relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBName)
}
}
// association foreign keys
var associationForeignKeys []string
if foreignKey := gormSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
associationForeignKeys = []string{gormSettings["ASSOCIATIONFOREIGNKEY"]}
} else {
for _, field := range toScope.PrimaryFields() {
associationForeignKeys = append(associationForeignKeys, field.DBName)
}
}
for _, name := range associationForeignKeys {
if field, ok := toScope.FieldByName(name); ok {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, name)
joinTableDBName := ToDBName(elemType.Name()) + "_" + field.DBName
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
}
}
2015-03-19 10:02:15 +03:00
2015-03-20 06:11:30 +03:00
joinTableHandler := JoinTableHandler{}
joinTableHandler.Setup(relationship, many2many, scopeType, elemType)
relationship.JoinTableHandler = &joinTableHandler
2015-02-18 07:30:09 +03:00
field.Relationship = relationship
2015-03-19 10:02:15 +03:00
} else {
2015-07-30 12:58:49 +03:00
relationship.Kind = "has_many"
2015-07-30 09:26:48 +03:00
if len(foreignKeys) == 0 {
for _, field := range scope.PrimaryFields() {
2015-07-30 12:58:49 +03:00
if foreignField := getForeignField(scopeType.Name()+field.Name, toScope.GetStructFields()); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, field.DBName)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
2015-07-30 09:26:48 +03:00
}
2015-07-30 12:58:49 +03:00
} else {
for _, foreignKey := range foreignKeys {
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scope.PrimaryField().Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scope.PrimaryField().DBName)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
2015-07-30 09:26:48 +03:00
}
}
if len(relationship.ForeignFieldNames) != 0 {
2015-03-19 10:02:15 +03:00
field.Relationship = relationship
}
2015-02-18 07:30:09 +03:00
}
2015-03-19 10:02:15 +03:00
} else {
field.IsNormal = true
2015-02-18 06:37:08 +03:00
}
2015-03-19 10:02:15 +03:00
case reflect.Struct:
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
for _, toField := range toScope.GetStructFields() {
toField = toField.clone()
toField.Names = append([]string{fieldStruct.Name}, toField.Names...)
modelStruct.StructFields = append(modelStruct.StructFields, toField)
if toField.IsPrimaryKey {
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, toField)
}
2015-02-25 05:56:05 +03:00
}
2015-03-19 10:02:15 +03:00
continue
2015-02-15 18:01:09 +03:00
} else {
if len(foreignKeys) == 0 {
for _, f := range toScope.PrimaryFields() {
if foreignField := getForeignField(field.Name+f.Name, fields); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, f.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, f.DBName)
2015-07-30 12:58:49 +03:00
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
2015-07-30 09:26:48 +03:00
}
2015-07-30 12:58:49 +03:00
} else {
for _, foreignKey := range foreignKeys {
if foreignField := getForeignField(foreignKey, fields); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, toScope.PrimaryField().Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, toScope.PrimaryField().DBName)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
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-03-19 10:02:15 +03:00
relationship.Kind = "belongs_to"
2015-02-18 07:30:09 +03:00
field.Relationship = relationship
2015-03-19 10:02:15 +03:00
} else {
2015-07-30 13:56:05 +03:00
if len(foreignKeys) == 0 {
2015-07-30 17:36:04 +03:00
for _, f := range scope.PrimaryFields() {
if foreignField := getForeignField(modelStruct.ModelType.Name()+f.Name, toScope.GetStructFields()); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, f.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, f.DBName)
2015-07-30 12:58:49 +03:00
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
2015-07-30 09:26:48 +03:00
}
2015-07-30 12:58:49 +03:00
} else {
2015-07-30 13:56:05 +03:00
for _, foreignKey := range foreignKeys {
2015-07-30 12:58:49 +03:00
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scope.PrimaryField().Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scope.PrimaryField().DBName)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
2015-07-30 09:26:48 +03:00
}
}
if len(relationship.ForeignFieldNames) != 0 {
relationship.Kind = "has_one"
2015-03-19 10:02:15 +03:00
field.Relationship = relationship
}
2015-02-18 07:30:09 +03:00
}
2015-02-18 06:37:08 +03:00
}
2015-03-19 10:02:15 +03:00
default:
field.IsNormal = true
2015-02-15 18:01:09 +03:00
}
}
2015-03-19 10:02:15 +03:00
if field.IsNormal {
if len(modelStruct.PrimaryFields) == 0 && field.DBName == "id" {
field.IsPrimaryKey = true
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
}
2015-02-18 06:37:08 +03:00
}
2015-02-17 12:40:21 +03:00
}
2015-03-19 10:02:15 +03:00
modelStruct.StructFields = append(modelStruct.StructFields, field)
2015-02-16 12:10:13 +03:00
}
2015-03-19 10:02:15 +03:00
}()
2015-02-15 18:01:09 +03:00
2015-03-11 13:33:50 +03:00
modelStructs[scopeType] = &modelStruct
2015-02-18 03:47:44 +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))
sqlSettings := parseTagSetting(field.Tag.Get("sql"))
if value, ok := sqlSettings["TYPE"]; ok {
sqlType = value
}
additionalType := sqlSettings["NOT NULL"] + " " + sqlSettings["UNIQUE"]
if value, ok := sqlSettings["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
if value, ok := sqlSettings["SIZE"]; ok {
size, _ = strconv.Atoi(value)
}
2015-03-11 12:05:58 +03:00
_, autoIncrease := sqlSettings["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-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
}
}
func parseTagSetting(str string) map[string]string {
tags := strings.Split(str, ";")
setting := map[string]string{}
for _, value := range tags {
v := strings.Split(value, ":")
k := strings.TrimSpace(strings.ToUpper(v[0]))
if len(v) == 2 {
setting[k] = v[1]
} else {
setting[k] = k
}
}
return setting
}