Documentation
¶
Overview ¶
Package sqlitebp provides opinionated best practices for opening SQLite databases with sensible defaults for connection pooling, pragmas, and configuration options.
Example ¶
package main
import (
"fmt"
"log"
"os"
"github.com/jacob2161/sqlitebp"
)
func main() {
os.Remove("example.db")
os.Remove("example.db-shm")
os.Remove("example.db-wal")
db, err := sqlitebp.OpenReadWriteCreate("example.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
defer func() { os.Remove("example.db"); os.Remove("example.db-shm"); os.Remove("example.db-wal") }()
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT NOT NULL) STRICT`)
if err != nil {
log.Fatal(err)
}
res, err := db.Exec("INSERT INTO users (name) VALUES (?)", "Alice")
if err != nil {
log.Fatal(err)
}
id, _ := res.LastInsertId()
fmt.Printf("Inserted user with ID: %d\n", id)
}
Output:
Example (CustomOptions) ¶
package main
import (
"fmt"
"log"
"os"
"github.com/jacob2161/sqlitebp"
)
func main() {
os.Remove("custom.db")
os.Remove("custom.db-shm")
os.Remove("custom.db-wal")
db, err := sqlitebp.OpenReadWriteCreate("custom.db",
sqlitebp.WithBusyTimeoutSeconds(30),
sqlitebp.WithCacheSizeMiB(64),
sqlitebp.WithSynchronous("FULL"),
)
if err != nil {
log.Fatal(err)
}
defer db.Close()
defer func() { os.Remove("custom.db"); os.Remove("custom.db-shm"); os.Remove("custom.db-wal") }()
fmt.Println("Database opened with custom options")
}
Output:
Example (ReadOnly) ¶
package main
import (
"fmt"
"log"
"os"
"github.com/jacob2161/sqlitebp"
)
func main() {
os.Remove("readonly.db")
os.Remove("readonly.db-shm")
os.Remove("readonly.db-wal")
// Ensure exists
db, err := sqlitebp.OpenReadWriteCreate("readonly.db")
if err != nil {
log.Fatal(err)
}
db.Close()
ro, err := sqlitebp.OpenReadOnly("readonly.db")
if err != nil {
log.Fatal(err)
}
defer ro.Close()
var count int
if err := ro.QueryRow("SELECT COUNT(*) FROM sqlite_master").Scan(&count); err != nil {
log.Fatal(err)
}
if _, err := ro.Exec("CREATE TABLE test (id INTEGER) STRICT"); err != nil {
fmt.Println("Write failed as expected in read-only mode")
}
defer func() { os.Remove("readonly.db"); os.Remove("readonly.db-shm"); os.Remove("readonly.db-wal") }()
}
Output:
Index ¶
- Variables
- func OpenReadOnly(filename string, opts ...Option) (*sql.DB, error)
- func OpenReadWrite(filename string, opts ...Option) (*sql.DB, error)
- func OpenReadWriteCreate(filename string, opts ...Option) (*sql.DB, error)
- type Option
- func WithBusyTimeoutSeconds(sec int) Option
- func WithCacheSizeMiB(mib int) Option
- func WithCaseSensitiveLike(enabled bool) Option
- func WithForeignKeys(enabled bool) Option
- func WithJournalMode(mode string) Option
- func WithMMapSize(bytes int64) Option
- func WithOptimize(enabled bool) Option
- func WithRecursiveTriggers(enabled bool) Option
- func WithSecureDelete(mode string) Option
- func WithSynchronous(level string) Option
- func WithTempStore(store string) Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmptyFilename indicates an empty filename was supplied. ErrEmptyFilename = errors.New("sqlitebp: filename cannot be empty") // ErrInvalidMode indicates an internal invalid mode value. ErrInvalidMode = errors.New("sqlitebp: invalid mode") // ErrOpenFailed indicates the database could not be opened. ErrOpenFailed = errors.New("sqlitebp: open failed") // ErrPragmaExec indicates a PRAGMA failed during connection initialization. ErrPragmaExec = errors.New("sqlitebp: pragma execution failed") // ErrPingFailed indicates ping validation failed after opening. ErrPingFailed = errors.New("sqlitebp: ping failed") // ErrInvalidConfigOption indicates an invalid configuration option was supplied. ErrInvalidConfigOption = errors.New("sqlitebp: invalid config option") )
Functions ¶
func OpenReadOnly ¶
OpenReadOnly opens an existing database in read-only mode (journal mode not forced; no writes).
func OpenReadWrite ¶
OpenReadWrite opens an existing database with read/write access (must exist).
Types ¶
type Option ¶
type Option func(*openConfig) error
Option configures database parameters prior to opening.
func WithBusyTimeoutSeconds ¶
WithBusyTimeoutSeconds sets the busy timeout (seconds >=0). Translated to _busy_timeout (ms).
func WithCacheSizeMiB ¶
WithCacheSizeMiB sets the page cache size in MiB (negative KiB form).
func WithCaseSensitiveLike ¶
WithCaseSensitiveLike toggles case_sensitive_like pragma.
func WithForeignKeys ¶
WithForeignKeys enables or disables foreign key enforcement.
func WithJournalMode ¶
WithJournalMode sets journal mode (ignored in read-only opens where we do not force WAL).
func WithMMapSize ¶
WithMMapSize sets the mmap size in bytes (0 disables memory mapping growth beyond default). Applies via DSN.
func WithOptimize ¶
WithOptimize enables or disables running PRAGMA optimize on each new connection (default enabled).
func WithRecursiveTriggers ¶
WithRecursiveTriggers toggles recursive_triggers.
func WithSecureDelete ¶
WithSecureDelete sets secure_delete mode (FAST, ON, OFF).
func WithSynchronous ¶
WithSynchronous sets the synchronous level.
func WithTempStore ¶
WithTempStore overrides temp_store using a per-connection PRAGMA (DEFAULT, FILE, MEMORY). This cannot be reliably set via DSN driver underscore parameter; we apply it through ConnectHook.