forked from mirror/gorm
Refactor GetModelStruct
This commit is contained in:
parent
6a5a2dbc55
commit
4bc06a21c1
|
@ -192,11 +192,19 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
var (
|
var (
|
||||||
relationship = &Relationship{}
|
relationship = &Relationship{}
|
||||||
toScope = scope.New(reflect.New(field.Struct.Type).Interface())
|
toScope = scope.New(reflect.New(field.Struct.Type).Interface())
|
||||||
foreignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ";")
|
foreignKeys []string
|
||||||
associationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ";")
|
associationForeignKeys []string
|
||||||
elemType = field.Struct.Type
|
elemType = field.Struct.Type
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
|
||||||
|
foreignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ";")
|
||||||
|
}
|
||||||
|
|
||||||
|
if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
|
||||||
|
associationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ";")
|
||||||
|
}
|
||||||
|
|
||||||
for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
|
for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
|
||||||
elemType = elemType.Elem()
|
elemType = elemType.Elem()
|
||||||
}
|
}
|
||||||
|
@ -248,23 +256,35 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
|
|
||||||
// if no foreign keys
|
// if no foreign keys
|
||||||
if len(foreignKeys) == 0 {
|
if len(foreignKeys) == 0 {
|
||||||
for _, field := range scope.PrimaryFields() { // FIXME
|
if len(associationForeignKeys) == 0 {
|
||||||
|
for _, field := range modelStruct.PrimaryFields {
|
||||||
foreignKeys = append(foreignKeys, reflectType.Name()+field.Name)
|
foreignKeys = append(foreignKeys, reflectType.Name()+field.Name)
|
||||||
|
associationForeignKeys = append(associationForeignKeys, field.Name)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, associationForeignKey := range associationForeignKeys {
|
||||||
|
if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
|
||||||
|
foreignKeys = append(foreignKeys, reflectType.Name()+foreignField.Name)
|
||||||
|
associationForeignKeys = append(associationForeignKeys, foreignField.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, foreignKey := range foreignKeys {
|
for idx, foreignKey := range foreignKeys {
|
||||||
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
||||||
|
if associationField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); associationField != nil {
|
||||||
// source foreign keys
|
// source foreign keys
|
||||||
foreignField.IsForeignKey = true
|
foreignField.IsForeignKey = true
|
||||||
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.Name)
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
|
||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, field.DBName)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
|
||||||
|
|
||||||
// association foreign keys
|
// association foreign keys
|
||||||
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(relationship.ForeignFieldNames) != 0 {
|
if len(relationship.ForeignFieldNames) != 0 {
|
||||||
field.Relationship = relationship
|
field.Relationship = relationship
|
||||||
|
@ -279,22 +299,43 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
var (
|
var (
|
||||||
relationship = &Relationship{}
|
relationship = &Relationship{}
|
||||||
toScope = scope.New(reflect.New(field.Struct.Type).Interface())
|
toScope = scope.New(reflect.New(field.Struct.Type).Interface())
|
||||||
tagForeignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ";")
|
tagForeignKeys []string
|
||||||
foreignKeys = tagForeignKeys
|
tagAssociationForeignKeys []string
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(tagForeignKeys) == 0 {
|
if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
|
||||||
for _, primaryField := range scope.PrimaryFields() {
|
tagForeignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ";")
|
||||||
|
}
|
||||||
|
|
||||||
|
if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
|
||||||
|
tagAssociationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ";")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Has One
|
||||||
|
{
|
||||||
|
var foreignKeys = tagForeignKeys
|
||||||
|
var associationForeignKeys = tagAssociationForeignKeys
|
||||||
|
if len(foreignKeys) == 0 {
|
||||||
|
if len(associationForeignKeys) == 0 {
|
||||||
|
for _, primaryField := range modelStruct.PrimaryFields {
|
||||||
foreignKeys = append(foreignKeys, modelStruct.ModelType.Name()+primaryField.Name)
|
foreignKeys = append(foreignKeys, modelStruct.ModelType.Name()+primaryField.Name)
|
||||||
|
associationForeignKeys = append(associationForeignKeys, primaryField.Name)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, associationForeignKey := range tagAssociationForeignKeys {
|
||||||
|
if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
|
||||||
|
foreignKeys = append(foreignKeys, modelStruct.ModelType.Name()+foreignField.Name)
|
||||||
|
associationForeignKeys = append(associationForeignKeys, foreignField.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if has one
|
for idx, foreignKey := range foreignKeys {
|
||||||
for _, foreignKey := range foreignKeys {
|
|
||||||
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
||||||
|
if scopeField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); scopeField != nil {
|
||||||
foreignField.IsForeignKey = true
|
foreignField.IsForeignKey = true
|
||||||
// source foreign keys
|
// source foreign keys
|
||||||
scopeField := getForeignField(foreignKey, modelStruct.StructFields)
|
|
||||||
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scopeField.Name)
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scopeField.Name)
|
||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scopeField.DBName)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scopeField.DBName)
|
||||||
|
|
||||||
|
@ -303,23 +344,37 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(relationship.ForeignFieldNames) != 0 {
|
if len(relationship.ForeignFieldNames) != 0 {
|
||||||
relationship.Kind = "has_one"
|
relationship.Kind = "has_one"
|
||||||
field.Relationship = relationship
|
field.Relationship = relationship
|
||||||
} else {
|
} else {
|
||||||
foreignKeys = tagForeignKeys
|
var foreignKeys = tagForeignKeys
|
||||||
|
var associationForeignKeys = tagAssociationForeignKeys
|
||||||
if len(foreignKeys) == 0 {
|
if len(foreignKeys) == 0 {
|
||||||
|
if len(associationForeignKeys) == 0 {
|
||||||
for _, f := range toScope.PrimaryFields() {
|
for _, f := range toScope.PrimaryFields() {
|
||||||
foreignKeys = append(foreignKeys, field.Name+f.Name)
|
foreignKeys = append(foreignKeys, field.Name+f.Name)
|
||||||
|
associationForeignKeys = append(associationForeignKeys, f.Name)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, associationForeignKey := range associationForeignKeys {
|
||||||
|
if foreignField := getForeignField(associationForeignKey, toScope.GetStructFields()); foreignField != nil {
|
||||||
|
foreignKeys = append(foreignKeys, field.Name+foreignField.Name)
|
||||||
|
associationForeignKeys = append(associationForeignKeys, foreignField.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, foreignKey := range foreignKeys {
|
for idx, foreignKey := range foreignKeys {
|
||||||
if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
|
if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
|
||||||
|
if associationField := getForeignField(associationForeignKeys[idx], toScope.GetStructFields()); associationField != nil {
|
||||||
// association foreign keys
|
// association foreign keys
|
||||||
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, toScope.PrimaryField().Name)
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
|
||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, toScope.PrimaryField().DBName)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
|
||||||
|
|
||||||
// source foreign keys
|
// source foreign keys
|
||||||
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
|
@ -327,6 +382,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
foreignField.IsForeignKey = true
|
foreignField.IsForeignKey = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(relationship.ForeignFieldNames) != 0 {
|
if len(relationship.ForeignFieldNames) != 0 {
|
||||||
relationship.Kind = "belongs_to"
|
relationship.Kind = "belongs_to"
|
||||||
|
|
Loading…
Reference in New Issue