Documentation
¶
Index ¶
- Constants
- type DB
- func (db *DB) BuildUpsertQuery(table string, insertCols []string, conflictCols []string, updateExprs []string) string
- func (db *DB) Driver() string
- func (db *DB) FetchFirstClause(n int) string
- func (db *DB) InitSchema(dbSchemaPath string, slogger *slog.Logger) error
- func (db *DB) InitSchemaSQL(ddl string, slogger *slog.Logger) error
- func (db *DB) InsertAndReturnID(query string, args ...any) (int64, error)
- func (db *DB) IsDuplicateKeyError(err error) bool
- func (db *DB) PaginationClause(limit, offset int) (string, []any)
- func (db *DB) Rebind(query string) string
Constants ¶
const ( // DriverSQLite is the driver name for SQLite3 DriverSQLite = "sqlite3" // DriverPostgres is the canonical driver name for PostgreSQL DriverPostgres = "postgres" // DriverPGX is the canonical driver name for PostgreSQL DriverPGX = "pgx" // DriverPostgreSQL is an alternate name for PostgreSQL (accepted in config) DriverPostgreSQL = "postgresql" // DriverSQLServer is the canonical driver name for SQL Server DriverSQLServer = "sqlserver" // DriverMSSQL is an alternate SQL Server driver name (accepted in config) DriverMSSQL = "mssql" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
DB holds the database connection
func NewConnection ¶
NewConnection creates a new database connection using configuration
func (*DB) BuildUpsertQuery ¶
func (db *DB) BuildUpsertQuery(table string, insertCols []string, conflictCols []string, updateExprs []string) string
BuildUpsertQuery generates a dialect-appropriate INSERT … ON CONFLICT … DO UPDATE query. insertCols are all columns being inserted (each maps to one ? placeholder). conflictCols are the columns that define uniqueness. updateExprs control what happens on conflict: "col" → col = excluded.col, "col=NULL" → col = NULL.
func (*DB) FetchFirstClause ¶
FetchFirstClause returns a row-limiting clause for a fixed number of rows, safe to embed directly into a query string (n is an integer constant, not user input). As with PaginationClause, SQL Server requires the statement to carry an ORDER BY; add "ORDER BY (SELECT NULL)" when ordering is irrelevant.
func (*DB) InitSchema ¶
InitSchema initializes the database schema Automatically selects the appropriate schema file based on the database driver. If dbSchemaPath is provided (e.g., "./internal/database/schema.sql"), it will be used to derive the directory and then select schema.{driver}.sql
func (*DB) InitSchemaSQL ¶
InitSchemaSQL applies the given SQL DDL string against the database using the same strategy as InitSchema. It is used by compile-time plugins to apply their own schema fragments without requiring an external schema file.
func (*DB) InsertAndReturnID ¶
InsertAndReturnID executes an INSERT query and returns the generated row ID.
func (*DB) IsDuplicateKeyError ¶
IsDuplicateKeyError reports whether err is a unique-constraint or duplicate-key violation for the current database driver.
func (*DB) PaginationClause ¶
PaginationClause returns a dialect-appropriate row-limiting clause to append after an ORDER BY, together with its bind arguments in the order the clause expects them. SQL Server has no LIMIT keyword and instead uses ANSI OFFSET/FETCH, which (a) requires an ORDER BY in the statement and (b) lists OFFSET before the row count — the reverse of "LIMIT ? OFFSET ?". The returned clause uses ? placeholders; pass the assembled query through Rebind as usual.