gorm/association.go

130 lines
4.2 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 {
2014-07-30 16:48:36 +04:00
Scope *Scope
PrimaryKey interface{}
Column string
Error error
Field *Field
2014-07-30 10:30:21 +04:00
}
2014-07-30 16:48:36 +04:00
func (association *Association) err(err error) *Association {
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)
return association.err(association.Scope.db.Error)
}
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
field := scope.IndirectValue().FieldByName(association.Column)
for _, value := range values {
reflectvalue := reflect.ValueOf(value)
if reflectvalue.Kind() == reflect.Ptr {
if reflectvalue.Elem().Kind() == reflect.Struct {
if field.Type().Elem().Kind() == reflect.Ptr {
field.Set(reflect.Append(field, reflectvalue))
} else if field.Type().Elem().Kind() == reflect.Struct {
field.Set(reflect.Append(field, reflectvalue.Elem()))
}
} else if reflectvalue.Elem().Kind() == reflect.Slice {
if field.Type().Elem().Kind() == reflect.Ptr {
field.Set(reflect.AppendSlice(field, reflectvalue))
} else if field.Type().Elem().Kind() == reflect.Struct {
field.Set(reflect.AppendSlice(field, reflectvalue.Elem()))
}
2014-07-30 12:22:26 +04:00
}
2014-07-30 16:48:36 +04:00
} else if reflectvalue.Kind() == reflect.Struct && field.Type().Elem().Kind() == reflect.Struct {
field.Set(reflect.Append(field, reflectvalue))
} else if reflectvalue.Kind() == reflect.Slice && field.Type().Elem() == reflectvalue.Type().Elem() {
field.Set(reflect.AppendSlice(field, reflectvalue))
2014-07-30 12:22:26 +04:00
} else {
2014-07-30 16:48:36 +04:00
association.err(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)
return association.err(scope.db.Error)
2014-07-30 10:30:21 +04:00
}
2014-07-30 16:48:36 +04:00
func (association *Association) Delete(values ...interface{}) *Association {
primaryKeys := []interface{}{}
scope := association.Scope
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)
}
}
}
if len(primaryKeys) == 0 {
association.err(errors.New("no primary key found"))
} 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-30 16:59:52 +04:00
if relationship.joinTable != "" {
whereSql := fmt.Sprintf("%v.%v IN (?)", relationship.joinTable, scope.Quote(ToSnake(relationship.associationForeignKey)))
scope.db.Table(relationship.joinTable).Where(whereSql, primaryKeys).Delete("")
2014-07-30 16:48:36 +04:00
} else {
association.err(errors.New("only many to many support delete"))
}
}
2014-07-30 12:22:26 +04:00
return association
2014-07-30 10:30:21 +04:00
}
2014-07-30 16:48:36 +04:00
func (association *Association) Replace(values interface{}) *Association {
2014-07-30 12:22:26 +04:00
return association
2014-07-30 10:30:21 +04:00
}
2014-07-30 12:22:26 +04:00
func (association *Association) Clear(value interface{}) *Association {
return association
2014-07-30 10:30:21 +04:00
}
2014-07-30 16:48:36 +04:00
func (association *Association) Count() (count int) {
2014-07-30 16:59:52 +04:00
relationship := association.Field.Relationship
2014-07-30 16:48:36 +04:00
scope := association.Scope
field := scope.IndirectValue().FieldByName(association.Column)
fieldValue := field.Interface()
2014-07-30 10:30:21 +04:00
2014-07-30 16:48:36 +04:00
// many to many
2014-07-30 16:59:52 +04:00
if relationship.joinTable != "" {
2014-07-30 16:48:36 +04:00
newScope := scope.New(fieldValue)
whereSql := fmt.Sprintf("%v.%v IN (SELECT %v.%v FROM %v WHERE %v.%v = ?)",
newScope.QuotedTableName(),
scope.Quote(newScope.PrimaryKey()),
2014-07-30 16:59:52 +04:00
relationship.joinTable,
scope.Quote(relationship.associationForeignKey),
relationship.joinTable,
relationship.joinTable,
scope.Quote(relationship.foreignKey))
2014-07-30 16:48:36 +04:00
scope.db.Table(newScope.QuotedTableName()).Where(whereSql, scope.PrimaryKey()).Count(&count)
}
// association.Scope.related(value, association.Column)
return -1
2014-07-30 10:30:21 +04:00
}