dbutil

package
v1.0.15 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDSNRequired = errors.New("database DSN is required")

ErrDSNRequired is returned when a nil config or empty DSN is provided.

Functions

func OpenDB

func OpenDB(configDriver, dsn string, opts *DBOptions) (*sql.DB, error)

OpenDB opens a *sql.DB using the config-level driver name, resolving it to the registered database/sql driver. An optional DBOptions overrides connection pool defaults.

func ResolveDriverName

func ResolveDriverName(configDriver string) string

ResolveDriverName maps a user-facing config driver name to the database/sql registered driver name.

Types

type DBOptions

type DBOptions struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
}

DBOptions controls connection pool settings for OpenDB.

type Dialect

type Dialect interface {
	// DriverName returns the database/sql registered driver name.
	DriverName() string

	// Placeholder returns the n-th parameter placeholder (1-indexed).
	Placeholder(index int) string

	// CreateMessagesTable returns DDL for the messages table.
	CreateMessagesTable(table string) string

	// CreateAuditTable returns DDL for the audit log table.
	CreateAuditTable(table string) string

	// UpsertMessage returns an upsert statement for the messages table.
	// The statement must accept the same parameter order as the Postgres
	// original: id, correlation_id, channel_id, stage, content,
	// content_size, status, timestamp, duration_ms, metadata.
	UpsertMessage(table string) string

	// InsertAudit returns an INSERT for the audit table.
	// Parameters: timestamp, event, username, details, source_ip.
	InsertAudit(table string) string

	// MigrateColumns returns best-effort ALTER TABLE statements to add
	// columns that may be missing in older schemas.
	MigrateColumns(table string) []string

	// CreateIndexes returns CREATE INDEX statements for the messages table.
	CreateIndexes(table, prefix string) []string

	// CreateAuditIndexes returns CREATE INDEX statements for the audit table.
	CreateAuditIndexes(table, prefix string) []string
}

Dialect abstracts database-specific SQL generation so that storage and audit implementations can work across Postgres, MySQL, SQLite, and MSSQL.

func DialectFor

func DialectFor(configDriver string) (Dialect, error)

DialectFor returns the Dialect matching a config-level driver name (e.g. "postgres", "mysql", "mssql", "sqlite").

type MSSQLDialect

type MSSQLDialect struct{}

MSSQLDialect implements Dialect for Microsoft SQL Server.

func (MSSQLDialect) CreateAuditIndexes

func (MSSQLDialect) CreateAuditIndexes(table, prefix string) []string

func (MSSQLDialect) CreateAuditTable

func (MSSQLDialect) CreateAuditTable(table string) string

func (MSSQLDialect) CreateIndexes

func (MSSQLDialect) CreateIndexes(table, prefix string) []string

func (MSSQLDialect) CreateMessagesTable

func (MSSQLDialect) CreateMessagesTable(table string) string

func (MSSQLDialect) DriverName

func (MSSQLDialect) DriverName() string

func (MSSQLDialect) InsertAudit

func (MSSQLDialect) InsertAudit(table string) string

func (MSSQLDialect) MigrateColumns

func (MSSQLDialect) MigrateColumns(_ string) []string

func (MSSQLDialect) Placeholder

func (MSSQLDialect) Placeholder(index int) string

func (MSSQLDialect) UpsertMessage

func (MSSQLDialect) UpsertMessage(table string) string

type MySQLDialect

type MySQLDialect struct{}

MySQLDialect implements Dialect for MySQL.

func (MySQLDialect) CreateAuditIndexes

func (MySQLDialect) CreateAuditIndexes(table, prefix string) []string

func (MySQLDialect) CreateAuditTable

func (MySQLDialect) CreateAuditTable(table string) string

func (MySQLDialect) CreateIndexes

func (MySQLDialect) CreateIndexes(table, prefix string) []string

func (MySQLDialect) CreateMessagesTable

func (MySQLDialect) CreateMessagesTable(table string) string

func (MySQLDialect) DriverName

func (MySQLDialect) DriverName() string

func (MySQLDialect) InsertAudit

func (MySQLDialect) InsertAudit(table string) string

func (MySQLDialect) MigrateColumns

func (MySQLDialect) MigrateColumns(table string) []string

func (MySQLDialect) Placeholder

func (MySQLDialect) Placeholder(_ int) string

func (MySQLDialect) UpsertMessage

func (MySQLDialect) UpsertMessage(table string) string

type PostgresDialect

type PostgresDialect struct{}

PostgresDialect implements Dialect for PostgreSQL (via pgx).

func (PostgresDialect) CreateAuditIndexes

func (PostgresDialect) CreateAuditIndexes(table, prefix string) []string

func (PostgresDialect) CreateAuditTable

func (PostgresDialect) CreateAuditTable(table string) string

func (PostgresDialect) CreateIndexes

func (PostgresDialect) CreateIndexes(table, prefix string) []string

func (PostgresDialect) CreateMessagesTable

func (PostgresDialect) CreateMessagesTable(table string) string

func (PostgresDialect) DriverName

func (PostgresDialect) DriverName() string

func (PostgresDialect) InsertAudit

func (PostgresDialect) InsertAudit(table string) string

func (PostgresDialect) MigrateColumns

func (PostgresDialect) MigrateColumns(table string) []string

func (PostgresDialect) Placeholder

func (PostgresDialect) Placeholder(index int) string

func (PostgresDialect) UpsertMessage

func (d PostgresDialect) UpsertMessage(table string) string

type SQLiteDialect

type SQLiteDialect struct{}

SQLiteDialect implements Dialect for SQLite (modernc.org/sqlite).

func (SQLiteDialect) CreateAuditIndexes

func (SQLiteDialect) CreateAuditIndexes(table, prefix string) []string

func (SQLiteDialect) CreateAuditTable

func (SQLiteDialect) CreateAuditTable(table string) string

func (SQLiteDialect) CreateIndexes

func (SQLiteDialect) CreateIndexes(table, prefix string) []string

func (SQLiteDialect) CreateMessagesTable

func (SQLiteDialect) CreateMessagesTable(table string) string

func (SQLiteDialect) DriverName

func (SQLiteDialect) DriverName() string

func (SQLiteDialect) InsertAudit

func (SQLiteDialect) InsertAudit(table string) string

func (SQLiteDialect) MigrateColumns

func (SQLiteDialect) MigrateColumns(_ string) []string

func (SQLiteDialect) Placeholder

func (SQLiteDialect) Placeholder(_ int) string

func (SQLiteDialect) UpsertMessage

func (SQLiteDialect) UpsertMessage(table string) string

Jump to

Keyboard shortcuts

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