sqlite

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package sqlite provides the SQLite dialect for Orjanda's DAL. Uses modernc.org/sqlite (pure-Go, no CGo). See TAD §2.3 and PRD §13.3.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

DB wraps *sql.DB and implements dal.Database for SQLite.

func Open

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

Open opens a SQLite database at the given DSN (file path or ":memory:").

Pragmas are configured per-connection via modernc shorthand DSN keys, not ExecContext: an ExecContext PRAGMA only touches the single pooled connection that executes it, leaving every later connection (and therefore concurrent writers) unconfigured.

  • _journal_mode=WAL: concurrent read/write safety.
  • _foreign_keys=1: enable foreign-key enforcement.
  • _busy_timeout=5000: wait a bounded time for a held write lock instead of failing immediately with SQLITE_BUSY.
  • _txlock=immediate: every transaction is BEGIN IMMEDIATE, taking the write lock at transaction start. A deferred transaction that reads first and writes later holds a stale WAL snapshot and fails the upgrade with SQLITE_BUSY_SNAPSHOT, which the busy handler does not retry — the exact failure mode of two serve instances racing the first-run bootstrap (REVIEW-2026-08-12 finding 12).

func (*DB) Close

func (d *DB) Close() error

Close closes the underlying connection pool.

func (*DB) CreateTables

func (d *DB) CreateTables(docs []*schema.CompiledDoc) error

CreateTables creates all tables for the given compiled docs (idempotent).

func (*DB) Delete

func (d *DB) Delete(ctx context.Context, docType, id string) error

Delete hard-deletes a record.

func (*DB) Dialect

func (d *DB) Dialect() dal.Dialect

Dialect returns the underlying dal.Dialect implementation.

func (*DB) ExistingColumns

func (d *DB) ExistingColumns(tableName string) (map[string]string, error)

ExistingColumns returns the live column names of a table mapped to the type each column reports (PRAGMA type strings, as written at CREATE time).

func (*DB) ExistingTables

func (d *DB) ExistingTables() (map[string]bool, error)

ExistingTables returns the set of table names currently in the database.

func (*DB) Insert

func (d *DB) Insert(ctx context.Context, docType string, data map[string]any) (string, error)

Insert writes a new record and returns the generated or provided ULID.

func (*DB) Query

func (d *DB) Query(ctx context.Context, q dal.Select) ([]map[string]any, error)

Query executes a Select and returns rows as maps.

func (*DB) RegisterDoc

func (d *DB) RegisterDoc(docType, tableName string)

RegisterDoc registers a docType→tableName mapping.

func (*DB) RegisterDocs

func (d *DB) RegisterDocs(docs []*schema.CompiledDoc)

RegisterDocs registers mappings from a slice of CompiledDocs, including the parent→child table mappings so child-table writes (e.g. core.UserRole) resolve.

func (*DB) Transaction

func (d *DB) Transaction(ctx context.Context, fn func(dal.Tx) error) error

Transaction runs fn inside a database transaction.

func (*DB) Underlying

func (d *DB) Underlying() *sql.DB

Underlying returns the raw *sql.DB.

func (*DB) Update

func (d *DB) Update(ctx context.Context, docType, id string, data map[string]any) error

Update mutates an existing record.

type Dialect

type Dialect struct{}

Dialect implements dal.Dialect for SQLite.

func New

func New() *Dialect

New creates a new SQLite Dialect.

func (*Dialect) AlterTable

func (d *Dialect) AlterTable(diff schema.TableAlteration) []string

AlterTable generates ALTER TABLE SQL for column additions and drops. Note: SQLite does not support DROP COLUMN in older versions natively, but modern sqlite (3.35.0+) supports ALTER TABLE DROP COLUMN. SQLite has no ALTER COLUMN TYPE; type changes are surfaced as a review comment so the author can write the required table rebuild manually (forward-only, TAD §14.1 step 2).

func (*Dialect) ColumnType

func (d *Dialect) ColumnType(f schema.Field) string

ColumnType returns the DDL type SQLite uses for the given Field.

func (*Dialect) CreateTable

func (d *Dialect) CreateTable(doc schema.CompiledDoc) string

CreateTable generates CREATE TABLE SQL for the given CompiledDoc.

func (*Dialect) DeleteSQL

func (d *Dialect) DeleteSQL(tableName, id string) (string, []any)

DeleteSQL renders a hard DELETE statement.

func (*Dialect) DriverName

func (d *Dialect) DriverName() string

DriverName returns "sqlite".

func (*Dialect) FullTextSearch

func (d *Dialect) FullTextSearch(tableName, query string, searchableFields []string) (string, []any)

FullTextSearch renders a full-text search query. For SQLite, uses LIKE %query% across all searchable text columns as a lightweight MVP implementation.

func (*Dialect) InsertSQL

func (d *Dialect) InsertSQL(tableName string, fields map[string]any) (string, []any)

InsertSQL renders an INSERT statement for SQLite (? placeholders).

func (*Dialect) Name

func (d *Dialect) Name() string

Name returns "sqlite".

func (*Dialect) NormalizeColumnType

func (d *Dialect) NormalizeColumnType(raw string) string

NormalizeColumnType canonicalizes a PRAGMA-reported column type (which for SQLite is the literal type string written at CREATE time) for comparison with ColumnType's output.

func (*Dialect) Placeholder

func (d *Dialect) Placeholder(_ int) string

Placeholder returns "?" for all positions (SQLite positional style).

func (*Dialect) Quote

func (d *Dialect) Quote(s string) string

Quote returns the double-quoted string identifier.

func (*Dialect) SelectSQL

func (d *Dialect) SelectSQL(q dal.Select) (string, []any)

SelectSQL renders a Select into SQLite SQL and a positional args slice.

func (*Dialect) UpdateSQL

func (d *Dialect) UpdateSQL(tableName, id string, fields map[string]any) (string, []any)

UpdateSQL renders an UPDATE statement for SQLite.

Jump to

Keyboard shortcuts

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