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

sqlite

package
v0.1.0-rc.5 Latest Latest
Warning

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

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

Documentation

Overview

Package sqlite owns file and transaction mechanics, not engine data.

Index

Constants

This section is empty.

Variables

View Source
var ErrMismatch = errors.New("migrations that do not match the file")

ErrMismatch is a history the given scripts do not match.

A script changed or renamed after the file ran it, fewer scripts than the file ran, or another engine's file all cause it.

View Source
var ErrOutcomeUnknown = errors.New("commit outcome unknown")

ErrOutcomeUnknown is a grouped write whose transaction failed to commit: it may or may not be in the file, and its caller reconciles before retrying.

View Source
var ErrPending = errors.New("migrations the file has not applied")

ErrPending is a script the file has not run, found by Verify, which runs none.

Functions

func Copy

func Copy(ctx context.Context, path, into string) (applied int, err error)

Copy writes a consistent copy of the file at path to into, as Snapshot does for a file that is open, whether or not a File of this process holds it.

func EachRow

func EachRow(rows *sql.Rows, what string, visit func(*sql.Rows) error) error

EachRow hands every row to visit and closes the rows before it returns, so that the connection can run its next query; the first error stops the rows.

func OpenDB

func OpenDB(name string) (*sql.DB, error)

OpenDB opens name, a path or a file: URL, through the store's own driver, for a database its caller keeps itself: a schema declared in memory, or a file a test makes.

func ReadCopy

func ReadCopy(ctx context.Context, path string, read func(Reader) error) (err error)

ReadCopy runs read on a connection of its own, opened read-only at a copy that Snapshot wrote, so that an engine can list what the copy names.

Types

type Config

type Config struct {
	Readers     int
	PageSize    int
	WriterCache int // bytes of the writer's page cache; zero keeps the readers' 1 MiB
	Statements  int // compiled statements each connection keeps; zero keeps 32

	// MaxLength is the longest string, blob or row a statement may make, since
	// SQLite allocates one whole before a budget can count it. Zero keeps
	// SQLite's default.
	MaxLength int

	// Connected runs on each connection as it opens. SQLite as the driver
	// builds it leaves FTS5 and R*Tree to each connection, and an
	// application's database registers them here.
	Connected func(*sqlite3.Conn) error

	// Waited is told once when a grouped write has waited Patience for the
	// writer, with the label UpdateGroupedAs gave the write.
	Waited func(label string)

	// GroupHold bounds a group's hold on the writer from when it holds it, and
	// Patience a grouped write's wait before Waited; zero keeps ten seconds.
	GroupHold, Patience time.Duration
}

Config sizes the reader pool once, because every pragma here is per connection and nothing in the pool may expire and reopen. PageSize applies only to a file this call creates; zero keeps SQLite's default.

type File

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

func Open

func Open(ctx context.Context, path string, config Config) (*File, error)

func (*File) Claim

func (f *File) Claim(ctx context.Context, applicationID int) error

Claim stamps a fresh file as Migrate would and runs no script, so that a file opened without migrations is still the engine's and refuses another's. A file the engine already claimed is left as it is, whatever it has run.

func (*File) Close

func (f *File) Close() error

Close releases connections; callers must first drain their operations.

func (*File) Lookup

func (f *File) Lookup(ctx context.Context, read func(Reader) error) error

Lookup runs read on a reader without a transaction of its own, so that each statement is its own snapshot: a point read pays for one statement, not for the BEGIN and COMMIT a View puts around it. read closes the rows it opens.

func (*File) Migrate

func (f *File) Migrate(ctx context.Context, applicationID int, scripts fs.FS) error

Migrate claims a fresh file or checks the existing engine's migration history, then runs, in name order, every script the file has not run yet:

history   0001_schema.sql
scripts   0001_schema.sql   0002_labels.sql
          checked           run and recorded

func (*File) MigrateHosted

func (f *File) MigrateHosted(ctx context.Context, engine string, scripts fs.FS) error

MigrateHosted runs an engine's scripts in a file another engine owns, as jobs' in an application's database: the owner's application id stays, and the guest keeps its own history beside the owner's, so that neither's scripts are the other's.

owner's history   _tinystore_migrations        0001_app.sql
guest's history   _tinystore_jobs_migrations   0001_schema.sql

func (*File) ReaderPrepares

func (f *File) ReaderPrepares() uint64

ReaderPrepares is how many statements the idle readers have compiled.

func (*File) Snapshot

func (f *File) Snapshot(ctx context.Context, into string) (applied int, err error)

Snapshot writes a consistent copy of the file to into, which must not exist, and returns how many migrations the copy has applied.

It runs on a connection of its own, opened read-only at the file: the query_only readers refuse VACUUM INTO, and a read-only file lets the writer keep writing.

func (*File) Update

func (f *File) Update(ctx context.Context, write func(*sql.Tx) error) error

func (*File) UpdateGrouped

func (f *File) UpdateGrouped(ctx context.Context, bytes int, write func(Writer) error) error

UpdateGrouped runs write in a savepoint of a transaction it may share with writes queued beside it, and returns once that transaction commits.

A failed write rolls back its savepoint and fails alone. If a caller's context ends before its write starts, that write never runs, whether the caller waits behind a leader or leads the wait for the writer. Once started, a write finishes with its group whatever its caller's context does.

A statement run through UntilDeadline is different: it ends at its caller's deadline, and if the deadline passes while SQLite is executing it, SQLite rolls back the whole transaction and the group fails with it.

A group holds the writer for at most Config.GroupHold, counted from when it acquires the writer. Waiting for the writer does not count, so a long transaction ahead fails none of the writes queued behind it. bytes counts toward the group's size bound, and a write heavier than the bound commits alone. A failed commit is ErrOutcomeUnknown.

func (*File) UpdateGroupedAs

func (f *File) UpdateGroupedAs(ctx context.Context, label string, bytes int, write func(Writer) error) error

UpdateGroupedAs is UpdateGrouped with the label Config.Waited is told.

func (*File) UpdatePrepared

func (f *File) UpdatePrepared(ctx context.Context, write func(Writer) error) error

UpdatePrepared reuses SQL programs on the single writer connection within one transaction.

func (*File) Verify

func (f *File) Verify(ctx context.Context, applicationID int, scripts fs.FS) error

Verify checks the scripts against the history the file ran, in a snapshot, and runs none.

A script changed or renamed after the file ran it, or a history longer than the scripts, is refused as Migrate refuses it. A script the file has not run, or a file no engine has claimed, is ErrPending.

history   0001_schema.sql
scripts   0001_schema.sql   0002_labels.sql
          checked           ErrPending

func (*File) View

func (f *File) View(ctx context.Context, read func(*sql.Tx) error) error

View pins every callback read to the same connection and snapshot.

func (*File) ViewPrepared

func (f *File) ViewPrepared(ctx context.Context, read func(Reader) error) error

ViewPrepared reuses bounded SQL programs, while every call gets a fresh snapshot.

func (*File) WriterCounters

func (f *File) WriterCounters(ctx context.Context) (WriterCounters, error)

type Reader

type Reader interface {
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}

type Row

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

func QueryRow

func QueryRow(ctx context.Context, reader Reader, query string, args ...any) *Row

QueryRow runs a statement that walks a range for its row, a count or a sum, which ctx interrupts while it runs.

func QueryRowByKey

func QueryRowByKey(ctx context.Context, reader Reader, query string, args ...any) *Row

QueryRowByKey runs a statement that finds its row by a key, or writes that row and returns it.

ctx is checked before the statement and not while it runs: a context that can end costs database/sql and the driver a goroutine each, more than such a statement takes.

func (*Row) Scan

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

type Writer

type Writer interface {
	Reader
	ExecContext(context.Context, string, ...any) (sql.Result, error)
}

func UntilDeadline

func UntilDeadline(w Writer) Writer

UntilDeadline is a grouped write's writer whose statements end at their caller's deadline, for SQL that may run without end, such as an application's.

If the deadline passes while SQLite is executing one of them, SQLite rolls back the whole transaction and the group fails with it. A cancel still lets the statement finish. Any other writer comes back as it is.

type WriterCounters

type WriterCounters struct {
	Commits, CacheWrites, CacheSpills, CacheHits, CacheMisses uint64
	Prepared, Evicted                                         uint64
	Programs                                                  int
}

Jump to

Keyboard shortcuts

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