2014-07-30 10:30:21 +04:00
|
|
|
package gorm
|
|
|
|
|
2014-07-30 12:22:26 +04:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2015-07-30 12:26:10 +03:00
|
|
|
"strings"
|
2014-07-30 12:22:26 +04:00
|
|
|
)
|
|
|
|
|
2014-07-30 10:30:21 +04:00
|
|
|
type Association struct {
|
2015-07-30 12:26:10 +03:00
|
|
|
Scope *Scope
|
|
|
|
Column string
|
|
|
|
Error error
|
|
|
|
Field *Field
|
2014-07-30 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 06:26:02 +03:00
|
|
|
func (association *Association) setErr(err error) *Association {
|
2014-07-30 16:48:36 +04:00
|
|
|
if err != nil {
|
|
|
|
association.Error = err
|
2014-07-30 12:22:26 +04:00
|
|
|
}
|
2014-07-30 16:48:36 +04:00
|
|
|
return association
|
|
|
|
}
|
|
|
|
|
|
|
|
func (association *Association) Find(value interface{}) *Association {
|
|
|
|
association.Scope.related(value, association.Column)
|
2015-02-13 06:26:02 +03:00
|
|
|
return association.setErr(association.Scope.db.Error)
|
2014-07-30 16:48:36 +04:00
|
|
|
}
|
2014-07-30 12:22:26 +04:00
|
|
|
|
2014-07-30 16:48:36 +04:00
|
|
|
func (association *Association) Append(values ...interface{}) *Association {
|
|
|
|
scope := association.Scope
|
2014-09-02 15:03:01 +04:00
|
|
|
field := association.Field
|
2015-12-25 10:13:40 +03:00
|
|
|
relationship := association.Field.Relationship
|
2014-09-02 15:03:01 +04:00
|
|
|
|
2015-12-25 10:13:40 +03:00
|
|
|
saveAssociation := func(reflectValue reflect.Value) {
|
|
|
|
// value has to been pointer
|
2015-09-30 07:10:22 +03:00
|
|
|
if reflectValue.Kind() != reflect.Ptr {
|
|
|
|
reflectPtr := reflect.New(reflectValue.Type())
|
|
|
|
reflectPtr.Elem().Set(reflectValue)
|
2015-12-25 10:13:40 +03:00
|
|
|
reflectValue = reflectPtr
|
2015-09-30 07:10:22 +03:00
|
|
|
}
|
|
|
|
|
2015-12-25 10:13:40 +03:00
|
|
|
// value has to been saved
|
|
|
|
if scope.New(reflectValue.Interface()).PrimaryKeyZero() {
|
|
|
|
scope.NewDB().Save(reflectValue.Interface())
|
2015-09-30 07:10:22 +03:00
|
|
|
}
|
|
|
|
|
2015-12-25 10:13:40 +03:00
|
|
|
// Assign Fields
|
|
|
|
fieldType := field.Field.Type()
|
|
|
|
if reflectValue.Type().AssignableTo(fieldType) {
|
|
|
|
field.Set(reflectValue)
|
|
|
|
} else if reflectValue.Type().Elem().AssignableTo(fieldType) {
|
|
|
|
field.Set(reflectValue.Elem())
|
|
|
|
} else if fieldType.Kind() == reflect.Slice {
|
|
|
|
if reflectValue.Type().AssignableTo(fieldType.Elem()) {
|
|
|
|
field.Set(reflect.Append(field.Field, reflectValue))
|
|
|
|
} else if reflectValue.Type().Elem().AssignableTo(fieldType.Elem()) {
|
|
|
|
field.Set(reflect.Append(field.Field, reflectValue.Elem()))
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 07:10:22 +03:00
|
|
|
|
2015-12-25 10:13:40 +03:00
|
|
|
if relationship.Kind == "many_to_many" {
|
|
|
|
association.setErr(relationship.JoinTableHandler.Add(relationship.JoinTableHandler, scope.NewDB(), scope.Value, reflectValue.Interface()))
|
|
|
|
} else {
|
|
|
|
association.setErr(scope.NewDB().Select(field.Name).Save(scope.Value).Error)
|
2015-09-30 07:10:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 16:48:36 +04:00
|
|
|
for _, value := range values {
|
2015-12-25 10:13:40 +03:00
|
|
|
reflectValue := reflect.ValueOf(value)
|
|
|
|
indirectReflectValue := reflect.Indirect(reflectValue)
|
|
|
|
if indirectReflectValue.Kind() == reflect.Struct {
|
|
|
|
saveAssociation(reflectValue)
|
|
|
|
} else if indirectReflectValue.Kind() == reflect.Slice {
|
|
|
|
for i := 0; i < indirectReflectValue.Len(); i++ {
|
|
|
|
saveAssociation(indirectReflectValue.Index(i))
|
2015-09-30 07:10:22 +03:00
|
|
|
}
|
2014-07-30 12:22:26 +04:00
|
|
|
} else {
|
2015-12-25 10:13:40 +03:00
|
|
|
association.setErr(errors.New("invalid value type"))
|
2014-07-30 12:22:26 +04:00
|
|
|
}
|
|
|
|
}
|
2015-09-30 07:10:22 +03:00
|
|
|
return association
|
2014-07-30 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
2015-12-25 12:32:28 +03:00
|
|
|
func (association *Association) Replace(values ...interface{}) *Association {
|
|
|
|
relationship := association.Field.Relationship
|
|
|
|
scope := association.Scope
|
|
|
|
field := association.Field.Field
|
|
|
|
|
2015-12-25 15:43:51 +03:00
|
|
|
// get old primary keys
|
2015-12-25 12:32:28 +03:00
|
|
|
oldPrimaryKeys := association.getPrimaryKeys(relationship.AssociationForeignFieldNames, field.Interface())
|
2015-12-25 15:43:51 +03:00
|
|
|
|
|
|
|
// append new values
|
2015-12-25 12:32:28 +03:00
|
|
|
association.Field.Set(reflect.Zero(association.Field.Field.Type()))
|
|
|
|
association.Append(values...)
|
2015-12-25 15:43:51 +03:00
|
|
|
|
|
|
|
// get new primary keys
|
|
|
|
var newPrimaryKeys [][]interface{}
|
|
|
|
if len(values) > 0 {
|
|
|
|
newPrimaryKeys = association.getPrimaryKeys(relationship.AssociationForeignFieldNames, field.Interface())
|
|
|
|
}
|
2015-12-25 12:32:28 +03:00
|
|
|
|
|
|
|
var addedPrimaryKeys = [][]interface{}{}
|
|
|
|
for _, newKey := range newPrimaryKeys {
|
|
|
|
hasEqual := false
|
|
|
|
for _, oldKey := range oldPrimaryKeys {
|
|
|
|
if equalAsString(newKey, oldKey) {
|
|
|
|
hasEqual = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasEqual {
|
|
|
|
addedPrimaryKeys = append(addedPrimaryKeys, newKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, primaryKey := range association.getPrimaryKeys(relationship.AssociationForeignFieldNames, values...) {
|
|
|
|
addedPrimaryKeys = append(addedPrimaryKeys, primaryKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := scope.NewDB()
|
|
|
|
var foreignKeyMap = map[string]interface{}{}
|
2015-12-25 15:43:51 +03:00
|
|
|
|
|
|
|
if relationship.Kind == "belongs_to" {
|
|
|
|
for idx, foreignKey := range relationship.AssociationForeignDBNames {
|
|
|
|
if field, ok := scope.FieldByName(relationship.AssociationForeignFieldNames[idx]); ok {
|
|
|
|
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
|
|
|
|
}
|
2015-12-25 12:32:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-25 15:43:51 +03:00
|
|
|
for _, foreignKey := range relationship.ForeignDBNames {
|
|
|
|
foreignKeyMap[foreignKey] = nil
|
|
|
|
}
|
2015-12-25 12:32:28 +03:00
|
|
|
|
2015-12-25 15:43:51 +03:00
|
|
|
if len(addedPrimaryKeys) > 0 {
|
|
|
|
sql := fmt.Sprintf("%v NOT IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(addedPrimaryKeys))
|
|
|
|
query = query.Where(sql, toQueryValues(addedPrimaryKeys)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
modelValue := scope.Value
|
|
|
|
// if replacing with a new value, don't reset current foreign key
|
|
|
|
// if clearing foreign value, then reset the foreign key to null
|
|
|
|
if len(values) > 0 {
|
|
|
|
modelValue = reflect.New(scope.GetModelStruct().ModelType).Interface()
|
|
|
|
}
|
|
|
|
association.setErr(query.Model(modelValue).UpdateColumn(foreignKeyMap).Error)
|
|
|
|
} else {
|
|
|
|
for idx, foreignKey := range relationship.ForeignDBNames {
|
|
|
|
foreignKeyMap[foreignKey] = nil
|
|
|
|
if field, ok := scope.FieldByName(relationship.AssociationForeignFieldNames[idx]); ok {
|
|
|
|
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addedPrimaryKeys) > 0 {
|
|
|
|
sql := fmt.Sprintf("%v NOT IN (%v)", toQueryCondition(scope, relationship.AssociationForeignDBNames), toQueryMarks(addedPrimaryKeys))
|
|
|
|
query = query.Where(sql, toQueryValues(addedPrimaryKeys)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if relationship.Kind == "many_to_many" {
|
|
|
|
association.setErr(relationship.JoinTableHandler.Delete(relationship.JoinTableHandler, query, relationship))
|
|
|
|
} else if relationship.Kind == "has_one" || relationship.Kind == "has_many" {
|
|
|
|
fieldValue := reflect.New(association.Field.Field.Type()).Interface()
|
|
|
|
association.setErr(query.Model(fieldValue).UpdateColumn(foreignKeyMap).Error)
|
|
|
|
}
|
2015-12-25 12:32:28 +03:00
|
|
|
}
|
|
|
|
return association
|
|
|
|
}
|
|
|
|
|
2015-07-30 12:26:10 +03:00
|
|
|
func (association *Association) Delete(values ...interface{}) *Association {
|
2014-07-30 16:48:36 +04:00
|
|
|
scope := association.Scope
|
2015-12-25 12:09:20 +03:00
|
|
|
query := scope.NewDB()
|
2015-07-30 12:26:10 +03:00
|
|
|
relationship := association.Field.Relationship
|
2014-07-30 18:10:12 +04:00
|
|
|
|
2015-12-25 13:42:06 +03:00
|
|
|
if len(values) == 0 {
|
|
|
|
return association
|
|
|
|
}
|
|
|
|
|
2015-07-30 12:26:10 +03:00
|
|
|
// many to many
|
|
|
|
if relationship.Kind == "many_to_many" {
|
2015-12-25 12:09:20 +03:00
|
|
|
// current value's foreign keys
|
2015-07-30 12:26:10 +03:00
|
|
|
for idx, foreignKey := range relationship.ForeignDBNames {
|
|
|
|
if field, ok := scope.FieldByName(relationship.ForeignFieldNames[idx]); ok {
|
|
|
|
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
|
2014-07-30 16:48:36 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 18:10:12 +04:00
|
|
|
|
2015-12-25 12:09:20 +03:00
|
|
|
// deleting value's foreign keys
|
2015-07-30 17:18:56 +03:00
|
|
|
primaryKeys := association.getPrimaryKeys(relationship.AssociationForeignFieldNames, values...)
|
2015-07-30 12:26:10 +03:00
|
|
|
sql := fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.AssociationForeignDBNames), toQueryMarks(primaryKeys))
|
|
|
|
query = query.Where(sql, toQueryValues(primaryKeys)...)
|
2014-07-30 16:48:36 +04:00
|
|
|
|
2015-07-30 12:26:10 +03:00
|
|
|
if err := relationship.JoinTableHandler.Delete(relationship.JoinTableHandler, query, relationship); err == nil {
|
|
|
|
leftValues := reflect.Zero(association.Field.Field.Type())
|
|
|
|
for i := 0; i < association.Field.Field.Len(); i++ {
|
2015-07-30 17:18:56 +03:00
|
|
|
reflectValue := association.Field.Field.Index(i)
|
|
|
|
primaryKey := association.getPrimaryKeys(relationship.ForeignFieldNames, reflectValue.Interface())[0]
|
|
|
|
var included = false
|
|
|
|
for _, pk := range primaryKeys {
|
|
|
|
if equalAsString(primaryKey, pk) {
|
|
|
|
included = true
|
2015-07-30 12:26:10 +03:00
|
|
|
}
|
2015-02-24 13:48:48 +03:00
|
|
|
}
|
2015-07-30 17:18:56 +03:00
|
|
|
if !included {
|
|
|
|
leftValues = reflect.Append(leftValues, reflectValue)
|
|
|
|
}
|
2015-02-24 13:48:48 +03:00
|
|
|
}
|
2015-07-30 12:26:10 +03:00
|
|
|
association.Field.Set(leftValues)
|
2014-07-30 16:48:36 +04:00
|
|
|
}
|
2015-07-30 12:26:10 +03:00
|
|
|
} else {
|
2015-12-25 12:09:20 +03:00
|
|
|
association.Field.Set(reflect.Zero(association.Field.Field.Type()))
|
|
|
|
|
|
|
|
if relationship.Kind == "belongs_to" {
|
|
|
|
var foreignKeyMap = map[string]interface{}{}
|
|
|
|
for _, foreignKey := range relationship.ForeignDBNames {
|
|
|
|
foreignKeyMap[foreignKey] = nil
|
|
|
|
}
|
|
|
|
primaryKeys := association.getPrimaryKeys(relationship.AssociationForeignFieldNames, values...)
|
|
|
|
sql := fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(primaryKeys))
|
2015-12-25 13:42:06 +03:00
|
|
|
|
2015-12-25 15:43:51 +03:00
|
|
|
association.setErr(query.Model(scope.Value).Where(sql, toQueryValues(primaryKeys)...).UpdateColumn(foreignKeyMap).Error)
|
2015-12-25 12:09:20 +03:00
|
|
|
} else if relationship.Kind == "has_one" || relationship.Kind == "has_many" {
|
2015-12-25 13:42:06 +03:00
|
|
|
var foreignKeyMap = map[string]interface{}{}
|
|
|
|
for _, foreignKey := range relationship.ForeignDBNames {
|
|
|
|
foreignKeyMap[foreignKey] = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
primaryKeys := association.getPrimaryKeys(relationship.AssociationForeignFieldNames, scope.Value)
|
|
|
|
sql := fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(primaryKeys))
|
|
|
|
|
|
|
|
var primaryFieldNames, primaryFieldDBNames []string
|
|
|
|
for _, field := range scope.New(values[0]).Fields() {
|
|
|
|
if field.IsPrimaryKey {
|
|
|
|
primaryFieldNames = append(primaryFieldNames, field.Name)
|
|
|
|
primaryFieldDBNames = append(primaryFieldDBNames, field.DBName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
relationsPrimaryKeys := association.getPrimaryKeys(primaryFieldNames, values...)
|
|
|
|
sql += fmt.Sprintf(" AND %v IN (%v)", toQueryCondition(scope, primaryFieldDBNames), toQueryMarks(relationsPrimaryKeys))
|
|
|
|
|
|
|
|
query.Model(association.Field.Field.Interface()).Where(sql, append(toQueryValues(primaryKeys), toQueryValues(relationsPrimaryKeys)...)...).UpdateColumn(foreignKeyMap)
|
2015-12-25 12:09:20 +03:00
|
|
|
}
|
2014-07-30 16:48:36 +04:00
|
|
|
}
|
2014-07-30 12:22:26 +04:00
|
|
|
return association
|
2014-07-30 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
2015-12-25 12:32:28 +03:00
|
|
|
func (association *Association) Clear() *Association {
|
2015-12-25 14:33:57 +03:00
|
|
|
return association.Replace()
|
2014-07-30 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
2014-07-30 17:43:53 +04:00
|
|
|
func (association *Association) Count() int {
|
|
|
|
count := -1
|
2014-07-30 16:59:52 +04:00
|
|
|
relationship := association.Field.Relationship
|
2014-07-30 16:48:36 +04:00
|
|
|
scope := association.Scope
|
2015-02-17 15:19:47 +03:00
|
|
|
newScope := scope.New(association.Field.Field.Interface())
|
2014-07-30 10:30:21 +04:00
|
|
|
|
2014-07-31 07:08:26 +04:00
|
|
|
if relationship.Kind == "many_to_many" {
|
2015-06-19 06:32:11 +03:00
|
|
|
relationship.JoinTableHandler.JoinWith(relationship.JoinTableHandler, scope.NewDB(), association.Scope.Value).Table(newScope.TableName()).Count(&count)
|
2014-11-25 22:42:05 +03:00
|
|
|
} else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
|
2015-07-30 12:26:10 +03:00
|
|
|
query := scope.DB()
|
|
|
|
for idx, foreignKey := range relationship.ForeignDBNames {
|
|
|
|
if field, ok := scope.FieldByName(relationship.AssociationForeignDBNames[idx]); ok {
|
|
|
|
query = query.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), scope.Quote(foreignKey)),
|
|
|
|
field.Field.Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 07:30:09 +03:00
|
|
|
if relationship.PolymorphicType != "" {
|
2015-07-30 12:26:10 +03:00
|
|
|
query = query.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(relationship.PolymorphicDBName)), scope.TableName())
|
2014-11-26 08:23:43 +03:00
|
|
|
}
|
2015-07-30 17:59:25 +03:00
|
|
|
query.Table(newScope.TableName()).Count(&count)
|
2014-07-31 07:08:26 +04:00
|
|
|
} else if relationship.Kind == "belongs_to" {
|
2015-07-30 12:26:10 +03:00
|
|
|
query := scope.DB()
|
|
|
|
for idx, foreignKey := range relationship.ForeignDBNames {
|
|
|
|
if field, ok := scope.FieldByName(relationship.AssociationForeignDBNames[idx]); ok {
|
|
|
|
query = query.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), scope.Quote(foreignKey)),
|
|
|
|
field.Field.Interface())
|
|
|
|
}
|
2014-07-30 17:43:53 +04:00
|
|
|
}
|
2015-07-30 12:26:10 +03:00
|
|
|
query.Table(newScope.TableName()).Count(&count)
|
2014-07-30 16:48:36 +04:00
|
|
|
}
|
2014-07-30 17:43:53 +04:00
|
|
|
|
|
|
|
return count
|
2014-07-30 10:30:21 +04:00
|
|
|
}
|
2015-07-30 12:26:10 +03:00
|
|
|
|
2015-12-25 15:43:51 +03:00
|
|
|
func (association *Association) getPrimaryKeys(columns []string, values ...interface{}) (results [][]interface{}) {
|
2015-07-30 12:26:10 +03:00
|
|
|
scope := association.Scope
|
|
|
|
|
|
|
|
for _, value := range values {
|
|
|
|
reflectValue := reflect.Indirect(reflect.ValueOf(value))
|
|
|
|
if reflectValue.Kind() == reflect.Slice {
|
|
|
|
for i := 0; i < reflectValue.Len(); i++ {
|
2015-07-30 17:18:56 +03:00
|
|
|
primaryKeys := []interface{}{}
|
2015-07-30 12:26:10 +03:00
|
|
|
newScope := scope.New(reflectValue.Index(i).Interface())
|
|
|
|
for _, column := range columns {
|
|
|
|
if field, ok := newScope.FieldByName(column); ok {
|
|
|
|
primaryKeys = append(primaryKeys, field.Field.Interface())
|
|
|
|
} else {
|
|
|
|
primaryKeys = append(primaryKeys, "")
|
|
|
|
}
|
|
|
|
}
|
2015-07-30 17:18:56 +03:00
|
|
|
results = append(results, primaryKeys)
|
2015-07-30 12:26:10 +03:00
|
|
|
}
|
|
|
|
} else if reflectValue.Kind() == reflect.Struct {
|
|
|
|
newScope := scope.New(value)
|
2015-07-30 17:18:56 +03:00
|
|
|
var primaryKeys []interface{}
|
2015-07-30 12:26:10 +03:00
|
|
|
for _, column := range columns {
|
|
|
|
if field, ok := newScope.FieldByName(column); ok {
|
|
|
|
primaryKeys = append(primaryKeys, field.Field.Interface())
|
|
|
|
} else {
|
|
|
|
primaryKeys = append(primaryKeys, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:18:56 +03:00
|
|
|
results = append(results, primaryKeys)
|
|
|
|
}
|
2015-07-30 12:26:10 +03:00
|
|
|
}
|
2015-12-25 15:43:51 +03:00
|
|
|
|
|
|
|
return
|
2015-07-30 12:26:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func toQueryMarks(primaryValues [][]interface{}) string {
|
|
|
|
var results []string
|
|
|
|
|
|
|
|
for _, primaryValue := range primaryValues {
|
|
|
|
var marks []string
|
2015-09-30 07:10:22 +03:00
|
|
|
for _, _ = range primaryValue {
|
2015-07-30 12:26:10 +03:00
|
|
|
marks = append(marks, "?")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(marks) > 1 {
|
|
|
|
results = append(results, fmt.Sprintf("(%v)", strings.Join(marks, ",")))
|
|
|
|
} else {
|
|
|
|
results = append(results, strings.Join(marks, ""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(results, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func toQueryCondition(scope *Scope, columns []string) string {
|
|
|
|
var newColumns []string
|
|
|
|
for _, column := range columns {
|
|
|
|
newColumns = append(newColumns, scope.Quote(column))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(columns) > 1 {
|
|
|
|
return fmt.Sprintf("(%v)", strings.Join(newColumns, ","))
|
|
|
|
} else {
|
|
|
|
return strings.Join(columns, ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toQueryValues(primaryValues [][]interface{}) (values []interface{}) {
|
|
|
|
for _, primaryValue := range primaryValues {
|
|
|
|
for _, value := range primaryValue {
|
|
|
|
values = append(values, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|