Rename joinTable to relationship

This commit is contained in:
Jinzhu 2014-07-30 20:59:52 +08:00
parent e1e68debdc
commit 2356182ea7
6 changed files with 44 additions and 44 deletions

View File

@ -85,11 +85,11 @@ func (association *Association) Delete(values ...interface{}) *Association {
if len(primaryKeys) == 0 { if len(primaryKeys) == 0 {
association.err(errors.New("no primary key found")) association.err(errors.New("no primary key found"))
} else { } else {
joinTable := association.Field.JoinTable relationship := association.Field.Relationship
// many to many // many to many
if joinTable.joinTable != "" { if relationship.joinTable != "" {
whereSql := fmt.Sprintf("%v.%v IN (?)", joinTable.joinTable, scope.Quote(ToSnake(joinTable.associationForeignKey))) whereSql := fmt.Sprintf("%v.%v IN (?)", relationship.joinTable, scope.Quote(ToSnake(relationship.associationForeignKey)))
scope.db.Table(joinTable.joinTable).Where(whereSql, primaryKeys).Delete("") scope.db.Table(relationship.joinTable).Where(whereSql, primaryKeys).Delete("")
} else { } else {
association.err(errors.New("only many to many support delete")) association.err(errors.New("only many to many support delete"))
} }
@ -106,22 +106,22 @@ func (association *Association) Clear(value interface{}) *Association {
} }
func (association *Association) Count() (count int) { func (association *Association) Count() (count int) {
joinTable := association.Field.JoinTable relationship := association.Field.Relationship
scope := association.Scope scope := association.Scope
field := scope.IndirectValue().FieldByName(association.Column) field := scope.IndirectValue().FieldByName(association.Column)
fieldValue := field.Interface() fieldValue := field.Interface()
// many to many // many to many
if joinTable.joinTable != "" { if relationship.joinTable != "" {
newScope := scope.New(fieldValue) newScope := scope.New(fieldValue)
whereSql := fmt.Sprintf("%v.%v IN (SELECT %v.%v FROM %v WHERE %v.%v = ?)", whereSql := fmt.Sprintf("%v.%v IN (SELECT %v.%v FROM %v WHERE %v.%v = ?)",
newScope.QuotedTableName(), newScope.QuotedTableName(),
scope.Quote(newScope.PrimaryKey()), scope.Quote(newScope.PrimaryKey()),
joinTable.joinTable, relationship.joinTable,
scope.Quote(joinTable.associationForeignKey), scope.Quote(relationship.associationForeignKey),
joinTable.joinTable, relationship.joinTable,
joinTable.joinTable, relationship.joinTable,
scope.Quote(joinTable.foreignKey)) scope.Quote(relationship.foreignKey))
scope.db.Table(newScope.QuotedTableName()).Where(whereSql, scope.PrimaryKey()).Count(&count) scope.db.Table(newScope.QuotedTableName()).Where(whereSql, scope.PrimaryKey()).Count(&count)
} }
// association.Scope.related(value, association.Column) // association.Scope.related(value, association.Column)

View File

@ -32,8 +32,8 @@ func SaveBeforeAssociations(scope *Scope) {
scope.SetColumn(field.Name, value.Interface()) scope.SetColumn(field.Name, value.Interface())
} }
if field.JoinTable != nil && field.JoinTable.foreignKey != "" { if field.Relationship != nil && field.Relationship.foreignKey != "" {
scope.SetColumn(field.JoinTable.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue()) scope.SetColumn(field.Relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
} }
} }
} }
@ -50,18 +50,18 @@ func SaveAfterAssociations(scope *Scope) {
newDB := scope.NewDB() newDB := scope.NewDB()
elem := value.Index(i).Addr().Interface() elem := value.Index(i).Addr().Interface()
if field.JoinTable != nil && field.JoinTable.joinTable == "" && field.JoinTable.foreignKey != "" { if field.Relationship != nil && field.Relationship.joinTable == "" && field.Relationship.foreignKey != "" {
newDB.NewScope(elem).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue()) newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
} }
scope.Err(newDB.Save(elem).Error) scope.Err(newDB.Save(elem).Error)
if field.JoinTable != nil && field.JoinTable.joinTable != "" { if field.Relationship != nil && field.Relationship.joinTable != "" {
newScope := scope.New(elem) newScope := scope.New(elem)
joinTable := field.JoinTable.joinTable joinTable := field.Relationship.joinTable
foreignKey := ToSnake(field.JoinTable.foreignKey) foreignKey := ToSnake(field.Relationship.foreignKey)
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue()) foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
associationForeignKey := ToSnake(field.JoinTable.associationForeignKey) associationForeignKey := ToSnake(field.Relationship.associationForeignKey)
associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue()) associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue())
newScope.Raw(fmt.Sprintf( newScope.Raw(fmt.Sprintf(
@ -84,8 +84,8 @@ func SaveAfterAssociations(scope *Scope) {
default: default:
newDB := scope.NewDB() newDB := scope.NewDB()
if value.CanAddr() { if value.CanAddr() {
if field.JoinTable != nil { if field.Relationship != nil {
newDB.NewScope(field.Value).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue()) newDB.NewScope(field.Value).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
} }
scope.Err(newDB.Save(field.Value).Error) scope.Err(newDB.Save(field.Value).Error)
} else { } else {
@ -96,8 +96,8 @@ func SaveAfterAssociations(scope *Scope) {
} }
elem := destValue.Addr().Interface() elem := destValue.Addr().Interface()
if field.JoinTable != nil { if field.Relationship != nil {
newDB.NewScope(elem).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue()) newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
} }
scope.Err(newDB.Save(elem).Error) scope.Err(newDB.Save(elem).Error)
scope.SetColumn(field.Name, destValue.Interface()) scope.SetColumn(field.Name, destValue.Interface())

View File

@ -6,7 +6,7 @@ import (
"time" "time"
) )
type joinTable struct { type relationship struct {
joinTable string joinTable string
foreignKey string foreignKey string
associationForeignKey string associationForeignKey string
@ -23,7 +23,7 @@ type Field struct {
BeforeAssociation bool BeforeAssociation bool
AfterAssociation bool AfterAssociation bool
isPrimaryKey bool isPrimaryKey bool
JoinTable *joinTable Relationship *relationship
} }
func (f *Field) IsScanner() bool { func (f *Field) IsScanner() bool {

View File

@ -366,7 +366,7 @@ func (s *DB) Association(column string) *Association {
scopeType := scope.IndirectValue().Type() scopeType := scope.IndirectValue().Type()
if f, ok := scopeType.FieldByName(SnakeToUpperCamel(column)); ok { if f, ok := scopeType.FieldByName(SnakeToUpperCamel(column)); ok {
field = scope.fieldFromStruct(f) field = scope.fieldFromStruct(f)
if field.JoinTable == nil || field.JoinTable.foreignKey == "" { if field.Relationship == nil || field.Relationship.foreignKey == "" {
scope.Err(errors.New(fmt.Sprintf("invalid association %v for %v", column, scopeType))) scope.Err(errors.New(fmt.Sprintf("invalid association %v for %v", column, scopeType)))
} }
} else { } else {

View File

@ -285,7 +285,7 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field {
} }
field.AfterAssociation = true field.AfterAssociation = true
field.JoinTable = &joinTable{ field.Relationship = &relationship{
joinTable: many2many, joinTable: many2many,
foreignKey: foreignKey, foreignKey: foreignKey,
associationForeignKey: associationForeignKey, associationForeignKey: associationForeignKey,
@ -294,17 +294,17 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field {
case reflect.Struct: case reflect.Struct:
if !field.IsTime() && !field.IsScanner() { if !field.IsTime() && !field.IsScanner() {
if foreignKey == "" && scope.HasColumn(field.Name+"Id") { if foreignKey == "" && scope.HasColumn(field.Name+"Id") {
field.JoinTable = &joinTable{foreignKey: field.Name + "Id"} field.Relationship = &relationship{foreignKey: field.Name + "Id"}
field.BeforeAssociation = true field.BeforeAssociation = true
} else if scope.HasColumn(foreignKey) { } else if scope.HasColumn(foreignKey) {
field.JoinTable = &joinTable{foreignKey: foreignKey} field.Relationship = &relationship{foreignKey: foreignKey}
field.BeforeAssociation = true field.BeforeAssociation = true
} else { } else {
if foreignKey == "" { if foreignKey == "" {
foreignKey = scopeTyp.Name() + "Id" foreignKey = scopeTyp.Name() + "Id"
} }
if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() { if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
field.JoinTable = &joinTable{foreignKey: foreignKey} field.Relationship = &relationship{foreignKey: foreignKey}
} }
field.AfterAssociation = true field.AfterAssociation = true
} }

View File

@ -419,20 +419,20 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
scopeType := scope.IndirectValue().Type() scopeType := scope.IndirectValue().Type()
if f, ok := scopeType.FieldByName(SnakeToUpperCamel(foreignKey)); ok { if f, ok := scopeType.FieldByName(SnakeToUpperCamel(foreignKey)); ok {
field := scope.fieldFromStruct(f) field := scope.fieldFromStruct(f)
joinTable := field.JoinTable relationship := field.Relationship
if joinTable != nil && joinTable.foreignKey != "" { if relationship != nil && relationship.foreignKey != "" {
foreignKey = joinTable.foreignKey foreignKey = relationship.foreignKey
// many to many relations // many to many relations
if joinTable.joinTable != "" { if relationship.joinTable != "" {
joinSql := fmt.Sprintf( joinSql := fmt.Sprintf(
"INNER JOIN %v ON %v.%v = %v.%v", "INNER JOIN %v ON %v.%v = %v.%v",
scope.Quote(joinTable.joinTable), scope.Quote(relationship.joinTable),
scope.Quote(joinTable.joinTable), scope.Quote(relationship.joinTable),
scope.Quote(ToSnake(joinTable.associationForeignKey)), scope.Quote(ToSnake(relationship.associationForeignKey)),
toScope.QuotedTableName(), toScope.QuotedTableName(),
scope.Quote(toScope.PrimaryKey())) scope.Quote(toScope.PrimaryKey()))
whereSql := fmt.Sprintf("%v.%v = ?", scope.Quote(joinTable.joinTable), scope.Quote(ToSnake(joinTable.foreignKey))) whereSql := fmt.Sprintf("%v.%v = ?", scope.Quote(relationship.joinTable), scope.Quote(ToSnake(relationship.foreignKey)))
toScope.db.Joins(joinSql).Where(whereSql, scope.PrimaryKeyValue()).Find(value) toScope.db.Joins(joinSql).Where(whereSql, scope.PrimaryKeyValue()).Find(value)
return scope return scope
} }
@ -451,20 +451,20 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
return toScope.inlineCondition(sql, scope.PrimaryKeyValue()).callCallbacks(scope.db.parent.callback.queries) return toScope.inlineCondition(sql, scope.PrimaryKeyValue()).callCallbacks(scope.db.parent.callback.queries)
} }
} }
scope.Err(errors.New(fmt.Sprintf("invalid association %v", foreignKeys))) scope.Err(fmt.Errorf("invalid association %v", foreignKeys))
return scope return scope
} }
func (scope *Scope) createJoinTable(field *Field) { func (scope *Scope) createJoinTable(field *Field) {
if field.JoinTable != nil && field.JoinTable.joinTable != "" { if field.Relationship != nil && field.Relationship.joinTable != "" {
if !scope.Dialect().HasTable(scope, field.JoinTable.joinTable) { if !scope.Dialect().HasTable(scope, field.Relationship.joinTable) {
newScope := scope.db.NewScope("") newScope := scope.db.NewScope("")
primaryKeySqlType := scope.Dialect().SqlTag(reflect.ValueOf(scope.PrimaryKeyValue()), 255) primaryKeySqlType := scope.Dialect().SqlTag(reflect.ValueOf(scope.PrimaryKeyValue()), 255)
newScope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)", newScope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)",
field.JoinTable.joinTable, field.Relationship.joinTable,
strings.Join([]string{ strings.Join([]string{
scope.Quote(ToSnake(field.JoinTable.foreignKey)) + " " + primaryKeySqlType, scope.Quote(ToSnake(field.Relationship.foreignKey)) + " " + primaryKeySqlType,
scope.Quote(ToSnake(field.JoinTable.associationForeignKey)) + " " + primaryKeySqlType}, ",")), scope.Quote(ToSnake(field.Relationship.associationForeignKey)) + " " + primaryKeySqlType}, ",")),
).Exec() ).Exec()
scope.Err(newScope.db.Error) scope.Err(newScope.db.Error)
} }