PgRest.SQL.Where (PgRest v0.3.0)

Copy Markdown View Source

Compiles filter and logic-tree AST nodes (PgRest.AST.Condition, PgRest.AST.Logic) to a SQL boolean expression.

Every request value is a bind parameter bound as text and cast in SQL to the column's catalog type ($n::text::<type>), so enum, citext, int4 declared :decimal and timestamptz declared :utc_datetime columns compare in their real type and keep their indexes. Malformed values surface as Postgres's own 22P02. Lists (in, any, all) bind as text[] and cast as <type>[]; array literals are never built from request text. JSON-path keys are always bind parameters, never spliced into the SQL text.

Type text from the catalog is trusted. Its typmod is dropped before it is used as a cast target (numeric(10,2) becomes numeric, character varying(3) becomes character varying, character(3) becomes bpchar, bit(3) becomes varbit), because an explicit cast to a typmod type rounds or truncates the request value (price=eq.9.994 must not match 9.99); PostgREST compares untruncated. Pattern operators and JSON-path keys bind a bare $n, so Postgres infers their type like PostgREST's unknown literals. String ("S") and enum ("E") columns bind a bare $n too (no ::text::<type> cast) for every operator that takes its type from the column (comparisons, in, any/all, isdistinct and the raw operators such as cs), so Postgres infers varchar, citext or the enum type from the column itself. A ->> / -> JSON-path target is not a column of that category: it is cast by its result type (text / jsonb).

The bare bind is encoded by Postgrex for the inferred type. The name type (category "S") therefore accepts at most 63 bytes: a longer filter value raises DBConnection.EncodeError instead of comparing as a longer, non-matching string, which is a known divergence from PostgREST.

in.() and in.("") are the empty list, as upstream.

Known divergences

The any/all quantifier array {a,b} is split by the grammar, not by Postgres, so a literal NULL stays the text "NULL" and backslashes are not array escapes, unlike PostgREST, which hands the literal to Postgres.

Options

  • :categories - %{field_name => pg_type.typcategory} from PgRest.Catalog.column_categories/3; "S" (string) and "E" (enum) columns compare against a bare $n. Defaults to none.
  • :columns - %{field_name => source_column} for resources whose Ecto field names differ from the database column (source:). Defaults to the identity.

Summary

Functions

Compiles node against the table aliased table_alias.

Functions

compile(node, table_alias, column_types, builder, opts \\ [])

Compiles node against the table aliased table_alias.

column_types maps field names to catalog types (a missing type binds as plain text).