mirror of https://github.com/go-gorm/gorm.git
Generate Query Conds for Relationship
This commit is contained in:
parent
59365b776b
commit
922a8efc53
|
@ -3,6 +3,7 @@ package gorm
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm/clause"
|
||||||
"github.com/jinzhu/gorm/schema"
|
"github.com/jinzhu/gorm/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,10 +32,6 @@ func (db *DB) Association(column string) *Association {
|
||||||
|
|
||||||
func (association *Association) Find(out interface{}, conds ...interface{}) error {
|
func (association *Association) Find(out interface{}, conds ...interface{}) error {
|
||||||
if association.Error == nil {
|
if association.Error == nil {
|
||||||
for _, ref := range association.Relationship.References {
|
|
||||||
if ref.OwnPrimaryKey {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return association.Error
|
return association.Error
|
||||||
|
@ -53,9 +50,27 @@ func (association *Association) Delete(values ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (association *Association) Clear() error {
|
func (association *Association) Clear() error {
|
||||||
return association.Error
|
return association.Replace()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (association *Association) Count() int {
|
func (association *Association) Count() (count int) {
|
||||||
return 0
|
if association.Error == nil {
|
||||||
|
var (
|
||||||
|
tx = association.DB
|
||||||
|
conds = association.Relationship.ToQueryConditions(tx.Statement.ReflectValue)
|
||||||
|
)
|
||||||
|
|
||||||
|
if association.Relationship.JoinTable != nil {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm/clause"
|
||||||
"github.com/jinzhu/inflection"
|
"github.com/jinzhu/inflection"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -345,3 +346,47 @@ func (rel *Relationship) ParseConstraint() *Constraint {
|
||||||
|
|
||||||
return &constraint
|
return &constraint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rel *Relationship) ToQueryConditions(reflectValue reflect.Value) (conds []clause.Expression) {
|
||||||
|
foreignFields := []*Field{}
|
||||||
|
relForeignKeys := []string{}
|
||||||
|
|
||||||
|
if rel.JoinTable != nil {
|
||||||
|
for _, ref := range rel.References {
|
||||||
|
if ref.OwnPrimaryKey {
|
||||||
|
foreignFields = append(foreignFields, ref.PrimaryKey)
|
||||||
|
relForeignKeys = append(relForeignKeys, ref.PrimaryKey.DBName)
|
||||||
|
} else if ref.PrimaryValue != "" {
|
||||||
|
conds = append(conds, clause.Eq{
|
||||||
|
Column: clause.Column{Table: rel.JoinTable.Table, Name: ref.ForeignKey.DBName},
|
||||||
|
Value: ref.PrimaryValue,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
conds = append(conds, clause.Eq{
|
||||||
|
Column: clause.Column{Table: rel.JoinTable.Table, Name: ref.ForeignKey.DBName},
|
||||||
|
Value: clause.Column{Table: rel.FieldSchema.Table, Name: ref.PrimaryKey.DBName},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, ref := range rel.References {
|
||||||
|
if ref.OwnPrimaryKey {
|
||||||
|
relForeignKeys = append(relForeignKeys, ref.ForeignKey.DBName)
|
||||||
|
foreignFields = append(foreignFields, ref.PrimaryKey)
|
||||||
|
} else if ref.PrimaryValue != "" {
|
||||||
|
conds = append(conds, clause.Eq{
|
||||||
|
Column: clause.Column{Table: rel.FieldSchema.Table, Name: ref.ForeignKey.DBName},
|
||||||
|
Value: ref.PrimaryValue,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
relForeignKeys = append(relForeignKeys, ref.PrimaryKey.DBName)
|
||||||
|
foreignFields = append(foreignFields, ref.ForeignKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, foreignValues := GetIdentityFieldValuesMap(reflectValue, foreignFields)
|
||||||
|
column, values := ToQueryValues(relForeignKeys, foreignValues)
|
||||||
|
conds = append(conds, clause.IN{Column: column, Values: values})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm/clause"
|
||||||
"github.com/jinzhu/gorm/logger"
|
"github.com/jinzhu/gorm/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,6 +27,10 @@ type Schema struct {
|
||||||
FieldsByDBName map[string]*Field
|
FieldsByDBName map[string]*Field
|
||||||
FieldsWithDefaultDBValue map[string]*Field // fields with default value assigned by database
|
FieldsWithDefaultDBValue map[string]*Field // fields with default value assigned by database
|
||||||
Relationships Relationships
|
Relationships Relationships
|
||||||
|
CreateClauses []clause.Interface
|
||||||
|
QueryClauses []clause.Interface
|
||||||
|
UpdateClauses []clause.Interface
|
||||||
|
DeleteClauses []clause.Interface
|
||||||
BeforeCreate, AfterCreate bool
|
BeforeCreate, AfterCreate bool
|
||||||
BeforeUpdate, AfterUpdate bool
|
BeforeUpdate, AfterUpdate bool
|
||||||
BeforeDelete, AfterDelete bool
|
BeforeDelete, AfterDelete bool
|
||||||
|
|
Loading…
Reference in New Issue