Typed SQL runtime for Hot.

Write portable SQL with ? (positional) or :name (named) placeholders; drivers compile them to their native style ($1 for PostgreSQL; ? passes through for MySQL and SQLite). ?? escapes a literal ? (PostgreSQL JSONB operators: ??, ??|, ??&), and strings, quoted identifiers, comments, casts, and dollar-quoted bodies are never rewritten (see ::sql::compile/compile). Results and errors have one shape across drivers.

Error contract: every function returns a Result.Err carrying a SqlError on failure — branch with is-err / if-err. An unhandled error halts at first use with the same message.

Quick start

::sql ::sql
// drivers install separately: hot.dev/sql-pg, sql-mysql, sql-sqlite
db ::sql::pg/open({user: "postgres", database: "app"})
// or: ::sql::mysql/open({user: "root", database: "app"})
// or: ::sql::sqlite/open("data/app.db")   — embedded, no server

rows ::sql/query(db, "SELECT id, email FROM users WHERE active = ?", [true])
user ::sql/query-one(db, "SELECT * FROM users WHERE id = ?", [42])
n ::sql/scalar(db, "SELECT count(*) FROM users")

::sql/transaction(db, (tx) {
  ::sql/execute(tx, "UPDATE accounts SET balance = balance - ? WHERE id = ?", [100, 1])
  ::sql/execute(tx, "UPDATE accounts SET balance = balance + ? WHERE id = ?", [100, 2])
})

::sql/close(db)

Typed queries

db ::sql::pg/open({user: "postgres", database: "app"})
User type { id: Int, email: Str, name: Str? }

get-user ::sql/Query({
  sql: "SELECT id, email, name FROM users WHERE id = :id",
  returns: User,
  mode: "one"
})

user ::sql/run(db, get-user, {id: 42})

Functions

ansi-quote-ident

fn (name: Str): Str

Quote an identifier ANSI-style: wrapped in double quotes with embedded double quotes doubled (weird"name → "weird""name"). Correct for PostgreSQL and SQLite. Rejects empty identifiers and identifiers containing NUL.

Example

::sql/ansi-quote-ident("orders")  // "\"orders\""
::sql/ansi-quote-ident("weird\"name")  // "\"weird\"\"name\""

close

fn (db: Db): Bool

Close the database connection. Safe to call more than once.

Example

db ::sql::pg/open({user: "postgres", database: "app"})
::sql/query(db, "SELECT id FROM users")
::sql/close(db)

execute

fn (db: Db, sql: Str): Map
fn (db: Db, sql: Str, params: Any): Map

Run a statement for its effect. Returns {command: Str, rows-affected: Int}.

Example

db ::sql::pg/open({user: "postgres", database: "app"})
::sql/execute(db, "DELETE FROM sessions WHERE expires_at < ?", ["2026-01-01"])

open

fn (driver: Driver, options: Map): Db

Open a database using a driver, e.g. ::sql/open(::sql::pg/driver, {user: "postgres", database: "app"}). Driver packages usually provide a shortcut (::sql::pg/open(options)). Returns a Db, or an Err carrying a SqlError.

Example

db ::sql/open(::sql::pg/driver, {user: "postgres", database: "app"})
::sql/scalar(db, "SELECT count(*) FROM users")
::sql/close(db)

query

fn (db: Db, sql: Str): Vec
fn (db: Db, sql: Str, params: Any): Vec

Run a query and return rows as Vec<Map> (column name -> value). Placeholders are ? (bind a Vec) or :name (bind a Map).

Example

db ::sql::pg/open({user: "postgres", database: "app"})
::sql/query(db, "SELECT * FROM users WHERE active = ?", [true])
::sql/query(db, "SELECT * FROM users WHERE email = :email", {email: "a@b.com"})

query-one

fn (db: Db, sql: Str): Any
fn (db: Db, sql: Str, params: Any): Any

Run a query and return the first row as a Map, or null when there are no rows.

Example

db ::sql::pg/open({user: "postgres", database: "app"})
user ::sql/query-one(db, "SELECT id, email FROM users WHERE id = ?", [42])
if(is-null(user), "no such user", user.email)

quote-ident

fn (db: Db, name: Str): Str

Quote an identifier (table or column name) in the database's dialect, via the driver's quote-ident or the ANSI default. Values must use placeholders — this is only for the identifiers that placeholders cannot parameterize. Quote each part of a qualified name separately.

Example

db ::sql::pg/open({user: "postgres", database: "app"})
user-chosen-table "orders"
table ::sql/quote-ident(db, user-chosen-table)
rows ::sql/query(db, `SELECT * FROM ${table} WHERE id = ?`, [42])

// Qualified names: quote each part
target `${::sql/quote-ident(db, "sales")}.${::sql/quote-ident(db, "orders")}`

run

fn (db: Db, q: Query): Any
fn (db: Db, q: Query, params: Any): Any

Run a typed Query value. Parameters bind like query (Vec for ?, Map for :name). Rows are coerced through returns when set, and the result shape follows mode:

  • "many" (default) — Vec of rows
  • "one" — first row or null
  • "scalar" — first column of the first row, or null
  • "execute" — {command, rows-affected}

Example

db ::sql::pg/open({user: "postgres", database: "app"})
User type { id: Int, email: Str, name: Str? }

get-user ::sql/Query({
  sql: "SELECT id, email, name FROM users WHERE id = :id",
  returns: User,
  mode: "one"
})
user ::sql/run(db, get-user, {id: 42})

scalar

fn (db: Db, sql: Str): Any
fn (db: Db, sql: Str, params: Any): Any

Run a query and return the first column of the first row, or null when there are no rows.

Example

db ::sql::pg/open({user: "postgres", database: "app"})
n ::sql/scalar(db, "SELECT count(*) FROM users")

transaction

fn (db: Db, body: Any): Any

Run body (a fn taking the Db) inside BEGIN/COMMIT. If body returns an Err, the transaction rolls back and that Err is returned; otherwise it commits and returns the body's result.

A halt (fail()) inside body ends the run; the server discards the uncommitted transaction when the connection closes.

Example

db ::sql::pg/open({user: "postgres", database: "app"})
::sql/transaction(db, (tx) {
  if-ok(::sql/execute(tx, "UPDATE accounts SET balance = balance - ? WHERE id = ?", [100, 1]), (debited) {
    ::sql/execute(tx, "UPDATE accounts SET balance = balance + ? WHERE id = ?", [100, 2])
  })
})

Types

Db

Db type {
    driver: Driver,
    conn: Any
}

An open database handle: the driver plus its live connection. Pass it to query/execute/run/transaction.

Driver

Driver type {
    name: Str,
    placeholder: Str,
    connect: Any,
    query: Any,
    execute: Any,
    close: Any,
    quote-ident: Any?
}

The driver contract. A driver supplies its native placeholder style and the four connection primitives; everything else (placeholder compilation, typed queries, transactions, error normalization) is shared runtime.

Fields

  • name — driver label used in SqlError.driver; also selects the placeholder compiler's dialect lexing ("pg", "mysql", "sqlite" — other names get the placeholder style's defaults)
  • placeholder — "dollar" ($1, $2, ...) or "question" (?)
  • connect — fn (options: Map) returning a connection or Err
  • query — fn (conn, sql: Str, params: Vec) returning Vec<Map> or Err
  • execute — fn (conn, sql: Str, params: Vec) returning {command, rows-affected} or Err
  • close — fn (conn) returning Bool
  • quote-ident — optional fn (name: Str) quoting an identifier in the driver's dialect; when absent the runtime uses ANSI double-quoting (correct for PostgreSQL and SQLite; a MySQL driver supplies backtick quoting here)

Query

Query type {
    sql: Str,
    returns: Any?,
    mode: Str?
}

A typed, reusable query value (inspired by Rust's sqlx).

Fields

  • sql — SQL with ? or :name placeholders
  • returns — optional row type; rows are coerced through it
  • mode — "many" (default, Vec of rows), "one" (first row or null), "scalar" (first column of first row), or "execute" ({command, rows-affected})

Example

db ::sql::pg/open({user: "postgres", database: "app"})
User type { id: Int, email: Str }

ActiveUsers ::sql/Query({
  sql: "SELECT id, email FROM users WHERE active = :active",
  returns: User,
  mode: "many"
})
users ::sql/run(db, ActiveUsers, {active: true})

SqlError

SqlError type {
    driver: Str,
    code: Str?,
    message: Str,
    detail: Str?,
    cause: Any?
}

Normalized SQL error shape shared by all drivers.

Fields

  • driver — which driver produced the error (e.g. "pg")
  • code — driver/native error code when available (PostgreSQL SQLSTATE)
  • message — human-readable message
  • detail — additional detail when the driver provides it
  • cause — the raw driver error payload