forked from mirror/gorm
Remove not necessary PreloadWithJoin
This commit is contained in:
parent
7ec2b87312
commit
6a6c1bf762
|
@ -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
|
||||
|
|
|
@ -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:]...)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue