Lotus.SQL.Transformer (Lotus v1.2.0)

Copy Markdown View Source

Shared SQL transformation utilities used by dialect implementations.

Dialect modules call these helpers from their transform_statement/1 callback to normalize SQL before variables are bound.

Every transform tokenizes with Lotus.Query.Tokenizer under the dialect's Lotus.Query.Tokenizer.Profile and only rewrites string literals and code, so a pattern inside a comment, a quoted identifier or a dollar-quoted body is left alone. The profile defaults to ANSI SQL.

Summary

Functions

Strip quote wrappers from simple variable placeholders.

Transform PostgreSQL INTERVAL syntax with variable placeholders.

Transform quoted wildcard patterns around variable placeholders into concatenation expressions using the given operator.

Functions

strip_quoted_variables(sql, profile \\ Profile.for_language("sql"))

@spec strip_quoted_variables(String.t(), Lotus.Query.Tokenizer.Profile.t()) ::
  String.t()

Strip quote wrappers from simple variable placeholders.

Converts '{{email}}' → {{email}} but leaves complex expressions like '%{{q}}%' unchanged (those are handled by wildcard transforms). Any string quote of the profile counts, so MySQL's "{{email}}" is unwrapped too, while a double-quoted identifier on Postgres is not.

transform_pg_intervals(sql, profile \\ Profile.for_language("sql"))

@spec transform_pg_intervals(String.t(), Lotus.Query.Tokenizer.Profile.t()) ::
  String.t()

Transform PostgreSQL INTERVAL syntax with variable placeholders.

Handles patterns like:

  • INTERVAL {{var}} → ({{var}}::text)::interval
  • INTERVAL '{{var}}' → CAST({{var}} AS interval)
  • INTERVAL '{{n}} days' → make_interval(days => ({{n}})::integer)

The keyword is matched without regard to case.

transform_wildcards(sql, operator \\ :pipe, profile \\ Profile.for_language("sql"))

@spec transform_wildcards(
  String.t(),
  :pipe | :concat_fn,
  Lotus.Query.Tokenizer.Profile.t()
) ::
  String.t()

Transform quoted wildcard patterns around variable placeholders into concatenation expressions using the given operator.

Operators

  • :pipe — uses || (Postgres, SQLite, default SQL)
  • :concat_fn — uses CONCAT() (MySQL)

Examples

transform_wildcards("'%{{q}}%'", :pipe)
# => "'%' || {{q}} || '%'"

transform_wildcards("'%{{q}}%'", :concat_fn)
# => "CONCAT('%', {{q}}, '%')"