Documentation

Hale.PostgREST.PostgREST.Query.SqlFragment

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
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
      Instances For

        Format a column reference: "schema"."table"."column". $$\text{pgFmtColumn}(t, c) = \text{pgFmtQi}(t) \cdot \texttt{.} \cdot \text{pgFmtIdent}(c)$$

        Equations
        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 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
            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
                      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
                        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{''}$$