sqlitepool

package
v0.0.0-...-b529d1d Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package sqlitepool implements a pool of SQLite database connections.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyAll

func CopyAll(db sqliteh.DB, dstSchemaName, srcSchemaName string) (err error)

CopyAll copies the contents of one database to another.

Traditionally this is done in sqlite by closing the database and copying the file. However it can be useful to do it online: a single exclusive transaction can cross multiple databases, and if multiple processes are using a file, this lets one replace the database without first communicating with the other processes, asking them to close the DB first.

The dstSchemaName and srcSchemaName parameters follow the SQLite PRAMGA schema-name conventions: https://sqlite.org/pragma.html#syntax

func Exec

func Exec(db sqliteh.DB, sql string, args ...any) error

Exec is like database/sql.Tx.Exec. Only use this for one-off/rare queries. For normal queries, see the Exec method on Tx.

func ExecScript

func ExecScript(db sqliteh.DB, queries string) error

ExecScript executes a series of SQL statements against a database connection. It is intended for one-off scripts, so the prepared Stmt objects are not cached for future calls.

Types

type Pool

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

A Pool is a fixed-size pool of SQLite database connections. One is reserved for writable transactions, the others are used for read-only transactions.

func NewPool

func NewPool(filename string, poolSize int, initFn func(sqliteh.DB) error, tracer sqliteh.Tracer) (_ *Pool, err error)

NewPool creates a Pool of poolSize database connections.

For each connection, initFn is called to initialize the connection. Tracer is used to report statistics about the use of the Pool.

func (*Pool) BeginRx

func (p *Pool) BeginRx(ctx context.Context, why string) (*Rx, error)

BeginRx creates a read-only transaction. The parameter why is passed to the Tracer for debugging.

func (*Pool) BeginTx

func (p *Pool) BeginTx(ctx context.Context, why string) (*Tx, error)

BeginTx creates a writable transaction using BEGIN IMMEDIATE. The parameter why is passed to the Tracer for debugging.

func (*Pool) Close

func (p *Pool) Close() error

type Row

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

Row is like database/sql.Tx.Row.

func QueryRow

func QueryRow(db sqliteh.DB, sql string, args ...any) *Row

QueryRow is like database/sql.Tx.QueryRow. Only use this for one-off/rare queries. For normal queries, see the methods on Rx.

func (*Row) Err

func (r *Row) Err() error

func (*Row) Scan

func (r *Row) Scan(dest ...any) error

type Rows

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

Rows is like database/sql.Tx.Rows.

func Query

func Query(db sqliteh.DB, sql string, args ...any) (*Rows, error)

Query is like database/sql.Tx.Query. Only use this for one-off/rare queries. For normal queries, see the methods on Rx.

func (*Rows) Close

func (rs *Rows) Close() error

func (*Rows) Err

func (rs *Rows) Err() error

func (*Rows) Next

func (rs *Rows) Next() bool

func (*Rows) Scan

func (rs *Rows) Scan(dest ...any) error

type Rx

type Rx struct {

	// OnRollback is an optional function called after rollback.
	// If Rx is part of a Tx and it is committed, then OnRollback
	// is not called.
	OnRollback func()
	// contains filtered or unexported fields
}

Rx is a read-only transaction.

It is *not* safe for concurrent use.

func (*Rx) DB

func (rx *Rx) DB() sqliteh.DB

DB returns the underlying database connection.

Be careful: a transaction is in progress. Any use of BEGIN/COMMIT/ROLLBACK should be modelled as a nested transaction, and when done the original outer transaction should be left in-progress.

func (*Rx) Exec

func (rx *Rx) Exec(sql string) error

Exec executes an SQL statement with no result.

func (*Rx) Prepare

func (rx *Rx) Prepare(sql string) sqliteh.Stmt

Prepare prepares an SQL statement. The Stmt is cached on the connection, so subsequent calls are fast.

func (*Rx) Query

func (rx *Rx) Query(sql string, args ...any) (*Rows, error)

Query is like database/sql.Tx.Query.

func (*Rx) QueryRow

func (rx *Rx) QueryRow(sql string, args ...any) *Row

QueryRow is like database/sql.Tx.QueryRow.

func (*Rx) Rollback

func (rx *Rx) Rollback()

Rollback executes ROLLBACK and cleans up the Rx. It is a no-op if Rx is already rolled back.

type Tx

type Tx struct {
	*Rx

	// OnCommit is an optional function called after successful commit.
	OnCommit func()
}

Tx is a writable SQLite database transaction.

It is *not* safe for concurrent use.

A Tx contains an embedded Rx, which can be used to pass to functions that want to perform read-only queries on the writable Tx.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit executes COMMIT and cleans up the Tx. It is an error to call if the Tx is already rolled back or committed.

func (*Tx) Exec

func (tx *Tx) Exec(sql string, args ...any) error

Exec is like database/sql.Tx.Exec.

func (*Tx) ExecRes

func (tx *Tx) ExecRes(sql string, args ...any) (rowsAffected int64, err error)

func (*Tx) Rollback

func (tx *Tx) Rollback()

Rollback executes ROLLBACK and cleans up the Tx. It is a no-op if the Tx is already rolled back or committed.

Jump to

Keyboard shortcuts

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