sqlshared

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package sqlshared holds the part of the SQLite adapters that does not depend on the SQLite driver: the momo.Driver implementation over database/sql, transactions, read-only transactions, pinned connections and time scanning. driver/sqlitedriver (mattn, cgo) and driver/sqlitepure (modernc, pure Go) both build on it and supply only what differs: opening, DSN rules, error classification and capabilities.

The package has no build tags and imports neither mattn/go-sqlite3 nor modernc.org/sqlite, so linking it never pulls in a SQLite implementation.

Index

Constants

This section is empty.

Variables

View Source
var AfterBeginStmtHook func(stmt string) error

AfterBeginStmtHook lets a test make a statement of beginReadOnly report an error after it ran. It is for tests only and is nil otherwise; the same restrictions as ExecStmtHook apply.

View Source
var ExecStmtHook func(stmt string) error

ExecStmtHook lets a test make a statement that ends a read-only transaction (COMMIT or ROLLBACK) fail. It is for tests only and is nil otherwise. It is a plain package variable, read without synchronisation by every read-only transaction in the process, so a test that sets it must not run in parallel with any other test that uses a SQLite driver, and must reset it to nil.

Functions

func CheckArgs added in v0.8.0

func CheckArgs(op string, args []any) error

CheckArgs is checkArgs for the MySQL adapter, which shares the Date and Numeric rule.

func ConstraintKind

func ConstraintKind(msg string) error

ConstraintKind classifies a constraint failure by its message prefix, for a SQLITE_CONSTRAINT whose extended code names no kind (a bare code, or one a later SQLite adds). It returns nil for any other message.

func ConstraintTarget

func ConstraintTarget(msg string) (table, column string)

ConstraintTarget parses "UNIQUE constraint failed: table.column" into its table and column. It returns empty strings for any other message; for a multi-column constraint (a comma-separated list) it returns the first.

Types

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver implements momo.Driver, momo.Pinner, momo.PoolStater and momo.ErrorClassifier over a *sql.DB. The adapters embed it and add Dialect and Capabilities.

func New

func New(db *sql.DB, h Hooks) *Driver

New returns a Driver over db.

func (*Driver) Begin

func (d *Driver) Begin(ctx context.Context, opts momo.TxOptions) (momo.DriverTx, error)

Begin starts a transaction. SQLite is serializable, so the default and sql.LevelSerializable are accepted and every other level is momo.ErrUnsupported. A read-only transaction runs PRAGMA query_only=ON on a dedicated connection and resets it when the transaction finishes.

func (*Driver) ClassifyError

func (d *Driver) ClassifyError(err error) error

ClassifyError implements momo.ErrorClassifier.

func (*Driver) Exec

func (d *Driver) Exec(ctx context.Context, q string, args []any) (int64, error)

Exec runs a statement on the pool.

func (*Driver) Pin

func (d *Driver) Pin(ctx context.Context) (momo.PinnedConn, error)

Pin checks one connection out of the database/sql pool.

func (*Driver) PoolStats

func (d *Driver) PoolStats() momo.PoolStats

PoolStats maps sql.DBStats onto momo.PoolStats.

func (*Driver) Query

func (d *Driver) Query(ctx context.Context, q string, args []any) (momo.Rows, error)

Query runs a query on the pool.

type DriverError

type DriverError struct {
	Cause error
	Msg   string
}

DriverError carries a driver error while exposing only a fixed message. The cause stays reachable through errors.As and Unwrap.

func (DriverError) Error

func (e DriverError) Error() string

func (DriverError) Unwrap

func (e DriverError) Unwrap() error

type Hooks

type Hooks struct {
	// Classify maps a driver error to a *momo.Error. op names the operation
	// ("select", "exec", "begin", ...) or is empty. It must return errors it
	// does not recognise unchanged and must accept nil.
	Classify func(op string, err error) error
	// IsDriverErr reports whether err is, or wraps, an error of the SQLite
	// driver itself. Scan uses it to tell a driver error from a database/sql
	// conversion failure, whose text can quote a stored value.
	IsDriverErr func(error) bool
}

Hooks carries what differs between the two SQLite drivers.

Jump to

Keyboard shortcuts

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