Current section

Files

Jump to
cake src cake@fragment.erl
Raw

src/cake@fragment.erl

-module(cake@fragment).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/cake/fragment.gleam").
-export([int/1, prepared/2, literal/1, bool/1, true/0, false/0, float/1, string/1, null/0, date/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Fragments are low level building blocks of queries which allow direct\n"
" manipulation of the query string.\n"
"\n"
" If you want to insert parameters, you are required to use prepared\n"
" fragments, which will be validated against the number of parameters given\n"
" and the parameters are automatically escaped by the RDBMS to prevent SQL\n"
" injections.\n"
"\n"
).
-file("src/cake/fragment.gleam", 147).
?DOC(" Create a new `Param` with an `Int` value.\n").
-spec int(integer()) -> cake@param:param().
int(Value) ->
_pipe = Value,
{int_param, _pipe}.
-file("src/cake/fragment.gleam", 51).
?DOC(
" Create a new fragment from a string and a list of parameters.\n"
"\n"
" ⛔ ⛔ ⛔\n"
"\n"
" If you mismatch the number of placeholders with the number of\n"
" parameters, an error will be printed to stderr and the fragment will be\n"
" created with the given parameters:\n"
"\n"
" - If there are too many placeholders, the fragment will be created with the\n"
" given parameters and the last parameter will be repeated for the remaining\n"
" placeholders.\n"
" - If there are too many parameters, the fragment will be created with the\n"
" given parameters and the excess parameters will be ignored.\n"
"\n"
" ⛔ ⛔ ⛔\n"
).
-spec prepared(binary(), list(cake@param:param())) -> cake@internal@read_query:fragment().
prepared(String, Params) ->
Placeholder_count = begin
_pipe = String,
_pipe@1 = cake@internal@read_query:fragment_prepared_split_string(_pipe),
cake@internal@read_query:fragment_count_placeholders(_pipe@1)
end,
Param_count = begin
_pipe@2 = Params,
erlang:length(_pipe@2)
end,
case {Placeholder_count,
Param_count,
begin
_pipe@3 = Placeholder_count,
gleam@int:compare(_pipe@3, Param_count)
end} of
{0, 0, eq} ->
_pipe@4 = String,
{fragment_literal, _pipe@4};
{_, _, eq} ->
_pipe@5 = String,
{fragment_prepared, _pipe@5, Params};
{0, _, _} ->
gleam_stdlib:println_error(
<<<<<<<<"Fragment had 0 "/utf8, (<<"$"/utf8>>)/binary>>/binary,
"-placeholders, but "/utf8>>/binary,
(begin
_pipe@6 = Param_count,
erlang:integer_to_binary(_pipe@6)
end)/binary>>/binary,
" params given!"/utf8>>
),
_pipe@7 = String,
{fragment_literal, _pipe@7};
{_, 0, _} ->
gleam_stdlib:println_error(
<<<<<<<<"Fragment had "/utf8,
(begin
_pipe@8 = Placeholder_count,
erlang:integer_to_binary(_pipe@8)
end)/binary>>/binary,
" "/utf8>>/binary,
(<<"$"/utf8>>)/binary>>/binary,
"-placeholders, but 0 params given!"/utf8>>
),
_pipe@9 = String,
{fragment_literal, _pipe@9};
{_, _, _} ->
gleam_stdlib:println_error(
<<<<<<<<<<<<"Fragment had "/utf8,
(begin
_pipe@10 = Placeholder_count,
erlang:integer_to_binary(_pipe@10)
end)/binary>>/binary,
" "/utf8>>/binary,
(<<"$"/utf8>>)/binary>>/binary,
"-placeholders, but "/utf8>>/binary,
(begin
_pipe@11 = Param_count,
erlang:integer_to_binary(_pipe@11)
end)/binary>>/binary,
" params given!"/utf8>>
),
_pipe@12 = String,
{fragment_prepared, _pipe@12, Params}
end.
-file("src/cake/fragment.gleam", 113).
?DOC(
" Create a new fragment from a literal string.\n"
"\n"
" ⛔ ⛔ ⛔\n"
"\n"
" WARNING: YOU ARE FORBIDDEN TO INSERT UNCONTROLLED USER INPUT THIS WAY!\n"
"\n"
" ⛔ ⛔ ⛔\n"
).
-spec literal(binary()) -> cake@internal@read_query:fragment().
literal(String) ->
_pipe = String,
{fragment_literal, _pipe}.
-file("src/cake/fragment.gleam", 123).
?DOC(" Create a new `Param` with a `Bool` value.\n").
-spec bool(boolean()) -> cake@param:param().
bool(Value) ->
_pipe = Value,
{bool_param, _pipe}.
-file("src/cake/fragment.gleam", 129).
?DOC(" Create a new `Param` with a `True` value.\n").
-spec true() -> cake@param:param().
true() ->
_pipe = true,
{bool_param, _pipe}.
-file("src/cake/fragment.gleam", 135).
?DOC(" Create a new `Param` with a `False` value.\n").
-spec false() -> cake@param:param().
false() ->
_pipe = false,
{bool_param, _pipe}.
-file("src/cake/fragment.gleam", 141).
?DOC(" Create a new `Param` with a `Float` value.\n").
-spec float(float()) -> cake@param:param().
float(Value) ->
_pipe = Value,
{float_param, _pipe}.
-file("src/cake/fragment.gleam", 153).
?DOC(" Create a new `Param` with a `String` value.\n").
-spec string(binary()) -> cake@param:param().
string(Value) ->
_pipe = Value,
{string_param, _pipe}.
-file("src/cake/fragment.gleam", 159).
?DOC(" Create a new `Param` with an SQL `NULL` value.\n").
-spec null() -> cake@param:param().
null() ->
null_param.
-file("src/cake/fragment.gleam", 165).
?DOC(" Create a new `Param` with a `calendar.Date` value.\n").
-spec date(gleam@time@calendar:date()) -> cake@param:param().
date(Value) ->
_pipe = Value,
{date_param, _pipe}.