Proposal, Add Specific on for Joins queries
This commit is contained in:
		
							parent
							
								
									eaa63d15e7
								
							
						
					
					
						commit
						895c1178a0
					
				| @ -125,6 +125,7 @@ func BuildQuerySQL(db *gorm.DB) { | ||||
| 						}) | ||||
| 					} | ||||
| 
 | ||||
| 					if join.On != nil { | ||||
| 						exprs := make([]clause.Expression, len(relation.References)) | ||||
| 						for idx, ref := range relation.References { | ||||
| 							if ref.OwnPrimaryKey { | ||||
| @ -146,12 +147,18 @@ 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 { | ||||
| 						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}}, | ||||
| 						}) | ||||
| 					} | ||||
| 				} else { | ||||
| 					joins = append(joins, clause.Join{ | ||||
| 						Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds}, | ||||
|  | ||||
| @ -177,6 +177,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
	 Adrien Carreira
						Adrien Carreira