From c301aeb524234036192ceaca1a7bee18ce1de4fa Mon Sep 17 00:00:00 2001 From: Adrien Carreira Date: Sun, 18 Jul 2021 12:04:18 +0200 Subject: [PATCH] Refactor for readability --- callbacks/query.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/callbacks/query.go b/callbacks/query.go index 570a85d0..a4093c63 100644 --- a/callbacks/query.go +++ b/callbacks/query.go @@ -125,7 +125,19 @@ func BuildQuerySQL(db *gorm.DB) { }) } - if join.On == nil { + if join.On != nil { + primaryFields := make([]clause.Column, len(relation.FieldSchema.PrimaryFieldDBNames)) + for idx, ref := range relation.FieldSchema.PrimaryFieldDBNames { + primaryFields[idx] = clause.Column{Table: tableAliasName, Name: ref} + } + + exprs := db.Statement.BuildCondition("(?) = (?)", primaryFields, 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 { exprs := make([]clause.Expression, len(relation.References)) for idx, ref := range relation.References { if ref.OwnPrimaryKey { @@ -147,18 +159,7 @@ func BuildQuerySQL(db *gorm.DB) { } } } - joins = append(joins, clause.Join{ - Type: clause.LeftJoin, - Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName}, - ON: clause.Where{Exprs: exprs}, - }) - } else { - primaryFields := make([]clause.Column, len(relation.FieldSchema.PrimaryFieldDBNames)) - for idx, ref := range relation.FieldSchema.PrimaryFieldDBNames { - primaryFields[idx] = clause.Column{Table: tableAliasName, Name: ref} - } - exprs := db.Statement.BuildCondition("(?) = (?)", primaryFields, join.On) joins = append(joins, clause.Join{ Type: clause.LeftJoin, Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName},