Quote a SQL identifier by wrapping in double-quotes and escaping internal double-quotes. $$\text{pgFmtIdent}(s) = \texttt{"}\ s[\texttt{"} \mapsto \texttt{""}]\ \texttt{"}$$ This prevents SQL injection in identifier contexts.
Equations
- PostgREST.Query.pgFmtIdent s = "\"" ++ s.replace "\"" "\"\"" ++ "\""
Instances For
Format a schema-qualified identifier as "schema"."name".
$$\text{pgFmtQi}(qi) = \text{pgFmtIdent}(qi.\text{schema}) \cdot \texttt{.} \cdot \text{pgFmtIdent}(qi.\text{name})$$
Equations
Instances For
Quote a SQL literal by wrapping in single-quotes and escaping internal single-quotes. $$\text{pgFmtLit}(s) = \texttt{'}\ s[\texttt{'} \mapsto \texttt{''}]\ \texttt{'}$$ Use for known-safe values only, NOT arbitrary user input (use parameterized queries for user input).
Equations
- PostgREST.Query.pgFmtLit s = "'" ++ s.replace "'" "''" ++ "'"
Instances For
Format a column reference: "schema"."table"."column".
$$\text{pgFmtColumn}(t, c) = \text{pgFmtQi}(t) \cdot \texttt{.} \cdot \text{pgFmtIdent}(c)$$
Equations
- PostgREST.Query.pgFmtColumn table col = PostgREST.Query.pgFmtQi table ++ "." ++ PostgREST.Query.pgFmtIdent col
Instances For
Format a coercible field reference with optional JSON path traversal
and type cast.
$$\text{pgFmtField}(cf, t?) = \text{base} \circ \text{jsonOps} \circ \text{cast}$$
where base is "table"."col" or just "col", jsonOps chains
-> and ->> operators, and cast appends ::type.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Map a PostgREST simple operator name to its SQL equivalent. $$\text{simpleOpToSql} : \text{String} \to \text{String}$$
Equations
- PostgREST.Query.simpleOpToSql "eq" = "="
- PostgREST.Query.simpleOpToSql "neq" = "<>"
- PostgREST.Query.simpleOpToSql "gt" = ">"
- PostgREST.Query.simpleOpToSql "gte" = ">="
- PostgREST.Query.simpleOpToSql "lt" = "<"
- PostgREST.Query.simpleOpToSql "lte" = "<="
- PostgREST.Query.simpleOpToSql "like" = "LIKE"
- PostgREST.Query.simpleOpToSql "ilike" = "ILIKE"
- PostgREST.Query.simpleOpToSql "in" = "IN"
- PostgREST.Query.simpleOpToSql "is" = "IS"
- PostgREST.Query.simpleOpToSql "isdistinct" = "IS DISTINCT FROM"
- PostgREST.Query.simpleOpToSql "cs" = "@>"
- PostgREST.Query.simpleOpToSql "cd" = "<@"
- PostgREST.Query.simpleOpToSql "ov" = "&&"
- PostgREST.Query.simpleOpToSql "sl" = "<<"
- PostgREST.Query.simpleOpToSql "sr" = ">>"
- PostgREST.Query.simpleOpToSql "nxl" = "&<"
- PostgREST.Query.simpleOpToSql "nxr" = "&>"
- PostgREST.Query.simpleOpToSql "adj" = "-|-"
- PostgREST.Query.simpleOpToSql "match" = "~"
- PostgREST.Query.simpleOpToSql "imatch" = "~*"
- PostgREST.Query.simpleOpToSql x✝ = x✝
Instances For
Map a PostgREST full-text search operator to its SQL equivalent.
All FTS variants use the @@ operator; the difference is in
how the query term is constructed.
Equations
- PostgREST.Query.ftsOpToSql "fts" = "@@"
- PostgREST.Query.ftsOpToSql "plfts" = "@@"
- PostgREST.Query.ftsOpToSql "phfts" = "@@"
- PostgREST.Query.ftsOpToSql "wfts" = "@@"
- PostgREST.Query.ftsOpToSql x✝ = x✝
Instances For
Format a filter expression as a SQL WHERE clause fragment.
Uses $? as placeholder for parameterized values.
$$\text{pgFmtFilter}(f, t?) = \text{field}\ \text{op}\ \$?$$
Equations
- One or more equations did not get rendered due to their size.
Instances For
Format a logic tree as a SQL boolean expression. $$\text{pgFmtLogicTree}(t, \text{tbl}?) = \begin{cases} \text{pgFmtFilter}(f, \text{tbl}?) & \text{if } t = \text{stmnt}(f) \\ [\lnot]\ (\text{child}_1\ \text{op}\ \text{child}_2\ \ldots) & \text{if } t = \text{expr}(\neg?, \text{op}, \text{children}) \end{cases}$$
Format an ORDER BY term as a SQL fragment. $$\text{pgFmtOrderTerm}(t, \text{tbl}?) = \text{field}\ \text{dir}\ [\text{NULLS FIRST|LAST}]$$
Equations
- One or more equations did not get rendered due to their size.
Instances For
Wrap a SQL expression in json_agg with a coalesce fallback to '[]'.
$$\text{asJsonF}(sql) = \texttt{coalesce(json\_agg(}sql\texttt{), '[]'::json)}$$
Equations
Instances For
Wrap a SQL expression in json_agg and extract the first element,
with a coalesce fallback to 'null'.
$$\text{asJsonSingleF}(sql) = \texttt{coalesce((json\_agg(}sql\texttt{))->0, 'null'::json)}$$
Equations
Instances For
Generate a SET LOCAL statement for a GUC variable.
The key is quoted as an identifier and the value as a literal.
$$\text{setConfigLocal}(k, v) = \texttt{SET LOCAL}\ \text{pgFmtIdent}(k)\ \texttt{=}\ \text{pgFmtLit}(v)$$
Equations
- PostgREST.Query.setConfigLocal key value = toString "SET LOCAL " ++ toString (PostgREST.Query.pgFmtIdent key) ++ toString " = " ++ toString (PostgREST.Query.pgFmtLit value)
Instances For
Generate a SET LOCAL statement with a constant (unescaped) key name.
Used for PostgREST internal settings where the key is a known constant.
$$\text{setConfigWithConstantName}(k, v) = \texttt{SET LOCAL "}k\texttt{" =}\ \text{pgFmtLit}(v)$$
Equations
- PostgREST.Query.setConfigWithConstantName key value = toString "SET LOCAL \"" ++ toString key ++ toString "\" = " ++ toString (PostgREST.Query.pgFmtLit value)
Instances For
Format an array of ORDER BY terms as a comma-separated SQL fragment.
Returns none if the array is empty.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Format an array of filters as an AND-connected WHERE clause fragment.
Returns none if the array is empty.
Equations
- One or more equations did not get rendered due to their size.
Instances For
pgFmtIdent always produces a string starting with ".
$$\forall s,\; \text{pgFmtIdent}(s).\text{startsWith}(\texttt{"}) = \text{true}$$
pgFmtLit always produces a string starting with '.
$$\forall s,\; \text{pgFmtLit}(s).\text{startsWith}(\texttt{'}) = \text{true}$$
Quoting and then identity-quoting an empty string:
pgFmtIdent "" = "\"\"".
$$\text{pgFmtIdent}(\varepsilon) = \texttt{""}$$
Quoting an empty literal yields '' (two single-quote characters).
$$\text{pgFmtLit}(\varepsilon) = \texttt{''}$$