gorm/association.go

267 lines
8.5 KiB
Go
Raw Normal View History

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
2014-07-30 16:48:36 +04:00
for _, value := range values {
2015-02-17 14:17:21 +03:00
reflectvalue := reflect.Indirect(reflect.ValueOf(value))
if reflectvalue.Kind() == reflect.Struct {
2014-09-02 15:03:01 +04:00
field.Set(reflect.Append(field.Field, reflectvalue))
2015-02-17 14:17:21 +03:00
} else if reflectvalue.Kind() == reflect.Slice {
2014-09-02 15:03:01 +04:00
field.Set(reflect.AppendSlice(field.Field, reflectvalue))
2014-07-30 12:22:26 +04:00
} else {
2015-02-13 06:26:02 +03:00
association.setErr(errors.New("invalid association type"))
2014-07-30 12:22:26 +04:00
}
}
scope.Search.Select(association.Column)
2014-07-30 16:48:36 +04:00
scope.callCallbacks(scope.db.parent.callback.updates)
2015-02-13 06:26:02 +03:00
return association.setErr(scope.db.Error)
2014-07-30 10:30:21 +04:00
}
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-07-30 12:26:10 +03:00
relationship := association.Field.Relationship
2014-07-30 18:10:12 +04:00
2015-07-30 12:26:10 +03:00
// many to many
if relationship.Kind == "many_to_many" {
query := scope.NewDB()
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-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 {
association.setErr(errors.New("delete only support many to many"))
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
}
2014-07-30 18:10:12 +04:00
func (association *Association) Replace(values ...interface{}) *Association {
relationship := association.Field.Relationship
scope := association.Scope
2014-07-31 07:08:26 +04:00
if relationship.Kind == "many_to_many" {
2014-09-02 15:03:01 +04:00
field := association.Field.Field
2014-07-30 18:10:12 +04:00
2015-07-30 17:18:56 +03:00
oldPrimaryKeys := association.getPrimaryKeys(relationship.AssociationForeignFieldNames, field.Interface())
2015-02-24 13:48:48 +03:00
association.Field.Set(reflect.Zero(association.Field.Field.Type()))
2014-07-30 18:10:12 +04:00
association.Append(values...)
2015-07-30 17:18:56 +03:00
newPrimaryKeys := association.getPrimaryKeys(relationship.AssociationForeignFieldNames, field.Interface())
2014-07-30 18:10:12 +04:00
2015-07-30 12:26:10 +03:00
var addedPrimaryKeys = [][]interface{}{}
2015-02-17 14:17:21 +03:00
for _, newKey := range newPrimaryKeys {
2014-07-30 18:10:12 +04:00
hasEqual := false
2015-02-17 14:17:21 +03:00
for _, oldKey := range oldPrimaryKeys {
2015-07-30 17:18:56 +03:00
if equalAsString(newKey, oldKey) {
2014-07-30 18:10:12 +04:00
hasEqual = true
break
}
}
if !hasEqual {
2015-02-17 14:17:21 +03:00
addedPrimaryKeys = append(addedPrimaryKeys, newKey)
2014-07-30 18:10:12 +04:00
}
}
2015-07-30 12:26:10 +03:00
2015-07-30 17:18:56 +03:00
for _, primaryKey := range association.getPrimaryKeys(relationship.AssociationForeignFieldNames, values...) {
2014-07-30 18:10:12 +04:00
addedPrimaryKeys = append(addedPrimaryKeys, primaryKey)
}
if len(addedPrimaryKeys) > 0 {
2015-07-30 12:26:10 +03:00
query := scope.NewDB()
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())
}
}
2015-07-30 17:18:56 +03:00
sql := fmt.Sprintf("%v NOT IN (%v)", toQueryCondition(scope, relationship.AssociationForeignDBNames), toQueryMarks(addedPrimaryKeys))
2015-07-30 12:26:10 +03:00
query = query.Where(sql, toQueryValues(addedPrimaryKeys)...)
2015-06-19 06:32:11 +03:00
association.setErr(relationship.JoinTableHandler.Delete(relationship.JoinTableHandler, query, relationship))
}
2014-07-30 18:10:12 +04:00
} else {
2015-02-13 06:26:02 +03:00
association.setErr(errors.New("replace only support many to many"))
2014-07-30 18:10:12 +04:00
}
2014-07-30 12:22:26 +04:00
return association
2014-07-30 10:30:21 +04:00
}
2014-07-30 18:15:31 +04:00
func (association *Association) Clear() *Association {
relationship := association.Field.Relationship
scope := association.Scope
2014-07-31 07:08:26 +04:00
if relationship.Kind == "many_to_many" {
2015-07-30 12:26:10 +03:00
query := scope.NewDB()
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())
}
}
2015-06-19 06:32:11 +03:00
if err := relationship.JoinTableHandler.Delete(relationship.JoinTableHandler, query, relationship); err == nil {
2015-02-24 13:48:48 +03:00
association.Field.Set(reflect.Zero(association.Field.Field.Type()))
} else {
association.setErr(err)
}
2014-07-30 18:15:31 +04:00
} else {
2015-02-13 06:26:02 +03:00
association.setErr(errors.New("clear only support many to many"))
2014-07-30 18:15:31 +04:00
}
2014-07-30 12:22:26 +04:00
return association
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)
} 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())
}
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
func (association *Association) getPrimaryKeys(columns []string, values ...interface{}) [][]interface{} {
results := [][]interface{}{}
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
}
return results
}
func toQueryMarks(primaryValues [][]interface{}) string {
var results []string
for _, primaryValue := range primaryValues {
var marks []string
for range primaryValue {
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
}