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 {
|
|
|
|
Name string
|
|
|
|
DBName string
|
|
|
|
IsPrimaryKey bool
|
|
|
|
IsScanner bool
|
|
|
|
IsTime bool
|
|
|
|
IsNormal bool
|
|
|
|
IsIgnored bool
|
|
|
|
DefaultValue *string
|
2015-02-16 11:35:26 +03:00
|
|
|
GormSettings map[string]string
|
|
|
|
SqlSettings map[string]string
|
2015-02-15 18:01:09 +03:00
|
|
|
SqlTag string
|
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-16 11:35:26 +03:00
|
|
|
|
|
|
|
if value, ok := field.SqlSettings["TYPE"]; ok {
|
|
|
|
sqlType = value
|
|
|
|
}
|
|
|
|
|
|
|
|
additionalType := field.SqlSettings["NOT NULL"] + " " + field.SqlSettings["UNIQUE"]
|
|
|
|
if value, ok := field.SqlSettings["DEFAULT"]; ok {
|
|
|
|
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
|
|
|
|
|
|
|
|
if value, ok := field.SqlSettings["SIZE"]; ok {
|
|
|
|
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))
|
|
|
|
if reflectValue.Kind() == reflect.Slice {
|
|
|
|
reflectValue = reflect.Indirect(reflect.New(reflectValue.Elem().Type()))
|
|
|
|
}
|
|
|
|
scopeTyp := reflectValue.Type()
|
2015-02-16 11:35:26 +03:00
|
|
|
|
|
|
|
// Set tablename
|
|
|
|
if fm := reflect.New(scopeTyp).MethodByName("TableName"); fm.IsValid() {
|
|
|
|
if results := fm.Call([]reflect.Value{}); len(results) > 0 {
|
|
|
|
if name, ok := results[0].Interface().(string); ok {
|
|
|
|
modelStruct.TableName = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
modelStruct.TableName = ToSnake(scopeTyp.Name())
|
|
|
|
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-15 18:01:09 +03:00
|
|
|
for i := 0; i < scopeTyp.NumField(); i++ {
|
|
|
|
fieldStruct := scopeTyp.Field(i)
|
|
|
|
if !ast.IsExported(fieldStruct.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-02-16 11:42:01 +03:00
|
|
|
field := &StructField{Struct: fieldStruct}
|
2015-02-15 18:01:09 +03:00
|
|
|
if fieldStruct.Tag.Get("sql") == "-" {
|
|
|
|
field.IsIgnored = true
|
|
|
|
} else {
|
2015-02-16 11:35:26 +03:00
|
|
|
field.SqlSettings = parseTagSetting(fieldStruct.Tag.Get("sql"))
|
|
|
|
field.GormSettings = parseTagSetting(fieldStruct.Tag.Get("gorm"))
|
|
|
|
if _, ok := field.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-16 11:35:26 +03:00
|
|
|
if value, ok := field.SqlSettings["DEFAULT"]; ok {
|
2015-02-15 18:01:09 +03:00
|
|
|
field.DefaultValue = &value
|
|
|
|
}
|
|
|
|
|
2015-02-16 11:35:26 +03:00
|
|
|
if value, ok := field.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
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, isTime := reflect.New(indirectType).Interface().(time.Time); isTime {
|
|
|
|
field.IsTime, field.IsNormal = true, true
|
|
|
|
}
|
|
|
|
|
2015-02-16 11:35:26 +03:00
|
|
|
many2many := field.GormSettings["MANY2MANY"]
|
|
|
|
foreignKey := SnakeToUpperCamel(field.GormSettings["FOREIGNKEY"])
|
|
|
|
foreignType := SnakeToUpperCamel(field.GormSettings["FOREIGNTYPE"])
|
|
|
|
associationForeignKey := SnakeToUpperCamel(field.GormSettings["ASSOCIATIONFOREIGNKEY"])
|
|
|
|
if polymorphic := SnakeToUpperCamel(field.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 == "" {
|
|
|
|
foreignKey = indirectType.Name() + "Id"
|
|
|
|
}
|
|
|
|
|
|
|
|
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-15 18:01:09 +03:00
|
|
|
Kind: kind,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
field.IsNormal = true
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
2015-02-16 11:35:26 +03:00
|
|
|
if _, ok := field.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-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 == "" {
|
|
|
|
belongsToForeignKey = indirectType.Name() + "Id"
|
|
|
|
hasOneForeignKey = scopeTyp.Name() + "Id"
|
|
|
|
} else {
|
|
|
|
belongsToForeignKey = foreignKey
|
|
|
|
hasOneForeignKey = foreignKey
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := scopeTyp.FieldByName(belongsToForeignKey); ok {
|
|
|
|
foreignKey = belongsToForeignKey
|
|
|
|
kind = "belongs_to"
|
|
|
|
} else {
|
|
|
|
foreignKey = hasOneForeignKey
|
|
|
|
kind = "has_one"
|
|
|
|
}
|
|
|
|
|
2015-02-16 11:35:26 +03:00
|
|
|
field.Relationship = &Relationship{ForeignFieldName: 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-16 12:47:07 +03:00
|
|
|
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
|
|
|
}
|