Current section
Files
Jump to
Current section
Files
src/cake@adapter@mysql.erl
-module(cake@adapter@mysql).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/cake/adapter/mysql.gleam").
-export([read_query_to_prepared_statement/1, write_query_to_prepared_statement/1, execute_raw_sql/2, run_read_query/3, run_write_query/3, run_query/3, with_connection/6]).
-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(
" 🎂Cake 🐬MySQL adapter which passes `PreparedStatement`s\n"
" to the `shork` library for execution.\n"
"\n"
).
-file("src/cake/adapter/mysql.gleam", 50).
?DOC(" Convert a Cake `ReadQuery` to a `PreparedStatement`.\n").
-spec read_query_to_prepared_statement(cake@internal@read_query:read_query()) -> cake@internal@prepared_statement:prepared_statement().
read_query_to_prepared_statement(Query) ->
_pipe = Query,
cake@dialect@mysql_dialect:read_query_to_prepared_statement(_pipe).
-file("src/cake/adapter/mysql.gleam", 58).
?DOC(" Convert a Cake `WriteQuery` to a `PreparedStatement`.\n").
-spec write_query_to_prepared_statement(
cake@internal@write_query:write_query(any())
) -> cake@internal@prepared_statement:prepared_statement().
write_query_to_prepared_statement(Query) ->
_pipe = Query,
cake@dialect@mysql_dialect:write_query_to_prepared_statement(_pipe).
-file("src/cake/adapter/mysql.gleam", 133).
-spec execute_raw_sql(binary(), shork:connection()) -> {ok, shork:returned(nil)} |
{error, shork:query_error()}.
execute_raw_sql(Sql_string, On) ->
_pipe = Sql_string,
_pipe@1 = shork:'query'(_pipe),
shork:execute(_pipe@1, On).
-file("src/cake/adapter/mysql.gleam", 142).
-spec cake_param_to_client_param(cake@param:param()) -> shork:value().
cake_param_to_client_param(Param) ->
case Param of
{bool_param, Param@1} ->
shork_ffi:coerce(Param@1);
{float_param, Param@2} ->
shork_ffi:coerce(Param@2);
{int_param, Param@3} ->
shork_ffi:coerce(Param@3);
{string_param, Param@4} ->
shork_ffi:coerce(Param@4);
null_param ->
shork_ffi:null();
{date_param, Param@5} ->
{date, Year, Month, Day} = Param@5,
Year@1 = begin
_pipe = Year,
_pipe@1 = erlang:integer_to_binary(_pipe),
gleam@string:pad_start(_pipe@1, 4, <<"0"/utf8>>)
end,
Month@1 = begin
_pipe@2 = Month,
_pipe@3 = gleam@time@calendar:month_to_int(_pipe@2),
_pipe@4 = erlang:integer_to_binary(_pipe@3),
gleam@string:pad_start(_pipe@4, 2, <<"0"/utf8>>)
end,
Day@1 = begin
_pipe@5 = Day,
_pipe@6 = erlang:integer_to_binary(_pipe@5),
gleam@string:pad_start(_pipe@6, 2, <<"0"/utf8>>)
end,
Date = <<<<<<<<Year@1/binary, "-"/utf8>>/binary, Month@1/binary>>/binary,
"-"/utf8>>/binary,
Day@1/binary>>,
_pipe@7 = Date,
shork_ffi:coerce(_pipe@7)
end.
-file("src/cake/adapter/mysql.gleam", 166).
-spec shork_parameters(shork:'query'(STS), list(shork:value())) -> shork:'query'(STS).
shork_parameters(Pg_qry, Db_params) ->
_pipe = Db_params,
gleam@list:fold(
_pipe,
Pg_qry,
fun(Pg_qry@1, Db_param) -> _pipe@1 = Pg_qry@1,
shork:parameter(_pipe@1, Db_param) end
).
-file("src/cake/adapter/mysql.gleam", 64).
-spec run_read_query(
cake@internal@read_query:read_query(),
gleam@dynamic@decode:decoder(SSZ),
shork:connection()
) -> {ok, list(SSZ)} | {error, shork:query_error()}.
run_read_query(Query, Decoder, On) ->
Prepared_statement = begin
_pipe = Query,
read_query_to_prepared_statement(_pipe)
end,
Sql_string = begin
_pipe@1 = Prepared_statement,
cake:get_sql(_pipe@1)
end,
Db_params = begin
_pipe@2 = Prepared_statement,
_pipe@3 = cake:get_params(_pipe@2),
gleam@list:map(_pipe@3, fun cake_param_to_client_param/1)
end,
Result = begin
_pipe@4 = Sql_string,
_pipe@5 = shork:'query'(_pipe@4),
_pipe@6 = shork_parameters(_pipe@5, Db_params),
_pipe@7 = shork:returning(_pipe@6, Decoder),
shork:execute(_pipe@7, On)
end,
case Result of
{ok, {returned, _, V}} ->
{ok, V};
{error, E} ->
{error, E}
end.
-file("src/cake/adapter/mysql.gleam", 91).
?DOC(" Run a Cake `WriteQuery` against an MySQL database.\n").
-spec run_write_query(
cake@internal@write_query:write_query(any()),
gleam@dynamic@decode:decoder(STE),
shork:connection()
) -> {ok, list(STE)} | {error, shork:query_error()}.
run_write_query(Query, Decoder, On) ->
Prepared_statement = begin
_pipe = Query,
write_query_to_prepared_statement(_pipe)
end,
Sql_string = begin
_pipe@1 = Prepared_statement,
cake:get_sql(_pipe@1)
end,
Db_params = begin
_pipe@2 = Prepared_statement,
_pipe@3 = cake:get_params(_pipe@2),
gleam@list:map(_pipe@3, fun cake_param_to_client_param/1)
end,
Result = begin
_pipe@4 = Sql_string,
_pipe@5 = shork:'query'(_pipe@4),
_pipe@6 = shork_parameters(_pipe@5, Db_params),
_pipe@7 = shork:returning(_pipe@6, Decoder),
shork:execute(_pipe@7, On)
end,
case Result of
{ok, {returned, _, V}} ->
{ok, V};
{error, E} ->
{error, E}
end.
-file("src/cake/adapter/mysql.gleam", 120).
?DOC(
" Run a Cake `CakeQuery` against an MySQL database.\n"
"\n"
" This function is a wrapper around `run_read_query` and `run_write_query`.\n"
).
-spec run_query(
cake:cake_query(STJ),
gleam@dynamic@decode:decoder(STJ),
shork:connection()
) -> {ok, list(STJ)} | {error, shork:query_error()}.
run_query(Query, Decoder, Db_connection) ->
case Query of
{cake_read_query, Read_query} ->
_pipe = Read_query,
run_read_query(_pipe, Decoder, Db_connection);
{cake_write_query, Write_query} ->
_pipe@1 = Write_query,
run_write_query(_pipe@1, Decoder, Db_connection)
end.
-file("src/cake/adapter/mysql.gleam", 176).
-spec option_apply(STW, gleam@option:option(STX), fun((STW, STX) -> STW)) -> STW.
option_apply(Builder, Option, Function) ->
case Option of
{some, Value} ->
_pipe = Builder,
Function(_pipe, Value);
none ->
Builder
end.
-file("src/cake/adapter/mysql.gleam", 25).
?DOC(
" Connection to a MySQL database.\n"
"\n"
" This is a thin wrapper around the `shork` library's `Connection` type.\n"
).
-spec with_connection(
binary(),
integer(),
gleam@option:option(binary()),
binary(),
binary(),
fun((shork:connection()) -> SSW)
) -> SSW.
with_connection(Host, Port, Username, Password, Database, Callback) ->
Connection = begin
_pipe = shork:default_config(),
_pipe@1 = shork:host(_pipe, Host),
_pipe@2 = shork:port(_pipe@1, Port),
_pipe@3 = option_apply(_pipe@2, Username, fun shork:user/2),
_pipe@4 = shork:password(_pipe@3, Password),
_pipe@5 = shork:database(_pipe@4, Database),
shork:connect(_pipe@5)
end,
Value = Callback(Connection),
shork:disconnect(Connection),
Value.