kura_query (kura v2.31.3)
View SourceComposable, functional query builder.
Build queries by piping through from/1, where/2, select/2, order_by/2,
etc. Queries are compiled to parameterized SQL by kura_query_compiler.
Q = kura_query:from(my_user),
Q1 = kura_query:where(Q, {active, true}),
Q2 = kura_query:order_by(Q1, [{name, asc}]),
Q3 = kura_query:limit(Q2, 10).
Summary
Types
One locking clause: a strength, alone, with a wait policy, or with the tables it locks and a wait policy. A table is a schema module, a table or alias name as an atom, or one as a binary.
A schema or table a query reads, and how: as its from, through a join,
or in a preload. cte marks a from or a join that names one of the
query's CTEs, which is a name and not a table: what the CTE reads is listed
from its body. A source names a CTE by its table name, a schema's being
that of its table/0, and only in a query with no prefix/2, since a
prefix qualifies the name and a qualified name is a table's.
Functions
Raise if any condition of Query would bind an encrypted field's plaintext,
with the reasons where/2 and having/2 raise, wherever the condition sits:
in the query's wheres or havings, a subquery, a CTE body or a union branch.
It also raises where order_by/2, group_by/2, distinct/2, join/5,
select/2 and select_expr/2 would: on ordering, grouping, DISTINCT ON,
joining, windowing or aggregating (sum, avg, min, max) by an
encrypted field or an alias of one, and on selecting a distance of one
({kura, {encrypted_field_in_select, Field}}). A term in a column
position that is not a column, such as a distance, is reported as
not_a_column in place of the field.
Set query to SELECT COUNT(*).
Build a pgvector distance between the vector field Field and Vector,
for use in order_by/2, select_expr/2, and as a threshold
{Distance, Op, Number} in where/2 and having/2, where Op is
'<', '<=', '>' or '>='.
Set DISTINCT ON fields. An encrypted field raises
{kura, {encrypted_field_in_distinct, Field}}, as group_by/2 does.
Escape Text for use inside a like or ilike pattern: %, _ and \
are each prefixed with a backslash, so they match themselves.
Combine queries with EXCEPT.
Start a query from a schema module, a bare table atom, or a binary table or
CTE name (see with_cte/3). Rows read from a schema are loaded through its
field types; rows read from a name come back as the driver returns them.
Set GROUP BY fields. Grouping by an encrypted field raises
{kura, {encrypted_field_in_group_by, Field}}: equal values encrypt to
different ciphertexts, so each row would be its own group.
Combine queries with INTERSECT.
Add a JOIN clause. Table can be a schema module, a raw table atom, or a
binary table or CTE name.
Conditions already added with where/2 or having/2 are checked against the
joined schema's encrypted fields, as if added after the join.
On is {LeftCol, RightCol} where LeftCol is on the previous table
(FROM for the first join, or the last joined table) and RightCol is on
the joined table. For chained joins, the left side automatically advances.
A column of On that is encrypted on its side raises
{kura, {encrypted_field_in_join, Field}}.
Lock the selected rows.
Return only soft-deleted records.
Set ORDER BY clauses as [{Expr, asc | desc}], where Expr is a field
atom or a vector distance from distance/3.
Build a window-function expression for use inside select_expr/2.
Add associations to preload after query execution.
Apply a composable query transform function.
Set the SELECT fields. Pass atoms for columns or {agg, field} tuples for
aggregates.
Set SELECT expressions with aliases. Each expression is {Alias, Expr}
where Expr is a plain field atom, a {fragment, SQL, Params}, a
window expression from over/2, or a vector distance from distance/3.
Every schema and table Query reads, at any depth: its from and joins,
then those of its CTE bodies, of the queries combined with it, and of the
in, exists and not_exists subqueries in its WHERE and HAVING
conditions, and last what its preloads read.
Combine queries with UNION (removes duplicates).
Combine queries with UNION ALL (keeps duplicates).
Add a WHERE condition. Conditions: {field, value}, {field, op, value},
{'and', [...]}, etc.
Add a Common Table Expression (WITH clause).
Include soft-deleted records in query results.
Types
-type distance() :: {distance, atom(), distance_metric(), binary()}.
-type distance_metric() :: l2 | cosine | inner_product.
What lock/2 takes: one clause, several, or SQL the caller vouches for.
-type lock_strength() :: for_update | for_no_key_update | for_share | for_key_share.
-type lock_wait() :: nowait | skip_locked.
-type row_lock() :: lock_strength() | {lock_strength(), lock_wait()} | {lock_strength(), #{tables => [atom() | binary(), ...], wait => lock_wait()}}.
One locking clause: a strength, alone, with a wait policy, or with the tables it locks and a wait policy. A table is a schema module, a table or alias name as an atom, or one as a binary.
A schema or table a query reads, and how: as its from, through a join,
or in a preload. cte marks a from or a join that names one of the
query's CTEs, which is a name and not a table: what the CTE reads is listed
from its body. A source names a CTE by its table name, a schema's being
that of its table/0, and only in a query with no prefix/2, since a
prefix qualifies the name and a qualified name is a table's.
A CTE's body sees the CTEs listed before it, as the WITH kura writes has
no RECURSIVE: in the body, the CTE's own name and the name of a CTE listed
after it are tables.
A later release may add a role, so a caller matches the roles it knows and passes over any other.
-type window_fn() :: {count | sum | avg | min | max, atom()} | row_number | rank | dense_rank.
Functions
-spec avg(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, atom()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec check_encrypted(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> ok.
Raise if any condition of Query would bind an encrypted field's plaintext,
with the reasons where/2 and having/2 raise, wherever the condition sits:
in the query's wheres or havings, a subquery, a CTE body or a union branch.
It also raises where order_by/2, group_by/2, distinct/2, join/5,
select/2 and select_expr/2 would: on ordering, grouping, DISTINCT ON,
joining, windowing or aggregating (sum, avg, min, max) by an
encrypted field or an alias of one, and on selecting a distance of one
({kura, {encrypted_field_in_select, Field}}). A term in a column
position that is not a column, such as a distance, is reported as
not_a_column in place of the field.
A deterministic lookup ({Field, encrypted_in | encrypted_not_in, Frames})
passes only on a field the query's scope may look up (as where/2 decides)
and only when Frames is a list of binaries laid out as deterministic
frames (version 2, mode 1, a full header); anything else raises
{kura, {invalid_encrypted_lookup, Field}}. That keeps a lookup off a
randomized or plain field and keeps any non-binary value out of one. It
does not prove the frames are ciphertexts of that column: bytes in the
frame layout pass. Only where/2 and having/2 build a lookup, and they
refuse one passed to them, so such a value can only come from a record
written by hand, which is trusted like a {fragment, SQL, Params}.
The verdict depends only on the query's structure (sources, selects, CTEs and
the shape of every condition), never on a bound value, except whether a
lookup's values are in the frame layout, which the query's shape records
(see kura_query_shape). kura_query_compiler relies on that: it calls
this on every query it compiles, except a cache hit in to_sql_cached/2,
whose shape already passed. So a #kura_query{} built or updated without
where/2 is checked too.
-spec count(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Set query to SELECT COUNT(*).
-spec count(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, atom()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec distance(atom(), distance_metric(), [number()]) -> distance().
Build a pgvector distance between the vector field Field and Vector,
for use in order_by/2, select_expr/2, and as a threshold
{Distance, Op, Number} in where/2 and having/2, where Op is
'<', '<=', '>' or '>='.
Metric is l2, cosine or inner_product. For every metric a smaller
value is nearer: inner_product is the negative inner product, as
pgvector's <#> returns it. Order asc to get the nearest rows first;
desc cannot use an approximate-nearest-neighbour index.
Vector is a list of numbers. It is cast and dumped to pgvector's text
form here, so only a binary enters the query. A list that is empty or
holds anything but numbers raises.
The query must be from/1 a schema in which Field is a vector or
{vector, N} field, and for {vector, N} the vector must have N
elements. order_by/2, select_expr/2, where/2 and having/2 raise
otherwise. A threshold that is not a number raises {kura, {invalid_distance_threshold, Field}}.
A distance anywhere else, in any other condition of where/2 or
having/2, group_by/2, distinct/2, select/2 (and the aggregate
helpers), count/2, a join column or a window from over/2, raises
{kura, {distance_not_supported_here, Clause}}.
D = kura_query:distance(embedding, cosine, QueryVector),
Q = kura_query:select_expr(kura_query:from(memory), [{id, id}, {dist, D}]),
Q1 = kura_query:where(Q, {D, '<', 0.35}),
Q2 = kura_query:limit(kura_query:order_by(Q1, [{dist, asc}]), 10).Ordering by the alias of a selected distance sends the vector once and
still uses an index. Order by D itself when the distance is not
selected.
-spec distinct(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec distinct(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, [atom()]) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Set DISTINCT ON fields. An encrypted field raises
{kura, {encrypted_field_in_distinct, Field}}, as group_by/2 does.
Escape Text for use inside a like or ilike pattern: %, _ and \
are each prefixed with a backslash, so they match themselves.
Pattern = <<(kura_query:escape_like(Input))/binary, "%">>,
Q = kura_query:where(kura_query:from(my_user), {name, like, Pattern}).The backslash is the escape character of like and ilike on every
backend: PostgreSQL's LIKE has it by default, and a dialect for a backend
whose LIKE has none, as kura_sqlite's, names it in an ESCAPE clause (see
kura_dialect). Where the whole value is to be matched as it is, use
contains, starts_with or ends_with in where/2, which escape the
value in SQL.
-spec except(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Combine queries with EXCEPT.
-spec from(module() | binary()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Start a query from a schema module, a bare table atom, or a binary table or
CTE name (see with_cte/3). Rows read from a schema are loaded through its
field types; rows read from a name come back as the driver returns them.
-spec group_by(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, [atom()]) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Set GROUP BY fields. Grouping by an encrypted field raises
{kura, {encrypted_field_in_group_by, Field}}: equal values encrypt to
different ciphertexts, so each row would be its own group.
-spec having(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, term()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Add a HAVING condition, in the same forms as where/2. A condition that
compares an encrypted field to a value raises
{kura, {encrypted_field_in_having, Field}}, as where/2 does, and an
equality on a deterministic field becomes a lookup, as in where/2.
-spec intersect(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Combine queries with INTERSECT.
-spec join(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, inner | left | right | full, module() | binary(), {atom(), atom()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Add a JOIN clause. Table can be a schema module, a raw table atom, or a
binary table or CTE name.
Conditions already added with where/2 or having/2 are checked against the
joined schema's encrypted fields, as if added after the join.
On is {LeftCol, RightCol} where LeftCol is on the previous table
(FROM for the first join, or the last joined table) and RightCol is on
the joined table. For chained joins, the left side automatically advances.
A column of On that is encrypted on its side raises
{kura, {encrypted_field_in_join, Field}}.
Q = kura_query:from(user_schema),
Q1 = kura_query:join(Q, inner, post_schema, {id, user_id}),
%% => users.id = posts.user_id
Q2 = kura_query:join(Q1, inner, comment_schema, {id, post_id}).
%% => posts.id = comments.post_id
-spec join(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, inner | left | right | full, module() | binary(), {atom(), atom()}, atom() | undefined) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec limit(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, non_neg_integer()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec lock(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, lock() | binary()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Lock the selected rows.
Lock is a row-lock strength, for_update, for_no_key_update, for_share
or for_key_share, alone, with a wait policy, or with the tables to lock:
kura_query:lock(Q, for_update).
kura_query:lock(Q, {for_update, skip_locked}).
kura_query:lock(Q, {for_update, #{tables => [posts], wait => nowait}}).
kura_query:lock(Q, [{for_update, #{tables => [posts]}}, {for_share, #{tables => [users]}}]).tables names what OF lists: a schema module, or a table or alias name as
an atom or a binary. An atom that is a schema module is always read as that
schema's table, so an alias a schema module shares its name with is given as
a binary. The dialect quotes each name, so none is ever written into the
statement as SQL. A list holds several clauses.
The same clauses are read from SQL text, whatever the case and spacing, and
stored as the term: ~"FOR UPDATE", ~"for share nowait",
~"FOR UPDATE OF posts, comments SKIP LOCKED". An unquoted table name is
folded to lower case, as PostgreSQL folds it.
Text that is not such a clause is written into the statement as it is, as
2.30 wrote any text. While strict_locks is unset in the kura
application environment that logs one warning per boot, naming the text;
{strict_locks, false} allows it without the warning. With
{strict_locks, true} it raises {kura, {invalid_lock, Lock}} instead;
that is the default in kura 3.0.
For SQL kura should not read, say so with {fragment, SQL}: it is written
as it is under either setting, without a warning, and must never hold
outside input.
Anything else raises {kura, {invalid_lock, Lock}}.
The lock Lock stands for, as lock/2 stores it. A dialect calls this on
the lock of a query, which may be a record built without lock/2, and
compiles what comes back:
- a strength, or
{Strength, Wait}; {Strength, #{tables := [Name, ...]}}, withwaitwhen there is one. EachNameis a binary, to be quoted as an identifier;- a list of two or more of those, one per clause;
{fragment, SQL}, written as it is.
Raises {kura, {invalid_lock, Lock}} as lock/2 does.
-spec max(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, atom()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec min(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, atom()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec offset(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, non_neg_integer()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec only_deleted(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Return only soft-deleted records.
-spec order_by(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, [order()]) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Set ORDER BY clauses as [{Expr, asc | desc}], where Expr is a field
atom or a vector distance from distance/3.
Ordering by an encrypted field raises
{kura, {encrypted_field_in_order_by, Field}}: the order of ciphertexts
says nothing about the order of the values. A distance is checked by its
column, so the reason names the field and never carries the vector.
-spec over(window_fn(), window_spec()) -> {over, window_fn(), window_spec()}.
Build a window-function expression for use inside select_expr/2.
WindowFn is an aggregate ({count, '*'}, {count, Field}, {sum, Field},
{avg, Field}, {min, Field}, {max, Field}) or a ranking function atom
(row_number, rank, dense_rank). Spec is a map with optional
partition_by => [Field] and order_by => [{Field, asc | desc}].
Window functions require a backend declaring the window_functions
capability (PostgreSQL, or SQLite 3.25+).
-spec prefix(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, binary()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec preload(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, [atom() | {atom(), list() | map()}]) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Add associations to preload after query execution.
Each is an association's name, {Name, Nested} to preload the Nested
associations on the records it loads, or {Name, Opts} to load a
has_many or has_one association with conditions: Opts is a map of
where, a condition as where/2 takes it, order_by, as order_by/2
takes it, and limit. They apply to the one query that loads the
association for every record, so limit caps the rows of that query, not
the rows of each record.
kura_query:preload(Q, [author, {comments, [author]}]),
kura_query:preload(Q, [{comments, #{where => {approved, true}, order_by => [{inserted_at, desc}]}}]).A belongs_to or many_to_many association given Opts is loaded whole:
the map is not applied to it. A through association is preloaded by its
name only.
-spec scope(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, fun((#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()})) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Apply a composable query transform function.
-spec select(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, [atom() | term()]) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Set the SELECT fields. Pass atoms for columns or {agg, field} tuples for
aggregates.
sum, avg, min or max of an encrypted field raises
{kura, {encrypted_field_in_aggregate, Field}}: it would be computed over
ciphertexts. count is allowed.
-spec select_expr(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, [term()]) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Set SELECT expressions with aliases. Each expression is {Alias, Expr}
where Expr is a plain field atom, a {fragment, SQL, Params}, a
window expression from over/2, or a vector distance from distance/3.
A distance over a field that may hold ciphertext, such as any column of a
CTE whose encrypted columns cannot be named, raises
{kura, {encrypted_field_in_select, Field}}: the distance would be
computed over ciphertexts.
Q = kura_query:select_expr(kura_query:from(sales), [
{category, category},
{rn, kura_query:over(row_number, #{partition_by => [category], order_by => [{amount, desc}]})},
{running_total, kura_query:over({sum, amount}, #{partition_by => [category], order_by => [{day, asc}]})}
]).
-spec sources(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> [source()].
Every schema and table Query reads, at any depth: its from and joins,
then those of its CTE bodies, of the queries combined with it, and of the
in, exists and not_exists subqueries in its WHERE and HAVING
conditions, and last what its preloads read.
A preload is resolved from the associations of the from schema: the
associated schema, the join table of a many-to-many, and each step of a
through. Only the preloads of Query itself are listed, since those of a
nested query are never run. A source may be listed more than once.
Each source is {Role, SchemaOrTable}, the role being from, join, cte
or preload (source/0). The set of roles may grow in a later release:
match the roles you need and tolerate one you do not know.
Q = kura_query:preload(kura_query:join(kura_query:from(post), inner, author, {author_id, id}), [tags]),
[{from, post}, {join, author}, {preload, tag}, {preload, ~"posts_tags"}] = kura_query:sources(Q).
-spec sum(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, atom()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
-spec union(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Combine queries with UNION (removes duplicates).
-spec union_all(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Combine queries with UNION ALL (keeps duplicates).
-spec where(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, term()) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Add a WHERE condition. Conditions: {field, value}, {field, op, value},
{'and', [...]}, etc.
{Field, is_nil} and {Field, is_not_nil} are the NULL tests, so in the
two-element form those two atoms are operators, never values. A condition
whose value is an atom that comes from outside, such as an enum filter read
from a request, must use the three-element form: {Field, '=', Value} always
binds Value.
{Field, contains, Text}, {Field, starts_with, Text} and
{Field, ends_with, Text} match Text as it is: % and _ in it are not
wildcards. icontains, istarts_with and iends_with do the same ignoring
case. {Field, like, Pattern} and {Field, ilike, Pattern} take a pattern,
in which % and _ are wildcards; see escape_like/1 before putting
outside input into one.
{Distance, Op, Threshold} keeps the rows whose vector distance from
distance/3 compares to the number Threshold by '<', '<=', '>'
or '>='. The distance is checked against the schema as in
order_by/2, and its column by the encrypted-field guard below.
A condition that compares an encrypted field (see
kura_types:encryption/1) to a value raises
{kura, {encrypted_field_in_where, Field}}, wherever the field appears: under
and, or or not, in an in list, or as one column of a row-value in.
Randomized encryption uses a random nonce, so a freshly encrypted value never
equals the stored ciphertext: the condition cannot match, and it would ship
the plaintext to the server as a bind parameter, into pg_stat_statements,
the query log and telemetry.
A deterministic field ({encrypted, Inner, #{mode => deterministic}}) can be
looked up by equality. {Field, Value}, {Field, '=', Value} and
{Field, in, Values} become {Field, encrypted_in, Ciphertexts}, and
{Field, '!=', Value} and {Field, not_in, Values} become
{Field, encrypted_not_in, Ciphertexts}, here, before the condition is
stored: each value is cast and normalised as a write of it would be and
encrypted under every lookup key (see kura_encryption), so the plaintext
never enters the query, its cache key, telemetry or logs. The dialect
compiles them to IN and NOT IN. A value that does not cast to a non-null
value raises {kura, {invalid_lookup_value, Field}}; use is_nil for
NULL. Every other operator on a deterministic field raises as on a
randomized one, and so do a row-value in and an in subquery. A field is
looked up only when exactly one source of the query (the from schema or a
joined one, not one read through a CTE) declares it encrypted; otherwise it
raises as on a randomized field. encrypted_in and encrypted_not_in are
built by kura: passing one to where/2 raises
{kura, {reserved_operator, Op}}.
The lookup reveals which rows share the value, and the ciphertexts bound as parameters are a stable pseudonym of it in telemetry and logged parameters.
For a deterministic field where/2 reads the keyring, so it can raise a
keyring error such as {kura_crypto, no_deterministic_key}. != and
not_in on a deterministic field return the rows stored under a key
generation missing from deterministic_keys, since those never equal the
excluded value.
Fields of the from schema and of every joined schema are
checked. So are the conditions of an in or exists subquery, against the
subquery's own schemas and against the outer query's encrypted fields that
its schemas do not declare: the database resolves such a name to the outer
column. {Field, is_nil} and {Field, is_not_nil} bind nothing and are
allowed. A {fragment, SQL, Params} is not inspected: what it binds is the
caller's responsibility. A query over a bare table name has no schema and is
not checked. A {subquery, Q} anywhere but exists, not_exists or a
single-column in raises {kura, unrecognised_condition}.
The condition is stored as given. Whether a value is converted to the
field's type before it is bound is up to the dialect, when the query is
compiled: kura_dialect_pg does it (see kura_dialect_pg:convert_values/1),
a dialect without convert_values/1 binds every value as given.
-spec with_cte(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, binary(), #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Add a Common Table Expression (WITH clause).
A query, subquery or CTE that reads Name (by its name, or by a schema whose
table is named Name) gets the CTE's encrypted fields in its filter guard
scope: every encrypted field of CteQuery's sources, and every alias it
selects one under. The conditions already in the query, and the bodies of
its CTEs, are checked again, so the order of where/2 and with_cte/3 does
not matter. A name used by more than one CTE, or by a CTE and a schema, gets
the encrypted fields of all of them.
-spec with_deleted(#kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Include soft-deleted records in query results.
A schema with a deleted_at field is soft-deletable: kura_repo_worker's
reads (all/2, get/3, one/2, exists/2, count/2, aggregate/3,4),
update_all/3, delete_all/2 and kura_stream:stream/3,4 leave out its
rows whose deleted_at is set unless the query is marked with this
function. The filter applies to the query's own from schema; the bodies
of its CTEs, subqueries and combined queries are not filtered, so add
{deleted_at, is_nil} to them where needed.