2015-02-28 06:48:18 +03:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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-18 06:47:11 +03:00
|
|
|
type JoinTableSource struct {
|
|
|
|
ForeignKey string
|
|
|
|
ForeignKeyPrefix string
|
|
|
|
ModelStruct
|
|
|
|
}
|
|
|
|
|
|
|
|
type JoinTableHandler struct {
|
|
|
|
TableName string
|
|
|
|
Source1 JoinTableSource
|
|
|
|
Source2 JoinTableSource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jt JoinTableHandler) Table(*DB) string {
|
|
|
|
return jt.TableName
|
|
|
|
}
|
2015-02-28 06:48:18 +03:00
|
|
|
|
2015-03-18 06:47:11 +03:00
|
|
|
func (jt JoinTableHandler) GetValueMap(db *DB, sources ...interface{}) map[string]interface{} {
|
|
|
|
values := map[string]interface{}{}
|
|
|
|
for _, source := range sources {
|
|
|
|
scope := db.NewScope(source)
|
|
|
|
for _, primaryField := range scope.GetModelStruct().PrimaryFields {
|
|
|
|
if field, ok := scope.Fields()[primaryField.DBName]; ok {
|
|
|
|
values[primaryField.DBName] = field.Field.Interface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values
|
2015-03-04 07:16:16 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 06:47:11 +03:00
|
|
|
func (jt JoinTableHandler) Add(db *DB, source1 interface{}, source2 interface{}) error {
|
2015-02-28 06:48:18 +03:00
|
|
|
scope := db.NewScope("")
|
2015-03-18 06:47:11 +03:00
|
|
|
valueMap := jt.GetValueMap(db, source1, source2)
|
|
|
|
|
|
|
|
var setColumns, setBinVars, queryConditions []string
|
|
|
|
var values []interface{}
|
|
|
|
for key, value := range valueMap {
|
|
|
|
setColumns = append(setColumns, key)
|
|
|
|
setBinVars = append(setBinVars, `?`)
|
|
|
|
queryConditions = append(queryConditions, fmt.Sprintf("%v = ?", scope.Quote(key)))
|
|
|
|
values = append(values, value)
|
|
|
|
}
|
2015-02-28 06:48:18 +03:00
|
|
|
|
2015-03-18 06:47:11 +03:00
|
|
|
for _, value := range valueMap {
|
|
|
|
values = append(values, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
quotedTable := jt.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,
|
|
|
|
strings.Join(setColumns, ","),
|
|
|
|
strings.Join(setBinVars, ","),
|
2015-02-28 06:48:18 +03:00
|
|
|
scope.Dialect().SelectFromDummyTable(),
|
2015-03-18 06:47:11 +03:00
|
|
|
quotedTable,
|
|
|
|
strings.Join(queryConditions, " 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 06:47:11 +03:00
|
|
|
func (jt JoinTableHandler) Delete(db *DB, sources ...interface{}) error {
|
|
|
|
// return db.Table(jt.Table(db)).Delete("").Error
|
|
|
|
return nil
|
2015-02-28 06:48:18 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 06:47:11 +03:00
|
|
|
func (jt JoinTableHandler) JoinWith(db *DB, sources interface{}) *DB {
|
|
|
|
return db
|
2015-02-28 06:48:18 +03:00
|
|
|
}
|