database

package
v0.11.0-cloud1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
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

type DB struct {
	*sql.DB
	// contains filtered or unexported fields
}

DB holds the database connection

func NewConnection

func NewConnection(cfg *config.Database, slogger *slog.Logger) (*DB, error)

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) Driver

func (db *DB) Driver() string

Driver returns the underlying database driver name (e.g., sqlite3, postgres).

func (*DB) FetchFirstClause

func (db *DB) FetchFirstClause(n int) string

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

func (db *DB) InitSchema(dbSchemaPath string, slogger *slog.Logger) error

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

func (db *DB) InitSchemaSQL(ddl string, slogger *slog.Logger) error

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

func (db *DB) InsertAndReturnID(query string, args ...any) (int64, error)

InsertAndReturnID executes an INSERT query and returns the generated row ID.

func (*DB) IsDuplicateKeyError

func (db *DB) IsDuplicateKeyError(err error) bool

IsDuplicateKeyError reports whether err is a unique-constraint or duplicate-key violation for the current database driver.

func (*DB) PaginationClause

func (db *DB) PaginationClause(limit, offset int) (string, []any)

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.

func (*DB) Rebind

func (db *DB) Rebind(query string) string

Rebind converts a SQL query with `?` placeholders to the appropriate format for the current database driver. For PostgreSQL, converts `?` to `$1, $2, ...`. For SQLite, leaves `?` as-is.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL