Proposal, Add Specific on for Joins queries
This commit is contained in:
parent
eaa63d15e7
commit
895c1178a0
@ -125,33 +125,40 @@ func BuildQuerySQL(db *gorm.DB) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
exprs := make([]clause.Expression, len(relation.References))
|
if join.On != nil {
|
||||||
for idx, ref := range relation.References {
|
exprs := make([]clause.Expression, len(relation.References))
|
||||||
if ref.OwnPrimaryKey {
|
for idx, ref := range relation.References {
|
||||||
exprs[idx] = clause.Eq{
|
if ref.OwnPrimaryKey {
|
||||||
Column: clause.Column{Table: clause.CurrentTable, Name: ref.PrimaryKey.DBName},
|
|
||||||
Value: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ref.PrimaryValue == "" {
|
|
||||||
exprs[idx] = clause.Eq{
|
exprs[idx] = clause.Eq{
|
||||||
Column: clause.Column{Table: clause.CurrentTable, Name: ref.ForeignKey.DBName},
|
Column: clause.Column{Table: clause.CurrentTable, Name: ref.PrimaryKey.DBName},
|
||||||
Value: clause.Column{Table: tableAliasName, Name: ref.PrimaryKey.DBName},
|
Value: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
exprs[idx] = clause.Eq{
|
if ref.PrimaryValue == "" {
|
||||||
Column: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
|
exprs[idx] = clause.Eq{
|
||||||
Value: ref.PrimaryValue,
|
Column: clause.Column{Table: clause.CurrentTable, Name: ref.ForeignKey.DBName},
|
||||||
|
Value: clause.Column{Table: tableAliasName, Name: ref.PrimaryKey.DBName},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
exprs[idx] = clause.Eq{
|
||||||
|
Column: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
|
||||||
|
Value: ref.PrimaryValue,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
joins = append(joins, clause.Join{
|
||||||
|
Type: clause.LeftJoin,
|
||||||
|
Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName},
|
||||||
|
ON: clause.Where{Exprs: exprs},
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
joins = append(joins, clause.Join{
|
||||||
|
Type: clause.LeftJoin,
|
||||||
|
Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName},
|
||||||
|
ON: clause.Where{Exprs: []clause.Expression{join.On}},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
joins = append(joins, clause.Join{
|
|
||||||
Type: clause.LeftJoin,
|
|
||||||
Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName},
|
|
||||||
ON: clause.Where{Exprs: exprs},
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
joins = append(joins, clause.Join{
|
joins = append(joins, clause.Join{
|
||||||
Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds},
|
Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds},
|
||||||
|
@ -177,6 +177,12 @@ func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) JoinsOn(query string, on clause.Expression, args ...interface{}) (tx *DB) {
|
||||||
|
tx = db.getInstance()
|
||||||
|
tx.Statement.Joins = append(tx.Statement.Joins, join{Name: query, Conds: args, On: on})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Group specify the group method on the find
|
// Group specify the group method on the find
|
||||||
func (db *DB) Group(name string) (tx *DB) {
|
func (db *DB) Group(name string) (tx *DB) {
|
||||||
tx = db.getInstance()
|
tx = db.getInstance()
|
||||||
|
@ -50,6 +50,7 @@ type Statement struct {
|
|||||||
type join struct {
|
type join struct {
|
||||||
Name string
|
Name string
|
||||||
Conds []interface{}
|
Conds []interface{}
|
||||||
|
On clause.Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
// StatementModifier statement modifier interface
|
// StatementModifier statement modifier interface
|
||||||
|
Loading…
x
Reference in New Issue
Block a user