PgRest.SQL.Builder (PgRest v0.2.0)

Copy Markdown View Source

Accumulates bind parameters and emits SQL fragments as iodata.

Request text reaches SQL only as a bind parameter (param/4) or through ident/1 and cast/2, which validate what they emit.

Summary

Types

How a parameter value is bound.

t()

Functions

Appends already-dumped values whose placeholders are written elsewhere (spliced SQL), keeping the numbering of later param/4 calls consistent.

Binds an integer using the shared text-to-bigint SQL representation.

CAST(<expr> AS <type>). The type name is re-validated with PgRest.Grammar.Common.cast_name?/1; invalid input raises ArgumentError (the grammar has already turned it into a 400, so a raise means a bug). This is the only way a request-supplied type name reaches SQL.

A column of table_alias with its JSON path (json_path/3). As PostgREST, a path on a column whose type (column_types, keyed by field name) is not json or jsonb reads it through to_jsonb.

Double-quotes an identifier. Raises ArgumentError on a NUL byte.

Quoted identifiers joined with ,.

Appends JSON path steps ({:arrow | :double_arrow, key}) to column. Every key is a bind parameter, never spliced into the SQL text.

A bound LIMIT clause, or no clause when the limit is nil.

cast/2, or expr unchanged when type is nil.

Starts an empty builder.

Binds value and returns its placeholder.

Number of parameters bound so far.

A schema-qualified, quoted relation name.

Finalises the statement: the SQL text and the parameters in placeholder order.

Types

mode()

@type mode() :: :text | :native

How a parameter value is bound.

t()

@type t() :: %PgRest.SQL.Builder{
  count: non_neg_integer(),
  params: [term()],
  repo: module() | nil
}

Functions

append_params(builder, values)

@spec append_params(t(), [term()]) :: t()

Appends already-dumped values whose placeholders are written elsewhere (spliced SQL), keeping the numbering of later param/4 calls consistent.

bigint(builder, value)

@spec bigint(t(), integer()) :: {iodata(), t()}

Binds an integer using the shared text-to-bigint SQL representation.

cast(expr, type)

@spec cast(iodata(), String.t()) :: iodata()

CAST(<expr> AS <type>). The type name is re-validated with PgRest.Grammar.Common.cast_name?/1; invalid input raises ArgumentError (the grammar has already turned it into a 400, so a raise means a bug). This is the only way a request-supplied type name reaches SQL.

column(table_alias, field, column_types, builder)

@spec column(
  String.t(),
  PgRest.AST.Field.t(),
  %{required(String.t()) => String.t()},
  t()
) ::
  {iodata(), t()}

A column of table_alias with its JSON path (json_path/3). As PostgREST, a path on a column whose type (column_types, keyed by field name) is not json or jsonb reads it through to_jsonb.

ident(name)

@spec ident(String.t()) :: iodata()

Double-quotes an identifier. Raises ArgumentError on a NUL byte.

ident_list(names)

@spec ident_list([String.t()]) :: iodata()

Quoted identifiers joined with ,.

json_path(column, steps, builder)

@spec json_path(iodata(), [PgRest.AST.Field.json_step()], t()) :: {iodata(), t()}

Appends JSON path steps ({:arrow | :double_arrow, key}) to column. Every key is a bind parameter, never spliced into the SQL text.

Array indexes are unbounded integer text: bound as text and cast, never natively. PostgREST sends a non-negative index with a leading +, which shows in the 22003 message of an out-of-range index, so this does too. String keys bind as a bare $n so Postgres infers their type exactly as it does for PostgREST's untyped literals (jsonb ->> unknown is a key lookup, text ->> unknown is 42883).

limit(builder, value)

@spec limit(t(), non_neg_integer() | nil) :: {iodata(), t()}

A bound LIMIT clause, or no clause when the limit is nil.

maybe_cast(expr, type)

@spec maybe_cast(iodata(), String.t() | nil) :: iodata()

cast/2, or expr unchanged when type is nil.

new(opts \\ [])

@spec new(keyword()) :: t()

Starts an empty builder.

Options

  • :repo - the Ecto repo used to compile scoped Ecto queries to SQL

param(builder, value, type, mode)

@spec param(t(), term(), String.t() | nil, mode()) :: {iodata(), t()}

Binds value and returns its placeholder.

In :text mode (request-derived values) the value is bound as text and cast in SQL: $n::text or $n::text::<type>; a list binds as $n::text[]::<type>[]. In :native mode (values produced by Ecto.Type.dump/2) it emits $n, or $n::<type> when a type is given. type is always trusted text (a catalog type or a fixed PgRest type), never request text.

param_count(builder)

@spec param_count(t()) :: non_neg_integer()

Number of parameters bound so far.

qualified(schema, table)

@spec qualified(String.t(), String.t()) :: iodata()

A schema-qualified, quoted relation name.

to_sql(builder, iodata)

@spec to_sql(t(), iodata()) :: {String.t(), [term()]}

Finalises the statement: the SQL text and the parameters in placeholder order.