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 ¶
- Variables
- func CheckArgs(op string, args []any) error
- func ConstraintKind(msg string) error
- func ConstraintTarget(msg string) (table, column string)
- type Driver
- func (d *Driver) Begin(ctx context.Context, opts momo.TxOptions) (momo.DriverTx, error)
- func (d *Driver) ClassifyError(err error) error
- func (d *Driver) Exec(ctx context.Context, q string, args []any) (int64, error)
- func (d *Driver) Pin(ctx context.Context) (momo.PinnedConn, error)
- func (d *Driver) PoolStats() momo.PoolStats
- func (d *Driver) Query(ctx context.Context, q string, args []any) (momo.Rows, error)
- type DriverError
- type Hooks
Constants ¶
This section is empty.
Variables ¶
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.
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
CheckArgs is checkArgs for the MySQL adapter, which shares the Date and Numeric rule.
func ConstraintKind ¶
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 ¶
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 (*Driver) Begin ¶
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 ¶
ClassifyError implements momo.ErrorClassifier.
type DriverError ¶
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.