Proposal, Add Specific on for Joins queries
This commit is contained in:
		
							parent
							
								
									16579e00c6
								
							
						
					
					
						commit
						18d32cd7dd
					
				@ -125,33 +125,40 @@ func BuildQuerySQL(db *gorm.DB) {
 | 
			
		||||
						})
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					exprs := make([]clause.Expression, len(relation.References))
 | 
			
		||||
					for idx, ref := range relation.References {
 | 
			
		||||
						if ref.OwnPrimaryKey {
 | 
			
		||||
							exprs[idx] = clause.Eq{
 | 
			
		||||
								Column: clause.Column{Table: clause.CurrentTable, Name: ref.PrimaryKey.DBName},
 | 
			
		||||
								Value:  clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
 | 
			
		||||
							}
 | 
			
		||||
						} else {
 | 
			
		||||
							if ref.PrimaryValue == "" {
 | 
			
		||||
					if join.On != nil {
 | 
			
		||||
						exprs := make([]clause.Expression, len(relation.References))
 | 
			
		||||
						for idx, ref := range relation.References {
 | 
			
		||||
							if ref.OwnPrimaryKey {
 | 
			
		||||
								exprs[idx] = clause.Eq{
 | 
			
		||||
									Column: clause.Column{Table: clause.CurrentTable, Name: ref.ForeignKey.DBName},
 | 
			
		||||
									Value:  clause.Column{Table: tableAliasName, Name: ref.PrimaryKey.DBName},
 | 
			
		||||
									Column: clause.Column{Table: clause.CurrentTable, Name: ref.PrimaryKey.DBName},
 | 
			
		||||
									Value:  clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
 | 
			
		||||
								}
 | 
			
		||||
							} else {
 | 
			
		||||
								exprs[idx] = clause.Eq{
 | 
			
		||||
									Column: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
 | 
			
		||||
									Value:  ref.PrimaryValue,
 | 
			
		||||
								if ref.PrimaryValue == "" {
 | 
			
		||||
									exprs[idx] = clause.Eq{
 | 
			
		||||
										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 {
 | 
			
		||||
					joins = append(joins, clause.Join{
 | 
			
		||||
						Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds},
 | 
			
		||||
 | 
			
		||||
@ -178,6 +178,12 @@ func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
 | 
			
		||||
	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
 | 
			
		||||
func (db *DB) Group(name string) (tx *DB) {
 | 
			
		||||
	tx = db.getInstance()
 | 
			
		||||
 | 
			
		||||
@ -50,6 +50,7 @@ type Statement struct {
 | 
			
		||||
type join struct {
 | 
			
		||||
	Name  string
 | 
			
		||||
	Conds []interface{}
 | 
			
		||||
	On    clause.Expression
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// StatementModifier statement modifier interface
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user