Processes [[...]] optional clause syntax in any text-based query language.
Clauses wrapped in double brackets are stripped entirely when the enclosed variables have no value, making them optional. When all variables inside a block have values, the brackets are removed and the content is kept.
The [[ ... ]] / {{var}} template syntax is language-agnostic — it works
on SQL, JSON DSLs, Cypher, or any other textual query format. Adapters that
work on AST representations should apply this before serialization.
Blocks are found with Lotus.Query.Tokenizer, so [[ inside a string
literal or a comment is plain text, and a {{var}} inside a comment does
not make a block conditional. Blocks nest: an inner block is resolved on
its own, and the outer block only depends on the variables directly inside
it. Every function takes an optional Lotus.Query.Tokenizer.Profile; the
default is ANSI SQL.
Example (SQL)
SELECT * FROM users
WHERE 1=1
[[AND "name" ILIKE '%' || {{name}} || '%']]
[[AND "status" = {{status}}]]If name has no value, the first [[...]] block is removed entirely.
If status has a value, the second block becomes AND "status" = {{status}}.
Summary
Functions
Returns a MapSet of variable names that appear inside [[...]] blocks.
Processes optional clauses in SQL. Removes [[...]] blocks where any
enclosed variable has no value. Keeps content (without brackets) when
all variables have values.
Strips [[ and ]] brackets from the string, keeping the inner content.
Functions
@spec extract_optional_variable_names(String.t(), Lotus.Query.Tokenizer.Profile.t()) :: MapSet.t()
Returns a MapSet of variable names that appear inside [[...]] blocks.
@spec process(String.t(), map(), Lotus.Query.Tokenizer.Profile.t()) :: String.t()
Processes optional clauses in SQL. Removes [[...]] blocks where any
enclosed variable has no value. Keeps content (without brackets) when
all variables have values.
A variable is considered to have "no value" when it is missing from
supplied_vars, is nil, or is "".
@spec strip_brackets(String.t(), Lotus.Query.Tokenizer.Profile.t()) :: String.t()
Strips [[ and ]] brackets from the string, keeping the inner content.
Unlike process/2, this does not evaluate variables — it unconditionally
removes all bracket pairs. Useful for preparing SQL for validation where
all optional clauses should be included.
Examples
iex> Lotus.Query.OptionalClause.strip_brackets("WHERE 1=1 [[AND status = 'active']]")
"WHERE 1=1 AND status = 'active'"