Skip to content

Commit

Permalink
Add query builder to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Dec 11, 2024
1 parent dbe6f5c commit 86e878c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 39 deletions.
38 changes: 4 additions & 34 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type DB struct {
Options *Options

addr string
queryBuilder QueryBuilder
columnMap ColumnMap
logger *logging.Logger
tableSemaphores map[string]*semaphore.Weighted
Expand Down Expand Up @@ -256,6 +257,7 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry
return &DB{
DB: db,
Options: &c.Options,
queryBuilder: NewQueryBuilder(db.DriverName()),
columnMap: NewColumnMap(db.Mapper),
addr: addr,
logger: logger,
Expand Down Expand Up @@ -894,38 +896,6 @@ func (db *DB) Log(ctx context.Context, query string, counter *com.Counter) perio
}))
}

func BuildUpsertStatement(db *DB, stmt UpsertStatement) (string, int, error) {
return NewQueryBuilder(db.DriverName()).UpsertStatement(stmt)
}

func BuildInsertStatement(db *DB, stmt InsertStatement) string {
return NewQueryBuilder(db.DriverName()).InsertStatement(stmt)
}

func BuildInsertIgnoreStatement(db *DB, stmt InsertStatement) (string, error) {
return NewQueryBuilder(db.DriverName()).InsertIgnoreStatement(stmt)
}

func BuildInsertSelectStatement(db *DB, stmt InsertSelectStatement) (string, error) {
return NewQueryBuilder(db.DriverName()).InsertSelectStatement(stmt)
}

func BuildSelectStatement(db *DB, stmt SelectStatement) string {
return NewQueryBuilder(db.DriverName()).SelectStatement(stmt)
}

func BuildUpdateStatement(db *DB, stmt UpdateStatement) (string, error) {
return NewQueryBuilder(db.DriverName()).UpdateStatement(stmt)
}

func BuildUpdateAllStatement(db *DB, stmt UpdateStatement) (string, error) {
return NewQueryBuilder(db.DriverName()).UpdateAllStatement(stmt)
}

func BuildDeleteStatement(db *DB, stmt DeleteStatement) (string, error) {
return NewQueryBuilder(db.DriverName()).DeleteStatement(stmt)
}

func BuildDeleteAllStatement(db *DB, stmt DeleteStatement) (string, error) {
return NewQueryBuilder(db.DriverName()).DeleteAllStatement(stmt)
func (db *DB) QueryBuilder() QueryBuilder {
return db.queryBuilder
}
10 changes: 8 additions & 2 deletions database/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,15 @@ func DeleteStreamed(
var stmt string

if opts.stmt != nil {
stmt, _ = BuildDeleteStatement(db, opts.stmt)
stmt, err = db.QueryBuilder().DeleteStatement(opts.stmt)
if err != nil {
return err
}
} else {
stmt = db.BuildDeleteStmt(entityType)
stmt, err = db.QueryBuilder().DeleteStatement(NewDeleteStatement(entityType))
if err != nil {
return err
}
}

switch reflect.TypeOf(first).Kind() {
Expand Down
2 changes: 1 addition & 1 deletion database/example_upsert_streamed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func ExampleUpsertStreamed() {
g, ctx = errgroup.WithContext(context.Background())
entities = make(chan User, len(testEntites))
logs = getTestLogging()
log = logs.GetLogger()
db = getTestDb(logs)
log = logs.GetLogger()
err error
)

Expand Down
7 changes: 5 additions & 2 deletions database/upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,15 @@ func UpsertStreamed[T any, V EntityConstraint[T]](
}

if opts.stmt != nil {
stmt, placeholders, err = BuildUpsertStatement(db, opts.stmt)
stmt, placeholders, err = db.QueryBuilder().UpsertStatement(opts.stmt)
if err != nil {
return err
}
} else {
stmt, placeholders = db.BuildUpsertStmt(entityType)
stmt, placeholders, err = db.QueryBuilder().UpsertStatement(NewUpsertStatement(entityType))
if err != nil {
return err
}
}

return namedBulkExec[T](
Expand Down

0 comments on commit 86e878c

Please sign in to comment.