kura_dialect_pg (kura v2.31.3)

View Source

PostgreSQL dialect. Translates a #kura_query{} AST into parameterized SQL using PostgreSQL's $N placeholder convention.

This module is an implementation of kura_dialect. Public callers go through kura_query_compiler, which delegates to the dialect configured for the running app.

Summary

Functions

Convert the values of a query's where conditions to what PostgreSQL would parse them as. kura_query_compiler calls this before it compiles the query or splits it into a cached shape.

Compile a query record into {SQL, Params}.

Compile a query starting parameter numbering from StartCounter. Returns {SQL, Params, NextCounter}.

Functions

column_type(Type)

-spec column_type(kura_types:kura_type()) -> binary().

convert_values/1

-spec convert_values(#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()}.

Convert the values of a query's where conditions to what PostgreSQL would parse them as. kura_query_compiler calls this before it compiles the query or splits it into a cached shape.

A value compared to a field of a from schema is converted only where it then binds the value the database would parse from it: an atom on an {enum, [...]} field binds its binary ({status, published} binds ~"published"), and a binary in the form kura_changeset:cast/4 accepts binds the value it spells on an id, integer, bigint, smallint, float, boolean, date or time field (~"42" binds 42). A datetime or time binary is cast only without a UTC offset other than Z and without fractional seconds, since the cast drops a fraction and drops the offset of a time or naive datetime. An integer binary longer than 20 bytes, or a float binary longer than 64, is not parsed. This covers =, !=, <, >, <=, >=, in, not_in and between, under and, or and not, in the query and in every CTE, combined query and subquery it holds, each against its own from schema.

Everything else is bound as given: a value the type rejects, decimal (its cast parses a float, which would lose digits), string, text, binary, uuid, jsonb, embed, array, vector and custom fields, fields of joined schemas, CTEs and bare table names, like, ilike, matches and fragments. Havings are not converted.

The conversion is PostgreSQL's reading of a value, so it lives here and not in kura_query:where/2: SQLite, for one, would compare a converted boolean true with an INTEGER column as the text 'true'.

delete(SchemaOrTable, KeyClauses)

-spec delete(kura_dialect:table(), [{atom(), term()}]) -> {iodata(), [term()]}.

delete_all/1

-spec delete_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()}) ->
                    {iodata(), [term()]}.

format_default/1

-spec format_default(term()) -> binary().

insert(SchemaOrTable, Fields, Data)

-spec insert(kura_dialect:table(), [atom()], map()) -> {iodata(), [term()]}.

insert/4

-spec insert(kura_dialect:table(), [atom()], map(), map()) -> {iodata(), [term()]}.

insert_all(SchemaOrTable, Fields, Rows)

-spec insert_all(kura_dialect:table(), [atom()], [map()]) -> {iodata(), [term()]}.

insert_all(SchemaOrTable, Fields, Rows, Opts)

-spec insert_all(kura_dialect:table(), [atom()], [map()], map()) -> {iodata(), [term()]}.

placeholder(N)

-spec placeholder(pos_integer()) -> iodata().

to_sql(Query)

-spec to_sql(#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()}) ->
                {iodata(), [term()]}.

Compile a query record into {SQL, Params}.

to_sql_from/2

-spec to_sql_from(#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()},
                  pos_integer()) ->
                     {iodata(), [term()], pos_integer()}.

Compile a query starting parameter numbering from StartCounter. Returns {SQL, Params, NextCounter}.

update(SchemaOrTable, Fields, Changes, KeyClauses)

-spec update(kura_dialect:table(), [atom()], map(), [{atom(), term()}]) -> {iodata(), [term()]}.

update_all/2

-spec update_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()},
                 map()) ->
                    {iodata(), [term()]}.