gorm/model_struct.go

285 lines
7.7 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-02-16 07:04:46 +03:00
type ModelStruct struct {
PrimaryKeyField *StructField
StructFields []*StructField
TableName string
}
2015-02-15 18:01:09 +03:00
type StructField struct {
DBName string
2015-02-17 09:30:37 +03:00
Name string
Names []string
2015-02-15 18:01:09 +03:00
IsPrimaryKey bool
IsScanner bool
IsTime bool
IsNormal bool
IsIgnored bool
DefaultValue *string
SqlTag string
2015-02-17 13:52:10 +03:00
Tag reflect.StructTag
2015-02-16 11:35:26 +03:00
Struct reflect.StructField
2015-02-16 07:04:46 +03:00
Relationship *Relationship
}
type Relationship struct {
Kind string
ForeignType string
ForeignFieldName string
ForeignDBName string
AssociationForeignFieldName string
AssociationForeignDBName string
JoinTable string
2015-02-15 18:01:09 +03:00
}
2015-02-16 12:47:07 +03:00
func (scope *Scope) generateSqlTag(field *StructField) {
2015-02-16 11:35:26 +03:00
var sqlType string
2015-02-16 12:10:13 +03:00
reflectValue := reflect.Indirect(reflect.New(field.Struct.Type))
2015-02-17 13:52:10 +03:00
sqlSettings := parseTagSetting(field.Tag.Get("sql"))
2015-02-16 11:35:26 +03:00
2015-02-17 13:52:10 +03:00
if value, ok := sqlSettings["TYPE"]; ok {
2015-02-16 11:35:26 +03:00
sqlType = value
}
2015-02-17 13:52:10 +03:00
additionalType := sqlSettings["NOT NULL"] + " " + sqlSettings["UNIQUE"]
if value, ok := sqlSettings["DEFAULT"]; ok {
2015-02-16 11:35:26 +03:00
additionalType = additionalType + "DEFAULT " + value
}
if field.IsScanner {
var getScannerValue func(reflect.Value)
2015-02-16 12:10:13 +03:00
getScannerValue = func(value reflect.Value) {
reflectValue = value
if _, isScanner := reflect.New(reflectValue.Type()).Interface().(sql.Scanner); isScanner && reflectValue.Kind() == reflect.Struct {
2015-02-16 11:35:26 +03:00
getScannerValue(reflectValue.Field(0))
}
}
2015-02-16 12:10:13 +03:00
getScannerValue(reflectValue)
2015-02-16 11:35:26 +03:00
}
if sqlType == "" {
var size = 255
2015-02-17 13:52:10 +03:00
if value, ok := sqlSettings["SIZE"]; ok {
2015-02-16 11:35:26 +03:00
size, _ = strconv.Atoi(value)
}
if field.IsPrimaryKey {
sqlType = scope.Dialect().PrimaryKeyTag(reflectValue, size)
} else {
sqlType = scope.Dialect().SqlTag(reflectValue, size)
}
}
if strings.TrimSpace(additionalType) == "" {
field.SqlTag = sqlType
} else {
field.SqlTag = fmt.Sprintf("%v %v", sqlType, additionalType)
}
}
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"}
func (scope *Scope) GetModelStruct() *ModelStruct {
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-17 13:37:47 +03:00
if scopeType.Kind() == reflect.Ptr {
scopeType = scopeType.Elem()
}
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-02-17 12:40:21 +03:00
if fm := reflect.New(scopeType).MethodByName("TableName"); fm.IsValid() {
2015-02-16 11:35:26 +03:00
if results := fm.Call([]reflect.Value{}); len(results) > 0 {
if name, ok := results[0].Interface().(string); ok {
modelStruct.TableName = name
}
}
} else {
2015-02-17 12:40:21 +03:00
modelStruct.TableName = ToSnake(scopeType.Name())
2015-02-16 11:35:26 +03:00
if scope.db == nil || !scope.db.parent.singularTable {
for index, reg := range pluralMapKeys {
if reg.MatchString(modelStruct.TableName) {
modelStruct.TableName = reg.ReplaceAllString(modelStruct.TableName, pluralMapValues[index])
}
}
}
}
// Set fields
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
modelStruct.PrimaryKeyField = field
}
2015-02-15 18:01:09 +03:00
2015-02-17 17:55:14 +03:00
if value, ok := sqlSettings["DEFAULT"]; ok {
field.DefaultValue = &value
}
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 {
field.DBName = ToSnake(fieldStruct.Name)
}
2015-02-15 18:01:09 +03:00
2015-02-17 17:55:14 +03:00
fieldType, indirectType := fieldStruct.Type, fieldStruct.Type
if indirectType.Kind() == reflect.Ptr {
indirectType = indirectType.Elem()
}
2015-02-15 18:01:09 +03:00
2015-02-17 17:55:14 +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-02-17 17:55:14 +03:00
if _, isTime := reflect.New(indirectType).Interface().(*time.Time); isTime {
field.IsTime, field.IsNormal = true, true
}
2015-02-15 18:01:09 +03:00
2015-02-17 17:55:14 +03:00
many2many := gormSettings["MANY2MANY"]
foreignKey := SnakeToUpperCamel(gormSettings["FOREIGNKEY"])
foreignType := SnakeToUpperCamel(gormSettings["FOREIGNTYPE"])
associationForeignKey := SnakeToUpperCamel(gormSettings["ASSOCIATIONFOREIGNKEY"])
if polymorphic := SnakeToUpperCamel(gormSettings["POLYMORPHIC"]); polymorphic != "" {
foreignKey = polymorphic + "Id"
foreignType = polymorphic + "Type"
}
2015-02-15 18:01:09 +03:00
2015-02-17 17:55:14 +03:00
if !field.IsNormal {
switch indirectType.Kind() {
case reflect.Slice:
typ := indirectType.Elem()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
2015-02-15 18:01:09 +03:00
}
2015-02-17 17:55:14 +03:00
if typ.Kind() == reflect.Struct {
kind := "has_many"
if foreignKey == "" {
foreignKey = scopeType.Name() + "Id"
}
if associationForeignKey == "" {
associationForeignKey = typ.Name() + "Id"
}
if many2many != "" {
kind = "many_to_many"
} else if !reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
foreignKey = ""
}
field.Relationship = &Relationship{
JoinTable: many2many,
ForeignType: foreignType,
ForeignFieldName: foreignKey,
AssociationForeignFieldName: associationForeignKey,
ForeignDBName: ToSnake(foreignKey),
AssociationForeignDBName: ToSnake(associationForeignKey),
Kind: kind,
}
2015-02-15 18:01:09 +03:00
} else {
2015-02-17 17:55:14 +03:00
field.IsNormal = true
2015-02-15 18:01:09 +03:00
}
2015-02-17 17:55:14 +03:00
case reflect.Struct:
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
for _, field := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() {
field.Names = append([]string{fieldStruct.Name}, field.Names...)
modelStruct.StructFields = append(modelStruct.StructFields, field)
}
break
2015-02-15 18:01:09 +03:00
} else {
2015-02-17 17:55:14 +03:00
var belongsToForeignKey, hasOneForeignKey, kind string
if foreignKey == "" {
belongsToForeignKey = field.Name + "Id"
hasOneForeignKey = scopeType.Name() + "Id"
} else {
belongsToForeignKey = foreignKey
hasOneForeignKey = foreignKey
}
if _, ok := scopeType.FieldByName(belongsToForeignKey); ok {
kind = "belongs_to"
foreignKey = belongsToForeignKey
} else {
foreignKey = hasOneForeignKey
kind = "has_one"
}
field.Relationship = &Relationship{
ForeignFieldName: foreignKey,
ForeignDBName: ToSnake(foreignKey),
ForeignType: foreignType,
Kind: kind,
}
2015-02-15 18:01:09 +03:00
}
2015-02-17 17:55:14 +03:00
default:
field.IsNormal = true
2015-02-15 18:01:09 +03:00
}
}
}
2015-02-17 17:55:14 +03:00
modelStruct.StructFields = append(modelStruct.StructFields, field)
2015-02-15 18:01:09 +03:00
}
}
2015-02-16 11:35:26 +03:00
for _, field := range modelStruct.StructFields {
2015-02-16 12:10:13 +03:00
if field.IsNormal {
if modelStruct.PrimaryKeyField == nil && field.DBName == "id" {
field.IsPrimaryKey = true
modelStruct.PrimaryKeyField = field
}
2015-02-15 18:01:09 +03:00
2015-02-17 12:40:21 +03:00
if scope.db != nil {
scope.generateSqlTag(field)
}
2015-02-16 12:10:13 +03:00
}
2015-02-16 11:35:26 +03:00
}
2015-02-15 18:01:09 +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
}