generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.go
590 lines (515 loc) · 20.3 KB
/
models.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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package datastore
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/mrz1836/go-datastore/nrgorm"
"github.com/newrelic/go-agent/v3/newrelic"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/plugin/dbresolver"
)
// SaveModel will handle creating or updating a model based on its primary key, abstracting the database operations.
// It supports both SQL and MongoDB engines. For MongoDB, it uses a session context for transaction support if available.
// For SQL databases, it uses GORM to create or update the table schema.
//
// Parameters:
// - ctx: The context for the save operation, used for logging and tracing.
// - model: A pointer to the model to be saved.
// - tx: The transaction object to be used for the save operation.
// - newRecord: A boolean indicating whether the model is a new record (true) or an existing record (false).
// - commitTx: A boolean indicating whether to commit the transaction after saving the model.
//
// Returns:
// - An error if the save operation fails.
//
// The function performs the following steps:
// 1. Checks the database engine and handles MongoDB separately as it does not support transactions.
// 2. Sets the NewRelic transaction to the GORM database if using SQL.
// 3. Captures any panics during the save operation and rolls back the transaction if a panic occurs.
// 4. For new records, it creates the model in the database, omitting associations.
// 5. For existing records, it updates the model in the database, omitting associations.
// 6. Commits the transaction if commitTx is true.
// 7. Returns any errors encountered during the save operation.
func (c *Client) SaveModel(
ctx context.Context,
model interface{},
tx *Transaction,
newRecord, commitTx bool,
) error {
// MongoDB (does not support transactions at this time)
if c.Engine() == MongoDB {
sessionContext := ctx //nolint:contextcheck // we need to overwrite the ctx for transaction support
if tx.mongoTx != nil {
// set the context to the session context -> mongo transaction
sessionContext = *tx.mongoTx
}
return c.saveWithMongo(sessionContext, model, newRecord)
} else if !IsSQLEngine(c.Engine()) {
return ErrUnsupportedEngine
}
// Set the NewRelic txn
c.options.db = nrgorm.SetTxnToGorm(newrelic.FromContext(ctx), c.options.db)
// Capture any panics
defer func() {
if r := recover(); r != nil {
c.DebugLog(context.Background(), fmt.Sprintf("panic recovered: %v", r))
_ = tx.Rollback()
}
}()
if err := tx.sqlTx.Error; err != nil {
return err
}
// Create vs Update
if newRecord {
if err := tx.sqlTx.Omit(clause.Associations).Create(model).Error; err != nil {
_ = tx.Rollback()
// todo add duplicate key check for MySQL, Postgres and SQLite
return err
}
} else {
if err := tx.sqlTx.Omit(clause.Associations).Save(model).Error; err != nil {
_ = tx.Rollback()
return err
}
}
// Commit & check for errors
if commitTx {
if err := tx.Commit(); err != nil {
return err
}
}
// Return the tx
return nil
}
// IncrementModel will increment the given field atomically in the database and return the new value.
// It supports both SQL and MongoDB engines. For MongoDB, it uses a session context for transaction support if available.
// For SQL databases, it uses GORM to perform the increment operation within a transaction.
//
// Parameters:
// - ctx: The context for the increment operation, used for logging and tracing.
// - model: A pointer to the model to be incremented.
// - fieldName: The name of the field to be incremented.
// - increment: The value by which to increment the field.
//
// Returns:
// - newValue: The new value of the incremented field.
// - err: An error if the increment operation fails.
//
// The function performs the following steps:
// 1. Checks the database engine and handles MongoDB separately as it does not support transactions.
// 2. Sets the NewRelic transaction to the GORM database if using SQL.
// 3. Creates a new transaction and locks the row for update to ensure atomicity.
// 4. Retrieves the current value of the field and increments it by the specified amount.
// 5. Updates the field with the new value in the database.
// 6. Returns the new value and any errors encountered during the increment operation.
func (c *Client) IncrementModel(
ctx context.Context,
model interface{},
fieldName string,
increment int64,
) (newValue int64, err error) {
if c.Engine() == MongoDB {
return c.incrementWithMongo(ctx, model, fieldName, increment)
} else if !IsSQLEngine(c.Engine()) {
return 0, ErrUnsupportedEngine
}
// Set the NewRelic txn
c.options.db = nrgorm.SetTxnToGorm(newrelic.FromContext(ctx), c.options.db)
// Create a new transaction
if err = c.options.db.Transaction(func(tx *gorm.DB) error {
// Get the id of the model
id := GetModelStringAttribute(model, sqlIDFieldProper)
if id == nil {
return errors.New("model is missing an " + sqlIDFieldProper + " field")
}
// Get model if exist
var result map[string]interface{}
if err = tx.Model(&model).Clauses(clause.Locking{Strength: "UPDATE"}).Where(sqlIDField+" = ?", id).First(&result).Error; err != nil {
return err
}
if result == nil {
newValue = increment
return nil
}
// Increment Counter
newValue = convertToInt64(result[fieldName]) + increment
return tx.Model(&model).Where(sqlIDField+" = ?", id).Update(fieldName, newValue).Error
}); err != nil {
return
}
return
}
// CreateInBatches creates all the models given in batches, supporting both SQL and MongoDB engines.
// For MongoDB, it uses a session context for transaction support if available. For SQL databases,
// it uses GORM to perform the batch creation.
//
// Parameters:
// - ctx: The context for the batch creation operation, used for logging and tracing.
// - models: A slice of models to be created in batches.
// - batchSize: The number of models to include in each batch.
//
// Returns:
// - An error if the batch creation operation fails.
//
// The function performs the following steps:
// 1. Checks the database engine and handles MongoDB separately as it does not support transactions.
// 2. For SQL databases, it uses GORM's CreateInBatches method to insert the models in batches.
// 3. Returns any errors encountered during the batch creation operation.
func (c *Client) CreateInBatches(
ctx context.Context,
models interface{},
batchSize int,
) error {
if c.Engine() == MongoDB {
return c.CreateInBatchesMongo(ctx, models, batchSize)
}
tx := c.options.db.CreateInBatches(models, batchSize)
return tx.Error
}
// convertToInt64 will convert an interface to an int64
func convertToInt64(i interface{}) int64 {
switch v := i.(type) {
case int:
return int64(v)
case int32:
return int64(v)
case uint32:
return int64(v)
case uint64:
return int64(v)
}
return i.(int64)
}
type gormWhere struct {
tx *gorm.DB
}
// Where will help fire the tx.Where method
func (g *gormWhere) Where(query interface{}, args ...interface{}) {
g.tx.Where(query, args...)
}
// getGormTx returns the GORM db tx
func (g *gormWhere) getGormTx() *gorm.DB {
return g.tx
}
// GetModel will retrieve a single model from the datastore based on the provided conditions.
// It supports both SQL and MongoDB engines. For MongoDB, it uses a session context for transaction support if available.
// For SQL databases, it uses GORM to perform the query.
//
// Parameters:
// - ctx: The context for the retrieval operation, used for logging and tracing.
// - model: A pointer to the model to be retrieved.
// - conditions: A map of conditions to filter the query.
// - timeout: The duration to wait before timing out the query.
// - forceWriteDB: A boolean indicating whether to force the query to use the write database (only applicable for MySQL and PostgreSQL).
//
// Returns:
// - An error if the retrieval operation fails or if no results are found.
//
// The function performs the following steps:
// 1. Checks the database engine and handles MongoDB separately as it does not support transactions.
// 2. Sets the NewRelic transaction to the GORM database if using SQL.
// 3. Creates a new context and database transaction with the specified timeout.
// 4. Constructs the query based on the provided conditions and executes it.
// 5. If forceWriteDB is true, it uses the write database for the query (only for MySQL and PostgreSQL).
// 6. Returns any errors encountered during the retrieval operation or if no results are found.
func (c *Client) GetModel(
ctx context.Context,
model interface{},
conditions map[string]interface{},
timeout time.Duration,
forceWriteDB bool,
) error {
// Switch on the datastore engines
if c.Engine() == MongoDB { // Get using Mongo
return c.getWithMongo(ctx, model, conditions, nil, nil)
} else if !IsSQLEngine(c.Engine()) {
return ErrUnsupportedEngine
}
// Set the NewRelic txn
c.options.db = nrgorm.SetTxnToGorm(newrelic.FromContext(ctx), c.options.db)
// Create a new context, and new db tx
ctxDB, cancel := createCtx(ctx, c.options.db, timeout, c.IsDebug(), c.options.loggerDB)
defer cancel()
// Get the model data using a select
// todo: optimize by specific fields
var tx *gorm.DB
if forceWriteDB { // Use the "write" database for this query (Only MySQL and Postgres)
if c.Engine() == MySQL || c.Engine() == PostgreSQL {
tx = ctxDB.Clauses(dbresolver.Write).Select("*")
} else {
tx = ctxDB.Select("*")
}
} else { // Use a replica if found
tx = ctxDB.Select("*")
}
// Add conditions
if len(conditions) > 0 {
gtx := gormWhere{tx: tx}
return checkResult(c.CustomWhere(>x, conditions, c.Engine()).(*gorm.DB).Find(model))
}
return checkResult(tx.Find(model))
}
// GetModels will return a slice of models based on the given conditions and query parameters.
// It supports both SQL and MongoDB engines. For MongoDB, it uses a session context for transaction support if available.
// For SQL databases, it uses GORM to perform the query.
//
// Parameters:
// - ctx: The context for the retrieval operation, used for logging and tracing.
// - models: A pointer to a slice of models to be retrieved.
// - conditions: A map of conditions to filter the query.
// - queryParams: A pointer to QueryParams struct containing pagination and sorting information.
// - fieldResults: A pointer to a slice where the results will be stored if not nil.
// - timeout: The duration to wait before timing out the query.
//
// Returns:
// - An error if the retrieval operation fails or if no results are found.
//
// The function performs the following steps:
// 1. Initializes default values for queryParams if not provided.
// 2. Checks the database engine and handles MongoDB separately as it does not support transactions.
// 3. Sets the NewRelic transaction to the GORM database if using SQL.
// 4. Creates a new context and database transaction with the specified timeout.
// 5. Constructs the query based on the provided conditions, pagination, and sorting information.
// 6. Executes the query and stores the results in the provided models or fieldResults slice.
// 7. Returns any errors encountered during the retrieval operation or if no results are found.
func (c *Client) GetModels(
ctx context.Context,
models interface{},
conditions map[string]interface{},
queryParams *QueryParams,
fieldResults interface{},
timeout time.Duration,
) error {
if queryParams == nil {
// init a new empty object for the default queryParams
queryParams = &QueryParams{}
}
// Set default page size
if queryParams.Page > 0 && queryParams.PageSize < 1 {
queryParams.PageSize = defaultPageSize
}
// lower case the sort direction (asc / desc)
queryParams.SortDirection = strings.ToLower(queryParams.SortDirection)
// Switch on the datastore engines
if c.Engine() == MongoDB { // Get using Mongo
return c.getWithMongo(ctx, models, conditions, fieldResults, queryParams)
} else if !IsSQLEngine(c.Engine()) {
return ErrUnsupportedEngine
}
return c.find(ctx, models, conditions, queryParams, fieldResults, timeout)
}
// GetModelCount will return a count of the models matching the provided conditions.
// It supports both SQL and MongoDB engines. For MongoDB, it uses a session context for transaction support if available.
// For SQL databases, it uses GORM to perform the count operation.
//
// Parameters:
// - ctx: The context for the count operation, used for logging and tracing.
// - model: A pointer to the model type for which the count is to be retrieved.
// - conditions: A map of conditions to filter the count query.
// - timeout: The duration to wait before timing out the query.
//
// Returns:
// - count: The number of models matching the provided conditions.
// - err: An error if the count operation fails.
//
// The function performs the following steps:
// 1. Checks the database engine and handles MongoDB separately as it does not support transactions.
// 2. Sets the NewRelic transaction to the GORM database if using SQL.
// 3. Creates a new context and database transaction with the specified timeout.
// 4. Constructs the count query based on the provided conditions and executes it.
// 5. Returns the count of models and any errors encountered during the count operation.
func (c *Client) GetModelCount(
ctx context.Context,
model interface{},
conditions map[string]interface{},
timeout time.Duration,
) (int64, error) {
// Switch on the datastore engines
if c.Engine() == MongoDB {
return c.countWithMongo(ctx, model, conditions)
} else if !IsSQLEngine(c.Engine()) {
return 0, ErrUnsupportedEngine
}
return c.count(ctx, model, conditions, timeout)
}
// GetModelsAggregate will return an aggregate count of the model matching conditions.
// It supports both SQL and MongoDB engines. For MongoDB, it uses a session context for transaction support if available.
// For SQL databases, it uses GORM to perform the aggregate operation.
//
// Parameters:
// - ctx: The context for the aggregate operation, used for logging and tracing.
// - models: A pointer to a slice of models to be aggregated.
// - conditions: A map of conditions to filter the aggregate query.
// - aggregateColumn: The name of the column to aggregate.
// - timeout: The duration to wait before timing out the query.
//
// Returns:
// - result: A map where the keys are the aggregated column values and the values are the counts of models matching the conditions.
// - err: An error if the aggregate operation fails.
//
// The function performs the following steps:
// 1. Checks the database engine and handles MongoDB separately as it does not support transactions.
// 2. Sets the NewRelic transaction to the GORM database if using SQL.
// 3. Creates a new context and database transaction with the specified timeout.
// 4. Constructs the aggregate query based on the provided conditions and executes it.
// 5. For date fields, formats the date according to the database engine.
// 6. Returns the aggregate result and any errors encountered during the aggregate operation.
func (c *Client) GetModelsAggregate(ctx context.Context, models interface{},
conditions map[string]interface{}, aggregateColumn string, timeout time.Duration) (map[string]interface{}, error) {
// Switch on the datastore engines
if c.Engine() == MongoDB {
return c.aggregateWithMongo(ctx, models, conditions, aggregateColumn, timeout)
} else if !IsSQLEngine(c.Engine()) {
return nil, ErrUnsupportedEngine
}
return c.aggregate(ctx, models, conditions, aggregateColumn, timeout)
}
// find will get records and return
func (c *Client) find(ctx context.Context, result interface{}, conditions map[string]interface{},
queryParams *QueryParams, fieldResults interface{}, timeout time.Duration) error {
// Find the type
if reflect.TypeOf(result).Elem().Kind() != reflect.Slice {
return errors.New("field: result is not a slice, found: " + reflect.TypeOf(result).Kind().String())
}
// Set the NewRelic txn
c.options.db = nrgorm.SetTxnToGorm(newrelic.FromContext(ctx), c.options.db)
// Create a new context, and new db tx
ctxDB, cancel := createCtx(ctx, c.options.db, timeout, c.IsDebug(), c.options.loggerDB)
defer cancel()
tx := ctxDB.Model(result)
// Create the offset
offset := (queryParams.Page - 1) * queryParams.PageSize
// Use the limit and offset
if queryParams.Page > 0 && queryParams.PageSize > 0 {
tx = tx.Limit(queryParams.PageSize).Offset(offset)
}
// Use an order field/sort
if len(queryParams.OrderByField) > 0 {
tx = tx.Order(clause.OrderByColumn{
Column: clause.Column{
Name: queryParams.OrderByField,
},
Desc: strings.ToLower(queryParams.SortDirection) == SortDesc,
})
}
// Check for errors or no records found
if len(conditions) > 0 {
gtx := gormWhere{tx: tx}
if fieldResults != nil {
return checkResult(c.CustomWhere(>x, conditions, c.Engine()).(*gorm.DB).Find(fieldResults))
}
return checkResult(c.CustomWhere(>x, conditions, c.Engine()).(*gorm.DB).Find(result))
}
// Skip the conditions
if fieldResults != nil {
return checkResult(tx.Find(fieldResults))
}
return checkResult(tx.Find(result))
}
// find will get records and return
func (c *Client) count(ctx context.Context, model interface{}, conditions map[string]interface{},
timeout time.Duration) (int64, error) {
// Set the NewRelic txn
c.options.db = nrgorm.SetTxnToGorm(newrelic.FromContext(ctx), c.options.db)
// Create a new context, and new db tx
ctxDB, cancel := createCtx(ctx, c.options.db, timeout, c.IsDebug(), c.options.loggerDB)
defer cancel()
tx := ctxDB.Model(model)
// Check for errors or no records found
if len(conditions) > 0 {
gtx := gormWhere{tx: tx}
var count int64
err := checkResult(c.CustomWhere(>x, conditions, c.Engine()).(*gorm.DB).Model(model).Count(&count))
return count, err
}
var count int64
err := checkResult(tx.Count(&count))
return count, err
}
// find will get records and return
func (c *Client) aggregate(ctx context.Context, model interface{}, conditions map[string]interface{},
aggregateColumn string, timeout time.Duration) (map[string]interface{}, error) {
// Find the type
if reflect.TypeOf(model).Elem().Kind() != reflect.Slice {
return nil, errors.New("field: result is not a slice, found: " + reflect.TypeOf(model).Kind().String())
}
// Set the NewRelic txn
c.options.db = nrgorm.SetTxnToGorm(newrelic.FromContext(ctx), c.options.db)
// Create a new context, and new db tx
ctxDB, cancel := createCtx(ctx, c.options.db, timeout, c.IsDebug(), c.options.loggerDB)
defer cancel()
// Get the tx
tx := ctxDB.Model(model)
// Check for errors or no records found
var aggregate []map[string]interface{}
if len(conditions) > 0 {
gtx := gormWhere{tx: tx}
err := checkResult(c.CustomWhere(>x, conditions, c.Engine()).(*gorm.DB).Model(model).Group(aggregateColumn).Scan(&aggregate))
if err != nil {
return nil, err
}
} else {
aggregateCol := aggregateColumn
// Check for a known date field
if StringInSlice(aggregateCol, DateFields) {
if c.Engine() == MySQL {
aggregateCol = "DATE_FORMAT(" + aggregateCol + ", '%Y%m%d')"
} else if c.Engine() == Postgres {
aggregateCol = "to_char(" + aggregateCol + ", 'YYYYMMDD')"
} else {
aggregateCol = "strftime('%Y%m%d', " + aggregateCol + ")"
}
}
err := checkResult(tx.Select(aggregateCol + " as _id, COUNT(id) AS count").Group(aggregateCol).Scan(&aggregate))
if err != nil {
return nil, err
}
}
// Create the result
aggregateResult := make(map[string]interface{})
for _, item := range aggregate {
key := item[mongoIDField].(string)
aggregateResult[key] = item[accumulationCountField]
}
return aggregateResult, nil
}
// Execute a SQL query
func (c *Client) Execute(query string) *gorm.DB {
if IsSQLEngine(c.Engine()) {
return c.options.db.Exec(query)
}
return nil
}
// Raw a raw SQL query
func (c *Client) Raw(query string) *gorm.DB {
if IsSQLEngine(c.Engine()) {
return c.options.db.Raw(query)
}
return nil
}
// checkResult will check for records or error
func checkResult(result *gorm.DB) error {
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return ErrNoResults
}
return result.Error
}
// We should actually have some rows according to GORM
if result.RowsAffected == 0 {
return ErrNoResults
}
return nil
}
// createCtx will make a new DB context
func createCtx(ctx context.Context, db *gorm.DB, timeout time.Duration, debug bool,
optionalLogger logger.Interface) (*gorm.DB, context.CancelFunc) {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
return db.Session(getGormSessionConfig(db.PrepareStmt, debug, optionalLogger)).WithContext(ctx), cancel
}