added arbitrary join conditions as a tag option for many2many relationships
This commit is contained in:
parent
9f1a7f5351
commit
6ecce561e6
@ -42,6 +42,7 @@ type JoinTableHandler struct {
|
|||||||
TableName string `sql:"-"`
|
TableName string `sql:"-"`
|
||||||
Source JoinTableSource `sql:"-"`
|
Source JoinTableSource `sql:"-"`
|
||||||
Destination JoinTableSource `sql:"-"`
|
Destination JoinTableSource `sql:"-"`
|
||||||
|
ArbitraryJoinConditions []string `sql:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SourceForeignKeys return source foreign keys
|
// SourceForeignKeys return source foreign keys
|
||||||
@ -75,6 +76,8 @@ func (s *JoinTableHandler) Setup(relationship *Relationship, tableName string, s
|
|||||||
AssociationDBName: dbName,
|
AssociationDBName: dbName,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.ArbitraryJoinConditions = relationship.ArbitraryJoinConditions
|
||||||
}
|
}
|
||||||
|
|
||||||
// Table return join table's table name
|
// Table return join table's table name
|
||||||
@ -174,6 +177,7 @@ func (s JoinTableHandler) JoinWith(handler JoinTableHandlerInterface, db *DB, so
|
|||||||
for _, foreignKey := range s.Destination.ForeignKeys {
|
for _, foreignKey := range s.Destination.ForeignKeys {
|
||||||
joinConditions = append(joinConditions, fmt.Sprintf("%v.%v = %v.%v", quotedTableName, scope.Quote(foreignKey.DBName), destinationTableName, scope.Quote(foreignKey.AssociationDBName)))
|
joinConditions = append(joinConditions, fmt.Sprintf("%v.%v = %v.%v", quotedTableName, scope.Quote(foreignKey.DBName), destinationTableName, scope.Quote(foreignKey.AssociationDBName)))
|
||||||
}
|
}
|
||||||
|
joinConditions = append(joinConditions, s.ArbitraryJoinConditions...)
|
||||||
|
|
||||||
var foreignDBNames []string
|
var foreignDBNames []string
|
||||||
var foreignFieldNames []string
|
var foreignFieldNames []string
|
||||||
|
@ -133,6 +133,7 @@ type Relationship struct {
|
|||||||
AssociationForeignFieldNames []string
|
AssociationForeignFieldNames []string
|
||||||
AssociationForeignDBNames []string
|
AssociationForeignDBNames []string
|
||||||
JoinTableHandler JoinTableHandlerInterface
|
JoinTableHandler JoinTableHandlerInterface
|
||||||
|
ArbitraryJoinConditions []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func getForeignField(column string, fields []*StructField) *StructField {
|
func getForeignField(column string, fields []*StructField) *StructField {
|
||||||
@ -339,6 +340,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for arbitrary join conditions supplied in the tag
|
||||||
|
|
||||||
|
if arbitraryConditions,_ := field.TagSettingsGet("ARBITRARY_JOIN_CONDITIONS"); arbitraryConditions != "" {
|
||||||
|
relationship.ArbitraryJoinConditions = strings.Split(arbitraryConditions, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
joinTableHandler := JoinTableHandler{}
|
joinTableHandler := JoinTableHandler{}
|
||||||
joinTableHandler.Setup(relationship, ToTableName(many2many), reflectType, elemType)
|
joinTableHandler.Setup(relationship, ToTableName(many2many), reflectType, elemType)
|
||||||
relationship.JoinTableHandler = &joinTableHandler
|
relationship.JoinTableHandler = &joinTableHandler
|
||||||
|
Loading…
x
Reference in New Issue
Block a user