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 Classify: 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.
Example ¶
An in-memory database keeps one connection, so every query sees the same database. Named queries bind as ?, and a constraint violation is classified.
package main
import (
"context"
"errors"
"fmt"
"github.com/assanoff/skit/dbx"
"github.com/assanoff/skit/dbx/sqlite"
_ "modernc.org/sqlite"
)
func main() {
ctx := context.Background()
db, err := sqlite.Open(sqlite.Config{Path: ":memory:", ForeignKeys: true})
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = db.Close() }()
const schema = `CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE)`
if err := dbx.Exec(ctx, db, schema); err != nil {
fmt.Println(err)
return
}
const ins = `INSERT INTO users (id, email) VALUES (:id, :email)`
err = dbx.NamedExec(ctx, db, ins, map[string]any{"id": 1, "email": "ann@example.com"})
fmt.Println("insert:", err)
err = dbx.NamedExec(ctx, db, ins, map[string]any{"id": 2, "email": "ann@example.com"})
fmt.Println("duplicate:", errors.Is(err, dbx.ErrDBDuplicatedEntry))
// The driver error stays reachable, with its extended result code.
type coder interface {
error
Code() int
}
if c, ok := errors.AsType[coder](err); ok {
fmt.Println("code:", c.Code())
}
}
Output: insert: <nil> duplicate: true code: 2067
Index ¶
Examples ¶
Constants ¶
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 Classify ¶ added in v0.15.0
Classify is the dbx.Classifier of SQLite. It maps 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 Open ¶
Open opens a SQLite pool through a driver the caller registered, bound to Classify 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 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.