PgRest.Grammar.Filter (PgRest v0.2.0)

Copy Markdown View Source

Parser for PostgREST horizontal filters (col=op.value) and logic trees (or=(a.eq.1,and(...))).

Operator prefixes, list values and quoted values are NimbleParsec combinators; the logic tree is walked in plain Elixir so nesting depth is counted explicitly (the cap is 16) and failures carry PostgREST-shaped positions.

Summary

Functions

Whether value starts with an operator prefix, [not.]op[(modifier)]., whatever follows it. A value that has one but fails parse_condition/2 is a malformed filter (is.maybe, in.abc); one without is not a filter at all (hello).

Splits a PostgreSQL array literal ({a,"b,c"}) into its elements, unquoting quoted elements and trimming blanks around unquoted ones. Anything else (a range, JSON, text after the closing brace) is :error.

Parses one filter. key is the query-string key (a name with an optional JSON path), value is the text after =, e.g. not.in.(1,2).

Parses one filter whose key may carry an embed path (a.b.col->x, "a.b".col), returning {embed_path, condition}.

Parses a logic tree. key is or, and, not.or or not.and, optionally behind an embed path (client.or); value is the parenthesised body, e.g. (a.eq.1,and(b.gt.2,c.not.is.null)). The embed path is dropped; use parse_logic_tree/2 when it matters.

Like parse_logic/2, returning {embed_path, tree} (upstream pLogicPath).

Functions

operator_prefix?(value)

@spec operator_prefix?(String.t()) :: boolean()

Whether value starts with an operator prefix, [not.]op[(modifier)]., whatever follows it. A value that has one but fails parse_condition/2 is a malformed filter (is.maybe, in.abc); one without is not a filter at all (hello).

parse_array_literal(value)

@spec parse_array_literal(String.t()) :: {:ok, [String.t()]} | :error

Splits a PostgreSQL array literal ({a,"b,c"}) into its elements, unquoting quoted elements and trimming blanks around unquoted ones. Anything else (a range, JSON, text after the closing brace) is :error.

parse_condition(key, value)

@spec parse_condition(String.t(), String.t()) ::
  {:ok, PgRest.AST.Condition.t()} | {:error, PgRest.Error.t()}

Parses one filter. key is the query-string key (a name with an optional JSON path), value is the text after =, e.g. not.in.(1,2).

An embed path in the key (rel.col) is parsed but dropped; use parse_filter/2 when the path matters.

parse_filter(key, value)

@spec parse_filter(String.t(), String.t()) ::
  {:ok, {[String.t()], PgRest.AST.Condition.t()}} | {:error, PgRest.Error.t()}

Parses one filter whose key may carry an embed path (a.b.col->x, "a.b".col), returning {embed_path, condition}.

parse_logic(key, value)

@spec parse_logic(String.t(), String.t()) ::
  {:ok, PgRest.AST.Logic.t()} | {:error, PgRest.Error.t()}

Parses a logic tree. key is or, and, not.or or not.and, optionally behind an embed path (client.or); value is the parenthesised body, e.g. (a.eq.1,and(b.gt.2,c.not.is.null)). The embed path is dropped; use parse_logic_tree/2 when it matters.

parse_logic_tree(key, value)

@spec parse_logic_tree(String.t(), String.t()) ::
  {:ok, {[String.t()], PgRest.AST.Logic.t()}} | {:error, PgRest.Error.t()}

Like parse_logic/2, returning {embed_path, tree} (upstream pLogicPath).