sqlite

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package sqlite opens a SQLite pool for dbx through a driver the caller registers, modernc.org/sqlite by default, with a Classifier of SQLite result codes.

Usage

import _ "modernc.org/sqlite"

db, err := sqlite.Open(sqlite.Config{Path: "app.db", JournalMode: "WAL"})
if err != nil {
    return err
}
defer db.Close()

The returned *dbx.DB runs every dbx helper with the sqlx bind style "sqlite3", so named queries bind as ?. Its errors are classified by Classifier: a unique (2067) or primary key (1555) constraint wraps dbx.ErrDBDuplicatedEntry; the driver error stays reachable through errors.As.

Transactions

A DSN built from Config opens every transaction with BEGIN IMMEDIATE (_txlock=immediate) and waits for a lock up to BusyTimeout. A transaction that reads and then writes therefore takes the write lock up front instead of failing with SQLITE_BUSY at its first write when another one writes meanwhile. A DSN of your own (Config.DSN) sets neither.

Pitfalls

  • Time is stored as text. A column filled both by a DEFAULT and from Go holds two formats, which compare as strings: do not page by it.

Config

Config fields: Path (a file or ":memory:"), Driver (empty → DefaultDriver, "sqlite"), JournalMode, BusyTimeout (zero keeps 5s), ForeignKeys, DSN (used verbatim; the other DSN fields are ignored), MaxOpenConns and MaxIdleConns (zero leaves the open connections unbounded and keeps 2 idle). An in-memory database keeps one connection that never closes by default: each connection to it opens its own empty database.

Index

Constants

View Source
const DefaultDriver = "sqlite"

DefaultDriver is the database/sql driver name Open uses when Config.Driver is empty: modernc.org/sqlite, the pure-Go driver (no cgo). The caller blank-imports it — dbx adds no SQLite driver dependency of its own.

Variables

This section is empty.

Functions

func Open

func Open(cfg Config) (*dbx.DB, error)

Open opens a SQLite pool through a driver the caller registered, bound to Classifier and to the sqlx bind style "sqlite3". It does not verify connectivity; call dbx.StatusCheck for that. A built DSN opens every transaction with BEGIN IMMEDIATE (_txlock=immediate), so a transaction that reads and then writes takes the write lock up front instead of failing with SQLITE_BUSY at its first write.

Types

type Classifier

type Classifier struct{}

Classifier maps SQLite extended result codes onto the dbx sentinels: a unique (2067) or primary key (1555) constraint → dbx.ErrDBDuplicatedEntry. It reads the code through Code() int, which modernc.org/sqlite errors have, so it imports no driver; the errors of mattn/go-sqlite3 carry the code in a field and pass unchanged. It wraps the driver error, so errors.As still reaches it.

func (Classifier) Classify

func (Classifier) Classify(err error) error

Classify implements dbx.Classifier.

type Config

type Config struct {
	// Path is the database file, or ":memory:".
	Path string
	// Driver is the registered database/sql driver name; empty → DefaultDriver.
	Driver string
	// Pragmas applied through the modernc DSN (_pragma=…); ignored when DSN
	// is set.
	JournalMode string        // e.g. "WAL"; empty keeps the engine default
	BusyTimeout time.Duration // busy_timeout; zero keeps 5s
	ForeignKeys bool          // foreign_keys(1)
	// DSN, when set, is used verbatim as the driver DSN — for drivers whose
	// DSN syntax differs from modernc (e.g. mattn/go-sqlite3).
	DSN string
	// MaxOpenConns and MaxIdleConns bound the pool; zero leaves the open
	// connections unbounded and keeps 2 idle. An in-memory database
	// (":memory:" or mode=memory) keeps one connection by default: each
	// connection to it opens its own empty database.
	MaxOpenConns int
	MaxIdleConns int
}

Config configures a SQLite database opened by Open.

Jump to

Keyboard shortcuts

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