Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/bitcoin-sv/spv-wallet int…
Browse files Browse the repository at this point in the history
…o BUX-246/Linters
  • Loading branch information
Nazarii-4chain committed Apr 5, 2024
2 parents 66ac26e + e06f774 commit d804335
Show file tree
Hide file tree
Showing 8 changed files with 877 additions and 745 deletions.
38 changes: 38 additions & 0 deletions engine/datastore/column_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package datastore

import (
"sync"

"gorm.io/gorm"
"gorm.io/gorm/schema"
)

var modelsCache = sync.Map{}

// GetColumnName checks if the model has provided columnName as DBName or (struct field)Name
// Returns (DBName, true) if the column exists otherwise (_, false)
// Uses global cache store (thread safe)
// Checking is case-sensitive
// The gdb param is optional. When is provided, the actual naming strategy is used; otherwise default
func GetColumnName(columnName string, model interface{}, gdb *gorm.DB) (string, bool) {
var namer schema.Namer
if gdb != nil {
namer = gdb.NamingStrategy
} else {
namer = schema.NamingStrategy{}
}

sch, err := schema.Parse(model, &modelsCache, namer)
if err != nil {
return "", false
}
if field, ok := sch.FieldsByDBName[columnName]; ok {
return field.DBName, true
}

if field, ok := sch.FieldsByName[columnName]; ok {
return field.DBName, true
}

return "", false
}
1 change: 0 additions & 1 deletion engine/datastore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
type StorageService interface {
AutoMigrateDatabase(ctx context.Context, models ...interface{}) error
CreateInBatches(ctx context.Context, models interface{}, batchSize int) error
CustomWhere(tx CustomWhereInterface, conditions map[string]interface{}, engine Engine) interface{}
Execute(query string) *gorm.DB
GetModel(ctx context.Context, model interface{}, conditions map[string]interface{},
timeout time.Duration, forceWriteDB bool) error
Expand Down
61 changes: 22 additions & 39 deletions engine/datastore/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,6 @@ func convertToInt64(i interface{}) int64 {
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 get a model from the datastore
func (c *Client) GetModel(
ctx context.Context,
Expand All @@ -176,23 +162,19 @@ func (c *Client) GetModel(
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("*")
tx := ctxDB.Model(model)

if forceWriteDB && (c.Engine() == MySQL || c.Engine() == PostgreSQL) {
tx = ctxDB.Clauses(dbresolver.Write)
}

// Add conditions
tx = tx.Select("*") // todo: optimize by specific fields

if len(conditions) > 0 {
gtx := gormWhere{tx: tx}
return checkResult(c.CustomWhere(&gtx, conditions, c.Engine()).(*gorm.DB).Find(model))
var err error
if tx, err = ApplyCustomWhere(c, tx, conditions, model); err != nil {
return err
}
}

return checkResult(tx.Find(model))
Expand Down Expand Up @@ -292,13 +274,11 @@ func (c *Client) find(ctx context.Context, result interface{}, conditions map[st
})
}

// Check for errors or no records found
if len(conditions) > 0 {
gtx := gormWhere{tx: tx}
if fieldResults != nil {
return checkResult(c.CustomWhere(&gtx, conditions, c.Engine()).(*gorm.DB).Find(fieldResults))
var err error
if tx, err = ApplyCustomWhere(c, tx, conditions, result); err != nil {
return err
}
return checkResult(c.CustomWhere(&gtx, conditions, c.Engine()).(*gorm.DB).Find(result))
}

// Skip the conditions
Expand All @@ -320,10 +300,10 @@ func (c *Client) count(ctx context.Context, model interface{}, conditions map[st

// Check for errors or no records found
if len(conditions) > 0 {
gtx := gormWhere{tx: tx}
var count int64
err := checkResult(c.CustomWhere(&gtx, conditions, c.Engine()).(*gorm.DB).Model(model).Count(&count))
return count, err
var err error
if tx, err = ApplyCustomWhere(c, tx, conditions, model); err != nil {
return 0, err
}
}
var count int64
err := checkResult(tx.Count(&count))
Expand All @@ -350,8 +330,11 @@ func (c *Client) aggregate(ctx context.Context, model interface{}, conditions ma
// Check for errors or no records found
var aggregate []map[string]interface{}
if len(conditions) > 0 {
gtx := gormWhere{tx: tx}
err := checkResult(c.CustomWhere(&gtx, conditions, c.Engine()).(*gorm.DB).Model(model).Group(aggregateColumn).Scan(&aggregate))
var err error
if tx, err = ApplyCustomWhere(c, tx, conditions, model); err != nil {
return nil, err
}
err = checkResult(tx.Group(aggregateColumn).Scan(&aggregate))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit d804335

Please sign in to comment.