sqlitex

package
v2.0.4-main.3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sqlitex keeps a SQLite file in the shape the store that owns it declares: the shape read from the store's own DDL, a file reconciled with it in place where SQLite allows, and a file of another shape or a damaged one moved aside under the lock of its first open

Index

Constants

View Source
const (
	NotADatabase = "notadb"
	Zeroed       = "zeroed"
	OtherVersion = "other-version"
)

Kinds of file a database is moved aside as: one that starts some other way, one that holds only zero bytes, one of another layout

View Source
const (
	Index   = "index"
	Trigger = "trigger"
	View    = "view"
	Virtual = "virtual"
)

Kinds of object besides tables

Variables

This section is empty.

Functions

func Aside

func Aside(path, kind string) (string, error)

Moves the database file and its journals to a name beside it that says what it is and when: path.kind-YYYYMMDD-HHMMSS-pid.bak, numbered when that is taken. The new path; the file stays where it was when it or a journal can't be moved

func Copy

func Copy(ctx context.Context, src, dst string, wait time.Duration) error

Copies the database at src into a new file at dst through SQLite, as it stands at one moment while other connections go on writing it: a write-ahead log database at once, a rollback journal one in steps that let go of its read lock between them, refused when copying it whole would hold its writers up for longer than a moment. Only SQLite reads the file, so a database this process holds open keeps its locks. dst is made for its owner alone and removed when the copy fails; wait is how long a step waits for writers holding the database

func Damaged

func Damaged(path string) (string, int64, error)

How a file meant to hold a database is damaged: Zeroed when it holds only zero bytes, or nothing while a journal of it is left; NotADatabase when it starts some other way; empty when it starts as a database, is empty with no journal, as SQLite starts a new one in, or is not there. Its size in bytes

func FirstOpen

func FirstOpen(path string, timeout time.Duration, fn func(locked bool) error) error

Runs the function holding the lock of the database's first open, path.quarantine.lock, under which the file is moved aside or its shape changed one process at a time

func Holders

func Holders(path string) []int

Other processes of this computer holding the database, or a journal of it, open, by pid; none when the scan finds none or can't look

func Locked

func Locked(lock string, timeout time.Duration, fn func(locked bool) error) error

Runs the function holding the lock file, waiting for it until the timeout; told whether it got the lock

func Normalize

func Normalize(statement string) string

Statement as any spelling of it writes it: its comments and spacing dropped, names and keywords in lower case and unquoted where they need no quotes, string literals as they are, one space between tokens

func Quote

func Quote(name string) string

Name quoted for a statement

func Remove

func Remove(path string)

Removes a database and the journals beside it, what is not there counting as removed

func Restore

func Restore(ctx context.Context, src, dst string, wait time.Duration) error

Writes the database at src over the one at dst through SQLite, page by page into the file in place, so it keeps its identity and the connections other processes hold on it read the new pages; a dst that is not there is made from a copy. Where SQLite can't write the pages, such as over a file that holds no database, the file is replaced, which is refused while another process holds it open. wait is how long the restore waits for connections holding dst

func Sidecar

func Sidecar(name string) bool

Whether the name is that of a journal SQLite keeps beside a database, which belongs to it and to no copy of it

func Verify

func Verify(ctx context.Context, path string, limit int64) error

Why the database at the path can't be used, nil when SQLite finds it whole: a file that holds nothing or no database, or what PRAGMA integrity_check finds in it; PRAGMA quick_check, which leaves out the indexes, for a file larger than limit when limit is above zero. Only SQLite reads the file, so a database this process holds open keeps its locks

Types

type Column

type Column struct {
	Name string
	// Type as declared
	Type    string
	NotNull bool
	// Default as any spelling of it writes it, empty for none
	Default string
	// Place in the primary key from 1, 0 outside it
	Key int
	// 0 for a plain column, 2 for one generated as it is read, 3 for one generated as it is stored
	Hidden int
	// Collating sequence named in its definition, empty for the default
	Collation string
	// Numbered with AUTOINCREMENT, and a primary key of its own declared DESC, which keeps it from standing for the rowid
	Auto, Descending bool
	// Linked to another table by a REFERENCES of its own definition, which adding the column as declared brings with it
	Links bool
	// Definition as the CREATE TABLE statement writes it, which adds the column to a table as declared
	Definition string
}

Column of a table

type Link struct {
	From []string
	Text string
}

Foreign key of a table: the columns it links from, and the whole link as any spelling of it writes it

type Mismatch

type Mismatch struct {
	Reasons []string
}

Why a database is not of its schema and can't be made so in place

func (*Mismatch) Error

func (m *Mismatch) Error() string

type Object

type Object struct {
	Type, Name, Table, SQL string
}

Index, trigger, view or virtual table, with the statement that makes it

type Plan

type Plan struct {
	Missing []string
	Added   []string
	Dropped []Object
	// contains filtered or unexported fields
}

What reconciling a database with its schema takes: the tables it lacks, which the store's DDL makes; the columns added, as table.column; the objects dropped for that DDL to make again

func Inspect

func Inspect(ctx context.Context, q Querier, want *Schema) (Plan, error)

What reconciling the database the querier reaches with the schema takes, changing nothing: a *Mismatch saying why when it is not of the schema and can't be made so in place

func Reconcile

func Reconcile(ctx context.Context, tx Querier, want *Schema) (Plan, error)

Brings the database the transaction writes to into the shape of the schema where SQLite can do it in place: the columns it lacks are added as declared, and its views, triggers, indexes and virtual tables whose statement differs from the declared one, or that the schema does not declare, are dropped for the store's DDL to make again. Tables it lacks are left to that DDL too. A database that is not of the schema gets a *Mismatch, and the caller rolls the transaction back

type Querier

type Querier interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

What runs statements: a database, a connection or a transaction

type Schema

type Schema struct {
	Tables  map[string]*Table
	Objects map[string]Object
}

Shape of a database: its tables, and its indexes, triggers, views and virtual tables, each by its name in lower case

func Declare

func Declare(ctx context.Context, ddl ...string) (*Schema, error)

Shape the DDL makes, read back from an empty database in memory, so a store's shape comes from the statements that make it

func Read

func Read(ctx context.Context, q Querier) (*Schema, error)

Shape of the database the querier reaches

type Table

type Table struct {
	Name    string
	Columns []Column
	// Columns of the primary key in order; those of each UNIQUE constraint, joined by commas
	Key    []string
	Unique []string
	// Foreign keys and CHECK rules, as any spelling of the same one writes it
	Foreign []Link
	Checks  []string
	// Kept without a rowid, and STRICT
	WithoutRowid, Strict bool
}

Table as the database keeps it

Jump to

Keyboard shortcuts

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