sqlite

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0, MIT Imports: 13 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 sqlite opens SQLite databases with the pure-Go modernc.org/sqlite driver, so CGO_ENABLED=0 builds work, and builds per-test database clones from a migrated template.

Index

Constants

View Source
const DefaultTimeFormat = "sqlite"

DefaultTimeFormat is the modernc _time_format Open sets when the DSN has none: "2006-01-02 15:04:05.999999999-07:00", which sorts, matches CURRENT_TIMESTAMP, and is read by SQLite's date and time functions.

View Source
const MemoryConns = 4

MemoryConns is how many connections an in-memory database opened with ":memory:" or a shared-cache memory URI may use at once.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	*sql.DB
	// contains filtered or unexported fields
}

DB is a *sql.DB that can describe itself for `fletch ax inspect`.

func Open

func Open(dsn string) (*DB, error)

Open returns a database for dsn, a file path, a file: URI, or ":memory:". It is OpenWithOptions with no options.

func OpenWithOptions

func OpenWithOptions(dsn string, opts ...Option) (*DB, error)

OpenWithOptions returns a database for dsn, a file path, a file: URI, or ":memory:", with foreign keys on, a 5 second busy timeout, transactions that take the write lock when they begin (BEGIN IMMEDIATE; read-only transactions excepted), times written in SQLite's own layout, and WAL for file databases. Settings already in the DSN are kept.

":memory:" (or "" or "file::memory:") gets a database of its own under a unique shared-cache name, with MemoryConns connections and read_uncommitted on, so a query can run while *sql.Rows are open or while a transaction is open on another connection; such a query sees the transaction's uncommitted rows. A write outside that transaction still waits for it to end. A shared-cache memory URI ("file::memory:?cache=shared" or "file:name?mode=memory&cache=shared") keeps its name and is pooled the same way. Other in-memory DSNs are used as given, with one connection.

A file database does no I/O until first use. An in-memory one holds a connection from Open to Close, since SQLite frees it with its last connection.

func (*DB) Close

func (d *DB) Close() error

Close closes the database. An in-memory database is freed here, so close d, not d.DB.

func (*DB) Describe

func (d *DB) Describe() data.Description

Describe reports the provider and a redacted DSN.

type Option

type Option func(*config)

Option configures OpenWithOptions.

func WithTimeFormat

func WithTimeFormat(format string) Option

WithTimeFormat sets the modernc _time_format used to write time.Time values: "sqlite" (the default), "datetime", or "string_no_monotonic". An empty format keeps the driver's own, Go's Time.String. A _time_format or _time_integer_format in the DSN wins over it.

type Template

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

Template is a migrated database image that tests clone.

func NewTemplate

func NewTemplate(ctx context.Context, migrations fs.FS) (*Template, error)

NewTemplate migrates a fresh in-memory database with migrations (nil for an empty schema) and keeps its image. Call it once per test package, for example in TestMain.

func (*Template) Clone

func (t *Template) Clone(tb testing.TB) *DB

Clone returns a new, isolated in-memory database holding the template's schema and data, opened like Open(":memory:"). It is closed when the test ends.

Jump to

Keyboard shortcuts

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