sqlite

package
v0.0.0-...-2d15036 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

pkg/sqlite

SQLite database wrapper with WAL-mode initialization, connection pooling, and nil-guard validation.

Overview

The sqlite package provides a thin wrapper around database/sql for the CGo-free modernc.org/sqlite driver. It configures performance pragmas, connection pooling, and exposes a ValidateDB sentinel used by every other package.

Key Types

DB
type DB struct {
    Conn *sql.DB
}

Wraps *sql.DB for method attachment. All packages access the database through this type.

Init() (*DB, error)

Initializes the database at the default path .sentinel/graph.db. Creates parent directory if missing (0750).

InitAtPath(dbPath string) (*DB, error)

Same as Init but with a custom path.

Configuration applied:

  • PRAGMA journal_mode=WAL — write-ahead logging for concurrent readers
  • PRAGMA foreign_keys=ON — referential integrity enforcement
  • PRAGMA busy_timeout=5000 — 5-second busy wait before SQLITE_BUSY
  • PRAGMA synchronous=NORMAL — balance between durability and performance
  • SetMaxOpenConns(8), SetMaxIdleConns(8) — connection pooling for WAL mode
  • db.PingContext(ctx) — connection health check before returning
Close() error

Closes the underlying database connection.

Validation

ErrNilDB
var ErrNilDB = errors.New("nil db")

Sentinel error for nil database detection. All packages check errors.Is(err, sqlite.ErrNilDB) for graceful nil handling.

ValidateDB(db *DB, caller string) error

Returns "<caller>: nil db" if db == nil || db.Conn == nil. Used by every constructor and method that requires a database handle:

if err := sqlite.ValidateDB(db, "engine"); err != nil {
    return nil, err
}

This pattern ensures systematic nil-guard hardening across all 14 internal packages.

Dependencies

  • modernc.org/sqlite — CGo-free SQLite driver (imported for side effects)
  • database/sql — standard library

Usage

db, err := sqlite.Init()
if err != nil {
    log.Fatal(err)
}
defer db.Close()

// Validate before use
if err := sqlite.ValidateDB(db, "my-component"); err != nil {
    return err
}

// Use directly
rows, err := db.Conn.QueryContext(ctx, "SELECT ...", args...)

Documentation

Overview

Package sqlite provides the SQLite database wrapper and initialization.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilDB = errors.New("nil db")

ErrNilDB is returned by ValidateDB when the DB or its connection is nil.

Functions

func ValidateDB

func ValidateDB(db *DB, caller string) error

ValidateDB returns an error if the DB or its underlying connection is nil.

Types

type DB

type DB struct {
	Conn *sql.DB
}

DB wraps a database/sql connection.

func Init

func Init() (*DB, error)

Init establishes SQLite connection and configures Elite Pragmas

func InitAtPath

func InitAtPath(dbPath string) (*DB, error)

InitAtPath establishes SQLite connection at a specific path

func (*DB) Close

func (db *DB) Close() error

Close closes the database connection

Jump to

Keyboard shortcuts

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