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-07-12 08:49:42 +02:00
|
|
|
Column string
|
|
|
|
Nullable bool
|
2016-07-15 21:09:32 +02:00
|
|
|
Unique bool
|
2016-07-01 19:34:40 +02:00
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
ForeignTable string
|
|
|
|
ForeignColumn string
|
|
|
|
ForeignColumnNullable bool
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignColumnUnique bool
|
|
|
|
|
|
|
|
ToJoinTable bool
|
|
|
|
JoinTable string
|
|
|
|
|
|
|
|
JoinLocalColumn string
|
|
|
|
JoinLocalColumnNullable bool
|
|
|
|
JoinLocalColumnUnique bool
|
2016-07-12 08:49:42 +02:00
|
|
|
|
|
|
|
JoinForeignColumn string
|
|
|
|
JoinForeignColumnNullable bool
|
2016-07-15 21:09:32 +02:00
|
|
|
JoinForeignColumnUnique bool
|
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 {
|
2016-07-12 08:49:42 +02:00
|
|
|
localTable := GetTable(tables, table)
|
|
|
|
|
|
|
|
return toManyRelationships(localTable, tables)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toManyRelationships(table Table, tables []Table) []ToManyRelationship {
|
2016-06-23 07:03:05 +02:00
|
|
|
var relationships []ToManyRelationship
|
|
|
|
|
|
|
|
for _, t := range tables {
|
2016-07-12 08:49:42 +02:00
|
|
|
if t.Name == table.Name {
|
2016-06-23 07:03:05 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range t.FKeys {
|
2016-07-12 08:49:42 +02:00
|
|
|
if f.ForeignTable != table.Name {
|
2016-06-23 07:03:05 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
relationships = append(relationships, buildRelationship(table, f, t, tables))
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationships
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
func buildRelationship(localTable Table, foreignKey ForeignKey, foreignTable Table, tables []Table) ToManyRelationship {
|
2016-07-01 19:34:40 +02:00
|
|
|
if !foreignTable.IsJoinTable {
|
2016-07-12 08:49:42 +02:00
|
|
|
col := localTable.GetColumn(foreignKey.ForeignColumn)
|
2016-07-01 19:34:40 +02:00
|
|
|
return ToManyRelationship{
|
2016-07-12 08:49:42 +02:00
|
|
|
Column: foreignKey.ForeignColumn,
|
|
|
|
Nullable: col.Nullable,
|
2016-07-15 21:09:32 +02:00
|
|
|
Unique: col.Unique,
|
2016-07-12 08:49:42 +02:00
|
|
|
ForeignTable: foreignTable.Name,
|
|
|
|
ForeignColumn: foreignKey.Column,
|
|
|
|
ForeignColumnNullable: foreignKey.Nullable,
|
2016-07-15 21:09:32 +02:00
|
|
|
ForeignColumnUnique: foreignKey.Unique,
|
2016-07-12 08:49:42 +02:00
|
|
|
ToJoinTable: false,
|
2016-07-01 19:34:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
col := foreignTable.GetColumn(foreignKey.Column)
|
2016-07-01 19:34:40 +02:00
|
|
|
relationship := ToManyRelationship{
|
|
|
|
Column: foreignKey.ForeignColumn,
|
2016-07-12 08:49:42 +02:00
|
|
|
Nullable: col.Nullable,
|
2016-07-15 21:09:32 +02:00
|
|
|
Unique: col.Unique,
|
2016-07-01 19:34:40 +02:00
|
|
|
ToJoinTable: true,
|
|
|
|
JoinTable: foreignTable.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fk := range foreignTable.FKeys {
|
2016-07-12 08:49:42 +02:00
|
|
|
if fk.ForeignTable != localTable.Name {
|
2016-07-01 19:34:40 +02:00
|
|
|
relationship.JoinForeignColumn = fk.Column
|
2016-07-12 08:49:42 +02:00
|
|
|
relationship.JoinForeignColumnNullable = fk.Nullable
|
2016-07-15 21:09:32 +02:00
|
|
|
relationship.JoinForeignColumnUnique = fk.Unique
|
2016-07-12 08:49:42 +02:00
|
|
|
|
|
|
|
foreignTable := GetTable(tables, fk.ForeignTable)
|
|
|
|
foreignCol := foreignTable.GetColumn(fk.ForeignColumn)
|
2016-07-01 19:34:40 +02:00
|
|
|
relationship.ForeignTable = fk.ForeignTable
|
|
|
|
relationship.ForeignColumn = fk.ForeignColumn
|
2016-07-12 08:49:42 +02:00
|
|
|
relationship.ForeignColumnNullable = foreignCol.Nullable
|
2016-07-15 21:09:32 +02:00
|
|
|
relationship.ForeignColumnUnique = foreignCol.Unique
|
2016-07-01 19:34:40 +02:00
|
|
|
} else {
|
|
|
|
relationship.JoinLocalColumn = fk.Column
|
2016-07-12 08:49:42 +02:00
|
|
|
relationship.JoinLocalColumnNullable = fk.Nullable
|
2016-07-15 21:09:32 +02:00
|
|
|
relationship.JoinLocalColumnUnique = fk.Unique
|
2016-07-01 19:34:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationship
|
|
|
|
}
|