glimr_sqlite/db/query

SQLite Query Execution

The sqlight library returns its own error types but the rest of the framework expects DbError. This module bridges that gap — wrapping sqlight calls so all query errors surface as the unified DbError type and callers never depend on sqlight directly.

Types

Aliasing sqlight.Connection locally lets downstream code reference Connection without importing sqlight directly, so the driver can be swapped without touching every call site.

pub type Connection =
  sqlight.Connection

Values

pub fn exec(
  conn: sqlight.Connection,
  sql: String,
) -> Result(Nil, db.DbError)

DDL and write operations don’t return rows, so a separate function avoids forcing callers to supply a decoder they’d never use. Errors are still mapped to DbError for consistency with query.

pub fn query(
  conn: sqlight.Connection,
  sql: String,
  params: List(sqlight.Value),
  decoder: decode.Decoder(t),
) -> Result(List(t), db.DbError)

Thin wrapper so application code can run SELECT queries without importing sqlight or handling its error types. The decoder is passed through unchanged since sqlight already supports Gleam decoders.

pub fn vtable_exec(
  handle: dynamic.Dynamic,
  sql: String,
  params: List(db.Value),
) -> Result(Int, db.DbError)

sqlight.exec doesn’t accept parameters, so writes with params must go through sqlight.query with a dummy decoder. The branch on empty params avoids that workaround when no parameters are needed, using the more efficient exec path instead.

pub fn vtable_query(
  handle: dynamic.Dynamic,
  sql: String,
  params: List(db.Value),
  decoder: decode.Decoder(a),
) -> Result(db.QueryResult(a), db.DbError)

The framework’s db dispatches through a vtable of Dynamic-typed callbacks so it stays driver-agnostic. This function satisfies that interface by coercing the handle and converting generic Values to sqlight-specific values before executing.

Search Document