gorm/association.go

197 lines
7.0 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"
)
2014-07-30 10:30:21 +04:00
type Association struct {
Scope *Scope
PrimaryKey interface{}
PrimaryType interface{}
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
fieldType := field.Field.Type()
2014-07-30 16:48:36 +04:00
for _, value := range values {
reflectvalue := reflect.ValueOf(value)
if reflectvalue.Kind() == reflect.Ptr {
if reflectvalue.Elem().Kind() == reflect.Struct {
2014-09-02 15:03:01 +04:00
if fieldType.Elem().Kind() == reflect.Ptr {
field.Set(reflect.Append(field.Field, reflectvalue))
} else if fieldType.Elem().Kind() == reflect.Struct {
field.Set(reflect.Append(field.Field, reflectvalue.Elem()))
2014-07-30 16:48:36 +04:00
}
} else if reflectvalue.Elem().Kind() == reflect.Slice {
2014-09-02 15:03:01 +04:00
if fieldType.Elem().Kind() == reflect.Ptr {
field.Set(reflect.AppendSlice(field.Field, reflectvalue))
} else if fieldType.Elem().Kind() == reflect.Struct {
field.Set(reflect.AppendSlice(field.Field, reflectvalue.Elem()))
2014-07-30 16:48:36 +04:00
}
2014-07-30 12:22:26 +04:00
}
2014-09-02 15:03:01 +04:00
} else if reflectvalue.Kind() == reflect.Struct && fieldType.Elem().Kind() == reflect.Struct {
field.Set(reflect.Append(field.Field, reflectvalue))
} else if reflectvalue.Kind() == reflect.Slice && fieldType.Elem() == reflectvalue.Type().Elem() {
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
}
}
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
}
2014-07-30 18:10:12 +04:00
func (association *Association) getPrimaryKeys(values ...interface{}) []interface{} {
2014-07-30 16:48:36 +04:00
primaryKeys := []interface{}{}
scope := association.Scope
2014-07-30 18:10:12 +04:00
2014-07-30 16:48:36 +04:00
for _, value := range values {
reflectValue := reflect.ValueOf(value)
if reflectValue.Kind() == reflect.Ptr {
reflectValue = reflectValue.Elem()
}
if reflectValue.Kind() == reflect.Slice {
for i := 0; i < reflectValue.Len(); i++ {
newScope := scope.New(reflectValue.Index(i).Interface())
primaryKey := newScope.PrimaryKeyValue()
if !reflect.DeepEqual(reflect.ValueOf(primaryKey), reflect.Zero(reflect.ValueOf(primaryKey).Type())) {
primaryKeys = append(primaryKeys, primaryKey)
}
}
} else if reflectValue.Kind() == reflect.Struct {
newScope := scope.New(value)
primaryKey := newScope.PrimaryKeyValue()
if !reflect.DeepEqual(reflect.ValueOf(primaryKey), reflect.Zero(reflect.ValueOf(primaryKey).Type())) {
primaryKeys = append(primaryKeys, primaryKey)
}
}
}
2014-07-30 18:10:12 +04:00
return primaryKeys
}
func (association *Association) Delete(values ...interface{}) *Association {
primaryKeys := association.getPrimaryKeys(values...)
2014-07-30 16:48:36 +04:00
if len(primaryKeys) == 0 {
2015-02-13 06:26:02 +03:00
association.setErr(errors.New("no primary key found"))
2014-07-30 16:48:36 +04:00
} else {
2014-07-30 16:59:52 +04:00
relationship := association.Field.Relationship
2014-07-30 16:48:36 +04:00
// many to many
2014-07-31 07:08:26 +04:00
if relationship.Kind == "many_to_many" {
2015-01-16 05:15:53 +03:00
whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v IN (?)",
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignKey)),
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignKey)))
association.Scope.db.Model("").Table(relationship.JoinTable).
Where(whereSql, association.PrimaryKey, primaryKeys).Delete("")
2014-07-30 16:48:36 +04:00
} else {
2015-02-13 06:26:02 +03:00
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
oldPrimaryKeys := association.getPrimaryKeys(field.Interface())
association.Append(values...)
newPrimaryKeys := association.getPrimaryKeys(field.Interface())
var addedPrimaryKeys = []interface{}{}
for _, new := range newPrimaryKeys {
hasEqual := false
for _, old := range oldPrimaryKeys {
if reflect.DeepEqual(new, old) {
hasEqual = true
break
}
}
if !hasEqual {
addedPrimaryKeys = append(addedPrimaryKeys, new)
}
}
for _, primaryKey := range association.getPrimaryKeys(values...) {
addedPrimaryKeys = append(addedPrimaryKeys, primaryKey)
}
whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v NOT IN (?)",
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignKey)),
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignKey)))
scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey, addedPrimaryKeys).Delete("")
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" {
whereSql := fmt.Sprintf("%v.%v = ?", relationship.JoinTable, scope.Quote(ToSnake(relationship.ForeignKey)))
scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey).Delete("")
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
2014-09-02 15:03:01 +04:00
field := association.Field.Field
2014-07-30 16:48:36 +04:00
fieldValue := field.Interface()
2014-07-30 17:43:53 +04:00
newScope := scope.New(fieldValue)
2014-07-30 10:30:21 +04:00
2014-07-31 07:08:26 +04:00
if relationship.Kind == "many_to_many" {
2014-07-30 16:48:36 +04:00
whereSql := fmt.Sprintf("%v.%v IN (SELECT %v.%v FROM %v WHERE %v.%v = ?)",
newScope.QuotedTableName(),
scope.Quote(newScope.PrimaryKey()),
2014-07-31 07:08:26 +04:00
relationship.JoinTable,
scope.Quote(ToSnake(relationship.AssociationForeignKey)),
relationship.JoinTable,
relationship.JoinTable,
scope.Quote(ToSnake(relationship.ForeignKey)))
2014-07-30 18:10:12 +04:00
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey).Count(&count)
} else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
2014-07-31 07:08:26 +04:00
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignKey)))
countScope := scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey)
if relationship.ForeignType != "" {
countScope = countScope.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignType))), association.PrimaryType)
}
countScope.Count(&count)
2014-07-31 07:08:26 +04:00
} else if relationship.Kind == "belongs_to" {
if v, err := scope.FieldValueByName(association.Column); err == nil {
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignKey)))
2014-07-30 18:10:12 +04:00
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, v).Count(&count)
2014-07-30 17:43:53 +04:00
}
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
}