Current section
Files
Jump to
Current section
Files
src/corrosion@transaction.erl
-module(corrosion@transaction).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/corrosion/transaction.gleam").
-export([transact/2]).
-export_type([exec_result/0, exec_response/0]).
-type exec_result() :: {execute, integer(), float()} | {error, binary()}.
-type exec_response() :: {exec_response,
list(exec_result()),
float(),
gleam@option:option(integer()),
gleam@option:option(binary())}.
-file("src/corrosion/transaction.gleam", 27).
-spec transact(gleam@uri:uri(), list(corrosion@statement:statement())) -> {ok,
exec_response()} |
{error, httpp@hackney:error()}.
transact(Corro_uri, Statements) ->
Decoder = begin
Exec_result_decoder = begin
Execute_decoder = begin
gleam@dynamic@decode:field(
<<"rows_affected"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Rows_affected) ->
gleam@dynamic@decode:field(
<<"time"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Time) ->
gleam@dynamic@decode:success(
{execute, Rows_affected, Time}
)
end
)
end
)
end,
Error_decoder = begin
gleam@dynamic@decode:field(
<<"error"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Error) ->
gleam@dynamic@decode:success({error, Error})
end
)
end,
gleam@dynamic@decode:one_of(Execute_decoder, [Error_decoder])
end,
gleam@dynamic@decode:field(
<<"results"/utf8>>,
gleam@dynamic@decode:list(Exec_result_decoder),
fun(Results) ->
gleam@dynamic@decode:field(
<<"time"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Time@1) ->
gleam@dynamic@decode:optional_field(
<<"version"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_int/1}
),
fun(Version) ->
gleam@dynamic@decode:optional_field(
<<"actor_id"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Actor_id) ->
gleam@dynamic@decode:success(
{exec_response,
Results,
Time@1,
Version,
Actor_id}
)
end
)
end
)
end
)
end
)
end,
Uri = {uri,
erlang:element(2, Corro_uri),
erlang:element(3, Corro_uri),
erlang:element(4, Corro_uri),
erlang:element(5, Corro_uri),
<<"/v1/transactions"/utf8>>,
erlang:element(7, Corro_uri),
erlang:element(8, Corro_uri)},
Body = begin
_pipe = gleam@json:array(
Statements,
fun corrosion@internal@util:statement_to_json/1
),
_pipe@1 = gleam_json_ffi:json_to_iodata(_pipe),
gleam_stdlib:wrap_list(_pipe@1)
end,
Base_request@1 = case gleam@http@request:from_uri(Uri) of
{ok, Base_request} -> Base_request;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"corrosion/transaction"/utf8>>,
function => <<"transact"/utf8>>,
line => 71,
value => _assert_fail,
start => 1732,
'end' => 1783,
pattern_start => 1743,
pattern_end => 1759})
end,
Request = begin
_pipe@2 = Base_request@1,
_pipe@3 = gleam@http@request:set_header(
_pipe@2,
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
),
gleam@http@request:set_body(_pipe@3, Body)
end,
gleam@result:'try'(
httpp@send:send_bits(Request),
fun(Result) ->
Response@1 = case gleam@json:parse_bits(
erlang:element(4, Result),
Decoder
) of
{ok, Response} -> Response;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"corrosion/transaction"/utf8>>,
function => <<"transact"/utf8>>,
line => 78,
value => _assert_fail@1,
start => 1964,
'end' => 2027,
pattern_start => 1975,
pattern_end => 1987})
end,
{ok, Response@1}
end
).