# `PgRest.SQL.Builder`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/sql/builder.ex#L1)

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.

# `mode`

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

How a parameter value is bound.

# `t`

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

# `append_params`

```elixir
@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`

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

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

# `cast`

```elixir
@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`

```elixir
@spec column(
  String.t(),
  PgRest.AST.Field.t(),
  %{required(String.t()) =&gt; 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`

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

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

# `ident_list`

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

Quoted identifiers joined with `, `.

# `json_path`

```elixir
@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`

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

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

# `maybe_cast`

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

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

# `new`

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

Starts an empty builder.

## Options

  * `:repo` - the Ecto repo used to compile scoped Ecto queries to SQL

# `param`

```elixir
@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`

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

Number of parameters bound so far.

# `qualified`

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

A schema-qualified, quoted relation name.

# `to_sql`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
