mirror of https://github.com/go-gorm/gorm.git
Use field.TagSettings
This commit is contained in:
parent
19b85b1f17
commit
4e45e6dc2d
2
main.go
2
main.go
|
@ -512,7 +512,7 @@ func (s *DB) SetJoinTableHandler(source interface{}, column string, handler Join
|
||||||
scope := s.NewScope(source)
|
scope := s.NewScope(source)
|
||||||
for _, field := range scope.GetModelStruct().StructFields {
|
for _, field := range scope.GetModelStruct().StructFields {
|
||||||
if field.Name == column || field.DBName == column {
|
if field.Name == column || field.DBName == column {
|
||||||
if many2many := parseTagSetting(field.Tag)["MANY2MANY"]; many2many != "" {
|
if many2many := field.TagSettings["MANY2MANY"]; many2many != "" {
|
||||||
source := (&Scope{Value: source}).GetModelStruct().ModelType
|
source := (&Scope{Value: source}).GetModelStruct().ModelType
|
||||||
destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType
|
destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType
|
||||||
handler.Setup(field.Relationship, many2many, source, destination)
|
handler.Setup(field.Relationship, many2many, source, destination)
|
||||||
|
|
|
@ -62,6 +62,7 @@ type StructField struct {
|
||||||
IsScanner bool
|
IsScanner bool
|
||||||
HasDefaultValue bool
|
HasDefaultValue bool
|
||||||
Tag reflect.StructTag
|
Tag reflect.StructTag
|
||||||
|
TagSettings map[string]string
|
||||||
Struct reflect.StructField
|
Struct reflect.StructField
|
||||||
IsForeignKey bool
|
IsForeignKey bool
|
||||||
Relationship *Relationship
|
Relationship *Relationship
|
||||||
|
@ -139,23 +140,23 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
Name: fieldStruct.Name,
|
Name: fieldStruct.Name,
|
||||||
Names: []string{fieldStruct.Name},
|
Names: []string{fieldStruct.Name},
|
||||||
Tag: fieldStruct.Tag,
|
Tag: fieldStruct.Tag,
|
||||||
|
TagSettings: parseTagSetting(fieldStruct.Tag),
|
||||||
}
|
}
|
||||||
|
|
||||||
if fieldStruct.Tag.Get("sql") == "-" {
|
if fieldStruct.Tag.Get("sql") == "-" {
|
||||||
field.IsIgnored = true
|
field.IsIgnored = true
|
||||||
}
|
}
|
||||||
|
|
||||||
gormSettings := parseTagSetting(field.Tag)
|
if _, ok := field.TagSettings["PRIMARY_KEY"]; ok {
|
||||||
if _, ok := gormSettings["PRIMARY_KEY"]; ok {
|
|
||||||
field.IsPrimaryKey = true
|
field.IsPrimaryKey = true
|
||||||
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
|
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := gormSettings["DEFAULT"]; ok {
|
if _, ok := field.TagSettings["DEFAULT"]; ok {
|
||||||
field.HasDefaultValue = true
|
field.HasDefaultValue = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if value, ok := gormSettings["COLUMN"]; ok {
|
if value, ok := field.TagSettings["COLUMN"]; ok {
|
||||||
field.DBName = value
|
field.DBName = value
|
||||||
} else {
|
} else {
|
||||||
field.DBName = ToDBName(fieldStruct.Name)
|
field.DBName = ToDBName(fieldStruct.Name)
|
||||||
|
@ -184,7 +185,6 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !field.IsNormal {
|
if !field.IsNormal {
|
||||||
gormSettings := parseTagSetting(field.Tag)
|
|
||||||
toScope := scope.New(reflect.New(fieldStruct.Type).Interface())
|
toScope := scope.New(reflect.New(fieldStruct.Type).Interface())
|
||||||
|
|
||||||
getForeignField := func(column string, fields []*StructField) *StructField {
|
getForeignField := func(column string, fields []*StructField) *StructField {
|
||||||
|
@ -198,7 +198,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
|
|
||||||
var relationship = &Relationship{}
|
var relationship = &Relationship{}
|
||||||
|
|
||||||
if polymorphic := gormSettings["POLYMORPHIC"]; polymorphic != "" {
|
if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" {
|
||||||
if polymorphicField := getForeignField(polymorphic+"Id", toScope.GetStructFields()); polymorphicField != nil {
|
if polymorphicField := getForeignField(polymorphic+"Id", toScope.GetStructFields()); polymorphicField != nil {
|
||||||
if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil {
|
if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil {
|
||||||
relationship.ForeignFieldNames = []string{polymorphicField.Name}
|
relationship.ForeignFieldNames = []string{polymorphicField.Name}
|
||||||
|
@ -214,7 +214,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var foreignKeys []string
|
var foreignKeys []string
|
||||||
if foreignKey, ok := gormSettings["FOREIGNKEY"]; ok {
|
if foreignKey, ok := field.TagSettings["FOREIGNKEY"]; ok {
|
||||||
foreignKeys = append(foreignKeys, foreignKey)
|
foreignKeys = append(foreignKeys, foreignKey)
|
||||||
}
|
}
|
||||||
switch indirectType.Kind() {
|
switch indirectType.Kind() {
|
||||||
|
@ -225,7 +225,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
if elemType.Kind() == reflect.Struct {
|
if elemType.Kind() == reflect.Struct {
|
||||||
if many2many := gormSettings["MANY2MANY"]; many2many != "" {
|
if many2many := field.TagSettings["MANY2MANY"]; many2many != "" {
|
||||||
relationship.Kind = "many_to_many"
|
relationship.Kind = "many_to_many"
|
||||||
|
|
||||||
// foreign keys
|
// foreign keys
|
||||||
|
@ -245,8 +245,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
|
|
||||||
// association foreign keys
|
// association foreign keys
|
||||||
var associationForeignKeys []string
|
var associationForeignKeys []string
|
||||||
if foreignKey := gormSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
|
if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
|
||||||
associationForeignKeys = []string{gormSettings["ASSOCIATIONFOREIGNKEY"]}
|
associationForeignKeys = []string{foreignKey}
|
||||||
} else {
|
} else {
|
||||||
for _, field := range toScope.PrimaryFields() {
|
for _, field := range toScope.PrimaryFields() {
|
||||||
associationForeignKeys = append(associationForeignKeys, field.DBName)
|
associationForeignKeys = append(associationForeignKeys, field.DBName)
|
||||||
|
@ -298,7 +298,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
field.IsNormal = true
|
field.IsNormal = true
|
||||||
}
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
if _, ok := field.TagSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
||||||
for _, toField := range toScope.GetStructFields() {
|
for _, toField := range toScope.GetStructFields() {
|
||||||
toField = toField.clone()
|
toField = toField.clone()
|
||||||
toField.Names = append([]string{fieldStruct.Name}, toField.Names...)
|
toField.Names = append([]string{fieldStruct.Name}, toField.Names...)
|
||||||
|
@ -399,14 +399,13 @@ func (scope *Scope) generateSqlTag(field *StructField) string {
|
||||||
structType = structType.Elem()
|
structType = structType.Elem()
|
||||||
}
|
}
|
||||||
reflectValue := reflect.Indirect(reflect.New(structType))
|
reflectValue := reflect.Indirect(reflect.New(structType))
|
||||||
sqlSettings := parseTagSetting(field.Tag)
|
|
||||||
|
|
||||||
if value, ok := sqlSettings["TYPE"]; ok {
|
if value, ok := field.TagSettings["TYPE"]; ok {
|
||||||
sqlType = value
|
sqlType = value
|
||||||
}
|
}
|
||||||
|
|
||||||
additionalType := sqlSettings["NOT NULL"] + " " + sqlSettings["UNIQUE"]
|
additionalType := field.TagSettings["NOT NULL"] + " " + field.TagSettings["UNIQUE"]
|
||||||
if value, ok := sqlSettings["DEFAULT"]; ok {
|
if value, ok := field.TagSettings["DEFAULT"]; ok {
|
||||||
additionalType = additionalType + " DEFAULT " + value
|
additionalType = additionalType + " DEFAULT " + value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -424,11 +423,11 @@ func (scope *Scope) generateSqlTag(field *StructField) string {
|
||||||
if sqlType == "" {
|
if sqlType == "" {
|
||||||
var size = 255
|
var size = 255
|
||||||
|
|
||||||
if value, ok := sqlSettings["SIZE"]; ok {
|
if value, ok := field.TagSettings["SIZE"]; ok {
|
||||||
size, _ = strconv.Atoi(value)
|
size, _ = strconv.Atoi(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
v, autoIncrease := sqlSettings["AUTO_INCREMENT"]
|
v, autoIncrease := field.TagSettings["AUTO_INCREMENT"]
|
||||||
if field.IsPrimaryKey {
|
if field.IsPrimaryKey {
|
||||||
autoIncrease = true
|
autoIncrease = true
|
||||||
}
|
}
|
||||||
|
|
|
@ -630,15 +630,14 @@ func (scope *Scope) autoIndex() *Scope {
|
||||||
var uniqueIndexes = map[string][]string{}
|
var uniqueIndexes = map[string][]string{}
|
||||||
|
|
||||||
for _, field := range scope.GetStructFields() {
|
for _, field := range scope.GetStructFields() {
|
||||||
sqlSettings := parseTagSetting(field.Tag)
|
if name, ok := field.TagSettings["INDEX"]; ok {
|
||||||
if name, ok := sqlSettings["INDEX"]; ok {
|
|
||||||
if name == "INDEX" {
|
if name == "INDEX" {
|
||||||
name = fmt.Sprintf("idx_%v_%v", scope.TableName(), field.DBName)
|
name = fmt.Sprintf("idx_%v_%v", scope.TableName(), field.DBName)
|
||||||
}
|
}
|
||||||
indexes[name] = append(indexes[name], field.DBName)
|
indexes[name] = append(indexes[name], field.DBName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if name, ok := sqlSettings["UNIQUE_INDEX"]; ok {
|
if name, ok := field.TagSettings["UNIQUE_INDEX"]; ok {
|
||||||
if name == "UNIQUE_INDEX" {
|
if name == "UNIQUE_INDEX" {
|
||||||
name = fmt.Sprintf("uix_%v_%v", scope.TableName(), field.DBName)
|
name = fmt.Sprintf("uix_%v_%v", scope.TableName(), field.DBName)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue