Remove not necessary PreloadWithJoin

This commit is contained in:
Jinzhu 2015-08-18 08:05:44 +08:00
parent 7ec2b87312
commit 6a6c1bf762
2 changed files with 1 additions and 28 deletions

View File

@ -13,7 +13,6 @@ type JoinTableHandlerInterface interface {
Add(handler JoinTableHandlerInterface, db *DB, source interface{}, destination interface{}) error Add(handler JoinTableHandlerInterface, db *DB, source interface{}, destination interface{}) error
Delete(handler JoinTableHandlerInterface, db *DB, sources ...interface{}) error Delete(handler JoinTableHandlerInterface, db *DB, sources ...interface{}) error
JoinWith(handler JoinTableHandlerInterface, db *DB, source interface{}) *DB JoinWith(handler JoinTableHandlerInterface, db *DB, source interface{}) *DB
PreloadWithJoin(handler JoinTableHandlerInterface, db *DB, source interface{}) *DB
SourceForeignKeys() []JoinTableForeignKey SourceForeignKeys() []JoinTableForeignKey
DestinationForeignKeys() []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 { func (s JoinTableHandler) JoinWith(handler JoinTableHandlerInterface, db *DB, source interface{}) *DB {
quotedTable := handler.Table(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) scope := db.NewScope(source)
modelType := scope.GetModelStruct().ModelType modelType := scope.GetModelStruct().ModelType
var joinConditions []string var joinConditions []string

View File

@ -210,7 +210,7 @@ func (scope *Scope) handleHasManyToManyPreload(field *Field, conditions []interf
} }
db := scope.NewDB().Table(scope.New(reflect.New(destType).Interface()).TableName()) 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 { if len(conditions) > 0 {
preloadJoinDB = preloadJoinDB.Where(conditions[0], conditions[1:]...) preloadJoinDB = preloadJoinDB.Where(conditions[0], conditions[1:]...)
} }