forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliases.go
208 lines (172 loc) · 5.78 KB
/
aliases.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package boilingcore
import (
"fmt"
"github.com/volatiletech/sqlboiler/drivers"
"github.com/volatiletech/sqlboiler/strmangle"
)
// Aliases defines aliases for the generation run
type Aliases struct {
Tables map[string]TableAlias `toml:"tables,omitempty" json:"tables,omitempty"`
}
// TableAlias defines the spellings for a table name in Go
type TableAlias struct {
UpPlural string `toml:"up_plural,omitempty" json:"up_plural,omitempty"`
UpSingular string `toml:"up_singular,omitempty" json:"up_singular,omitempty"`
DownPlural string `toml:"down_plural,omitempty" json:"down_plural,omitempty"`
DownSingular string `toml:"down_singular,omitempty" json:"down_singular,omitempty"`
Columns map[string]string `toml:"columns,omitempty" json:"columns,omitempty"`
Relationships map[string]RelationshipAlias `toml:"relationships,omitempty" json:"relationships,omitempty"`
}
// RelationshipAlias defines the naming for both sides of
// a foreign key.
type RelationshipAlias struct {
Local string `toml:"local,omitempty" json:"local,omitempty"`
Foreign string `toml:"foreign,omitempty" json:"foreign,omitempty"`
}
// FillAliases takes the table information from the driver
// and fills in aliases where the user has provided none.
//
// This leaves us with a complete list of Go names for all tables,
// columns, and relationships.
func FillAliases(a *Aliases, tables []drivers.Table) {
if a.Tables == nil {
a.Tables = make(map[string]TableAlias)
}
for _, t := range tables {
if t.IsJoinTable {
jt, ok := a.Tables[t.Name];
if !ok {
a.Tables[t.Name] = TableAlias{Relationships: make(map[string]RelationshipAlias)}
} else if jt.Relationships == nil {
jt.Relationships = make(map[string]RelationshipAlias)
}
continue
}
table := a.Tables[t.Name]
if len(table.UpPlural) == 0 {
table.UpPlural = strmangle.TitleCase(strmangle.Plural(t.Name))
}
if len(table.UpSingular) == 0 {
table.UpSingular = strmangle.TitleCase(strmangle.Singular(t.Name))
}
if len(table.DownPlural) == 0 {
table.DownPlural = strmangle.CamelCase(strmangle.Plural(t.Name))
}
if len(table.DownSingular) == 0 {
table.DownSingular = strmangle.CamelCase(strmangle.Singular(t.Name))
}
if table.Columns == nil {
table.Columns = make(map[string]string)
}
if table.Relationships == nil {
table.Relationships = make(map[string]RelationshipAlias)
}
for _, c := range t.Columns {
if _, ok := table.Columns[c.Name]; !ok {
table.Columns[c.Name] = strmangle.TitleCase(c.Name)
}
}
a.Tables[t.Name] = table
for _, k := range t.FKeys {
r := table.Relationships[k.Name]
if len(r.Local) != 0 && len(r.Foreign) != 0 {
continue
}
local, foreign := txtNameToOne(k)
if len(r.Local) == 0 {
r.Local = local
}
if len(r.Foreign) == 0 {
r.Foreign = foreign
}
table.Relationships[k.Name] = r
}
}
for _, t := range tables {
if !t.IsJoinTable {
continue
}
table := a.Tables[t.Name]
lhs := t.FKeys[0]
rhs := t.FKeys[1]
lhsAlias, lhsOK := table.Relationships[lhs.Name]
rhsAlias, rhsOK := table.Relationships[rhs.Name]
if lhsOK && len(lhsAlias.Local) != 0 && len(lhsAlias.Foreign) != 0 &&
rhsOK && len(rhsAlias.Local) != 0 && len(rhsAlias.Foreign) != 0 {
continue
}
// Here we actually reverse the meaning of local/foreign to be
// consistent with the way normal one-to-many relationships are done.
// That's to say local = the side with the foreign key. Now in a many-to-many
// if we were able to not have a join table our foreign key say "videos_id"
// would be on the tags table. Hence the relationships should look like:
// videos_tags.relationships.fk_video_id.local = "Tags"
// videos_tags.relationships.fk_video_id.foreign = "Videos"
// Consistent, yes. Confusing? Also yes.
lhsName, rhsName := txtNameToMany(lhs, rhs)
if len(lhsAlias.Local) != 0 {
rhsName = lhsAlias.Local
} else if len(rhsAlias.Local) != 0 {
lhsName = rhsAlias.Local
}
if len(lhsAlias.Foreign) != 0 {
lhsName = lhsAlias.Foreign
} else if len(rhsAlias.Foreign) != 0 {
rhsName = rhsAlias.Foreign
}
if len(lhsAlias.Local) == 0 {
lhsAlias.Local = rhsName
}
if len(lhsAlias.Foreign) == 0 {
lhsAlias.Foreign = lhsName
}
if len(rhsAlias.Local) == 0 {
rhsAlias.Local = lhsName
}
if len(rhsAlias.Foreign) == 0 {
rhsAlias.Foreign = rhsName
}
table.Relationships[lhs.Name] = lhsAlias
table.Relationships[rhs.Name] = rhsAlias
}
}
// Table gets a table alias, panics if not found.
func (a Aliases) Table(table string) TableAlias {
t, ok := a.Tables[table]
if !ok {
panic("could not find table aliases for: " + table)
}
return t
}
// Column get's a column's aliased name, panics if not found.
func (t TableAlias) Column(column string) string {
c, ok := t.Columns[column]
if !ok {
panic(fmt.Sprintf("could not find column alias for: %s.%s", t.UpSingular, column))
}
return c
}
// Relationship looks up a relationship, panics if not found.
func (t TableAlias) Relationship(fkey string) RelationshipAlias {
r, ok := t.Relationships[fkey]
if !ok {
panic(fmt.Sprintf("could not find relationship alias for: %s.%s", t.UpSingular, fkey))
}
return r
}
// ManyRelationship looks up a relationship alias, panics if not found.
// It will first try to look up a join table relationship, then it will
// try a normal one-to-many relationship. That's to say joinTable/joinTableFKey
// are used if they're not empty.
//
// This allows us to skip additional conditionals in the templates.
func (a Aliases) ManyRelationship(table, fkey, joinTable, joinTableFKey string) RelationshipAlias {
var lookupTable, lookupFKey string
if len(joinTable) != 0 {
lookupTable, lookupFKey = joinTable, joinTableFKey
} else {
lookupTable, lookupFKey = table, fkey
}
t := a.Table(lookupTable)
return t.Relationship(lookupFKey)
}