lode/query/sql

Render a Query into parameterized PostgreSQL.

Returns the SQL string plus the ordered parameter values (the Lit nodes, lifted out as $1, $2, ...). This is the Postgres dialect; other dialects would implement the same Query -> #(String, List(Value)) shape. It also serves as Phase 3’s planner check: if a query renders to the expected SQL, its structure is sound.

Types

A SQL dialect: the handful of points where Postgres and SQLite diverge in rendering. Everything else — identifier quoting, RETURNING *, ON CONFLICT ... DO UPDATE, the clause structure — is shared.

pub type Dialect {
  Dialect(
    placeholder: fn(Int) -> String,
    ilike: String,
    supports_lock: Bool,
  )
}

Constructors

  • Dialect(
      placeholder: fn(Int) -> String,
      ilike: String,
      supports_lock: Bool,
    )

    Arguments

    placeholder

    Render the n-th (1-based) bound parameter’s placeholder.

    ilike

    Case-insensitive LIKE: Postgres ILIKE; SQLite has none, so LIKE (case-insensitive for ASCII).

    supports_lock

    Whether row-lock clauses (FOR UPDATE) are emitted (Postgres) or dropped (SQLite has no such clause).

Values

pub fn delete_sql(
  q: query.Query,
  dialect dialect: Dialect,
) -> #(String, List(value.Value))

DELETE FROM source AS alias WHERE ....

pub fn insert_all_sql(
  source source: String,
  columns columns: List(String),
  rows rows: List(List(value.Value)),
  on_conflict oc: on_conflict.OnConflict(row),
  dialect dialect: Dialect,
) -> #(String, List(value.Value))

INSERT INTO source (cols) VALUES ($1, ...), ($n, ...) [ON CONFLICT ...] RETURNING * — one statement for many rows. Every inner list of rows must align with columns (same length, same order); placeholders are numbered row-major, with any Set values after the row values.

pub fn insert_sql(
  source source: String,
  prefix prefix: option.Option(String),
  columns columns: List(String),
  values values: List(value.Value),
  on_conflict oc: on_conflict.OnConflict(row),
  dialect dialect: Dialect,
) -> #(String, List(value.Value))

INSERT INTO source (cols) VALUES ($1..) [ON CONFLICT ...] RETURNING *. columns and values must be aligned (same length, same order). The caller validates the on-conflict policy (see repo); a DO UPDATE here must have a target and set at least one column.

pub fn postgres() -> Dialect

The PostgreSQL dialect: $1, $2, ..., ILIKE, row locks.

pub fn sqlite() -> Dialect

The SQLite dialect: ?1, ?2, ..., LIKE (case-insensitive ASCII), no locks.

pub fn to_sql(
  q: query.Query,
  dialect dialect: Dialect,
) -> #(String, List(value.Value))

Render a query to #(sql, params) in the given dialect.

pub fn update_sql(
  query q: query.Query,
  set sets: List(query.Assignment),
  dialect dialect: Dialect,
) -> #(String, List(value.Value))

UPDATE source AS alias SET col = $n, ... WHERE ....

Search Document