qast (equery v0.22.0)

View Source

Low-level AST primitives and SQL emission.

All SQL fragments in equery are represented as ast_node() tuples:

ConstructorMeaning
qast:value(V) / qast:value(V, Opts)A $N parameter placeholder. V is captured as a query argument.
qast:raw(S) / qast:raw(S, Opts)Literal SQL fragment — inlined as-is. Never use with user input.
qast:alias(Ref) / qast:alias(Ref, Opts)Identifier alias resolved at emission time using a stable name like "__alias-0".
qast:exp(List) / qast:exp(List, Opts)A composition of nested AST nodes.
qast:field(TRef, Name, Opts)Shortcut for "alias"."name".

Opts is a map carrying optional metadata, most importantly #{type => T} — used for column type inference in projections.

Render with to_sql/1 to get a parameterized {Sql, Args} pair.

Summary

Types

alias()

-type alias() :: {'$alias', opts(), reference()}.

ast_node()

-type ast_node() :: raw() | value() | alias() | exp().

exp()

-type exp() :: {'$exp', opts(), [ast_node() | term()]}.

opts()

-type opts() :: #{type => term(), term() => term()}.

raw()

-type raw() :: {'$raw', opts(), iodata()}.

value()

-type value() :: {'$value', opts(), term()}.

Functions

alias(Ref)

-spec alias(reference()) -> alias().

alias(Ref, Opts)

-spec alias(reference(), opts()) -> alias().

exp(V)

-spec exp([ast_node() | term()]) -> exp().

exp(V, Opts)

-spec exp([ast_node() | term()], opts()) -> exp().

field(TableRef, Name, Opts)

-spec field(reference(), atom(), opts()) -> exp().

is_ast/1

-spec is_ast(term()) -> boolean().

join/2

-spec join([ast_node() | term()], ast_node()) -> exp().

opts/1

-spec opts(ast_node() | term()) -> opts().

raw(V)

-spec raw(iodata()) -> raw().

raw(V, Opts)

-spec raw(iodata(), opts()) -> raw().

set_opts/2

-spec set_opts(ast_node() | term(), opts()) -> ast_node().

to_sql(Ast)

-spec to_sql(ast_node()) -> {Sql :: binary(), Args :: [term()]}.

Render an AST into a parameterized SQL string.

Returns {Sql, Args} ready for epgsql:equery/3 or similar. Non-AST terms appearing anywhere in the AST are auto-wrapped as qast:value/1 placeholders.

value(V)

-spec value(term()) -> value().

value(V, Opts)

-spec value(term(), opts()) -> value().