-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathschema.xo.go.tpl
More file actions
435 lines (403 loc) · 11.9 KB
/
schema.xo.go.tpl
File metadata and controls
435 lines (403 loc) · 11.9 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
{{ define "enum" }}
{{- $e := .Data -}}
// {{ $e.GoName }} is the '{{ $e.SQLName }}' enum type from schema '{{ schema }}'.
type {{ $e.GoName }} uint16
// {{ $e.GoName }} values.
const (
{{ range $e.Values -}}
// {{ $e.GoName }}{{ .GoName }} is the '{{ .SQLName }}' {{ $e.SQLName }}.
{{ $e.GoName }}{{ .GoName }} {{ $e.GoName }} = {{ .ConstValue }}
{{ end -}}
)
// String satisfies the [fmt.Stringer] interface.
func ({{ short $e.GoName }} {{ $e.GoName }}) String() string {
switch {{ short $e.GoName }} {
{{ range $e.Values -}}
case {{ $e.GoName }}{{ .GoName }}:
return "{{ .SQLName }}"
{{ end -}}
}
return fmt.Sprintf("{{ $e.GoName }}(%d)", {{ short $e.GoName }})
}
// MarshalText marshals [{{ $e.GoName }}] into text.
func ({{ short $e.GoName }} {{ $e.GoName }}) MarshalText() ([]byte, error) {
return []byte({{ short $e.GoName }}.String()), nil
}
// UnmarshalText unmarshals [{{ $e.GoName }}] from text.
func ({{ short $e.GoName }} *{{ $e.GoName }}) UnmarshalText(buf []byte) error {
switch str := string(buf); str {
{{ range $e.Values -}}
case "{{ .SQLName }}":
*{{ short $e.GoName }} = {{ $e.GoName }}{{ .GoName }}
{{ end -}}
default:
return ErrInvalid{{ $e.GoName }}(str)
}
return nil
}
// Value satisfies the [driver.Valuer] interface.
func ({{ short $e.GoName }} {{ $e.GoName }}) Value() (driver.Value, error) {
return {{ short $e.GoName }}.String(), nil
}
// Scan satisfies the [sql.Scanner] interface.
func ({{ short $e.GoName }} *{{ $e.GoName }}) Scan(v interface{}) error {
switch x := v.(type) {
case []byte:
return {{ short $e.GoName }}.UnmarshalText(x)
case string:
return {{ short $e.GoName }}.UnmarshalText([]byte(x))
}
return ErrInvalid{{ $e.GoName }}(fmt.Sprintf("%T", v))
}
{{ $nullName := (printf "%s%s" "Null" $e.GoName) -}}
{{- $nullShort := (short $nullName) -}}
// {{ $nullName }} represents a null '{{ $e.SQLName }}' enum for schema '{{ schema }}'.
type {{ $nullName }} struct {
{{ $e.GoName }} {{ $e.GoName }}
// Valid is true if [{{ $e.GoName }}] is not null.
Valid bool
}
// Value satisfies the [driver.Valuer] interface.
func ({{ $nullShort }} {{ $nullName }}) Value() (driver.Value, error) {
if !{{ $nullShort }}.Valid {
return nil, nil
}
return {{ $nullShort }}.{{ $e.GoName }}.Value()
}
// Scan satisfies the [sql.Scanner] interface.
func ({{ $nullShort }} *{{ $nullName }}) Scan(v interface{}) error {
if v == nil {
{{ $nullShort }}.{{ $e.GoName }}, {{ $nullShort }}.Valid = 0, false
return nil
}
err := {{ $nullShort }}.{{ $e.GoName }}.Scan(v)
{{ $nullShort }}.Valid = err == nil
return err
}
// ErrInvalid{{ $e.GoName }} is the invalid [{{ $e.GoName }}] error.
type ErrInvalid{{ $e.GoName }} string
// Error satisfies the error interface.
func (err ErrInvalid{{ $e.GoName }}) Error() string {
return fmt.Sprintf("invalid {{ $e.GoName }}(%s)", string(err))
}
{{ end }}
{{ define "foreignkey" }}
{{- $k := .Data -}}
// {{ func_name_context $k }} returns the {{ $k.RefTable }} associated with the [{{ $k.Table.GoName }}]'s ({{ names "" $k.Fields }}).
//
// Generated from foreign key '{{ $k.SQLName }}'.
{{ recv_context $k.Table $k }} {
return {{ foreign_key_context $k }}
}
{{- if context_both }}
// {{ func_name $k }} returns the {{ $k.RefTable }} associated with the {{ $k.Table }}'s ({{ names "" $k.Fields }}).
//
// Generated from foreign key '{{ $k.SQLName }}'.
{{ recv $k.Table $k }} {
return {{ foreign_key $k }}
}
{{- end }}
{{ end }}
{{ define "index" }}
{{- $i := .Data -}}
// {{ func_name_context $i }} retrieves a row from '{{ schema $i.Table.SQLName }}' as a [{{ $i.Table.GoName }}].
//
// Generated from index '{{ $i.SQLName }}'.
{{ func_context $i }} {
// query
{{ sqlstr "index" $i }}
// run
logf(sqlstr, {{ params $i.Fields false }})
{{- if $i.IsUnique }}
{{ short $i.Table }} := {{ $i.Table.GoName }}{
{{- if $i.Table.PrimaryKeys }}
_exists: true,
{{ end -}}
}
if err := {{ db "QueryRow" $i }}.Scan({{ names (print "&" (short $i.Table) ".") $i.Table }}); err != nil {
return nil, logerror(err)
}
return &{{ short $i.Table }}, nil
{{- else }}
rows, err := {{ db "Query" $i }}
if err != nil {
return nil, logerror(err)
}
defer rows.Close()
// process
var res []*{{ $i.Table.GoName }}
for rows.Next() {
{{ short $i.Table }} := {{ $i.Table.GoName }}{
{{- if $i.Table.PrimaryKeys }}
_exists: true,
{{ end -}}
}
// scan
if err := rows.Scan({{ names_ignore (print "&" (short $i.Table) ".") $i.Table }}); err != nil {
return nil, logerror(err)
}
res = append(res, &{{ short $i.Table }})
}
if err := rows.Err(); err != nil {
return nil, logerror(err)
}
return res, nil
{{- end }}
}
{{ if context_both -}}
// {{ func_name $i }} retrieves a row from '{{ schema $i.Table.SQLName }}' as a [{{ $i.Table.GoName }}].
//
// Generated from index '{{ $i.SQLName }}'.
{{ func $i }} {
return {{ func_name_context $i }}({{ names "" "context.Background()" "db" $i }})
}
{{- end }}
{{end}}
{{ define "procs" }}
{{- $ps := .Data -}}
{{- range $p := $ps -}}
// {{ func_name_context $p }} calls the stored {{ $p.Type }} '{{ $p.Signature }}' on db.
{{ func_context $p }} {
{{- if and (driver "mysql") (eq $p.Type "procedure") (not $p.Void) }}
// At the moment, the Go MySQL driver does not support stored procedures
// with out parameters
return {{ zero $p.Returns }}, fmt.Errorf("unsupported")
{{- else }}
// call {{ schema $p.SQLName }}
{{ sqlstr "proc" $p }}
// run
{{- if not $p.Void }}
{{- range $p.Returns }}
var {{ check_name .GoName }} {{ type .Type }}
{{- end }}
logf(sqlstr, {{ params $p.Params false }})
{{- if and (driver "sqlserver" "oracle") (eq $p.Type "procedure")}}
if _, err := {{ db_named "Exec" $p }}; err != nil {
{{- else }}
if err := {{ db "QueryRow" $p }}.Scan({{ names "&" $p.Returns }}); err != nil {
{{- end }}
return {{ zero $p.Returns }}, logerror(err)
}
return {{ range $p.Returns }}{{ check_name .GoName }}, {{ end }}nil
{{- else }}
logf(sqlstr)
{{- if driver "sqlserver" "oracle" }}
if _, err := {{ db_named "Exec" $p }}; err != nil {
{{- else }}
if _, err := {{ db "Exec" $p }}; err != nil {
{{- end }}
return logerror(err)
}
return nil
{{- end }}
{{- end }}
}
{{ if context_both -}}
// {{ func_name $p }} calls the {{ $p.Type }} '{{ $p.Signature }}' on db.
{{ func $p }} {
return {{ func_name_context $p }}({{ names_all "" "context.Background()" "db" $p.Params }})
}
{{- end -}}
{{- end }}
{{ end }}
{{ define "typedef" }}
{{- $t := .Data -}}
{{- if $t.Comment -}}
/* {{ $t.Comment | eval $t.GoName }} */
{{- else -}}
// {{ $t.GoName }} represents a row from '{{ schema $t.SQLName }}'.
{{- end }}
type {{ $t.GoName }} struct {
{{ range $t.Fields -}}
{{ field . }}
{{ end }}
{{- if $t.PrimaryKeys -}}
// xo fields
_exists, _deleted bool
{{ end -}}
}
{{ if $t.PrimaryKeys -}}
// Exists returns true when the [{{ $t.GoName }}] exists in the database.
func ({{ short $t }} *{{ $t.GoName }}) Exists() bool {
return {{ short $t }}._exists
}
// Deleted returns true when the [{{ $t.GoName }}] has been marked for deletion
// from the database.
func ({{ short $t }} *{{ $t.GoName }}) Deleted() bool {
return {{ short $t }}._deleted
}
// {{ func_name_context "Insert" }} inserts the [{{ $t.GoName }}] to the database.
{{ recv_context $t "Insert" }} {
switch {
case {{ short $t }}._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case {{ short $t }}._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
{{ if $t.Manual -}}
// insert (manual)
{{ sqlstr "insert_manual" $t }}
// run
{{ logf $t }}
if _, err := {{ db_prefix "Exec" false $t }}; err != nil {
return logerror(err)
}
{{- else -}}
// insert (primary key generated and returned by database)
{{ sqlstr "insert" $t }}
// run
{{ logf $t $t.PrimaryKeys }}
{{ if (driver "postgres") -}}
if err := {{ db_prefix "QueryRow" true $t }}.Scan(&{{ short $t }}.{{ (index $t.PrimaryKeys 0).GoName }}); err != nil {
return logerror(err)
}
{{- else if (driver "sqlserver") -}}
rows, err := {{ db_prefix "Query" true $t }}
if err != nil {
return logerror(err)
}
defer rows.Close()
// retrieve id
var id int64
for rows.Next() {
if err := rows.Scan(&id); err != nil {
return logerror(err)
}
}
if err := rows.Err(); err != nil {
return logerror(err)
}
{{- else if (driver "oracle") -}}
var id int64
if _, err := {{ db_prefix "Exec" true $t (named "pk" "&id" true) }}; err != nil {
return logerror(err)
}
{{- else -}}
res, err := {{ db_prefix "Exec" true $t }}
if err != nil {
return logerror(err)
}
// retrieve id
id, err := res.LastInsertId()
if err != nil {
return logerror(err)
}
{{- end -}}
{{ if not (driver "postgres") -}}
// set primary key
{{ short $t }}.{{ (index $t.PrimaryKeys 0).GoName }} = {{ (index $t.PrimaryKeys 0).Type }}(id)
{{- end }}
{{- end }}
// set exists
{{ short $t }}._exists = true
return nil
}
{{ if context_both -}}
// Insert inserts the [{{ $t.GoName }}] to the database.
{{ recv $t "Insert" }} {
return {{ short $t }}.InsertContext(context.Background(), db)
}
{{- end }}
{{ if eq (len $t.Fields) (len $t.PrimaryKeys) -}}
// ------ NOTE: Update statements omitted due to lack of fields other than primary key ------
{{- else -}}
// {{ func_name_context "Update" }} updates a [{{ $t.GoName }}] in the database.
{{ recv_context $t "Update" }} {
switch {
case !{{ short $t }}._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case {{ short $t }}._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with {{ if driver "postgres" }}composite {{ end }}primary key
{{ sqlstr "update" $t }}
// run
{{ logf_update $t }}
if _, err := {{ db_update "Exec" $t }}; err != nil {
return logerror(err)
}
return nil
}
{{ if context_both -}}
// Update updates a [{{ $t.GoName }}] in the database.
{{ recv $t "Update" }} {
return {{ short $t }}.UpdateContext(context.Background(), db)
}
{{- end }}
// {{ func_name_context "Save" }} saves the [{{ $t.GoName }}] to the database.
{{ recv_context $t "Save" }} {
if {{ short $t }}.Exists() {
return {{ short $t }}.{{ func_name_context "Update" }}({{ if context }}ctx, {{ end }}db)
}
return {{ short $t }}.{{ func_name_context "Insert" }}({{ if context }}ctx, {{ end }}db)
}
{{ if context_both -}}
// Save saves the [{{ $t.GoName }}] to the database.
{{ recv $t "Save" }} {
if {{ short $t }}._exists {
return {{ short $t }}.UpdateContext(context.Background(), db)
}
return {{ short $t }}.InsertContext(context.Background(), db)
}
{{- end }}
// {{ func_name_context "Upsert" }} performs an upsert for [{{ $t.GoName }}].
{{ recv_context $t "Upsert" }} {
switch {
case {{ short $t }}._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
{{ sqlstr "upsert" $t }}
// run
{{ logf $t }}
if _, err := {{ db_prefix "Exec" false $t }}; err != nil {
return logerror(err)
}
// set exists
{{ short $t }}._exists = true
return nil
}
{{ if context_both -}}
// Upsert performs an upsert for [{{ $t.GoName }}].
{{ recv $t "Upsert" }} {
return {{ short $t }}.UpsertContext(context.Background(), db)
}
{{- end -}}
{{- end }}
// {{ func_name_context "Delete" }} deletes the [{{ $t.GoName }}] from the database.
{{ recv_context $t "Delete" }} {
switch {
case !{{ short $t }}._exists: // doesn't exist
return nil
case {{ short $t }}._deleted: // deleted
return nil
}
{{ if eq (len $t.PrimaryKeys) 1 -}}
// delete with single primary key
{{ sqlstr "delete" $t }}
// run
{{ logf_pkeys $t }}
if _, err := {{ db "Exec" (print (short $t) "." (index $t.PrimaryKeys 0).GoName) }}; err != nil {
return logerror(err)
}
{{- else -}}
// delete with composite primary key
{{ sqlstr "delete" $t }}
// run
{{ logf_pkeys $t }}
if _, err := {{ db "Exec" (names (print (short $t) ".") $t.PrimaryKeys) }}; err != nil {
return logerror(err)
}
{{- end }}
// set deleted
{{ short $t }}._deleted = true
return nil
}
{{ if context_both -}}
// Delete deletes the [{{ $t.GoName }}] from the database.
{{ recv $t "Delete" }} {
return {{ short $t }}.DeleteContext(context.Background(), db)
}
{{- end -}}
{{- end }}
{{ end }}