sqlitepool

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: BSD-3-Clause Imports: 6 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 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 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) 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) 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