Help us shape the future of Go! Take the 2026 Go Developer Survey

sqlite

package
v0.1.62 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 15 Imported by: 2

Documentation

Overview

Package sqlite opens a file-backed SQLite database with a serialized writer, a query-only read pool, and optional buffered asynchronous writes.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed is returned when a write is submitted after Close starts.
	ErrClosed = errors.New("sqlite database is closed")
	// ErrWriteQueueFull is returned when the asynchronous write queue is full.
	ErrWriteQueueFull = errors.New("sqlite asynchronous write queue is full")
	// ErrWriteErrorHandlerRequired is returned when an asynchronous write has no error handler.
	ErrWriteErrorHandlerRequired = errors.New("sqlite asynchronous writes require OnWriteError")
	// ErrReadOnly is returned when a write is submitted to a database opened
	// read-only whose writes were not enabled.
	ErrReadOnly = errors.New("sqlite database is read-only")
)

Functions

This section is empty.

Types

type DB

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

DB owns separate SQLite read and write pools over one file.

func Open

func Open(options Options) (*DB, error)

Open opens or creates options.Path and starts its asynchronous writer.

func (*DB) Close

func (d *DB) Close() error

Close drains accepted asynchronous writes and closes both pools.

func (*DB) EnableWrites added in v0.1.45

func (d *DB) EnableWrites() error

EnableWrites opens the writer of a database opened read-only, for a process that has become the one writing it. It does nothing once writes are enabled.

func (*DB) ExecAsync

func (d *DB) ExecAsync(statement string, args ...any) error

ExecAsync queues a parameterized SQL statement for asynchronous execution.

func (*DB) InsertAsync

func (d *DB) InsertAsync(table string, values map[string]any) error

InsertAsync queues one map-shaped row for insertion into table.

func (*DB) InsertTableAsync

func (d *DB) InsertTableAsync(table sqlitetable.Table, rows []query.Row) error

InsertTableAsync queues typed rows for insertion through sqlitetable.Table.

func (*DB) Lease

func (d *DB) Lease() func()

Lease prevents mutations until the returned release function is called.

func (*DB) Path

func (d *DB) Path() string

Path returns the absolute database file path.

func (*DB) ReadDSN

func (d *DB) ReadDSN() string

ReadDSN returns a query-only SQLite DSN for the database file.

func (*DB) Reader

func (d *DB) Reader() *sql.DB

Reader returns the query-only read pool. Callers that require several statements to observe one stable mutation boundary must hold Lease as well.

func (*DB) WatchDataVersion added in v0.1.45

func (d *DB) WatchDataVersion(ctx context.Context, every time.Duration, changed func()) error

WatchDataVersion calls changed whenever a commit to the file lands, checking every interval, until ctx ends or the database closes. It reads PRAGMA data_version on one pinned read connection, which changes with every commit another connection makes — another process's included. Close waits for the watch to end before it closes that connection.

func (*DB) Write

func (d *DB) Write(mutation Mutation) error

Write waits for exclusive access and runs mutation immediately.

func (*DB) WriteAsync

func (d *DB) WriteAsync(mutation Mutation) error

WriteAsync queues mutation for an asynchronous batch buffered for 100 milliseconds. It returns ErrWriteQueueFull rather than blocking.

type Mutation

type Mutation func(*sql.DB) error

Mutation changes a SQLite database through its serialized writer.

type Options

type Options struct {
	// Path is the SQLite database file.
	Path string

	// OnWriteError receives failures from accepted asynchronous mutations.
	// It is called after the write lock is released.
	OnWriteError func(error)

	// ReadOnly opens only the read pool over an existing file, for a process
	// that reads a database another process writes. Every write fails with
	// ErrReadOnly until EnableWrites.
	ReadOnly bool
}

Options configure a file-backed SQLite database.

Jump to

Keyboard shortcuts

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