2016-06-23 08:09:56 +02:00
|
|
|
package bdb
|
2016-06-23 07:03:05 +02:00
|
|
|
|
2016-09-19 01:10:20 +02:00
|
|
|
// ToOneRelationship describes a relationship between two tables where the local
|
|
|
|
// table has no id, and the foregin table has an id that matches a column in the
|
|
|
|
// local table, that column is also unique which changes the dynamic into a
|
|
|
|
// one-to-one style, not a to-many.
|
2016-09-18 08:11:50 +02:00
|
|
|
type ToOneRelationship struct {
|
|
|
|
Table string
|
|
|
|
Column string
|
|
|
|
Nullable bool
|
|
|
|
Unique bool
|
|
|
|
|
|
|
|
ForeignTable string
|
|
|
|
ForeignColumn string
|
|
|
|
ForeignColumnNullable bool
|
|
|
|
ForeignColumnUnique bool
|
|
|
|
}
|
|
|
|
|
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-08-24 08:20:41 +02:00
|
|
|
Table string
|
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
|
|
|
}
|
|
|
|
|
2016-09-18 08:11:50 +02:00
|
|
|
// ToOneRelationships relationship lookups
|
|
|
|
// Input should be the sql name of a table like: videos
|
|
|
|
func ToOneRelationships(table string, tables []Table) []ToOneRelationship {
|
|
|
|
localTable := GetTable(tables, table)
|
|
|
|
return toOneRelationships(localTable, tables)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-09-18 08:11:50 +02:00
|
|
|
func toOneRelationships(table Table, tables []Table) []ToOneRelationship {
|
|
|
|
var relationships []ToOneRelationship
|
|
|
|
|
|
|
|
for _, t := range tables {
|
|
|
|
for _, f := range t.FKeys {
|
2016-09-19 01:10:20 +02:00
|
|
|
if f.ForeignTable == table.Name && !t.IsJoinTable && f.Unique {
|
|
|
|
relationships = append(relationships, buildToOneRelationship(table, f, t, tables))
|
2016-09-18 08:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationships
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:49:42 +02:00
|
|
|
func toManyRelationships(table Table, tables []Table) []ToManyRelationship {
|
2016-06-23 07:03:05 +02:00
|
|
|
var relationships []ToManyRelationship
|
|
|
|
|
|
|
|
for _, t := range tables {
|
|
|
|
for _, f := range t.FKeys {
|
2016-09-22 04:48:24 +02:00
|
|
|
if f.ForeignTable == table.Name && (t.IsJoinTable || !f.Unique) {
|
2016-09-19 01:10:20 +02:00
|
|
|
relationships = append(relationships, buildToManyRelationship(table, f, t, tables))
|
2016-06-23 07:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationships
|
|
|
|
}
|
2016-07-01 19:34:40 +02:00
|
|
|
|
2016-09-18 08:11:50 +02:00
|
|
|
func buildToOneRelationship(localTable Table, foreignKey ForeignKey, foreignTable Table, tables []Table) ToOneRelationship {
|
|
|
|
return ToOneRelationship{
|
|
|
|
Table: localTable.Name,
|
|
|
|
Column: foreignKey.ForeignColumn,
|
|
|
|
Nullable: foreignKey.ForeignColumnNullable,
|
|
|
|
Unique: foreignKey.ForeignColumnUnique,
|
|
|
|
|
|
|
|
ForeignTable: foreignTable.Name,
|
|
|
|
ForeignColumn: foreignKey.Column,
|
|
|
|
ForeignColumnNullable: foreignKey.Nullable,
|
|
|
|
ForeignColumnUnique: foreignKey.Unique,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildToManyRelationship(localTable Table, foreignKey ForeignKey, foreignTable Table, tables []Table) ToManyRelationship {
|
2016-07-01 19:34:40 +02:00
|
|
|
if !foreignTable.IsJoinTable {
|
|
|
|
return ToManyRelationship{
|
2016-08-28 02:04:51 +02:00
|
|
|
Table: localTable.Name,
|
2016-07-12 08:49:42 +02:00
|
|
|
Column: foreignKey.ForeignColumn,
|
2016-09-22 04:48:24 +02:00
|
|
|
Nullable: foreignKey.ForeignColumnNullable,
|
|
|
|
Unique: foreignKey.ForeignColumnUnique,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
relationship := ToManyRelationship{
|
2016-09-22 04:48:24 +02:00
|
|
|
Table: localTable.Name,
|
|
|
|
Column: foreignKey.ForeignColumn,
|
|
|
|
Nullable: foreignKey.ForeignColumnNullable,
|
|
|
|
Unique: foreignKey.ForeignColumnUnique,
|
|
|
|
|
2016-07-01 19:34:40 +02:00
|
|
|
ToJoinTable: true,
|
|
|
|
JoinTable: foreignTable.Name,
|
2016-09-22 04:48:24 +02:00
|
|
|
|
|
|
|
JoinLocalColumn: foreignKey.Column,
|
|
|
|
JoinLocalColumnNullable: foreignKey.Nullable,
|
|
|
|
JoinLocalColumnUnique: foreignKey.Unique,
|
2016-07-01 19:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fk := range foreignTable.FKeys {
|
2016-09-22 04:48:24 +02:00
|
|
|
if fk.Name == foreignKey.Name {
|
|
|
|
continue
|
2016-07-01 19:34:40 +02:00
|
|
|
}
|
2016-09-22 04:48:24 +02:00
|
|
|
|
|
|
|
relationship.JoinForeignColumn = fk.Column
|
|
|
|
relationship.JoinForeignColumnNullable = fk.Nullable
|
|
|
|
relationship.JoinForeignColumnUnique = fk.Unique
|
|
|
|
|
|
|
|
relationship.ForeignTable = fk.ForeignTable
|
|
|
|
relationship.ForeignColumn = fk.ForeignColumn
|
|
|
|
relationship.ForeignColumnNullable = fk.ForeignColumnNullable
|
|
|
|
relationship.ForeignColumnUnique = fk.ForeignColumnUnique
|
2016-07-01 19:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return relationship
|
|
|
|
}
|