forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_helpers.go
288 lines (240 loc) · 8.92 KB
/
text_helpers.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package boilingcore
import (
"fmt"
"strings"
"github.com/volatiletech/sqlboiler/bdb"
"github.com/volatiletech/sqlboiler/strmangle"
)
// TxtToOne contains text that will be used by templates for a one-to-many or
// a one-to-one relationship.
type TxtToOne struct {
ForeignKey bdb.ForeignKey
LocalTable struct {
NameGo string
ColumnNameGo string
}
ForeignTable struct {
NameGo string
NamePluralGo string
ColumnNameGo string
ColumnName string
}
Function struct {
Name string
ForeignName string
UsesBytes bool
LocalAssignment string
ForeignAssignment string
}
}
func txtsFromFKey(tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) TxtToOne {
r := TxtToOne{}
r.ForeignKey = fkey
r.LocalTable.NameGo = strmangle.TitleCase(strmangle.Singular(table.Name))
r.LocalTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(fkey.Column))
r.ForeignTable.NameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignTable))
r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(fkey.ForeignTable))
r.ForeignTable.ColumnName = fkey.ForeignColumn
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignColumn))
r.Function.Name, r.Function.ForeignName = txtNameToOne(fkey)
if fkey.Nullable {
col := table.GetColumn(fkey.Column)
r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.Column), strings.TrimPrefix(col.Type, "null."))
} else {
r.Function.LocalAssignment = strmangle.TitleCase(fkey.Column)
}
foreignTable := bdb.GetTable(tables, fkey.ForeignTable)
foreignColumn := foreignTable.GetColumn(fkey.ForeignColumn)
if fkey.ForeignColumnNullable {
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.ForeignColumn), strings.TrimPrefix(foreignColumn.Type, "null."))
} else {
r.Function.ForeignAssignment = strmangle.TitleCase(fkey.ForeignColumn)
}
r.Function.UsesBytes = foreignColumn.Type == "[]byte"
return r
}
func txtsFromOneToOne(tables []bdb.Table, table bdb.Table, oneToOne bdb.ToOneRelationship) TxtToOne {
fkey := bdb.ForeignKey{
Table: oneToOne.Table,
Name: "none",
Column: oneToOne.Column,
Nullable: oneToOne.Nullable,
Unique: oneToOne.Unique,
ForeignTable: oneToOne.ForeignTable,
ForeignColumn: oneToOne.ForeignColumn,
ForeignColumnNullable: oneToOne.ForeignColumnNullable,
ForeignColumnUnique: oneToOne.ForeignColumnUnique,
}
rel := txtsFromFKey(tables, table, fkey)
col := table.GetColumn(oneToOne.Column)
// Reverse foreign key
rel.ForeignKey.Table, rel.ForeignKey.ForeignTable = rel.ForeignKey.ForeignTable, rel.ForeignKey.Table
rel.ForeignKey.Column, rel.ForeignKey.ForeignColumn = rel.ForeignKey.ForeignColumn, rel.ForeignKey.Column
rel.ForeignKey.Nullable, rel.ForeignKey.ForeignColumnNullable = rel.ForeignKey.ForeignColumnNullable, rel.ForeignKey.Nullable
rel.ForeignKey.Unique, rel.ForeignKey.ForeignColumnUnique = rel.ForeignKey.ForeignColumnUnique, rel.ForeignKey.Unique
rel.Function.UsesBytes = col.Type == "[]byte"
rel.Function.ForeignName, rel.Function.Name = txtNameToOne(bdb.ForeignKey{
Table: oneToOne.ForeignTable,
Column: oneToOne.ForeignColumn,
Unique: true,
ForeignTable: oneToOne.Table,
ForeignColumn: oneToOne.Column,
})
return rel
}
// TxtToMany contains text that will be used by many-to-one relationships.
type TxtToMany struct {
LocalTable struct {
NameGo string
ColumnNameGo string
}
ForeignTable struct {
NameGo string
NamePluralGo string
NameHumanReadable string
ColumnNameGo string
Slice string
}
Function struct {
Name string
ForeignName string
UsesBytes bool
LocalAssignment string
ForeignAssignment string
}
}
// txtsFromToMany creates a struct that does a lot of the text
// transformation in advance for a given relationship.
func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) TxtToMany {
r := TxtToMany{}
r.LocalTable.NameGo = strmangle.TitleCase(strmangle.Singular(table.Name))
r.LocalTable.ColumnNameGo = strmangle.TitleCase(rel.Column)
foreignNameSingular := strmangle.Singular(rel.ForeignTable)
r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(rel.ForeignTable))
r.ForeignTable.NameGo = strmangle.TitleCase(foreignNameSingular)
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(rel.ForeignColumn)
r.ForeignTable.Slice = fmt.Sprintf("%sSlice", strmangle.TitleCase(foreignNameSingular))
r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1)
r.Function.Name, r.Function.ForeignName = txtNameToMany(rel)
col := table.GetColumn(rel.Column)
if rel.Nullable {
r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.Column), strings.TrimPrefix(col.Type, "null."))
} else {
r.Function.LocalAssignment = strmangle.TitleCase(rel.Column)
}
if rel.ForeignColumnNullable {
foreignTable := bdb.GetTable(tables, rel.ForeignTable)
foreignColumn := foreignTable.GetColumn(rel.ForeignColumn)
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.ForeignColumn), strings.TrimPrefix(foreignColumn.Type, "null."))
} else {
r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn)
}
r.Function.UsesBytes = col.Type == "[]byte"
return r
}
// txtNameToOne creates the local and foreign function names for
// one-to-many and one-to-one relationships, where local == lhs (one).
//
// = many-to-one
// users - videos : user_id
// users - videos : producer_id
//
// fk == table = user.Videos | video.User
// fk != table = user.ProducerVideos | video.Producer
//
// = many-to-one
// industries - industries : parent_id
//
// fk == table = industry.Industries | industry.Industry
// fk != table = industry.ParentIndustries | industry.Parent
//
// = one-to-one
// users - videos : user_id
// users - videos : producer_id
//
// fk == table = user.Video | video.User
// fk != table = user.ProducerVideo | video.Producer
//
// = one-to-one
// industries - industries : parent_id
//
// fk == table = industry.Industry | industry.Industry
// fk != table = industry.ParentIndustry | industry.Industry
func txtNameToOne(fk bdb.ForeignKey) (localFn, foreignFn string) {
localFn = strmangle.Singular(trimSuffixes(fk.Column))
fkeyIsTableName := localFn != strmangle.Singular(fk.ForeignTable)
localFn = strmangle.TitleCase(localFn)
if fkeyIsTableName {
foreignFn = localFn
}
plurality := strmangle.Plural
if fk.Unique {
plurality = strmangle.Singular
}
foreignFn += strmangle.TitleCase(plurality(fk.Table))
return localFn, foreignFn
}
// txtNameToMany creates the local and foreign function names for
// many-to-one and many-to-many relationship, where local == lhs (many)
//
// cases:
// = many-to-many
// sponsors - constests
// sponsor_id contest_id
// fk == table = sponsor.Contests | contest.Sponsors
//
// = many-to-many
// sponsors - constests
// wiggle_id jiggle_id
// fk != table = sponsor.JiggleSponsors | contest.WiggleContests
//
// = many-to-many
// industries - industries
// industry_id mapped_industry_id
//
// fk == table = industry.Industries
// fk != table = industry.MappedIndustryIndustry
func txtNameToMany(toMany bdb.ToManyRelationship) (localFn, foreignFn string) {
if toMany.ToJoinTable {
localFkey := strmangle.Singular(trimSuffixes(toMany.JoinLocalColumn))
foreignFkey := strmangle.Singular(trimSuffixes(toMany.JoinForeignColumn))
if localFkey != strmangle.Singular(toMany.Table) {
foreignFn = strmangle.TitleCase(localFkey)
}
foreignFn += strmangle.TitleCase(strmangle.Plural(toMany.Table))
if foreignFkey != strmangle.Singular(toMany.ForeignTable) {
localFn = strmangle.TitleCase(foreignFkey)
}
localFn += strmangle.TitleCase(strmangle.Plural(toMany.ForeignTable))
return localFn, foreignFn
}
fkeyName := strmangle.Singular(trimSuffixes(toMany.ForeignColumn))
if fkeyName != strmangle.Singular(toMany.Table) {
localFn = strmangle.TitleCase(fkeyName)
}
localFn += strmangle.TitleCase(strmangle.Plural(toMany.ForeignTable))
foreignFn = strmangle.TitleCase(strmangle.Singular(fkeyName))
return localFn, foreignFn
}
// mkFunctionName checks to see if the foreign key name is the same as the local table name (minus _id suffix)
// Simple case: yes - we can name the function the same as the plural table name
// Not simple case: We have to name the function based off the foreign key and the foreign table name
func mkFunctionName(fkeyTableSingular, foreignTablePluralGo, fkeyColumn string, toJoinTable bool) string {
colName := trimSuffixes(fkeyColumn)
if toJoinTable || fkeyTableSingular == colName {
return foreignTablePluralGo
}
return strmangle.TitleCase(colName) + foreignTablePluralGo
}
var identifierSuffixes = []string{"_id", "_uuid", "_guid", "_oid"}
// trimSuffixes from the identifier
func trimSuffixes(str string) string {
ln := len(str)
for _, s := range identifierSuffixes {
str = strings.TrimSuffix(str, s)
if len(str) != ln {
break
}
}
return str
}