Versions in this module Expand all Collapse all v1 v1.0.1 Feb 15, 2024 v1.0.0 Feb 15, 2024 Changes in this version + const ContextKeyDatabaseTransaction + const ContextKeyDoTxnCallerSkip + var ErrAlreadyIterated = errors.New("this iterator has been already iterated") + var ErrForeignTables = errors.New("the database contains foreign tables") + var ErrNotOwned = errors.New("the database is owned by") + var ErrTxn = errors.New("transaction") + var ErrTxnBegin = fmt.Errorf("%w: begin", ErrTxn) + var ErrTxnCommit = fmt.Errorf("%w: commit", ErrTxn) + var ErrUnsupportedDatabaseVersion = errors.New("unsupported database schema version") + var ErrUnsupportedDialect = errors.New("unsupported database dialect") + func NumPtr[T constraints.Integer | constraints.Float](val T) *T + func ScanDataStruct[T NewableDataStruct[T]](rows Scannable) (T, error) + func ScanSingleColumn[T any](rows Scannable) (val T, err error) + func StrPtr[T ~string](val T) *string + func ValueOrErr[T any](val *T, err error) (*T, error) + type Config struct + ReadOnlyPool PoolConfig + type DataStruct interface + Scan func(row Scannable) (T, error) + type Database struct + Dialect Dialect + IgnoreForeignTables bool + IgnoreUnsupportedDatabase bool + Log DatabaseLogger + LoggingDB loggingDB + Owner string + RawDB *sql.DB + ReadOnlyDB *sql.DB + UpgradeTable UpgradeTable + VersionTable string + func NewFromConfig(owner string, cfg Config, logger DatabaseLogger) (*Database, error) + func NewWithDB(db *sql.DB, rawDialect string) (*Database, error) + func NewWithDialect(uri, rawDialect string) (*Database, error) + func (db *Database) BeginTx(ctx context.Context, opts *sql.TxOptions) (*LoggingTxn, error) + func (db *Database) Child(versionTable string, upgradeTable UpgradeTable, log DatabaseLogger) *Database + func (db *Database) Close() error + func (db *Database) ColumnExists(ctx context.Context, table, column string) (exists bool, err error) + func (db *Database) Configure(cfg Config) error + func (db *Database) Conn(ctx context.Context) Execable + func (db *Database) DoTxn(ctx context.Context, opts *sql.TxOptions, fn func(ctx context.Context) error) error + func (db *Database) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) + func (db *Database) Query(ctx context.Context, query string, args ...any) (Rows, error) + func (db *Database) QueryRow(ctx context.Context, query string, args ...any) *sql.Row + func (db *Database) TableExists(ctx context.Context, table string) (exists bool, err error) + func (db *Database) Upgrade(ctx context.Context) error + type DatabaseLogger interface + DoUpgrade func(from, to int, message string, txn bool) + PrepareUpgrade func(current, compat, latest int) + QueryTiming func(ctx context.Context, method, query string, args []any, nrows int, ...) + Warn func(msg string, args ...any) + WarnUnsupportedVersion func(current, compat, latest int) + var NoopLogger DatabaseLogger = &noopLogger{} + func ZeroLogger(log zerolog.Logger, cfg ...ZeroLogSettings) DatabaseLogger + func ZeroLoggerPtr(log *zerolog.Logger, cfg ...ZeroLogSettings) DatabaseLogger + type Dialect int + const DialectUnknown + const Postgres + const SQLite + func ParseDialect(engine string) (Dialect, error) + func (dialect Dialect) String() string + type Execable interface + ExecContext func(ctx context.Context, query string, args ...any) (sql.Result, error) + QueryContext func(ctx context.Context, query string, args ...any) (Rows, error) + QueryRowContext func(ctx context.Context, query string, args ...any) *sql.Row + type JSON struct + Data any + func (j JSON) Scan(i any) error + func (j JSON) Value() (driver.Value, error) + type LoggingExecable struct + UnderlyingExecable UnderlyingExecable + func (le *LoggingExecable) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) + func (le *LoggingExecable) QueryContext(ctx context.Context, query string, args ...any) (Rows, error) + func (le *LoggingExecable) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row + type LoggingRows struct + func (lrs *LoggingRows) Close() error + func (lrs *LoggingRows) ColumnTypes() ([]*sql.ColumnType, error) + func (lrs *LoggingRows) Columns() ([]string, error) + func (lrs *LoggingRows) Err() error + func (lrs *LoggingRows) Next() bool + func (lrs *LoggingRows) NextResultSet() bool + func (lrs *LoggingRows) Scan(dest ...any) error + type LoggingTxn struct + EndTime time.Time + StartTime time.Time + UnderlyingTx *sql.Tx + func (lt *LoggingTxn) Commit() error + func (lt *LoggingTxn) Rollback() error + type NewableDataStruct interface + New func() T + type PQErrorWithLine struct + Line string + Underlying error + func (pqe *PQErrorWithLine) Error() string + func (pqe *PQErrorWithLine) Unwrap() error + type PoolConfig struct + ConnMaxIdleTime string + ConnMaxLifetime string + MaxIdleConns int + MaxOpenConns int + Type string + URI string + type QueryHelper struct + func MakeQueryHelper[T DataStruct[T]](db *Database, new func(qh *QueryHelper[T]) T) *QueryHelper[T] + func (qh *QueryHelper[T]) Exec(ctx context.Context, query string, args ...any) error + func (qh *QueryHelper[T]) GetDB() *Database + func (qh *QueryHelper[T]) New() T + func (qh *QueryHelper[T]) QueryMany(ctx context.Context, query string, args ...any) ([]T, error) + func (qh *QueryHelper[T]) QueryOne(ctx context.Context, query string, args ...any) (val T, err error) + type RowIter interface + AsList func() ([]T, error) + Iter func(func(T) (bool, error)) error + func NewRowIterWithError[T any](rows Rows, convertFn func(Scannable) (T, error), err error) RowIter[T] + func NewRowIter[T any](rows Rows, convertFn func(Scannable) (T, error)) RowIter[T] + func NewSliceIterWithError[T any](items []T, err error) RowIter[T] + func NewSliceIter[T any](items []T) RowIter[T] + type Rows interface + Close func() error + ColumnTypes func() ([]*sql.ColumnType, error) + Columns func() ([]string, error) + Err func() error + Next func() bool + NextResultSet func() bool + Scan func(...any) error + type Scannable interface + Scan func(...any) error + type Transaction interface + Commit func() error + Rollback func() error + type UnderlyingExecable interface + ExecContext func(ctx context.Context, query string, args ...any) (sql.Result, error) + QueryContext func(ctx context.Context, query string, args ...any) (*sql.Rows, error) + QueryRowContext func(ctx context.Context, query string, args ...any) *sql.Row + type UpgradeTable []upgrade + func (ut *UpgradeTable) Register(from, to, compat int, message string, txn bool, fn upgradeFunc) + func (ut *UpgradeTable) RegisterFS(fs fullFS) + func (ut *UpgradeTable) RegisterFSPath(fs fullFS, dir string) + type ZeroLogSettings struct + Caller bool + CallerSkipFrame int + TraceLogAllQueries bool