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
- func Aside(path, kind string) (string, error)
- func Copy(ctx context.Context, src, dst string, wait time.Duration) error
- func Damaged(path string) (string, int64, error)
- func FirstOpen(path string, timeout time.Duration, fn func(locked bool) error) error
- func Holders(path string) []int
- func Locked(lock string, timeout time.Duration, fn func(locked bool) error) error
- func Normalize(statement string) string
- func Quote(name string) string
- func Remove(path string)
- func Restore(ctx context.Context, src, dst string, wait time.Duration) error
- func Sidecar(name string) bool
- func Verify(ctx context.Context, path string, limit int64) error
- type Column
- type Link
- type Mismatch
- type Object
- type Plan
- type Querier
- type Schema
- type Table
Constants ¶
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
const ( Index = "index" Trigger = "trigger" View = "view" Virtual = "virtual" )
Kinds of object besides tables
Variables ¶
This section is empty.
Functions ¶
func Aside ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Runs the function holding the lock file, waiting for it until the timeout. Told whether it got the lock
func Normalize ¶
Normalize returns the statement in a canonical spelling: comments and extra spacing removed, names and keywords in lower case and unquoted when quotes are not needed, string literals unchanged, and one space between tokens.
func Remove ¶
func Remove(path string)
Removes a database and the journals beside it, what is not there counting as removed
func Restore ¶
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 ¶
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 ¶
Verify returns why the database at the path can't be used, or nil when SQLite finds it intact: an empty file or one that is not a database, or every finding of PRAGMA integrity_check. A file larger than a positive limit gets PRAGMA quick_check, which skips the indexes. Only SQLite reads the file, so a database this process has 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 ¶
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
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 ¶
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 ¶
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 ¶
Shape of a database: its tables, and its indexes, triggers, views and virtual tables, each by its name in lower case
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