-
Notifications
You must be signed in to change notification settings - Fork 3
/
rw.go
374 lines (345 loc) · 11 KB
/
rw.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dbw
import (
"context"
"fmt"
"reflect"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
)
const (
noRowsAffected = 0
// DefaultLimit is the default for search results when no limit is specified
// via the WithLimit(...) option
DefaultLimit = 10000
)
// RW uses a DB as a connection for it's read/write operations. This is
// basically the primary type for the package's operations.
type RW struct {
underlying *DB
}
// ensure that RW implements the interfaces of: Reader and Writer
var (
_ Reader = (*RW)(nil)
_ Writer = (*RW)(nil)
)
// New creates a new RW using an open DB. Note: there can by many RWs that share
// the same DB, since the DB manages the connection pool.
func New(underlying *DB) *RW {
return &RW{underlying: underlying}
}
// DB returns the underlying DB
func (rw *RW) DB() *DB {
return rw.underlying
}
// Exec will execute the sql with the values as parameters. The int returned
// is the number of rows affected by the sql. The WithDebug option is supported.
func (rw *RW) Exec(ctx context.Context, sql string, values []interface{}, opt ...Option) (int, error) {
const op = "dbw.Exec"
if rw.underlying == nil {
return 0, fmt.Errorf("%s: missing underlying db: %w", op, ErrInternal)
}
if sql == "" {
return noRowsAffected, fmt.Errorf("%s: missing sql: %w", op, ErrInvalidParameter)
}
opts := GetOpts(opt...)
db := rw.underlying.wrapped.WithContext(ctx)
if opts.WithDebug {
db = db.Debug()
}
db = db.Exec(sql, values...)
if db.Error != nil {
return noRowsAffected, fmt.Errorf("%s: %w", op, db.Error)
}
return int(db.RowsAffected), nil
}
func (rw *RW) primaryFieldsAreZero(ctx context.Context, i interface{}) ([]string, bool, error) {
const op = "dbw.primaryFieldsAreZero"
var fieldNames []string
tx := rw.underlying.wrapped.Model(i)
if err := tx.Statement.Parse(i); err != nil {
return nil, false, fmt.Errorf("%s: %w", op, ErrInvalidParameter)
}
for _, f := range tx.Statement.Schema.PrimaryFields {
if f.PrimaryKey {
if _, isZero := f.ValueOf(ctx, reflect.ValueOf(i)); isZero {
fieldNames = append(fieldNames, f.Name)
}
}
}
return fieldNames, len(fieldNames) > 0, nil
}
func isNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}
func contains(ss []string, t string) bool {
for _, s := range ss {
if strings.EqualFold(s, t) {
return true
}
}
return false
}
func validateResourcesInterface(resources interface{}) error {
const op = "dbw.validateResourcesInterface"
vo := reflect.ValueOf(resources)
if vo.Kind() != reflect.Ptr {
return fmt.Errorf("%s: interface parameter must to be a pointer: %w", op, ErrInvalidParameter)
}
e := vo.Elem()
if e.Kind() == reflect.Slice {
if e.Type().Elem().Kind() != reflect.Ptr {
return fmt.Errorf("%s: interface parameter is a slice, but the elements of the slice are not pointers: %w", op, ErrInvalidParameter)
}
}
return nil
}
func raiseErrorOnHooks(i interface{}) error {
const op = "dbw.raiseErrorOnHooks"
v := i
valOf := reflect.ValueOf(i)
if valOf.Kind() == reflect.Slice {
if valOf.Len() == 0 {
return nil
}
v = valOf.Index(0).Interface()
}
switch v.(type) {
case
// create hooks
callbacks.BeforeCreateInterface,
callbacks.AfterCreateInterface,
callbacks.BeforeSaveInterface,
callbacks.AfterSaveInterface,
// update hooks
callbacks.BeforeUpdateInterface,
callbacks.AfterUpdateInterface,
// delete hooks
callbacks.BeforeDeleteInterface,
callbacks.AfterDeleteInterface,
// find hooks
callbacks.AfterFindInterface:
return fmt.Errorf("%s: gorm callback/hooks are not supported: %w", op, ErrInvalidParameter)
}
return nil
}
// IsTx returns true if there's an existing transaction in progress
func (rw *RW) IsTx() bool {
if rw.underlying == nil {
return false
}
switch rw.underlying.wrapped.Statement.ConnPool.(type) {
case gorm.TxBeginner, gorm.ConnPoolBeginner:
return false
default:
return true
}
}
func (rw *RW) whereClausesFromOpts(_ context.Context, i interface{}, opts Options) (string, []interface{}, error) {
const op = "dbw.whereClausesFromOpts"
var where []string
var args []interface{}
if opts.WithVersion != nil {
if *opts.WithVersion == 0 {
return "", nil, fmt.Errorf("%s: with version option is zero: %w", op, ErrInvalidParameter)
}
mDb := rw.underlying.wrapped.Model(i)
err := mDb.Statement.Parse(i)
if err != nil && mDb.Statement.Schema == nil {
return "", nil, fmt.Errorf("%s: (internal error) unable to parse stmt: %w", op, ErrUnknown)
}
if !contains(mDb.Statement.Schema.DBNames, "version") {
return "", nil, fmt.Errorf("%s: %s does not have a version field: %w", op, mDb.Statement.Schema.Table, ErrInvalidParameter)
}
if opts.WithOnConflict != nil {
// on conflict clauses requires the version to be qualified with a
// table name
var tableName string
switch {
case opts.WithTable != "":
tableName = opts.WithTable
default:
tableName = mDb.Statement.Schema.Table
}
where = append(where, fmt.Sprintf("%s.version = ?", tableName)) // we need to include the table name because of "on conflict" use cases
} else {
where = append(where, "version = ?")
}
args = append(args, opts.WithVersion)
}
if opts.WithWhereClause != "" {
where, args = append(where, opts.WithWhereClause), append(args, opts.WithWhereClauseArgs...)
}
return strings.Join(where, " and "), args, nil
}
// clearDefaultNullResourceFields will clear fields in the resource which are
// defaulted to a null value. This addresses the unfixed issue in gorm:
// https://github.com/go-gorm/gorm/issues/6351
func (rw *RW) clearDefaultNullResourceFields(ctx context.Context, i interface{}) error {
const op = "dbw.ClearResourceFields"
stmt := rw.underlying.wrapped.Model(i).Statement
if err := stmt.Parse(i); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
v := reflect.ValueOf(i)
for _, f := range stmt.Schema.Fields {
switch {
case f.PrimaryKey:
// seems a bit redundant, with the test for null, but it's very
// important to not clear the primary fields, so we'll make an
// explicit test
continue
case !f.Updatable:
// well, based on the gorm tags it's a read-only field, so we're done.
continue
case !strings.EqualFold(f.DefaultValue, "null"):
continue
default:
_, isZero := f.ValueOf(ctx, v)
if isZero {
continue
}
if err := f.Set(stmt.Context, v, f.DefaultValueInterface); err != nil {
return fmt.Errorf("%s: unable to set value of non-zero field: %w", op, err)
}
}
}
return nil
}
func (rw *RW) primaryKeysWhere(ctx context.Context, i interface{}) (string, []interface{}, error) {
const op = "dbw.primaryKeysWhere"
var fieldNames []string
var fieldValues []interface{}
tx := rw.underlying.wrapped.Model(i)
if err := tx.Statement.Parse(i); err != nil {
return "", nil, fmt.Errorf("%s: %w", op, err)
}
switch resourceType := i.(type) {
case ResourcePublicIder:
if resourceType.GetPublicId() == "" {
return "", nil, fmt.Errorf("%s: missing primary key: %w", op, ErrInvalidParameter)
}
fieldValues = []interface{}{resourceType.GetPublicId()}
fieldNames = []string{"public_id"}
case ResourcePrivateIder:
if resourceType.GetPrivateId() == "" {
return "", nil, fmt.Errorf("%s: missing primary key: %w", op, ErrInvalidParameter)
}
fieldValues = []interface{}{resourceType.GetPrivateId()}
fieldNames = []string{"private_id"}
default:
v := reflect.ValueOf(i)
for _, f := range tx.Statement.Schema.PrimaryFields {
if f.PrimaryKey {
val, isZero := f.ValueOf(ctx, v)
if isZero {
return "", nil, fmt.Errorf("%s: primary field %s is zero: %w", op, f.Name, ErrInvalidParameter)
}
fieldNames = append(fieldNames, f.DBName)
fieldValues = append(fieldValues, val)
}
}
}
if len(fieldNames) == 0 {
return "", nil, fmt.Errorf("%s: no primary key(s) for %t: %w", op, i, ErrInvalidParameter)
}
clauses := make([]string, 0, len(fieldNames))
for _, col := range fieldNames {
clauses = append(clauses, fmt.Sprintf("%s = ?", col))
}
return strings.Join(clauses, " and "), fieldValues, nil
}
// LookupWhere will lookup the first resource using a where clause with
// parameters (it only returns the first one). Supports WithDebug, and
// WithTable options.
func (rw *RW) LookupWhere(ctx context.Context, resource interface{}, where string, args []interface{}, opt ...Option) error {
const op = "dbw.LookupWhere"
if rw.underlying == nil {
return fmt.Errorf("%s: missing underlying db: %w", op, ErrInvalidParameter)
}
if err := validateResourcesInterface(resource); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
if err := raiseErrorOnHooks(resource); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
opts := GetOpts(opt...)
db := rw.underlying.wrapped.WithContext(ctx)
if opts.WithTable != "" {
db = db.Table(opts.WithTable)
}
if opts.WithDebug {
db = db.Debug()
}
if err := db.Where(where, args...).First(resource).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return fmt.Errorf("%s: %w", op, ErrRecordNotFound)
}
return fmt.Errorf("%s: %w", op, err)
}
return nil
}
// SearchWhere will search for all the resources it can find using a where
// clause with parameters. An error will be returned if args are provided without a
// where clause.
//
// Supports WithTable and WithLimit options. If WithLimit < 0, then unlimited results are returned.
// If WithLimit == 0, then default limits are used for results.
// Supports the WithOrder, WithTable, and WithDebug options.
func (rw *RW) SearchWhere(ctx context.Context, resources interface{}, where string, args []interface{}, opt ...Option) error {
const op = "dbw.SearchWhere"
opts := GetOpts(opt...)
if rw.underlying == nil {
return fmt.Errorf("%s: missing underlying db: %w", op, ErrInvalidParameter)
}
if where == "" && len(args) > 0 {
return fmt.Errorf("%s: args provided with empty where: %w", op, ErrInvalidParameter)
}
if err := raiseErrorOnHooks(resources); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
if err := validateResourcesInterface(resources); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
var err error
db := rw.underlying.wrapped.WithContext(ctx)
if opts.WithOrder != "" {
db = db.Order(opts.WithOrder)
}
if opts.WithDebug {
db = db.Debug()
}
if opts.WithTable != "" {
db = db.Table(opts.WithTable)
}
// Perform limiting
switch {
case opts.WithLimit < 0: // any negative number signals unlimited results
case opts.WithLimit == 0: // zero signals the default value and default limits
db = db.Limit(DefaultLimit)
default:
db = db.Limit(opts.WithLimit)
}
if where != "" {
db = db.Where(where, args...)
}
// Perform the query
err = db.Find(resources).Error
if err != nil {
// searching with a slice parameter does not return a gorm.ErrRecordNotFound
return fmt.Errorf("%s: %w", op, err)
}
return nil
}
func (rw *RW) Dialect() (_ DbType, rawName string, _ error) {
return rw.underlying.DbType()
}