pgc_statement (pgc v0.2.0)

Copy Markdown View Source

Statements for pgc:execute/2,3: a text, a text with its parameters, or a template.

pgc_statement:new(~"select 1"),
pgc_statement:new({~"select $1::int4", [42]}).

A template is a list of text and tokens. A {parameter, Info} token becomes a placeholder bound to its value. Parameters with the same key share a placeholder, and type adds a cast. An {identifier, Info} token becomes a quoted identifier.

Statement = pgc_statement:new([
    ~"select * from ", {identifier, #{value => ~"user"}},
    ~" where id = ", {parameter, #{key => id, value => 42, type => int8}},
    ~" or parent_id = ", {parameter, #{key => id, value => 42}}
]),
~"select * from \"user\" where id = ($1::int8) or parent_id = $1" =
    iolist_to_binary(pgc_statement:text(Statement)),
[42] = pgc_statement:parameters(Statement).

Summary

Functions

Return the statement parameters

Return the statement text

Types

t()

-type t() :: #statement{}.

template()

template_identifier()

-type template_identifier() :: {identifier, template_identifier_info()}.

template_identifier_info()

-type template_identifier_info() :: #{value := atom() | unicode:unicode_binary()}.

template_parameter()

-type template_parameter() :: {parameter, template_parameter_info()}.

template_parameter_info()

-type template_parameter_info() ::
          #{key => term(), type => atom() | unicode:unicode_binary() | string(), value := term()}.

Functions

new(Input)

-spec new(Input) -> t()
             when Input :: unicode:unicode_binary() | {unicode:chardata(), [term()]} | t() | template().

parameters/1

-spec parameters(t()) -> [term()].

Return the statement parameters

text/1

-spec text(t()) -> iodata().

Return the statement text