sqlite

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package sqlite reads another program's SQLite database without touching it: Open copies the database and its write-ahead log into a private temporary directory and opens the copy, which SQLite recovers as a crashed writer's, so every frame the log holds up to its last commit is read, checkpointed or not. A database is never opened in place, since SQLite creates a -wal and a -shm file beside a database in WAL mode even for a read-only connection, and a connection that closes last may checkpoint. The program that owns the database may keep writing while it is copied, so the copy is checked: the database file and the log's header must be the same after the copy as before it, else the copy is retried a few times and then refused with ErrBusy.

It imports modernc.org/sqlite, a SQLite written in Go, and is one of the packages scripts/check-deps.sh lets do so, its path ending in /sqlite; the format packages' sqlite packages build on it.

Index

Constants

This section is empty.

Variables

View Source
var ErrBusy = errors.New("the database kept changing while it was copied; the program writing it may be running, try again when it is idle")

ErrBusy is returned by Open when the database kept changing while it was copied: the program that owns it is writing to it.

Functions

func Fixture

func Fixture(path, script string) (io.Closer, error)

Fixture builds a database at path from SQL text, for tests: the fixtures of this module are SQL text, never database files. The database is put in WAL mode with automatic checkpoints off and returned open: rows the script writes stay in the write-ahead log, where a reader finds them only through the log, for as long as the returned closer is not closed. Closing it checkpoints the log into the database.

func IsDatabase

func IsDatabase(path string) bool

IsDatabase reports whether the file at path starts with SQLite's header.

func RemoveCopies

func RemoveCopies()

RemoveCopies removes the copy of every database open: what a program interrupted while it reads one calls before it exits, as a deferred Close does not run then. The copies are in os.TempDir, under $TMPDIR when it is set.

func Tables

func Tables(db *sql.DB) (map[string]bool, error)

Tables lists the tables of an open database.

Types

type Copy

type Copy struct {
	// DB is the copy, open.
	DB *sql.DB
	// contains filtered or unexported fields
}

Copy is a copy of a database, open for reading. It is not safe for concurrent use.

func Open

func Open(path, prefix string) (*Copy, error)

Open copies the database at path into a temporary directory whose name starts with prefix, and opens the copy. Close removes it.

func (*Copy) Close

func (c *Copy) Close() error

Close closes the copy and removes it.

func (*Copy) Columns

func (c *Copy) Columns(table string) (map[string]bool, error)

Columns lists a table's columns.

func (*Copy) Dir

func (c *Copy) Dir() string

Dir is the private directory the copy is in.

func (*Copy) Query

func (c *Copy) Query(q string, args ...any) ([]Row, error)

Query runs a query and returns its rows.

func (*Copy) Tables

func (c *Copy) Tables() (map[string]bool, error)

Tables lists the copy's tables.

type Row

type Row map[string]any

Row is one row of a query, by column name.

func (Row) Int

func (r Row) Int(name string) (int64, bool)

Int is a column as an integer; false when it is not one.

func (Row) String

func (r Row) String(name string) string

String is a column as text: a TEXT or BLOB value, a number written out, "" for NULL or an absent column.

Jump to

Keyboard shortcuts

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