2020-01-29 14:22:44 +03:00
|
|
|
package gorm
|
|
|
|
|
2020-05-14 15:54:50 +03:00
|
|
|
import (
|
2020-05-20 18:44:50 +03:00
|
|
|
"errors"
|
2020-05-14 15:54:50 +03:00
|
|
|
"fmt"
|
2020-05-19 21:03:43 +03:00
|
|
|
"reflect"
|
2020-06-01 17:31:50 +03:00
|
|
|
"strings"
|
2020-05-14 15:54:50 +03:00
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
"gorm.io/gorm/schema"
|
|
|
|
"gorm.io/gorm/utils"
|
2020-05-14 15:54:50 +03:00
|
|
|
)
|
|
|
|
|
2020-01-29 14:22:44 +03:00
|
|
|
// Association Mode contains some helper methods to handle relationship things easily.
|
|
|
|
type Association struct {
|
2020-05-14 15:54:50 +03:00
|
|
|
DB *DB
|
|
|
|
Relationship *schema.Relationship
|
|
|
|
Error error
|
2020-01-29 14:22:44 +03:00
|
|
|
}
|
2020-02-23 18:28:35 +03:00
|
|
|
|
|
|
|
func (db *DB) Association(column string) *Association {
|
2020-05-14 15:54:50 +03:00
|
|
|
association := &Association{DB: db}
|
2020-05-24 12:24:23 +03:00
|
|
|
table := db.Statement.Table
|
2020-05-14 15:54:50 +03:00
|
|
|
|
|
|
|
if err := db.Statement.Parse(db.Statement.Model); err == nil {
|
2020-05-24 12:24:23 +03:00
|
|
|
db.Statement.Table = table
|
2020-05-14 15:54:50 +03:00
|
|
|
association.Relationship = db.Statement.Schema.Relationships.Relations[column]
|
|
|
|
|
|
|
|
if association.Relationship == nil {
|
|
|
|
association.Error = fmt.Errorf("%w: %v", ErrUnsupportedRelation, column)
|
|
|
|
}
|
2020-05-23 06:57:28 +03:00
|
|
|
|
2020-07-10 17:53:03 +03:00
|
|
|
db.Statement.ReflectValue = reflect.ValueOf(db.Statement.Model)
|
|
|
|
for db.Statement.ReflectValue.Kind() == reflect.Ptr {
|
|
|
|
db.Statement.ReflectValue = db.Statement.ReflectValue.Elem()
|
|
|
|
}
|
2020-05-14 15:54:50 +03:00
|
|
|
} else {
|
|
|
|
association.Error = err
|
|
|
|
}
|
|
|
|
|
|
|
|
return association
|
|
|
|
}
|
|
|
|
|
|
|
|
func (association *Association) Find(out interface{}, conds ...interface{}) error {
|
|
|
|
if association.Error == nil {
|
2020-05-20 18:44:50 +03:00
|
|
|
var (
|
2020-05-23 06:57:28 +03:00
|
|
|
queryConds = association.Relationship.ToQueryConditions(association.DB.Statement.ReflectValue)
|
2020-05-26 04:48:12 +03:00
|
|
|
tx = association.DB.Model(out)
|
2020-05-20 18:44:50 +03:00
|
|
|
)
|
|
|
|
|
2020-06-01 17:31:50 +03:00
|
|
|
if association.Relationship.JoinTable != nil {
|
|
|
|
if !tx.Statement.Unscoped && len(association.Relationship.JoinTable.QueryClauses) > 0 {
|
|
|
|
joinStmt := Statement{DB: tx, Schema: association.Relationship.JoinTable, Table: association.Relationship.JoinTable.Table, Clauses: map[string]clause.Clause{}}
|
|
|
|
for _, queryClause := range association.Relationship.JoinTable.QueryClauses {
|
|
|
|
joinStmt.AddClause(queryClause)
|
|
|
|
}
|
|
|
|
joinStmt.Build("WHERE", "LIMIT")
|
|
|
|
tx.Clauses(clause.Expr{SQL: strings.Replace(joinStmt.SQL.String(), "WHERE ", "", 1), Vars: joinStmt.Vars})
|
|
|
|
}
|
|
|
|
|
2020-05-20 18:44:50 +03:00
|
|
|
tx.Clauses(clause.From{Joins: []clause.Join{{
|
|
|
|
Table: clause.Table{Name: association.Relationship.JoinTable.Table},
|
|
|
|
ON: clause.Where{Exprs: queryConds},
|
|
|
|
}}})
|
|
|
|
} else {
|
|
|
|
tx.Clauses(clause.Where{Exprs: queryConds})
|
|
|
|
}
|
|
|
|
|
|
|
|
association.Error = tx.Find(out, conds...).Error
|
2020-05-14 15:54:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return association.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (association *Association) Append(values ...interface{}) error {
|
2020-05-20 18:44:50 +03:00
|
|
|
if association.Error == nil {
|
|
|
|
switch association.Relationship.Type {
|
|
|
|
case schema.HasOne, schema.BelongsTo:
|
|
|
|
if len(values) > 0 {
|
|
|
|
association.Error = association.Replace(values...)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
association.saveAssociation(false, values...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:54:50 +03:00
|
|
|
return association.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (association *Association) Replace(values ...interface{}) error {
|
2020-05-20 18:44:50 +03:00
|
|
|
if association.Error == nil {
|
2020-05-26 04:48:12 +03:00
|
|
|
// save associations
|
2020-05-20 18:44:50 +03:00
|
|
|
association.saveAssociation(true, values...)
|
2020-05-26 04:48:12 +03:00
|
|
|
|
|
|
|
// set old associations's foreign key to null
|
2020-05-20 18:44:50 +03:00
|
|
|
reflectValue := association.DB.Statement.ReflectValue
|
|
|
|
rel := association.Relationship
|
|
|
|
switch rel.Type {
|
2020-05-24 12:24:23 +03:00
|
|
|
case schema.BelongsTo:
|
|
|
|
if len(values) == 0 {
|
|
|
|
updateMap := map[string]interface{}{}
|
2020-06-01 14:41:33 +03:00
|
|
|
switch reflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
for i := 0; i < reflectValue.Len(); i++ {
|
|
|
|
rel.Field.Set(reflectValue.Index(i), reflect.Zero(rel.Field.FieldType).Interface())
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
rel.Field.Set(reflectValue, reflect.Zero(rel.Field.FieldType).Interface())
|
|
|
|
}
|
2020-05-24 12:24:23 +03:00
|
|
|
|
|
|
|
for _, ref := range rel.References {
|
|
|
|
updateMap[ref.ForeignKey.DBName] = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
association.DB.UpdateColumns(updateMap)
|
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
case schema.HasOne, schema.HasMany:
|
|
|
|
var (
|
2020-05-26 04:48:12 +03:00
|
|
|
primaryFields []*schema.Field
|
|
|
|
foreignKeys []string
|
|
|
|
updateMap = map[string]interface{}{}
|
|
|
|
relValues = schema.GetRelationsValues(reflectValue, []*schema.Relationship{rel})
|
|
|
|
modelValue = reflect.New(rel.FieldSchema.ModelType).Interface()
|
|
|
|
tx = association.DB.Model(modelValue)
|
2020-05-20 18:44:50 +03:00
|
|
|
)
|
2020-05-24 17:52:16 +03:00
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
if _, rvs := schema.GetIdentityFieldValuesMap(relValues, rel.FieldSchema.PrimaryFields); len(rvs) > 0 {
|
2020-07-09 04:03:48 +03:00
|
|
|
if column, values := schema.ToQueryValues(rel.FieldSchema.Table, rel.FieldSchema.PrimaryFieldDBNames, rvs); len(values) > 0 {
|
2020-05-26 04:48:12 +03:00
|
|
|
tx.Not(clause.IN{Column: column, Values: values})
|
2020-05-24 17:52:16 +03:00
|
|
|
}
|
2020-05-24 12:24:23 +03:00
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
|
|
|
|
for _, ref := range rel.References {
|
|
|
|
if ref.OwnPrimaryKey {
|
|
|
|
primaryFields = append(primaryFields, ref.PrimaryKey)
|
|
|
|
foreignKeys = append(foreignKeys, ref.ForeignKey.DBName)
|
|
|
|
updateMap[ref.ForeignKey.DBName] = nil
|
2020-05-26 04:48:12 +03:00
|
|
|
} else if ref.PrimaryValue != "" {
|
|
|
|
tx.Where(clause.Eq{Column: ref.ForeignKey.DBName, Value: ref.PrimaryValue})
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-26 04:48:12 +03:00
|
|
|
|
|
|
|
if _, pvs := schema.GetIdentityFieldValuesMap(reflectValue, primaryFields); len(pvs) > 0 {
|
2020-07-09 04:03:48 +03:00
|
|
|
column, values := schema.ToQueryValues(rel.FieldSchema.Table, foreignKeys, pvs)
|
2020-05-26 04:48:12 +03:00
|
|
|
tx.Where(clause.IN{Column: column, Values: values}).UpdateColumns(updateMap)
|
2020-05-23 16:03:28 +03:00
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
case schema.Many2Many:
|
2020-05-26 04:48:12 +03:00
|
|
|
var (
|
|
|
|
primaryFields, relPrimaryFields []*schema.Field
|
|
|
|
joinPrimaryKeys, joinRelPrimaryKeys []string
|
|
|
|
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
|
|
|
|
tx = association.DB.Model(modelValue)
|
|
|
|
)
|
2020-05-20 18:44:50 +03:00
|
|
|
|
|
|
|
for _, ref := range rel.References {
|
2020-05-25 19:16:41 +03:00
|
|
|
if ref.PrimaryValue == "" {
|
|
|
|
if ref.OwnPrimaryKey {
|
|
|
|
primaryFields = append(primaryFields, ref.PrimaryKey)
|
|
|
|
joinPrimaryKeys = append(joinPrimaryKeys, ref.ForeignKey.DBName)
|
|
|
|
} else {
|
|
|
|
relPrimaryFields = append(relPrimaryFields, ref.PrimaryKey)
|
|
|
|
joinRelPrimaryKeys = append(joinRelPrimaryKeys, ref.ForeignKey.DBName)
|
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
} else {
|
2020-05-26 04:48:12 +03:00
|
|
|
tx.Clauses(clause.Eq{Column: ref.ForeignKey.DBName, Value: ref.PrimaryValue})
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
_, pvs := schema.GetIdentityFieldValuesMap(reflectValue, primaryFields)
|
2020-07-09 04:03:48 +03:00
|
|
|
if column, values := schema.ToQueryValues(rel.JoinTable.Table, joinPrimaryKeys, pvs); len(values) > 0 {
|
2020-05-26 04:48:12 +03:00
|
|
|
tx.Where(clause.IN{Column: column, Values: values})
|
2020-05-25 19:16:41 +03:00
|
|
|
} else {
|
|
|
|
return ErrorPrimaryKeyRequired
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
_, rvs := schema.GetIdentityFieldValuesMapFromValues(values, relPrimaryFields)
|
2020-07-09 04:03:48 +03:00
|
|
|
if relColumn, relValues := schema.ToQueryValues(rel.JoinTable.Table, joinRelPrimaryKeys, rvs); len(relValues) > 0 {
|
2020-05-26 04:48:12 +03:00
|
|
|
tx.Where(clause.Not(clause.IN{Column: relColumn, Values: relValues}))
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
tx.Delete(modelValue)
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 15:54:50 +03:00
|
|
|
return association.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (association *Association) Delete(values ...interface{}) error {
|
2020-05-19 21:03:43 +03:00
|
|
|
if association.Error == nil {
|
|
|
|
var (
|
2020-05-25 06:11:09 +03:00
|
|
|
reflectValue = association.DB.Statement.ReflectValue
|
|
|
|
rel = association.Relationship
|
|
|
|
primaryFields, foreignFields []*schema.Field
|
|
|
|
foreignKeys []string
|
|
|
|
updateAttrs = map[string]interface{}{}
|
2020-05-25 06:49:02 +03:00
|
|
|
conds []clause.Expression
|
2020-05-19 21:03:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, ref := range rel.References {
|
|
|
|
if ref.PrimaryValue == "" {
|
2020-05-25 06:11:09 +03:00
|
|
|
primaryFields = append(primaryFields, ref.PrimaryKey)
|
|
|
|
foreignFields = append(foreignFields, ref.ForeignKey)
|
|
|
|
foreignKeys = append(foreignKeys, ref.ForeignKey.DBName)
|
|
|
|
updateAttrs[ref.ForeignKey.DBName] = nil
|
2020-05-25 06:49:02 +03:00
|
|
|
} else {
|
|
|
|
conds = append(conds, clause.Eq{Column: ref.ForeignKey.DBName, Value: ref.PrimaryValue})
|
2020-05-19 21:03:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 18:44:50 +03:00
|
|
|
switch rel.Type {
|
2020-05-26 04:48:12 +03:00
|
|
|
case schema.BelongsTo:
|
|
|
|
tx := association.DB.Model(reflect.New(rel.Schema.ModelType).Interface())
|
|
|
|
|
|
|
|
_, pvs := schema.GetIdentityFieldValuesMap(reflectValue, rel.Schema.PrimaryFields)
|
2020-07-09 04:03:48 +03:00
|
|
|
pcolumn, pvalues := schema.ToQueryValues(rel.Schema.Table, rel.Schema.PrimaryFieldDBNames, pvs)
|
2020-05-26 04:48:12 +03:00
|
|
|
conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues})
|
2020-05-25 06:11:09 +03:00
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
_, rvs := schema.GetIdentityFieldValuesMapFromValues(values, primaryFields)
|
2020-07-09 04:03:48 +03:00
|
|
|
relColumn, relValues := schema.ToQueryValues(rel.Schema.Table, foreignKeys, rvs)
|
2020-05-25 06:49:02 +03:00
|
|
|
conds = append(conds, clause.IN{Column: relColumn, Values: relValues})
|
2020-05-25 06:11:09 +03:00
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
association.Error = tx.Clauses(conds...).UpdateColumns(updateAttrs).Error
|
|
|
|
case schema.HasOne, schema.HasMany:
|
|
|
|
tx := association.DB.Model(reflect.New(rel.FieldSchema.ModelType).Interface())
|
|
|
|
|
|
|
|
_, pvs := schema.GetIdentityFieldValuesMap(reflectValue, primaryFields)
|
2020-07-09 04:03:48 +03:00
|
|
|
pcolumn, pvalues := schema.ToQueryValues(rel.FieldSchema.Table, foreignKeys, pvs)
|
2020-05-26 04:48:12 +03:00
|
|
|
conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues})
|
2020-05-24 16:46:33 +03:00
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
_, rvs := schema.GetIdentityFieldValuesMapFromValues(values, rel.FieldSchema.PrimaryFields)
|
2020-07-09 04:03:48 +03:00
|
|
|
relColumn, relValues := schema.ToQueryValues(rel.FieldSchema.Table, rel.FieldSchema.PrimaryFieldDBNames, rvs)
|
2020-05-25 06:49:02 +03:00
|
|
|
conds = append(conds, clause.IN{Column: relColumn, Values: relValues})
|
2020-05-25 06:11:09 +03:00
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
association.Error = tx.Clauses(conds...).UpdateColumns(updateAttrs).Error
|
2020-05-19 21:03:43 +03:00
|
|
|
case schema.Many2Many:
|
2020-05-26 04:48:12 +03:00
|
|
|
var (
|
|
|
|
primaryFields, relPrimaryFields []*schema.Field
|
|
|
|
joinPrimaryKeys, joinRelPrimaryKeys []string
|
|
|
|
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
|
|
|
|
)
|
2020-05-25 19:16:41 +03:00
|
|
|
|
|
|
|
for _, ref := range rel.References {
|
|
|
|
if ref.PrimaryValue == "" {
|
|
|
|
if ref.OwnPrimaryKey {
|
|
|
|
primaryFields = append(primaryFields, ref.PrimaryKey)
|
|
|
|
joinPrimaryKeys = append(joinPrimaryKeys, ref.ForeignKey.DBName)
|
|
|
|
} else {
|
|
|
|
relPrimaryFields = append(relPrimaryFields, ref.PrimaryKey)
|
|
|
|
joinRelPrimaryKeys = append(joinRelPrimaryKeys, ref.ForeignKey.DBName)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
conds = append(conds, clause.Eq{Column: ref.ForeignKey.DBName, Value: ref.PrimaryValue})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
_, pvs := schema.GetIdentityFieldValuesMap(reflectValue, primaryFields)
|
2020-07-09 04:03:48 +03:00
|
|
|
pcolumn, pvalues := schema.ToQueryValues(rel.JoinTable.Table, joinPrimaryKeys, pvs)
|
2020-05-26 04:48:12 +03:00
|
|
|
conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues})
|
2020-05-25 19:16:41 +03:00
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
_, rvs := schema.GetIdentityFieldValuesMapFromValues(values, relPrimaryFields)
|
2020-07-09 04:03:48 +03:00
|
|
|
relColumn, relValues := schema.ToQueryValues(rel.JoinTable.Table, joinRelPrimaryKeys, rvs)
|
2020-05-25 19:16:41 +03:00
|
|
|
conds = append(conds, clause.IN{Column: relColumn, Values: relValues})
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
association.Error = association.DB.Where(clause.Where{Exprs: conds}).Model(nil).Delete(modelValue).Error
|
2020-05-19 21:03:43 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
if association.Error == nil {
|
|
|
|
relValuesMap, _ := schema.GetIdentityFieldValuesMapFromValues(values, rel.FieldSchema.PrimaryFields)
|
2020-05-25 06:11:09 +03:00
|
|
|
|
2020-05-19 21:03:43 +03:00
|
|
|
cleanUpDeletedRelations := func(data reflect.Value) {
|
|
|
|
if _, zero := rel.Field.ValueOf(data); !zero {
|
|
|
|
fieldValue := reflect.Indirect(rel.Field.ReflectValueOf(data))
|
2020-05-26 04:48:12 +03:00
|
|
|
primaryValues := make([]interface{}, len(rel.FieldSchema.PrimaryFields))
|
2020-05-19 21:03:43 +03:00
|
|
|
|
|
|
|
switch fieldValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
2020-05-26 04:48:12 +03:00
|
|
|
validFieldValues := reflect.Zero(rel.Field.IndirectFieldType)
|
2020-05-19 21:03:43 +03:00
|
|
|
for i := 0; i < fieldValue.Len(); i++ {
|
2020-05-25 06:11:09 +03:00
|
|
|
for idx, field := range rel.FieldSchema.PrimaryFields {
|
2020-05-26 04:48:12 +03:00
|
|
|
primaryValues[idx], _ = field.ValueOf(fieldValue.Index(i))
|
2020-05-19 21:03:43 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
if _, ok := relValuesMap[utils.ToStringKey(primaryValues...)]; !ok {
|
2020-05-19 21:03:43 +03:00
|
|
|
validFieldValues = reflect.Append(validFieldValues, fieldValue.Index(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 12:24:23 +03:00
|
|
|
rel.Field.Set(data, validFieldValues.Interface())
|
2020-05-19 21:03:43 +03:00
|
|
|
case reflect.Struct:
|
2020-05-25 06:11:09 +03:00
|
|
|
for idx, field := range rel.FieldSchema.PrimaryFields {
|
2020-05-26 04:48:12 +03:00
|
|
|
primaryValues[idx], _ = field.ValueOf(fieldValue)
|
2020-05-19 21:03:43 +03:00
|
|
|
}
|
2020-05-26 04:48:12 +03:00
|
|
|
|
|
|
|
if _, ok := relValuesMap[utils.ToStringKey(primaryValues...)]; ok {
|
2020-05-24 12:24:23 +03:00
|
|
|
rel.Field.Set(data, reflect.Zero(rel.FieldSchema.ModelType).Interface())
|
2020-05-26 04:48:12 +03:00
|
|
|
|
|
|
|
if rel.JoinTable == nil {
|
|
|
|
for _, ref := range rel.References {
|
|
|
|
if ref.OwnPrimaryKey || ref.PrimaryValue != "" {
|
|
|
|
ref.ForeignKey.Set(fieldValue, reflect.Zero(ref.ForeignKey.FieldType).Interface())
|
|
|
|
} else {
|
|
|
|
ref.ForeignKey.Set(data, reflect.Zero(ref.ForeignKey.FieldType).Interface())
|
|
|
|
}
|
2020-05-25 06:11:09 +03:00
|
|
|
}
|
2020-05-24 12:24:23 +03:00
|
|
|
}
|
2020-05-19 21:03:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch reflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
for i := 0; i < reflectValue.Len(); i++ {
|
|
|
|
cleanUpDeletedRelations(reflect.Indirect(reflectValue.Index(i)))
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
cleanUpDeletedRelations(reflectValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 04:48:12 +03:00
|
|
|
|
2020-05-14 15:54:50 +03:00
|
|
|
return association.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (association *Association) Clear() error {
|
2020-05-19 16:50:06 +03:00
|
|
|
return association.Replace()
|
2020-05-14 15:54:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 06:32:59 +03:00
|
|
|
func (association *Association) Count() (count int64) {
|
2020-05-19 16:50:06 +03:00
|
|
|
if association.Error == nil {
|
|
|
|
var (
|
2020-05-24 06:32:59 +03:00
|
|
|
conds = association.Relationship.ToQueryConditions(association.DB.Statement.ReflectValue)
|
|
|
|
modelValue = reflect.New(association.Relationship.FieldSchema.ModelType).Interface()
|
|
|
|
tx = association.DB.Model(modelValue)
|
2020-05-19 16:50:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if association.Relationship.JoinTable != nil {
|
2020-06-01 17:31:50 +03:00
|
|
|
if !tx.Statement.Unscoped && len(association.Relationship.JoinTable.QueryClauses) > 0 {
|
|
|
|
joinStmt := Statement{DB: tx, Schema: association.Relationship.JoinTable, Table: association.Relationship.JoinTable.Table, Clauses: map[string]clause.Clause{}}
|
2020-05-29 02:35:45 +03:00
|
|
|
for _, queryClause := range association.Relationship.JoinTable.QueryClauses {
|
2020-06-01 17:31:50 +03:00
|
|
|
joinStmt.AddClause(queryClause)
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
2020-06-01 17:31:50 +03:00
|
|
|
joinStmt.Build("WHERE", "LIMIT")
|
|
|
|
tx.Clauses(clause.Expr{SQL: strings.Replace(joinStmt.SQL.String(), "WHERE ", "", 1), Vars: joinStmt.Vars})
|
2020-05-19 21:03:43 +03:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:50:06 +03:00
|
|
|
tx.Clauses(clause.From{Joins: []clause.Join{{
|
|
|
|
Table: clause.Table{Name: association.Relationship.JoinTable.Table},
|
|
|
|
ON: clause.Where{Exprs: conds},
|
|
|
|
}}})
|
|
|
|
} else {
|
|
|
|
tx.Clauses(clause.Where{Exprs: conds})
|
|
|
|
}
|
|
|
|
|
|
|
|
association.Error = tx.Count(&count).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2020-02-23 18:28:35 +03:00
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
|
2020-05-25 20:21:15 +03:00
|
|
|
type assignBack struct {
|
|
|
|
Source reflect.Value
|
|
|
|
Index int
|
|
|
|
Dest reflect.Value
|
|
|
|
}
|
|
|
|
|
2020-05-20 18:44:50 +03:00
|
|
|
func (association *Association) saveAssociation(clear bool, values ...interface{}) {
|
2020-05-24 12:24:23 +03:00
|
|
|
var (
|
|
|
|
reflectValue = association.DB.Statement.ReflectValue
|
2020-05-26 04:48:12 +03:00
|
|
|
assignBacks []assignBack // assign association values back to arguments after save
|
2020-05-24 12:24:23 +03:00
|
|
|
)
|
2020-05-20 18:44:50 +03:00
|
|
|
|
|
|
|
appendToRelations := func(source, rv reflect.Value, clear bool) {
|
|
|
|
switch association.Relationship.Type {
|
|
|
|
case schema.HasOne, schema.BelongsTo:
|
|
|
|
switch rv.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
if rv.Len() > 0 {
|
2020-05-24 12:24:23 +03:00
|
|
|
association.Error = association.Relationship.Field.Set(source, rv.Index(0).Addr().Interface())
|
2020-05-26 04:48:12 +03:00
|
|
|
|
2020-05-25 20:21:15 +03:00
|
|
|
if association.Relationship.Field.FieldType.Kind() == reflect.Struct {
|
|
|
|
assignBacks = append(assignBacks, assignBack{Source: source, Dest: rv.Index(0)})
|
2020-05-24 12:24:23 +03:00
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
case reflect.Struct:
|
2020-05-24 12:24:23 +03:00
|
|
|
association.Error = association.Relationship.Field.Set(source, rv.Addr().Interface())
|
2020-05-26 04:48:12 +03:00
|
|
|
|
2020-05-25 20:21:15 +03:00
|
|
|
if association.Relationship.Field.FieldType.Kind() == reflect.Struct {
|
|
|
|
assignBacks = append(assignBacks, assignBack{Source: source, Dest: rv})
|
2020-05-24 12:24:23 +03:00
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
case schema.HasMany, schema.Many2Many:
|
|
|
|
elemType := association.Relationship.Field.IndirectFieldType.Elem()
|
2020-05-25 06:11:09 +03:00
|
|
|
fieldValue := reflect.Indirect(association.Relationship.Field.ReflectValueOf(source))
|
2020-05-20 18:44:50 +03:00
|
|
|
if clear {
|
2020-05-25 06:11:09 +03:00
|
|
|
fieldValue = reflect.New(association.Relationship.Field.IndirectFieldType).Elem()
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
appendToFieldValues := func(ev reflect.Value) {
|
|
|
|
if ev.Type().AssignableTo(elemType) {
|
|
|
|
fieldValue = reflect.Append(fieldValue, ev)
|
|
|
|
} else if ev.Type().Elem().AssignableTo(elemType) {
|
|
|
|
fieldValue = reflect.Append(fieldValue, ev.Elem())
|
|
|
|
} else {
|
|
|
|
association.Error = fmt.Errorf("unsupported data type: %v for relation %v", ev.Type(), association.Relationship.Name)
|
|
|
|
}
|
2020-05-25 20:21:15 +03:00
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
if elemType.Kind() == reflect.Struct {
|
|
|
|
assignBacks = append(assignBacks, assignBack{Source: source, Dest: ev, Index: fieldValue.Len()})
|
2020-05-25 20:21:15 +03:00
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch rv.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
for i := 0; i < rv.Len(); i++ {
|
2020-05-25 06:11:09 +03:00
|
|
|
appendToFieldValues(reflect.Indirect(rv.Index(i)).Addr())
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
case reflect.Struct:
|
2020-05-25 06:11:09 +03:00
|
|
|
appendToFieldValues(rv.Addr())
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if association.Error == nil {
|
2020-05-25 06:11:09 +03:00
|
|
|
association.Error = association.Relationship.Field.Set(source, fieldValue.Interface())
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
selectedSaveColumns := []string{association.Relationship.Name}
|
2020-05-20 18:44:50 +03:00
|
|
|
for _, ref := range association.Relationship.References {
|
|
|
|
if !ref.OwnPrimaryKey {
|
2020-05-26 04:48:12 +03:00
|
|
|
selectedSaveColumns = append(selectedSaveColumns, ref.ForeignKey.Name)
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch reflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
if len(values) != reflectValue.Len() {
|
|
|
|
if clear && len(values) == 0 {
|
|
|
|
for i := 0; i < reflectValue.Len(); i++ {
|
2020-05-24 12:24:23 +03:00
|
|
|
association.Relationship.Field.Set(reflectValue.Index(i), reflect.New(association.Relationship.Field.IndirectFieldType).Interface())
|
2020-05-26 04:48:12 +03:00
|
|
|
|
2020-05-25 20:57:22 +03:00
|
|
|
if association.Relationship.JoinTable == nil {
|
|
|
|
for _, ref := range association.Relationship.References {
|
|
|
|
if !ref.OwnPrimaryKey && ref.PrimaryValue == "" {
|
|
|
|
ref.ForeignKey.Set(reflectValue.Index(i), reflect.Zero(ref.ForeignKey.FieldType).Interface())
|
|
|
|
}
|
2020-05-24 15:44:37 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-05-26 04:48:12 +03:00
|
|
|
|
2020-05-20 18:44:50 +03:00
|
|
|
association.Error = errors.New("invalid association values, length doesn't match")
|
2020-05-24 16:46:33 +03:00
|
|
|
return
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < reflectValue.Len(); i++ {
|
|
|
|
appendToRelations(reflectValue.Index(i), reflect.Indirect(reflect.ValueOf(values[i])), clear)
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
// TODO support save slice data, sql with case?
|
|
|
|
association.Error = association.DB.Session(&Session{}).Select(selectedSaveColumns).Model(nil).Save(reflectValue.Index(i).Addr().Interface()).Error
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
if clear && len(values) == 0 {
|
2020-05-24 12:24:23 +03:00
|
|
|
association.Relationship.Field.Set(reflectValue, reflect.New(association.Relationship.Field.IndirectFieldType).Interface())
|
2020-05-26 04:48:12 +03:00
|
|
|
|
2020-05-25 20:57:22 +03:00
|
|
|
if association.Relationship.JoinTable == nil {
|
|
|
|
for _, ref := range association.Relationship.References {
|
|
|
|
if !ref.OwnPrimaryKey && ref.PrimaryValue == "" {
|
|
|
|
ref.ForeignKey.Set(reflectValue, reflect.Zero(ref.ForeignKey.FieldType).Interface())
|
|
|
|
}
|
2020-05-24 15:44:37 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx, value := range values {
|
2020-05-24 12:24:23 +03:00
|
|
|
rv := reflect.Indirect(reflect.ValueOf(value))
|
|
|
|
appendToRelations(reflectValue, rv, clear && idx == 0)
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 16:46:33 +03:00
|
|
|
if len(values) > 0 {
|
2020-05-26 04:48:12 +03:00
|
|
|
association.Error = association.DB.Session(&Session{}).Select(selectedSaveColumns).Model(nil).Save(reflectValue.Addr().Interface()).Error
|
2020-05-24 15:44:37 +03:00
|
|
|
}
|
2020-05-24 12:24:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, assignBack := range assignBacks {
|
2020-05-25 20:21:15 +03:00
|
|
|
fieldValue := reflect.Indirect(association.Relationship.Field.ReflectValueOf(assignBack.Source))
|
|
|
|
if assignBack.Index > 0 {
|
|
|
|
reflect.Indirect(assignBack.Dest).Set(fieldValue.Index(assignBack.Index - 1))
|
|
|
|
} else {
|
|
|
|
reflect.Indirect(assignBack.Dest).Set(fieldValue)
|
|
|
|
}
|
2020-05-20 18:44:50 +03:00
|
|
|
}
|
|
|
|
}
|