2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
2016-06-23 07:03:05 +02:00
|
|
|
|
|
|
|
// ToManyRelationship describes a relationship between two tables where the
|
|
|
|
// local table has no id, and the foreign table has an id that matches a column
|
|
|
|
// in the local table.
|
|
|
|
type ToManyRelationship struct {
|
2016-06-26 05:16:38 +02:00
|
|
|
Column string
|
2016-06-23 07:03:05 +02:00
|
|
|
ForeignTable string
|
|
|
|
ForeignColumn string
|
2016-07-01 19:34:40 +02:00
|
|
|
|
|
|
|
ToJoinTable bool
|
|
|
|
JoinTable string
|
|
|
|
JoinLocalColumn string
|
|
|
|
JoinForeignColumn string
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToManyRelationships relationship lookups
|
|
|
|
// Input should be the sql name of a table like: videos
|
|
|
|
func ToManyRelationships(table string, tables []Table) []ToManyRelationship {
|
|
|
|
var relationships []ToManyRelationship
|
|
|
|
|
|
|
|
for _, t := range tables {
|
|
|
|
if t.Name == table {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range t.FKeys {
|
|
|
|
if f.ForeignTable != table {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-01 19:34:40 +02:00
|
|
|
relationships = append(relationships, buildRelationship(table, f, t))
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationships
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
|
|
|
|
func buildRelationship(localTable string, foreignKey ForeignKey, foreignTable Table) ToManyRelationship {
|
|
|
|
if !foreignTable.IsJoinTable {
|
|
|
|
return ToManyRelationship{
|
|
|
|
Column: foreignKey.ForeignColumn,
|
|
|
|
ForeignTable: foreignTable.Name,
|
|
|
|
ForeignColumn: foreignKey.Column,
|
|
|
|
ToJoinTable: foreignTable.IsJoinTable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
relationship := ToManyRelationship{
|
|
|
|
Column: foreignKey.ForeignColumn,
|
|
|
|
ToJoinTable: true,
|
|
|
|
JoinTable: foreignTable.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fk := range foreignTable.FKeys {
|
|
|
|
if fk.ForeignTable != localTable {
|
|
|
|
relationship.JoinForeignColumn = fk.Column
|
|
|
|
relationship.ForeignTable = fk.ForeignTable
|
|
|
|
relationship.ForeignColumn = fk.ForeignColumn
|
|
|
|
} else {
|
|
|
|
relationship.JoinLocalColumn = fk.Column
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationship
|
|
|
|
}
|