package dbutil
import "go.mau.fi/util/dbutil"
Index ¶
- Constants
- Variables
- type Config
- type ContextExecable
- type Database
- 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 (ld *Database) Begin() (*LoggingTxn, error)
- func (ld *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(tx Execable, table, column string) (exists bool, err error)
- func (db *Database) Configure(cfg Config) error
- func (db *Database) Conn(ctx context.Context) ContextExecable
- func (db *Database) DoTxn(ctx context.Context, opts *sql.TxOptions, fn func(ctx context.Context) error) error
- func (db *Database) TableExists(tx Execable, table string) (exists bool, err error)
- func (db *Database) Upgrade() error
- type DatabaseLogger
- func ZeroLogger(log zerolog.Logger, cfg ...ZeroLogSettings) DatabaseLogger
- func ZeroLoggerPtr(log *zerolog.Logger, cfg ...ZeroLogSettings) DatabaseLogger
- type Dialect
- type Execable
- type JSON
- type LoggingExecable
- func (le *LoggingExecable) Exec(query string, args ...interface{}) (sql.Result, error)
- func (le *LoggingExecable) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (le *LoggingExecable) Query(query string, args ...interface{}) (Rows, error)
- func (le *LoggingExecable) QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error)
- func (le *LoggingExecable) QueryRow(query string, args ...interface{}) *sql.Row
- func (le *LoggingExecable) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- type LoggingRows
- 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
- type PoolConfig
- type Rows
- type Scannable
- type Transaction
- type UnderlyingContextExecable
- type UnderlyingExecable
- type UpgradeTable
- 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
Constants ¶
const ( ContextKeyDatabaseTransaction contextKey = iota ContextKeyDoTxnCallerSkip )
Variables ¶
var ( ErrTxn = errors.New("transaction") ErrTxnBegin = fmt.Errorf("%w: begin", ErrTxn) ErrTxnCommit = fmt.Errorf("%w: commit", ErrTxn) )
Types ¶
type Config ¶
type Config struct { PoolConfig `yaml:",inline"` ReadOnlyPool PoolConfig `yaml:"ro_pool"` }
type ContextExecable ¶
type ContextExecable interface { ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row }
type Database ¶
type Database struct { RawDB *sql.DB ReadOnlyDB *sql.DB Owner string VersionTable string Log DatabaseLogger Dialect Dialect UpgradeTable UpgradeTable IgnoreForeignTables bool IgnoreUnsupportedDatabase bool // contains filtered or unexported fields }
func NewFromConfig ¶
func NewFromConfig(owner string, cfg Config, logger DatabaseLogger) (*Database, error)
func NewWithDB ¶
func NewWithDialect ¶
func (*Database) Begin ¶
func (ld *Database) Begin() (*LoggingTxn, error)
func (*Database) BeginTx ¶
func (*Database) Child ¶
func (db *Database) Child(versionTable string, upgradeTable UpgradeTable, log DatabaseLogger) *Database
func (*Database) Close ¶
func (*Database) ColumnExists ¶
func (*Database) Configure ¶
func (*Database) Conn ¶
func (db *Database) Conn(ctx context.Context) ContextExecable
func (*Database) DoTxn ¶
func (db *Database) DoTxn(ctx context.Context, opts *sql.TxOptions, fn func(ctx context.Context) error) error
func (*Database) TableExists ¶
func (*Database) Upgrade ¶
type DatabaseLogger ¶
type DatabaseLogger interface { QueryTiming(ctx context.Context, method, query string, args []interface{}, nrows int, duration time.Duration, err error) WarnUnsupportedVersion(current, compat, latest int) PrepareUpgrade(current, compat, latest int) DoUpgrade(from, to int, message string, txn bool) // Deprecated: legacy warning method, return errors instead Warn(msg string, args ...interface{}) }
var NoopLogger DatabaseLogger = &noopLogger{}
func ZeroLogger ¶
func ZeroLogger(log zerolog.Logger, cfg ...ZeroLogSettings) DatabaseLogger
func ZeroLoggerPtr ¶
func ZeroLoggerPtr(log *zerolog.Logger, cfg ...ZeroLogSettings) DatabaseLogger
type Dialect ¶
type Dialect int
func ParseDialect ¶
func (Dialect) String ¶
type Execable ¶
type Execable interface { ContextExecable Exec(query string, args ...interface{}) (sql.Result, error) Query(query string, args ...interface{}) (Rows, error) QueryRow(query string, args ...interface{}) *sql.Row }
type JSON ¶
type JSON struct { Data any }
JSON is a utility type for using arbitrary JSON data as values in database Exec and Scan calls.
func (JSON) Scan ¶
func (JSON) Value ¶
type LoggingExecable ¶
type LoggingExecable struct { UnderlyingExecable UnderlyingExecable // contains filtered or unexported fields }
LoggingExecable is a wrapper for anything with database Exec methods (i.e. sql.Conn, sql.DB and sql.Tx) that can preprocess queries (e.g. replacing $ with ? on SQLite) and log query durations.
func (*LoggingExecable) Exec ¶
func (le *LoggingExecable) Exec(query string, args ...interface{}) (sql.Result, error)
func (*LoggingExecable) ExecContext ¶
func (le *LoggingExecable) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
func (*LoggingExecable) Query ¶
func (le *LoggingExecable) Query(query string, args ...interface{}) (Rows, error)
func (*LoggingExecable) QueryContext ¶
func (le *LoggingExecable) QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error)
func (*LoggingExecable) QueryRow ¶
func (le *LoggingExecable) QueryRow(query string, args ...interface{}) *sql.Row
func (*LoggingExecable) QueryRowContext ¶
func (le *LoggingExecable) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
type LoggingRows ¶
type LoggingRows struct {
// contains filtered or unexported fields
}
func (*LoggingRows) Close ¶
func (lrs *LoggingRows) Close() error
func (*LoggingRows) ColumnTypes ¶
func (lrs *LoggingRows) ColumnTypes() ([]*sql.ColumnType, error)
func (*LoggingRows) Columns ¶
func (lrs *LoggingRows) Columns() ([]string, error)
func (*LoggingRows) Err ¶
func (lrs *LoggingRows) Err() error
func (*LoggingRows) Next ¶
func (lrs *LoggingRows) Next() bool
func (*LoggingRows) NextResultSet ¶
func (lrs *LoggingRows) NextResultSet() bool
func (*LoggingRows) Scan ¶
func (lrs *LoggingRows) Scan(dest ...any) error
type LoggingTxn ¶
type LoggingTxn struct { LoggingExecable UnderlyingTx *sql.Tx StartTime time.Time EndTime time.Time // contains filtered or unexported fields }
func (*LoggingTxn) Commit ¶
func (lt *LoggingTxn) Commit() error
func (*LoggingTxn) Rollback ¶
func (lt *LoggingTxn) Rollback() error
type PoolConfig ¶
type PoolConfig struct { Type string `yaml:"type"` URI string `yaml:"uri"` MaxOpenConns int `yaml:"max_open_conns"` MaxIdleConns int `yaml:"max_idle_conns"` ConnMaxIdleTime string `yaml:"conn_max_idle_time"` ConnMaxLifetime string `yaml:"conn_max_lifetime"` }
type Rows ¶
type Rows interface { Close() error ColumnTypes() ([]*sql.ColumnType, error) Columns() ([]string, error) Err() error Next() bool NextResultSet() bool Scan(...any) error }
type Scannable ¶
type Scannable interface { Scan(...interface{}) error }
type Transaction ¶
type UnderlyingContextExecable ¶
type UnderlyingContextExecable interface { ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row }
type UnderlyingExecable ¶
type UnderlyingExecable interface { UnderlyingContextExecable Exec(query string, args ...interface{}) (sql.Result, error) Query(query string, args ...interface{}) (*sql.Rows, error) QueryRow(query string, args ...interface{}) *sql.Row }
type UpgradeTable ¶
type UpgradeTable []upgrade
func (*UpgradeTable) Register ¶
func (ut *UpgradeTable) Register(from, to, compat int, message string, txn bool, fn upgradeFunc)
func (*UpgradeTable) RegisterFS ¶
func (ut *UpgradeTable) RegisterFS(fs fullFS)
func (*UpgradeTable) RegisterFSPath ¶
func (ut *UpgradeTable) RegisterFSPath(fs fullFS, dir string)
type ZeroLogSettings ¶
type ZeroLogSettings struct { CallerSkipFrame int Caller bool // TraceLogAllQueries specifies whether or not all queries should be logged // at the TRACE level. TraceLogAllQueries bool }
Source Files ¶
connlog.go database.go json.go log.go transaction.go upgrades.go upgradetable.go
Directories ¶
Path | Synopsis |
---|---|
dbutil/litestream |
- Version
- v0.1.0
- Published
- Sep 6, 2023
- Platform
- darwin/amd64
- Imports
- 17 packages
- Last checked
- 3 hours ago –
Tools for package owners.