From 86e878c0d2091c541984dd22b33cacaec31b3517 Mon Sep 17 00:00:00 2001 From: Johannes Rauh Date: Wed, 11 Dec 2024 13:41:19 +0100 Subject: [PATCH] Add query builder to DB --- database/db.go | 38 +++--------------------- database/delete.go | 10 +++++-- database/example_upsert_streamed_test.go | 2 +- database/upsert.go | 7 +++-- 4 files changed, 18 insertions(+), 39 deletions(-) diff --git a/database/db.go b/database/db.go index 1c8c1c7..f1ac240 100644 --- a/database/db.go +++ b/database/db.go @@ -39,6 +39,7 @@ type DB struct { Options *Options addr string + queryBuilder QueryBuilder columnMap ColumnMap logger *logging.Logger tableSemaphores map[string]*semaphore.Weighted @@ -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, @@ -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 } diff --git a/database/delete.go b/database/delete.go index 0aecfa1..a9ca4ac 100644 --- a/database/delete.go +++ b/database/delete.go @@ -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() { diff --git a/database/example_upsert_streamed_test.go b/database/example_upsert_streamed_test.go index d9b9694..01f3449 100644 --- a/database/example_upsert_streamed_test.go +++ b/database/example_upsert_streamed_test.go @@ -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 ) diff --git a/database/upsert.go b/database/upsert.go index 9566d2b..8ad34d2 100644 --- a/database/upsert.go +++ b/database/upsert.go @@ -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](