2015-02-28 06:48:18 +03:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-18 13:14:28 +03:00
|
|
|
"reflect"
|
2015-02-28 06:48:18 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-03-18 06:47:11 +03:00
|
|
|
type JoinTableHandlerInterface interface {
|
|
|
|
Table(db *DB) string
|
|
|
|
Add(db *DB, source1 interface{}, source2 interface{}) error
|
|
|
|
Delete(db *DB, sources ...interface{}) error
|
|
|
|
JoinWith(db *DB, source interface{}) *DB
|
2015-02-28 06:48:18 +03:00
|
|
|
}
|
|
|
|
|
2015-03-19 10:02:15 +03:00
|
|
|
type JoinTableForeignKey struct {
|
|
|
|
DBName string
|
|
|
|
AssociationDBName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateJoinTableHandler(relationship *Relationship) {
|
|
|
|
handler := relationship.JoinTableHandler.(*JoinTableHandler)
|
|
|
|
|
|
|
|
destinationScope := &Scope{Value: reflect.New(handler.Destination.ModelType).Interface()}
|
|
|
|
for _, primaryField := range destinationScope.GetModelStruct().PrimaryFields {
|
|
|
|
db := relationship.ForeignDBName
|
|
|
|
handler.Destination.ForeignKeys = append(handler.Destination.ForeignKeys, JoinTableForeignKey{
|
|
|
|
DBName: db,
|
|
|
|
AssociationDBName: primaryField.DBName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceScope := &Scope{Value: reflect.New(handler.Source.ModelType).Interface()}
|
|
|
|
for _, primaryField := range sourceScope.GetModelStruct().PrimaryFields {
|
|
|
|
db := relationship.AssociationForeignDBName
|
|
|
|
handler.Source.ForeignKeys = append(handler.Source.ForeignKeys, JoinTableForeignKey{
|
|
|
|
DBName: db,
|
|
|
|
AssociationDBName: primaryField.DBName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 06:47:11 +03:00
|
|
|
type JoinTableSource struct {
|
2015-03-18 13:14:28 +03:00
|
|
|
ModelType reflect.Type
|
2015-03-19 10:02:15 +03:00
|
|
|
ForeignKeys []JoinTableForeignKey
|
2015-03-18 06:47:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type JoinTableHandler struct {
|
2015-03-18 13:14:28 +03:00
|
|
|
TableName string `sql:"-"`
|
|
|
|
Source JoinTableSource `sql:"-"`
|
|
|
|
Destination JoinTableSource `sql:"-"`
|
2015-03-18 06:47:11 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 13:14:28 +03:00
|
|
|
func (s JoinTableHandler) Table(*DB) string {
|
|
|
|
return s.TableName
|
2015-03-18 06:47:11 +03:00
|
|
|
}
|
2015-02-28 06:48:18 +03:00
|
|
|
|
2015-03-18 13:14:28 +03:00
|
|
|
func (s JoinTableHandler) GetSearchMap(db *DB, sources ...interface{}) map[string]interface{} {
|
2015-03-18 06:47:11 +03:00
|
|
|
values := map[string]interface{}{}
|
2015-03-18 13:14:28 +03:00
|
|
|
|
2015-03-18 06:47:11 +03:00
|
|
|
for _, source := range sources {
|
|
|
|
scope := db.NewScope(source)
|
2015-03-18 13:14:28 +03:00
|
|
|
modelType := scope.GetModelStruct().ModelType
|
|
|
|
|
|
|
|
if s.Source.ModelType == modelType {
|
|
|
|
for _, foreignKey := range s.Source.ForeignKeys {
|
|
|
|
values[foreignKey.DBName] = scope.Fields()[foreignKey.AssociationDBName].Field.Interface()
|
|
|
|
}
|
|
|
|
} else if s.Destination.ModelType == modelType {
|
|
|
|
for _, foreignKey := range s.Destination.ForeignKeys {
|
|
|
|
values[foreignKey.DBName] = scope.Fields()[foreignKey.AssociationDBName].Field.Interface()
|
2015-03-18 06:47:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values
|
2015-03-04 07:16:16 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 13:14:28 +03:00
|
|
|
func (s JoinTableHandler) Add(db *DB, source1 interface{}, source2 interface{}) error {
|
2015-02-28 06:48:18 +03:00
|
|
|
scope := db.NewScope("")
|
2015-03-18 13:14:28 +03:00
|
|
|
searchMap := s.GetSearchMap(db, source1, source2)
|
2015-03-18 06:47:11 +03:00
|
|
|
|
2015-03-18 13:14:28 +03:00
|
|
|
var assignColumns, binVars, conditions []string
|
2015-03-18 06:47:11 +03:00
|
|
|
var values []interface{}
|
2015-03-18 13:14:28 +03:00
|
|
|
for key, value := range searchMap {
|
|
|
|
assignColumns = append(assignColumns, key)
|
|
|
|
binVars = append(binVars, `?`)
|
|
|
|
conditions = append(conditions, fmt.Sprintf("%v = ?", scope.Quote(key)))
|
2015-03-18 06:47:11 +03:00
|
|
|
values = append(values, value)
|
|
|
|
}
|
2015-02-28 06:48:18 +03:00
|
|
|
|
2015-03-18 13:14:28 +03:00
|
|
|
for _, value := range searchMap {
|
2015-03-18 06:47:11 +03:00
|
|
|
values = append(values, value)
|
|
|
|
}
|
|
|
|
|
2015-03-18 13:14:28 +03:00
|
|
|
quotedTable := s.Table(db)
|
2015-02-28 06:48:18 +03:00
|
|
|
sql := fmt.Sprintf(
|
2015-03-18 06:47:11 +03:00
|
|
|
"INSERT INTO %v (%v) SELECT %v %v WHERE NOT EXISTS (SELECT * FROM %v WHERE %v);",
|
|
|
|
quotedTable,
|
2015-03-18 13:14:28 +03:00
|
|
|
strings.Join(assignColumns, ","),
|
|
|
|
strings.Join(binVars, ","),
|
2015-02-28 06:48:18 +03:00
|
|
|
scope.Dialect().SelectFromDummyTable(),
|
2015-03-18 06:47:11 +03:00
|
|
|
quotedTable,
|
2015-03-18 13:14:28 +03:00
|
|
|
strings.Join(conditions, " AND "),
|
2015-02-28 06:48:18 +03:00
|
|
|
)
|
|
|
|
|
2015-03-18 06:47:11 +03:00
|
|
|
return db.Exec(sql, values...).Error
|
2015-02-28 06:48:18 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 13:14:28 +03:00
|
|
|
func (s JoinTableHandler) Delete(db *DB, sources ...interface{}) error {
|
|
|
|
var conditions []string
|
|
|
|
var values []interface{}
|
|
|
|
|
|
|
|
for key, value := range s.GetSearchMap(db, sources...) {
|
|
|
|
conditions = append(conditions, fmt.Sprintf("%v = ?", key))
|
|
|
|
values = append(values, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.Table(s.Table(db)).Where(strings.Join(conditions, " AND "), values...).Delete("").Error
|
2015-02-28 06:48:18 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 13:14:28 +03:00
|
|
|
func (s JoinTableHandler) JoinWith(db *DB, source interface{}) *DB {
|
|
|
|
quotedTable := s.Table(db)
|
|
|
|
|
|
|
|
scope := db.NewScope(source)
|
|
|
|
modelType := scope.GetModelStruct().ModelType
|
|
|
|
var joinConditions []string
|
|
|
|
var queryConditions []string
|
|
|
|
var values []interface{}
|
|
|
|
if s.Source.ModelType == modelType {
|
|
|
|
for _, foreignKey := range s.Destination.ForeignKeys {
|
2015-03-19 11:42:13 +03:00
|
|
|
destinationTableName := scope.New(reflect.New(s.Destination.ModelType).Interface()).QuotedTableName()
|
|
|
|
joinConditions = append(joinConditions, fmt.Sprintf("%v.%v = %v.%v", quotedTable, scope.Quote(foreignKey.DBName), destinationTableName, scope.Quote(foreignKey.AssociationDBName)))
|
2015-03-18 13:14:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
} else if s.Destination.ModelType == modelType {
|
|
|
|
for _, foreignKey := range s.Source.ForeignKeys {
|
2015-03-19 11:42:13 +03:00
|
|
|
sourceTableName := scope.New(reflect.New(s.Source.ModelType).Interface()).QuotedTableName()
|
|
|
|
joinConditions = append(joinConditions, fmt.Sprintf("%v.%v = %v.%v", quotedTable, scope.Quote(foreignKey.DBName), sourceTableName, scope.Quote(foreignKey.AssociationDBName)))
|
2015-03-18 13:14:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, foreignKey := range s.Destination.ForeignKeys {
|
|
|
|
queryConditions = append(queryConditions, fmt.Sprintf("%v.%v = ?", quotedTable, scope.Quote(foreignKey.DBName)))
|
|
|
|
values = append(values, scope.Fields()[foreignKey.AssociationDBName].Field.Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 11:42:13 +03:00
|
|
|
return db.Joins(fmt.Sprintf("INNER JOIN %v ON %v", quotedTable, strings.Join(joinConditions, " AND "))).
|
2015-03-18 13:14:28 +03:00
|
|
|
Where(strings.Join(queryConditions, " AND "), values...)
|
2015-02-28 06:48:18 +03:00
|
|
|
}
|