From 6a6c1bf762176b817a8862960090711ec95a6d4e Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 18 Aug 2015 08:05:44 +0800 Subject: [PATCH] Remove not necessary PreloadWithJoin --- join_table_handler.go | 27 --------------------------- preload.go | 2 +- 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/join_table_handler.go b/join_table_handler.go index 162af4a8..ad788412 100644 --- a/join_table_handler.go +++ b/join_table_handler.go @@ -13,7 +13,6 @@ type JoinTableHandlerInterface interface { Add(handler JoinTableHandlerInterface, db *DB, source interface{}, destination interface{}) error Delete(handler JoinTableHandlerInterface, db *DB, sources ...interface{}) error JoinWith(handler JoinTableHandlerInterface, db *DB, source interface{}) *DB - PreloadWithJoin(handler JoinTableHandlerInterface, db *DB, source interface{}) *DB SourceForeignKeys() []JoinTableForeignKey DestinationForeignKeys() []JoinTableForeignKey } @@ -132,32 +131,6 @@ func (s JoinTableHandler) Delete(handler JoinTableHandlerInterface, db *DB, sour func (s JoinTableHandler) JoinWith(handler JoinTableHandlerInterface, db *DB, source interface{}) *DB { quotedTable := handler.Table(db) - scope := db.NewScope(source) - modelType := scope.GetModelStruct().ModelType - var joinConditions []string - var queryConditions []string - var values []interface{} - if s.Source.ModelType == modelType { - destinationTableName := db.NewScope(reflect.New(s.Destination.ModelType).Interface()).QuotedTableName() - for _, foreignKey := range s.Destination.ForeignKeys { - joinConditions = append(joinConditions, fmt.Sprintf("%v.%v = %v.%v", quotedTable, scope.Quote(foreignKey.DBName), destinationTableName, scope.Quote(foreignKey.AssociationDBName))) - } - - for _, foreignKey := range s.Source.ForeignKeys { - queryConditions = append(queryConditions, fmt.Sprintf("%v.%v = ?", quotedTable, scope.Quote(foreignKey.DBName))) - values = append(values, scope.Fields()[foreignKey.AssociationDBName].Field.Interface()) - } - return db.Joins(fmt.Sprintf("INNER JOIN %v ON %v", quotedTable, strings.Join(joinConditions, " AND "))). - Where(strings.Join(queryConditions, " AND "), values...) - } else { - db.Error = errors.New("wrong source type for join table handler") - return db - } -} - -func (s JoinTableHandler) PreloadWithJoin(handler JoinTableHandlerInterface, db *DB, source interface{}) *DB { - quotedTable := handler.Table(db) - scope := db.NewScope(source) modelType := scope.GetModelStruct().ModelType var joinConditions []string diff --git a/preload.go b/preload.go index de90077a..d5c8da10 100644 --- a/preload.go +++ b/preload.go @@ -210,7 +210,7 @@ func (scope *Scope) handleHasManyToManyPreload(field *Field, conditions []interf } db := scope.NewDB().Table(scope.New(reflect.New(destType).Interface()).TableName()) - preloadJoinDB := joinTableHandler.PreloadWithJoin(joinTableHandler, db, scope.Value) + preloadJoinDB := joinTableHandler.JoinWith(joinTableHandler, db, scope.Value) if len(conditions) > 0 { preloadJoinDB = preloadJoinDB.Where(conditions[0], conditions[1:]...) }