From 2356182ea74e702ddaaac638993ddcf61e372e60 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 30 Jul 2014 20:59:52 +0800 Subject: [PATCH] Rename joinTable to relationship --- association.go | 22 +++++++++++----------- callback_shared.go | 24 ++++++++++++------------ field.go | 4 ++-- main.go | 2 +- scope.go | 8 ++++---- scope_private.go | 28 ++++++++++++++-------------- 6 files changed, 44 insertions(+), 44 deletions(-) diff --git a/association.go b/association.go index 542a27ca..89ea2b9f 100644 --- a/association.go +++ b/association.go @@ -85,11 +85,11 @@ func (association *Association) Delete(values ...interface{}) *Association { if len(primaryKeys) == 0 { association.err(errors.New("no primary key found")) } else { - joinTable := association.Field.JoinTable + relationship := association.Field.Relationship // many to many - if joinTable.joinTable != "" { - whereSql := fmt.Sprintf("%v.%v IN (?)", joinTable.joinTable, scope.Quote(ToSnake(joinTable.associationForeignKey))) - scope.db.Table(joinTable.joinTable).Where(whereSql, primaryKeys).Delete("") + if relationship.joinTable != "" { + whereSql := fmt.Sprintf("%v.%v IN (?)", relationship.joinTable, scope.Quote(ToSnake(relationship.associationForeignKey))) + scope.db.Table(relationship.joinTable).Where(whereSql, primaryKeys).Delete("") } else { 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) { - joinTable := association.Field.JoinTable + relationship := association.Field.Relationship scope := association.Scope field := scope.IndirectValue().FieldByName(association.Column) fieldValue := field.Interface() // many to many - if joinTable.joinTable != "" { + if relationship.joinTable != "" { newScope := scope.New(fieldValue) whereSql := fmt.Sprintf("%v.%v IN (SELECT %v.%v FROM %v WHERE %v.%v = ?)", newScope.QuotedTableName(), scope.Quote(newScope.PrimaryKey()), - joinTable.joinTable, - scope.Quote(joinTable.associationForeignKey), - joinTable.joinTable, - joinTable.joinTable, - scope.Quote(joinTable.foreignKey)) + relationship.joinTable, + scope.Quote(relationship.associationForeignKey), + relationship.joinTable, + relationship.joinTable, + scope.Quote(relationship.foreignKey)) scope.db.Table(newScope.QuotedTableName()).Where(whereSql, scope.PrimaryKey()).Count(&count) } // association.Scope.related(value, association.Column) diff --git a/callback_shared.go b/callback_shared.go index b49ab017..d007ea7c 100644 --- a/callback_shared.go +++ b/callback_shared.go @@ -32,8 +32,8 @@ func SaveBeforeAssociations(scope *Scope) { scope.SetColumn(field.Name, value.Interface()) } - if field.JoinTable != nil && field.JoinTable.foreignKey != "" { - scope.SetColumn(field.JoinTable.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue()) + if field.Relationship != nil && field.Relationship.foreignKey != "" { + scope.SetColumn(field.Relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue()) } } } @@ -50,18 +50,18 @@ func SaveAfterAssociations(scope *Scope) { newDB := scope.NewDB() elem := value.Index(i).Addr().Interface() - if field.JoinTable != nil && field.JoinTable.joinTable == "" && field.JoinTable.foreignKey != "" { - newDB.NewScope(elem).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue()) + if field.Relationship != nil && field.Relationship.joinTable == "" && field.Relationship.foreignKey != "" { + newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue()) } scope.Err(newDB.Save(elem).Error) - if field.JoinTable != nil && field.JoinTable.joinTable != "" { + if field.Relationship != nil && field.Relationship.joinTable != "" { newScope := scope.New(elem) - joinTable := field.JoinTable.joinTable - foreignKey := ToSnake(field.JoinTable.foreignKey) + joinTable := field.Relationship.joinTable + foreignKey := ToSnake(field.Relationship.foreignKey) foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue()) - associationForeignKey := ToSnake(field.JoinTable.associationForeignKey) + associationForeignKey := ToSnake(field.Relationship.associationForeignKey) associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue()) newScope.Raw(fmt.Sprintf( @@ -84,8 +84,8 @@ func SaveAfterAssociations(scope *Scope) { default: newDB := scope.NewDB() if value.CanAddr() { - if field.JoinTable != nil { - newDB.NewScope(field.Value).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue()) + if field.Relationship != nil { + newDB.NewScope(field.Value).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue()) } scope.Err(newDB.Save(field.Value).Error) } else { @@ -96,8 +96,8 @@ func SaveAfterAssociations(scope *Scope) { } elem := destValue.Addr().Interface() - if field.JoinTable != nil { - newDB.NewScope(elem).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue()) + if field.Relationship != nil { + newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue()) } scope.Err(newDB.Save(elem).Error) scope.SetColumn(field.Name, destValue.Interface()) diff --git a/field.go b/field.go index 8452686c..57cd80ba 100644 --- a/field.go +++ b/field.go @@ -6,7 +6,7 @@ import ( "time" ) -type joinTable struct { +type relationship struct { joinTable string foreignKey string associationForeignKey string @@ -23,7 +23,7 @@ type Field struct { BeforeAssociation bool AfterAssociation bool isPrimaryKey bool - JoinTable *joinTable + Relationship *relationship } func (f *Field) IsScanner() bool { diff --git a/main.go b/main.go index e5e2fdb9..5f82a361 100644 --- a/main.go +++ b/main.go @@ -366,7 +366,7 @@ func (s *DB) Association(column string) *Association { scopeType := scope.IndirectValue().Type() if f, ok := scopeType.FieldByName(SnakeToUpperCamel(column)); ok { 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))) } } else { diff --git a/scope.go b/scope.go index fb7ac207..25747bf4 100644 --- a/scope.go +++ b/scope.go @@ -285,7 +285,7 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field { } field.AfterAssociation = true - field.JoinTable = &joinTable{ + field.Relationship = &relationship{ joinTable: many2many, foreignKey: foreignKey, associationForeignKey: associationForeignKey, @@ -294,17 +294,17 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field { case reflect.Struct: if !field.IsTime() && !field.IsScanner() { if foreignKey == "" && scope.HasColumn(field.Name+"Id") { - field.JoinTable = &joinTable{foreignKey: field.Name + "Id"} + field.Relationship = &relationship{foreignKey: field.Name + "Id"} field.BeforeAssociation = true } else if scope.HasColumn(foreignKey) { - field.JoinTable = &joinTable{foreignKey: foreignKey} + field.Relationship = &relationship{foreignKey: foreignKey} field.BeforeAssociation = true } else { if foreignKey == "" { foreignKey = scopeTyp.Name() + "Id" } if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() { - field.JoinTable = &joinTable{foreignKey: foreignKey} + field.Relationship = &relationship{foreignKey: foreignKey} } field.AfterAssociation = true } diff --git a/scope_private.go b/scope_private.go index de7fa8fd..b35ca3f8 100644 --- a/scope_private.go +++ b/scope_private.go @@ -419,20 +419,20 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { scopeType := scope.IndirectValue().Type() if f, ok := scopeType.FieldByName(SnakeToUpperCamel(foreignKey)); ok { field := scope.fieldFromStruct(f) - joinTable := field.JoinTable - if joinTable != nil && joinTable.foreignKey != "" { - foreignKey = joinTable.foreignKey + relationship := field.Relationship + if relationship != nil && relationship.foreignKey != "" { + foreignKey = relationship.foreignKey // many to many relations - if joinTable.joinTable != "" { + if relationship.joinTable != "" { joinSql := fmt.Sprintf( "INNER JOIN %v ON %v.%v = %v.%v", - scope.Quote(joinTable.joinTable), - scope.Quote(joinTable.joinTable), - scope.Quote(ToSnake(joinTable.associationForeignKey)), + scope.Quote(relationship.joinTable), + scope.Quote(relationship.joinTable), + scope.Quote(ToSnake(relationship.associationForeignKey)), toScope.QuotedTableName(), 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) 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) } } - scope.Err(errors.New(fmt.Sprintf("invalid association %v", foreignKeys))) + scope.Err(fmt.Errorf("invalid association %v", foreignKeys)) return scope } func (scope *Scope) createJoinTable(field *Field) { - if field.JoinTable != nil && field.JoinTable.joinTable != "" { - if !scope.Dialect().HasTable(scope, field.JoinTable.joinTable) { + if field.Relationship != nil && field.Relationship.joinTable != "" { + if !scope.Dialect().HasTable(scope, field.Relationship.joinTable) { newScope := scope.db.NewScope("") primaryKeySqlType := scope.Dialect().SqlTag(reflect.ValueOf(scope.PrimaryKeyValue()), 255) newScope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)", - field.JoinTable.joinTable, + field.Relationship.joinTable, strings.Join([]string{ - scope.Quote(ToSnake(field.JoinTable.foreignKey)) + " " + primaryKeySqlType, - scope.Quote(ToSnake(field.JoinTable.associationForeignKey)) + " " + primaryKeySqlType}, ",")), + scope.Quote(ToSnake(field.Relationship.foreignKey)) + " " + primaryKeySqlType, + scope.Quote(ToSnake(field.Relationship.associationForeignKey)) + " " + primaryKeySqlType}, ",")), ).Exec() scope.Err(newScope.db.Error) }