1
1
package bdb
2
2
3
+ // ToOneRelationship describes a relationship between two tables where the local
4
+ // table has no id, and the foregin table has an id that matches a column in the
5
+ // local table, that column is also unique which changes the dynamic into a
6
+ // one-to-one style, not a to-many.
7
+ type ToOneRelationship struct {
8
+ Table string
9
+ Column string
10
+ Nullable bool
11
+ Unique bool
12
+
13
+ ForeignTable string
14
+ ForeignColumn string
15
+ ForeignColumnNullable bool
16
+ ForeignColumnUnique bool
17
+ }
18
+
3
19
// ToManyRelationship describes a relationship between two tables where the
4
20
// local table has no id, and the foreign table has an id that matches a column
5
21
// in the local table.
@@ -26,31 +42,64 @@ type ToManyRelationship struct {
26
42
JoinForeignColumnUnique bool
27
43
}
28
44
45
+ // ToOneRelationships relationship lookups
46
+ // Input should be the sql name of a table like: videos
47
+ func ToOneRelationships (table string , tables []Table ) []ToOneRelationship {
48
+ localTable := GetTable (tables , table )
49
+ return toOneRelationships (localTable , tables )
50
+ }
51
+
29
52
// ToManyRelationships relationship lookups
30
53
// Input should be the sql name of a table like: videos
31
54
func ToManyRelationships (table string , tables []Table ) []ToManyRelationship {
32
55
localTable := GetTable (tables , table )
33
-
34
56
return toManyRelationships (localTable , tables )
35
57
}
36
58
59
+ func toOneRelationships (table Table , tables []Table ) []ToOneRelationship {
60
+ var relationships []ToOneRelationship
61
+
62
+ for _ , t := range tables {
63
+ for _ , f := range t .FKeys {
64
+ if f .ForeignTable == table .Name && ! t .IsJoinTable && f .Unique {
65
+ relationships = append (relationships , buildToOneRelationship (table , f , t , tables ))
66
+ }
67
+
68
+ }
69
+ }
70
+
71
+ return relationships
72
+ }
73
+
37
74
func toManyRelationships (table Table , tables []Table ) []ToManyRelationship {
38
75
var relationships []ToManyRelationship
39
76
40
77
for _ , t := range tables {
41
78
for _ , f := range t .FKeys {
42
- if f .ForeignTable != table .Name {
43
- continue
79
+ if f .ForeignTable == table .Name && ! f . Unique {
80
+ relationships = append ( relationships , buildToManyRelationship ( table , f , t , tables ))
44
81
}
45
-
46
- relationships = append (relationships , buildRelationship (table , f , t , tables ))
47
82
}
48
83
}
49
84
50
85
return relationships
51
86
}
52
87
53
- func buildRelationship (localTable Table , foreignKey ForeignKey , foreignTable Table , tables []Table ) ToManyRelationship {
88
+ func buildToOneRelationship (localTable Table , foreignKey ForeignKey , foreignTable Table , tables []Table ) ToOneRelationship {
89
+ return ToOneRelationship {
90
+ Table : localTable .Name ,
91
+ Column : foreignKey .ForeignColumn ,
92
+ Nullable : foreignKey .ForeignColumnNullable ,
93
+ Unique : foreignKey .ForeignColumnUnique ,
94
+
95
+ ForeignTable : foreignTable .Name ,
96
+ ForeignColumn : foreignKey .Column ,
97
+ ForeignColumnNullable : foreignKey .Nullable ,
98
+ ForeignColumnUnique : foreignKey .Unique ,
99
+ }
100
+ }
101
+
102
+ func buildToManyRelationship (localTable Table , foreignKey ForeignKey , foreignTable Table , tables []Table ) ToManyRelationship {
54
103
if ! foreignTable .IsJoinTable {
55
104
col := localTable .GetColumn (foreignKey .ForeignColumn )
56
105
return ToManyRelationship {
0 commit comments