sqlode/runtime

Values and query descriptors used by code that sqlode generate writes.

Generated modules import this module. Each query becomes a RawQuery whose SQL holds sqlode’s own parameter markers. prepare turns a query and its parameters into SQL for the target database and the list of values to bind, so a custom adapter only has to hand both to its driver.

let #(sql, values) = runtime.prepare(queries.get_author(), params)
// sql: "SELECT id, name FROM authors WHERE id = $1"
// values: [runtime.Int(1)]

Types

The placeholder syntax of the target database driver.

VariantSyntaxPostgreSQLMySQLSQLite
DollarNumbered$1, $2, …✓——
QuestionNumbered?1, ?2, …——✓
QuestionPositionalbare ?—✓✓
pub type PlaceholderStyle {
  DollarNumbered
  QuestionNumbered
  QuestionPositional
}

Constructors

  • DollarNumbered

    $1, $2, …, used by PostgreSQL.

  • QuestionNumbered

    ?1, ?2, …, used by SQLite.

  • QuestionPositional

    A bare ? per value, matched by position. Used by MySQL and SQLite.

The -- name: X :command annotation of a query, which decides what the generated function returns.

pub type QueryCommand {
  One
  Many
  Exec
  ExecResult
  ExecRows
  ExecLastId
}

Constructors

  • One

    :one: at most one row.

  • Many

    :many: a list of rows.

  • Exec

    :exec: no rows.

  • ExecResult

    :execresult: the driver’s result value.

  • ExecRows

    :execrows: the number of affected rows.

  • ExecLastId

    :execlastid: the id of the last inserted row.

The name, SQL and shape of a query, without its parameter encoder. Generated modules list every query as a QueryInfo in all().

pub type QueryInfo {
  QueryInfo(
    name: String,
    sql: String,
    command: QueryCommand,
    param_count: Int,
  )
}

Constructors

  • QueryInfo(
      name: String,
      sql: String,
      command: QueryCommand,
      param_count: Int,
    )

    Arguments

    name

    The name from -- name:.

    sql

    The SQL with placeholders rendered for the target database.

    command

    The query’s annotation.

    param_count

    The number of parameters before any slice is expanded.

A query as sqlode generate emits it. The type parameter p is the parameter type the query takes.

pub type RawQuery(p) {
  RawQuery(
    name: String,
    sql: String,
    command: QueryCommand,
    param_count: Int,
    placeholder_style: PlaceholderStyle,
    encode: fn(p) -> List(Value),
    slice_info: fn(p) -> List(#(Int, Int)),
  )
}

Constructors

  • RawQuery(
      name: String,
      sql: String,
      command: QueryCommand,
      param_count: Int,
      placeholder_style: PlaceholderStyle,
      encode: fn(p) -> List(Value),
      slice_info: fn(p) -> List(#(Int, Int)),
    )

    Arguments

    name

    The name from -- name:.

    sql

    The SQL with sqlode’s parameter markers in place of placeholders.

    command

    The query’s annotation.

    param_count

    The number of parameter markers in sql.

    placeholder_style

    The placeholder syntax prepare renders the markers in.

    encode

    Turns the parameters into the values to bind, in placeholder order.

    slice_info

    Returns #(index, length) for each sqlode.slice parameter: its 1-based parameter index and how many values the list holds.

A parameter value handed to the database driver.

Each variant is also a function, so an encoder can be passed where a fn(a) -> Value is expected, as in list.map(ids, runtime.Int).

pub type Value {
  Null
  String(String)
  Int(Int)
  Float(Float)
  Bool(Bool)
  Bytes(BitArray)
  Array(List(Value))
}

Constructors

  • Null

    SQL NULL.

  • String(String)

    A text value.

  • Int(Int)

    An integer.

  • Float(Float)

    A floating point number.

  • Bool(Bool)

    A boolean.

  • Bytes(BitArray)

    Binary data, such as a BYTEA or BLOB value.

  • Array(List(Value))

    A PostgreSQL array.

Values

pub fn nullable(
  value: option.Option(a),
  encode: fn(a) -> Value,
) -> Value

Encodes Some(x) with encode and None as Null.

Examples

assert runtime.nullable(Some("Ann"), runtime.String) == runtime.String("Ann")
assert runtime.nullable(None, runtime.String) == runtime.Null
pub fn prepare(
  query: RawQuery(p),
  params: p,
) -> #(String, List(Value))

Encodes the parameters and renders the SQL in the query’s placeholder style. Pass the returned SQL and values to the database driver.

A sqlode.slice parameter becomes one placeholder per list element, and an empty list becomes NULL, so IN (...) matches no rows.

Examples

let query =
  runtime.RawQuery(
    name: "ListBooks",
    sql: "SELECT id FROM books WHERE author_id = __sqlode_param_1__"
      <> " AND id IN (__sqlode_slice_2__)",
    command: runtime.Many,
    param_count: 2,
    placeholder_style: runtime.DollarNumbered,
    encode: fn(p: #(Int, List(Int))) {
      [runtime.Int(p.0), ..list.map(p.1, runtime.Int)]
    },
    slice_info: fn(p: #(Int, List(Int))) { [#(2, list.length(p.1))] },
  )

assert runtime.prepare(query, #(7, [1, 2]))
  == #(
    "SELECT id FROM books WHERE author_id = $1 AND id IN ($2, $3)",
    [runtime.Int(7), runtime.Int(1), runtime.Int(2)],
  )

Panics

Panics when param_count is negative, or when slice_info returns an index outside 1..param_count or a negative length. Generated code never does.

✨ Search Document