gorm/model_struct.go

288 lines
7.6 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++ {
fieldStruct := scopeType.Field(i)
2015-02-15 18:01:09 +03:00
if !ast.IsExported(fieldStruct.Name) {
continue
}
2015-02-17 13:52:10 +03:00
field := &StructField{
Struct: fieldStruct,
Name: fieldStruct.Name,
Names: []string{fieldStruct.Name},
Tag: fieldStruct.Tag,
}
2015-02-15 18:01:09 +03:00
if fieldStruct.Tag.Get("sql") == "-" {
field.IsIgnored = true
} else {
2015-02-17 13:52:10 +03:00
sqlSettings := parseTagSetting(field.Tag.Get("sql"))
gormSettings := parseTagSetting(field.Tag.Get("gorm"))
if _, ok := gormSettings["PRIMARY_KEY"]; ok {
2015-02-15 18:01:09 +03:00
field.IsPrimaryKey = true
2015-02-16 11:35:26 +03:00
modelStruct.PrimaryKeyField = field
2015-02-15 18:01:09 +03:00
}
2015-02-17 13:52:10 +03:00
if value, ok := sqlSettings["DEFAULT"]; ok {
2015-02-15 18:01:09 +03:00
field.DefaultValue = &value
}
2015-02-17 13:52:10 +03:00
if value, ok := gormSettings["COLUMN"]; ok {
2015-02-15 18:01:09 +03:00
field.DBName = value
} else {
field.DBName = ToSnake(fieldStruct.Name)
}
fieldType, indirectType := fieldStruct.Type, fieldStruct.Type
if indirectType.Kind() == reflect.Ptr {
indirectType = indirectType.Elem()
}
if _, isScanner := reflect.New(fieldType).Interface().(sql.Scanner); isScanner {
field.IsScanner, field.IsNormal = true, true
}
2015-02-17 12:40:21 +03:00
if _, isTime := reflect.New(indirectType).Interface().(*time.Time); isTime {
2015-02-15 18:01:09 +03:00
field.IsTime, field.IsNormal = true, true
}
2015-02-17 13:52:10 +03:00
many2many := gormSettings["MANY2MANY"]
foreignKey := SnakeToUpperCamel(gormSettings["FOREIGNKEY"])
foreignType := SnakeToUpperCamel(gormSettings["FOREIGNTYPE"])
associationForeignKey := SnakeToUpperCamel(gormSettings["ASSOCIATIONFOREIGNKEY"])
if polymorphic := SnakeToUpperCamel(gormSettings["POLYMORPHIC"]); polymorphic != "" {
2015-02-15 18:01:09 +03:00
foreignKey = polymorphic + "Id"
foreignType = polymorphic + "Type"
}
if !field.IsNormal {
switch indirectType.Kind() {
case reflect.Slice:
typ := indirectType.Elem()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if typ.Kind() == reflect.Struct {
kind := "has_many"
if foreignKey == "" {
2015-02-17 12:40:21 +03:00
foreignKey = scopeType.Name() + "Id"
2015-02-15 18:01:09 +03:00
}
if associationForeignKey == "" {
associationForeignKey = typ.Name() + "Id"
}
if many2many != "" {
kind = "many_to_many"
2015-02-16 12:10:13 +03:00
} else if !reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
2015-02-15 18:01:09 +03:00
foreignKey = ""
}
2015-02-16 11:35:26 +03:00
field.Relationship = &Relationship{
JoinTable: many2many,
ForeignType: foreignType,
ForeignFieldName: foreignKey,
AssociationForeignFieldName: associationForeignKey,
2015-02-17 12:40:21 +03:00
ForeignDBName: ToSnake(foreignKey),
AssociationForeignDBName: ToSnake(associationForeignKey),
2015-02-15 18:01:09 +03:00
Kind: kind,
}
} else {
field.IsNormal = true
}
case reflect.Struct:
2015-02-17 13:52:10 +03:00
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
2015-02-15 18:01:09 +03:00
for _, field := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() {
2015-02-17 09:30:37 +03:00
field.Names = append([]string{fieldStruct.Name}, field.Names...)
2015-02-16 11:35:26 +03:00
modelStruct.StructFields = append(modelStruct.StructFields, field)
2015-02-15 18:01:09 +03:00
}
break
} else {
var belongsToForeignKey, hasOneForeignKey, kind string
if foreignKey == "" {
2015-02-17 12:40:21 +03:00
belongsToForeignKey = field.Name + "Id"
hasOneForeignKey = scopeType.Name() + "Id"
2015-02-15 18:01:09 +03:00
} else {
belongsToForeignKey = foreignKey
hasOneForeignKey = foreignKey
}
2015-02-17 12:40:21 +03:00
if _, ok := scopeType.FieldByName(belongsToForeignKey); ok {
2015-02-15 18:01:09 +03:00
kind = "belongs_to"
2015-02-17 12:40:21 +03:00
foreignKey = belongsToForeignKey
2015-02-15 18:01:09 +03:00
} else {
foreignKey = hasOneForeignKey
kind = "has_one"
}
2015-02-17 12:40:21 +03:00
field.Relationship = &Relationship{
ForeignFieldName: foreignKey,
ForeignDBName: ToSnake(foreignKey),
ForeignType: foreignType,
Kind: kind,
}
2015-02-15 18:01:09 +03:00
}
default:
field.IsNormal = true
}
}
}
2015-02-16 11:35:26 +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
}