-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.go
More file actions
571 lines (483 loc) · 17 KB
/
table.go
File metadata and controls
571 lines (483 loc) · 17 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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package morph
import (
"errors"
"fmt"
"reflect"
"regexp"
"slices"
"sort"
"strings"
"time"
)
// 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")
// ErrMissingForeignKeyColumns represents an error encountered when establishing a reference
// between two tables but the foreign key columns are not present in the child table.
ErrMissingForeignKeyColumns = errors.New("morph: specified foreign key columns are missing from table")
// ErrMissingReference represents an error encountered when a reference is not found
// based on the provided criteria.
ErrMissingReference = errors.New("morph: no matching reference found")
)
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
references []Reference
}
// 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
}
// ReferencesTo retrieves all of the tables that this table references.
func (t Table) ReferencesTo() []Table {
refs := t.FindReferences(func(r Reference) bool {
return r.child.Equals(t)
})
tables := []Table{}
for _, ref := range refs {
tables = append(tables, ref.parent)
}
return tables
}
// ReferenceTo retrieves the reference to the provided table.
func (t Table) ReferenceTo(table Table) (Reference, error) {
ref, ok := t.FindReference(func(r Reference) bool {
return r.child.Equals(t) && r.parent.Equals(table)
})
if !ok {
return Reference{}, ErrMissingReference
}
return ref, nil
}
// HasReferenceTo checks if this table has a reference to the provided table.
func (t Table) HasReferenceTo(table Table) bool {
_, ok := t.FindReference(func(r Reference) bool {
return r.child.Equals(t) && r.parent.Equals(table)
})
return ok
}
// IsReferenced checks if this table is referenced by any other tables.
func (t Table) IsReferenced() bool {
_, ok := t.FindReference(func(r Reference) bool {
return r.parent.Equals(t)
})
return ok
}
// IsReferencedBy checks if this table is referenced by the provided table.
func (t Table) IsReferencedBy(table Table) bool {
_, ok := t.FindReference(func(r Reference) bool {
return r.child.Equals(table) && r.parent.Equals(t)
})
return ok
}
// ReferencedBy retrieves all of the tables that reference this table.
func (t Table) ReferencedBy() []Table {
refs := t.FindReferences(func(r Reference) bool {
return r.parent.Equals(t)
})
tables := []Table{}
for _, ref := range refs {
tables = append(tables, ref.child)
}
return tables
}
// FindReferences retrieves all of the references that match the provided predicate.
func (t Table) FindReferences(p func(Reference) bool) []Reference {
refs := []Reference{}
for _, ref := range t.references {
if p(ref) {
refs = append(refs, ref)
}
}
return refs
}
// FindReference retrieves the first reference that matches the provided predicate,
// returning a boolean indicating if a match was found.
func (t Table) FindReference(p func(Reference) bool) (Reference, bool) {
refs := t.FindReferences(p)
if len(refs) > 0 {
return refs[0], true
}
return Reference{}, false
}
func (t *Table) setReferences(refs []Reference) {
if refs == nil {
t.references = []Reference{}
return
}
if t.references == nil {
t.references = []Reference{}
}
r := make([]Reference, len(refs))
copy(r, refs)
t.references = r
}
// 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
}
// PrimaryKeyColumns retrieves all of the primary key columns for the table.
func (t *Table) PrimaryKeyColumns() []Column {
return t.FindColumns(func(c Column) bool {
return c.PrimaryKey()
})
}
// NonPrimaryKeyColumns retrieves all of the non-primary key columns for the table.
func (t *Table) NonPrimaryKeyColumns() []Column {
return t.FindColumns(func(c Column) bool {
return !c.PrimaryKey()
})
}
// HasColumn indicates whether the table has a column with the provided name.
func (t *Table) HasColumn(name string) bool {
if t.columnsByName == nil {
t.columnsByName = make(map[string]Column)
}
_, ok := t.columnsByName[name]
return ok
}
// 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 && val.Elem().Type() != reflect.TypeOf(time.Time{}) {
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
}
// InsertQuery generates an INSERT query for the table.
func (t *Table) InsertQuery(options ...QueryOption) (string, error) {
generator := newQueryGenerator(t, t.PrimaryKeyColumns(), t.NonPrimaryKeyColumns())
return generator.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())
generator := newQueryGenerator(t, t.PrimaryKeyColumns(), t.NonPrimaryKeyColumns())
return generator.InsertQueryWithArgs(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) {
generator := newQueryGenerator(t, t.PrimaryKeyColumns(), t.NonPrimaryKeyColumns())
return generator.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())
generator := newQueryGenerator(t, t.PrimaryKeyColumns(), t.NonPrimaryKeyColumns())
return generator.UpdateQueryWithArgs(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) {
generator := newQueryGenerator(t, t.PrimaryKeyColumns(), t.NonPrimaryKeyColumns())
return generator.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())
generator := newQueryGenerator(t, t.PrimaryKeyColumns(), t.NonPrimaryKeyColumns())
return generator.DeleteQueryWithArgs(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) {
generator := newQueryGenerator(t, t.PrimaryKeyColumns(), t.NonPrimaryKeyColumns())
return generator.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())
generator := newQueryGenerator(t, t.PrimaryKeyColumns(), t.NonPrimaryKeyColumns())
return generator.SelectQueryWithArgs(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...))
}
// References creates a reference between two tables, where the provided table
// is treated as the parent and the current table is treated as the child.
func (t *Table) References(table *Table, key []Column) (Reference, error) {
if err := t.validate(); err != nil {
return Reference{}, err
}
if err := table.validate(); err != nil {
return Reference{}, err
}
if len(t.FindColumns(func(c Column) bool { return slices.Contains(key, c) })) == 0 {
return Reference{}, ErrMissingForeignKeyColumns
}
ref := Reference{parent: *table, child: *t, foreignKey: key}
if !slices.ContainsFunc(t.references, func(r Reference) bool { return ref.equals(r) }) {
t.setReferences(append(t.references, ref))
}
if !slices.ContainsFunc(table.references, func(r Reference) bool { return ref.equals(r) }) {
table.setReferences(append(table.references, ref))
}
return Reference{parent: *table, child: *t, foreignKey: key}, nil
}
// Equals checks if two tables are equal.
func (t Table) Equals(other Table) bool {
sameTypeName := t.TypeName() == other.TypeName()
sameName := t.Name() == other.Name()
sameColumns := slices.EqualFunc(t.Columns(), other.Columns(), func(a, b Column) bool {
return a.equals(b)
})
return sameTypeName && sameName && sameColumns
}