This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.go
349 lines (307 loc) · 9.69 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
package sqlr
import (
"fmt"
"reflect"
"github.com/jjeffery/kv"
"github.com/jjeffery/sqlr/private/column"
)
// Table represents the information known about a database table.
// The information known about a table is derived from the row
// type (which must be a struct), and any configuration that was
// provided via the SchemaConfig when the schema was created.
type Table struct {
schema *Schema
rowType reflect.Type
tableName string
cols []*Column
pk []*Column
nk []*Column
autoincr *Column
createdAt *Column
updatedAt *Column
version *Column
}
// getRowType converts a row instance into a row type.
// Returns an error if row does not refer to a struct type.
func getRowType(row interface{}) (reflect.Type, error) {
var rowType reflect.Type
if t, ok := row.(reflect.Type); ok {
rowType = t
} else {
rowType = reflect.TypeOf(row)
}
for rowType.Kind() != reflect.Struct {
switch rowType.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Array, reflect.Chan, reflect.Map:
rowType = rowType.Elem()
break
default:
err := fmt.Errorf("expected row type to be a struct, found %v", rowType.String())
return nil, err
}
}
return rowType, nil
}
// getTableName returns the table name for the row type.
func getTableName(schema *Schema, rowType reflect.Type, cfg *TableConfig) string {
if cfg != nil && cfg.TableName != "" {
return cfg.TableName
}
// not specified in the config, so look for the first field
// in the struct with a "table" tag.
for _, colInfo := range column.ListForType(rowType) {
if tableName, ok := colInfo.Field.Tag.Lookup("table"); ok {
return tableName
}
}
// try to work out the name based on the naming convention
if rowTypeName := rowType.Name(); rowTypeName != "" {
convention := schema.convention
if convention == nil {
convention = defaultNamingConvention
}
return convention.TableName(rowTypeName)
}
// anonymous type: table name cannot be determined
return "__unknown_table_name__"
}
// newTable returns a new Table value for the row type. If cfg is non-nil,
// then it must have already been checked for any inconsistencies.
func newTable(schema *Schema, rowType reflect.Type, cfg *TableConfig) *Table {
columnNamer := schema.columnNamer()
tbl := &Table{
schema: schema,
rowType: rowType,
tableName: getTableName(schema, rowType, cfg),
}
for _, colInfo := range column.ListForType(rowType) {
if colInfo.Tag.Ignore {
continue
}
var colConfig ColumnConfig
var hasColConfig bool
if cfg != nil {
colConfig, hasColConfig = cfg.Columns[colInfo.FieldNames]
}
if colConfig.Ignore {
continue
}
col := &Column{
columnName: columnNamer.ColumnName(colInfo),
info: colInfo,
primaryKey: colInfo.Tag.PrimaryKey,
autoIncrement: colInfo.Tag.AutoIncrement,
emptyNull: colInfo.Tag.EmptyNull,
json: colInfo.Tag.JSON,
naturalKey: colInfo.Tag.NaturalKey,
version: colInfo.Tag.Version,
zeroValue: reflect.Zero(colInfo.Field.Type).Interface(),
}
if hasColConfig {
if colConfig.ColumnName != "" {
col.columnName = colConfig.ColumnName
}
if colConfig.OverrideStructTag {
col.primaryKey = colConfig.PrimaryKey
col.autoIncrement = colConfig.AutoIncrement
col.emptyNull = colConfig.EmptyNull
col.json = colConfig.JSON
col.naturalKey = colConfig.NaturalKey
} else {
col.primaryKey = col.primaryKey || colConfig.PrimaryKey
col.autoIncrement = col.autoIncrement || colConfig.AutoIncrement
col.emptyNull = col.emptyNull || colConfig.EmptyNull
col.json = col.json || colConfig.JSON
col.naturalKey = col.naturalKey || colConfig.NaturalKey
}
}
tbl.cols = append(tbl.cols, col)
if col.primaryKey {
tbl.pk = append(tbl.pk, col)
}
if col.naturalKey {
tbl.nk = append(tbl.nk, col)
}
if col.autoIncrement {
tbl.autoincr = col
}
if col.version {
tbl.version = col
}
// TODO(jpj): we should have another way to define these columns
// apart from their field names.
if col.info.FieldNames == "CreatedAt" {
tbl.createdAt = col
}
if col.info.FieldNames == "UpdatedAt" {
tbl.updatedAt = col
}
}
return tbl
}
func newTableWithConfig(schema *Schema, rowType reflect.Type, config *TableConfig) (*Table, error) {
// check that all of the field names in the config match field names in the row type
if len(config.Columns) > 0 {
fieldPaths := make(map[string]bool)
for _, colInfo := range column.ListForType(rowType) {
fieldPaths[colInfo.FieldNames] = true
}
for fieldPath := range config.Columns {
if !fieldPaths[fieldPath] {
return nil, fmt.Errorf("field %s not found in type %s", fieldPath, rowType)
}
}
}
tbl := newTable(schema, rowType, config)
var versionCols []string
var autoIncrementCols []string
for _, col := range tbl.Columns() {
if col.AutoIncrement() {
autoIncrementCols = append(autoIncrementCols, col.Name())
}
if col.Version() {
versionCols = append(versionCols, col.Name())
}
}
if len(versionCols) > 1 {
return nil, fmt.Errorf("%s: multiple version columns not permitted (%v)", rowType, versionCols)
}
if len(autoIncrementCols) > 1 {
return nil, fmt.Errorf("%s: multiple autoincrement columns not permitted (%v)", rowType, versionCols)
}
if tbl.version != nil {
kind := tbl.version.info.Field.Type.Kind()
if kind != reflect.Int && kind != reflect.Int32 && kind != reflect.Int64 {
return nil, fmt.Errorf("%s: version column should have kind Int, Int32 or Int64", rowType)
}
}
return tbl, nil
}
// Name returns the name of the table.
func (tbl *Table) Name() string {
return tbl.tableName
}
// RowType returns the row type, which is always a struct.
func (tbl *Table) RowType() reflect.Type {
return tbl.rowType
}
// PrimaryKey returns the column or columns that form the
// primary key for the table. Returns nil if no primary key
// has been defined.
func (tbl *Table) PrimaryKey() []*Column {
return columnSlice(tbl.pk)
}
// NaturalKey returns the natural key columns for the table.
// Returns nil if no natural key columns have been defined.
// Natural key columns are columns that are useful for identifying
// a row. They are used in error messages only. (And we might remove
// them to make the API simpler to start with).
func (tbl *Table) NaturalKey() []*Column {
return columnSlice(tbl.nk)
}
// Columns returns all columns defined for the table.
func (tbl *Table) Columns() []*Column {
return columnSlice(tbl.cols)
}
func (tbl *Table) getRowValue(row interface{}) (reflect.Value, error) {
rowValue := reflect.ValueOf(row)
for rowValue.Type().Kind() == reflect.Ptr {
rowValue = rowValue.Elem()
}
if rowValue.Type() != tbl.rowType {
return reflect.Value{}, kv.NewError("unexpected row type").With(
"want", tbl.rowType,
"have", rowValue.Type(),
)
}
return rowValue, nil
}
func (tbl *Table) mustGetRowValue(row interface{}) reflect.Value {
rowValue, err := tbl.getRowValue(row)
if err != nil {
panic(err)
}
return rowValue
}
// keyvals returns a list of key/value pairs to include in any log message
// or error message concerning the row. The list includes the type of the
// row, the primary key field(s), and any natural key field(s).
func (tbl *Table) keyvals(row interface{}) []interface{} {
rowValue := tbl.mustGetRowValue(row)
keyvals := []interface{}{
"rowType", rowValue.Type().String(),
}
for _, col := range tbl.pk {
keyvals = append(keyvals, col.info.FieldNames)
keyvals = append(keyvals, col.info.Index.ValueRO(rowValue).Interface())
}
for _, col := range tbl.nk {
keyvals = append(keyvals, col.info.FieldNames)
keyvals = append(keyvals, col.info.Index.ValueRO(rowValue).Interface())
}
return keyvals
}
// wrapRowError wraps an error with a description and key/value pairs that identify the
// row that was involved in the error condition.
func (tbl *Table) wrapRowError(err error, row interface{}, msg string) kv.Error {
keyvals := tbl.keyvals(row)
return kv.Wrap(err, msg).With(keyvals...)
}
// Column contains meta-data about a column in a database table.
type Column struct {
columnName string
primaryKey bool
autoIncrement bool
version bool
json bool
naturalKey bool
emptyNull bool
zeroValue interface{}
info *column.Info
}
// Name returns the name of the database column.
func (col *Column) Name() string {
return col.columnName
}
// fieldType returns the type of the field associated with the column.
func (col *Column) fieldType() reflect.Type {
return col.info.Field.Type
}
// fieldIndex returns the index sequence for Type.FieldByIndex
func (col *Column) fieldIndex() []int {
return []int(col.info.Index)
}
// PrimaryKey returns true if this column is the primary key,
// or forms part of the primary key.
func (col *Column) PrimaryKey() bool {
return col.primaryKey
}
// AutoIncrement returns true if this column is an auto-increment
// column.
func (col *Column) AutoIncrement() bool {
return col.autoIncrement
}
// Version returns true if this column is an optimistic locking version column.
func (col *Column) Version() bool {
return col.version
}
// EmptyNull returns true if the zero value for the associated field type
// should be stored as NULL in the database.
//
// This is commonly set for string values and time.Time values. It is common
// for an empty string value or an empty time.Time value to be represented
// as a database NULL.
func (col *Column) EmptyNull() bool {
return col.emptyNull
}
// JSON returns true if column's value is unmarshaled from JSON into
// the associated struct field, and if the struct field is marshaled into
// JSON to be stored in the database column.
func (col *Column) JSON() bool {
return col.json
}
func columnSlice(src []*Column) []*Column {
dest := make([]*Column, len(src), len(src))
copy(dest, src)
return dest
}