-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
495 lines (416 loc) · 13.6 KB
/
table.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package morph
import (
"bytes"
"errors"
"fmt"
"reflect"
"regexp"
"sort"
"strings"
"text/template"
)
// Defines the various errors that can occur when interacting with tables.
var (
// ErrMissingTypeName represents an error encountered when evaluation is attempted but the
// table does not have a type name configured.
ErrMissingTypeName = errors.New("morph: must have table type name to evaluate")
// ErrMissingTableName represents an error encountered when evaluation is attempted but the
// table does not have a table name configured.
ErrMissingTableName = errors.New("morph: must have table name to evaluate")
// ErrMissingTableAlias represents an error encountered when evaluation is attempted but the
// table does not have a table alias configured.
ErrMissingTableAlias = errors.New("morph: must have table alias to evaluate")
// ErrMissingColumns represents an error encountered when evaluation is attempted but the
// table does not have any columns configured.
ErrMissingColumns = errors.New("morph: must have columns to evaluate")
// ErrMismatchingTypeName represents an error encountered when evaluation is attempted but the
// table type name does not match the type name of the object being evaluated.
ErrMismatchingTypeName = errors.New("morph: must have matching type names to evaluate")
// ErrMissingPrimaryKey represents an error encountered when a table does not have any primary key columns.
ErrMissingPrimaryKey = errors.New("morph: table must have at least one primary key column")
// ErrMissingNonPrimaryKey represents an error encountered when a table does not have any non-primary key columns.
ErrMissingNonPrimaryKey = errors.New("morph: table must have at least one non-primary key column")
)
var (
namedParamRegExp *regexp.Regexp = regexp.MustCompile(`:[a-zA-Z0-9_]+`)
)
// EvaluationResult represents the result of evaluating a table against an object.
type EvaluationResult map[string]any
// Empties retrieves all of the keys in the result that have nil values.
func (r EvaluationResult) Empties() []string {
var empties []string
for key, val := range r {
if val == nil {
empties = append(empties, key)
}
}
return empties
}
// NonEmpties retrieves all of the keys in the result that have non-nil values.
func (r EvaluationResult) NonEmpties() []string {
var nonEmpties []string
for key, val := range r {
if val != nil {
nonEmpties = append(nonEmpties, key)
}
}
return nonEmpties
}
// Table represents a mapping between an entity and a database table.
type Table struct {
typeName string
name string
alias string
columnsByName map[string]Column
columnsByField map[string]Column
}
// SetType associates the entity type to the table.
func (t *Table) SetType(entity any) {
t.SetTypeName(fmt.Sprintf("%T", entity))
}
// SetTypeName modifies the entity type name for the table.
func (t *Table) SetTypeName(typeName string) {
t.typeName = strings.TrimSpace(typeName)
}
// TypeName retrieves the type name of the entity associated to the table.
func (t *Table) TypeName() string {
return t.typeName
}
// Name retrieves the the table name.
func (t *Table) Name() string {
return t.name
}
// SetName modifies the name of the table.
func (t *Table) SetName(name string) {
t.name = strings.TrimSpace(name)
}
// Alias retrieves the alias for the table.
func (t *Table) Alias() string {
return t.alias
}
// SetAlias modifies the alias of the table.
func (t *Table) SetAlias(alias string) {
t.alias = strings.TrimSpace(alias)
}
// ColumnNames retrieves all of the column names for the table.
func (t *Table) ColumnNames() []string {
var names []string
for _, col := range t.Columns() {
names = append(names, col.Name())
}
return names
}
// ColumnName retrieves the column name associated to the provide field name.
func (t *Table) ColumnName(field string) (string, error) {
if t.columnsByField == nil {
t.columnsByField = make(map[string]Column)
}
if column, ok := t.columnsByField[field]; ok {
return column.Name(), nil
}
return "", fmt.Errorf("morph: no mapping for field %q", field)
}
// FieldName retrieves the field name associated to the provided column name.
func (t *Table) FieldName(name string) (string, error) {
if t.columnsByName == nil {
t.columnsByName = make(map[string]Column)
}
if column, ok := t.columnsByName[name]; ok {
return column.Field(), nil
}
return "", fmt.Errorf("morph: no mapping for column %q", name)
}
// Columns retrieves all of the columns for the table.
func (t *Table) Columns() (columns []Column) {
if t.columnsByName == nil {
t.columnsByName = make(map[string]Column)
}
for _, c := range t.columnsByName {
columns = append(columns, c)
}
sort.Slice(columns, func(i, j int) bool {
return columns[i].Name() < columns[j].Name()
})
return
}
// FindColumn retrieves the columns that matches the provided predicate.
func (t Table) FindColumns(p ColumnPredicate) []Column {
cols := []Column{}
for _, col := range t.Columns() {
if p(col) {
cols = append(cols, col)
}
}
return cols
}
// AddColumn adds a column to the table.
func (t *Table) AddColumn(column Column) error {
if t.columnsByName == nil {
t.columnsByName = make(map[string]Column)
}
if _, ok := t.columnsByName[column.Name()]; ok {
return fmt.Errorf(
"morph: column with name %q already exists", column.Name())
}
if t.columnsByField == nil {
t.columnsByField = make(map[string]Column)
}
if _, ok := t.columnsByField[column.Field()]; ok {
return fmt.Errorf(
"morph: column with field %q already exists", column.Field())
}
t.columnsByName[column.Name()],
t.columnsByField[column.Field()] = column, column
return nil
}
// AddColumns adds all of the provided columns to the table.
func (t *Table) AddColumns(columns ...Column) error {
for _, column := range columns {
if err := t.AddColumn(column); err != nil {
return err
}
}
return nil
}
// Evaluate applies the table to the provided object to produce a result
// containing the column names and their respective values. The result
// can then be subsequently used to execute queries.
func (t *Table) Evaluate(obj any) (EvaluationResult, error) {
objType := reflect.TypeOf(obj)
objVal := reflect.ValueOf(obj)
// determine the type name for the pointer version of obj.
ptrTypeName := fmt.Sprintf("%T", obj)
if objType.Kind() != reflect.Ptr {
ptrTypeName = fmt.Sprintf("%T", reflect.New(objVal.Type()).Interface())
}
// determine the type name for the value version of obj.
valTypeName := fmt.Sprintf("%T", obj)
if objType.Kind() == reflect.Ptr {
valTypeName = fmt.Sprintf("%T", objVal.Elem().Interface())
}
// fail if the type name for the table doesn't match both the pointer and value type names.
if t.typeName != ptrTypeName && t.typeName != valTypeName {
return nil, ErrMismatchingTypeName
}
if err := t.validate(); err != nil {
return nil, err
}
results := make(EvaluationResult)
for fieldName, column := range t.columnsByField {
if column.UsingStructFieldStrategy() {
oVal := objVal
if oVal.Kind() == reflect.Ptr {
oVal = oVal.Elem()
}
val := oVal.FieldByName(fieldName)
if !val.IsValid() {
continue
}
if val.Kind() == reflect.Ptr && !val.IsZero() {
if val.Elem().Kind() == reflect.Struct {
continue
}
val = val.Elem()
}
if val.Kind() == reflect.Ptr && (val.IsZero() || val.IsNil()) {
results[column.Name()] = nil
continue
}
results[column.Name()] = val.Interface()
}
if column.UsingMethodStrategy() {
oType := objType
oVal := objVal
if oVal.Kind() != reflect.Ptr {
ptr := reflect.New(oVal.Type())
ptr.Elem().Set(oVal)
oVal = ptr
}
if oType.Kind() != reflect.Ptr {
oType = reflect.PointerTo(oType)
}
method, ok := oType.MethodByName(fieldName)
if !ok {
continue
}
valResults := method.Func.Call([]reflect.Value{oVal})
if len(valResults) == 0 || !valResults[0].IsValid() {
continue
}
if valResults[0].Kind() == reflect.Ptr && (valResults[0].IsZero() || valResults[0].IsNil()) {
results[column.Name()] = nil
continue
}
results[column.Name()] = valResults[0].Interface()
}
}
return results, nil
}
// MustEvaluate performs the same operation as Evaluate but panics if an error occurs.
func (t *Table) MustEvaluate(obj any) EvaluationResult {
results, err := t.Evaluate(obj)
if err != nil {
panic(err)
}
return results
}
// validate ensures that the table is properly configured.
func (t *Table) validate() error {
if len(t.typeName) == 0 {
return ErrMissingTypeName
}
if len(t.name) == 0 {
return ErrMissingTableName
}
if len(t.alias) == 0 {
return ErrMissingTableAlias
}
if len(t.columnsByName) == 0 {
return ErrMissingColumns
}
if len(t.FindColumns(func(c Column) bool { return c.PrimaryKey() })) == 0 {
return ErrMissingPrimaryKey
}
if len(t.FindColumns(func(c Column) bool { return !c.PrimaryKey() })) == 0 {
return ErrMissingNonPrimaryKey
}
return nil
}
// query generates a query for the table using the provided template and options.
func (t *Table) query(tmpl *template.Template, options ...QueryOption) (string, error) {
if err := t.validate(); err != nil {
return "", err
}
qo := &QueryOptions{}
opts := append(DefaultQueryOptions, options...)
for _, opt := range opts {
opt(qo)
}
data := struct {
Table *Table
PrimaryKeys []Column
NonPrimaryKeys []Column
Options *QueryOptions
Data EvaluationResult
}{
Table: t,
Options: qo,
PrimaryKeys: t.FindColumns(func(c Column) bool { return c.PrimaryKey() }),
NonPrimaryKeys: t.FindColumns(func(c Column) bool { return !c.PrimaryKey() }),
}
if qo.OmitEmpty && qo.obj != nil {
var err error
if data.Data, err = t.Evaluate(qo.obj); err != nil {
return "", err
}
}
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (t *Table) queryWithArgs(namedQuery string, obj any, options ...QueryOption) (string, []any, error) {
qo := &QueryOptions{}
opts := append(DefaultQueryOptions, options...)
for _, opt := range opts {
opt(qo)
}
result, err := t.Evaluate(obj)
if err != nil {
return "", nil, err
}
args := []any{}
missing := []string{}
count := 0
query := namedParamRegExp.ReplaceAllStringFunc(namedQuery, func(match string) string {
name := match[1:]
if arg, ok := result[name]; ok {
args = append(args, arg)
if qo.Ordered {
count += 1
return qo.Placeholder + fmt.Sprintf("%d", count)
}
return qo.Placeholder
}
missing = append(missing, name)
return match
})
if len(missing) > 0 {
return "", nil, errors.New("morph: missing values for named parameters: " + strings.Join(missing, ", "))
}
return query, args, nil
}
// InsertQuery generates an INSERT query for the table.
func (t *Table) InsertQuery(options ...QueryOption) (string, error) {
return t.query(insertTmpl, options...)
}
// InsertQueryWithArgs generates an INSERT query for the table along with arguments
// derived from the provided object.
func (t *Table) InsertQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
opts := append(options, WithNamedParameters())
query, err := t.InsertQuery(opts...)
if err != nil {
return "", nil, err
}
return t.queryWithArgs(query, obj, opts...)
}
// MustInsertQuery performs the same operation as InsertQuery but panics if an error occurs.
func (t *Table) MustInsertQuery(options ...QueryOption) string {
return Must(t.InsertQuery(options...))
}
// UpdateQuery generates an UPDATE query for the table.
func (t *Table) UpdateQuery(options ...QueryOption) (string, error) {
return t.query(updateTmpl, options...)
}
// UpdateQueryWithArgs generates an UPDATE query for the table along with arguments
// derived from the provided object.
func (t *Table) UpdateQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
opts := append(options, WithNamedParameters())
query, err := t.UpdateQuery(opts...)
if err != nil {
return "", nil, err
}
return t.queryWithArgs(query, obj, opts...)
}
// MustUpdateQuery performs the same operation as UpdateQuery but panics if an error occurs.
func (t *Table) MustUpdateQuery(options ...QueryOption) string {
return Must(t.UpdateQuery(options...))
}
// DeleteQuery generates a DELETE query for the table.
func (t *Table) DeleteQuery(options ...QueryOption) (string, error) {
return t.query(deleteTmpl, options...)
}
// DeleteQueryWithArgs generates a DELETE query for the table along with arguments
// derived from the provided object.
func (t *Table) DeleteQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
opts := append(options, WithNamedParameters())
query, err := t.DeleteQuery(opts...)
if err != nil {
return "", nil, err
}
return t.queryWithArgs(query, obj, opts...)
}
// MustDeleteQuery performs the same operation as DeleteQuery but panics if an error occurs.
func (t *Table) MustDeleteQuery(options ...QueryOption) string {
return Must(t.DeleteQuery(options...))
}
// SelectQuery generates a SELECT query for the table.
func (t *Table) SelectQuery(options ...QueryOption) (string, error) {
return t.query(selectTmpl, options...)
}
// SelectQueryWithArgs generates a SELECT query for the table along with arguments
// derived from the provided object.
func (t *Table) SelectQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
opts := append(options, WithNamedParameters())
query, err := t.SelectQuery(opts...)
if err != nil {
return "", nil, err
}
return t.queryWithArgs(query, obj, opts...)
}
// MustSelectQuery performs the same operation as SelectQuery but panics if an error occurs.
func (t *Table) MustSelectQuery(options ...QueryOption) string {
return Must(t.SelectQuery(options...))
}