Current section

Files

Jump to
feather src feather@pool.erl
Raw

src/feather@pool.erl

-module(feather@pool).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([start/2, with_connection/3, with_transaction/3]).
-spec start(feather:config(), integer()) -> {ok,
gleam@erlang@process:subject(puddle:manager_message(sqlight:connection(), any()))} |
{error, gleam@otp@actor:start_error()}.
start(Config, Count) ->
puddle:start(Count, fun() -> _pipe = feather:connect(Config),
gleam@result:nil_error(_pipe) end).
-spec with_connection(
gleam@erlang@process:subject(puddle:manager_message(sqlight:connection(), KND)),
integer(),
fun((sqlight:connection()) -> KND)
) -> {ok, KND} | {error, nil}.
with_connection(Pool, Timeout, Fxn) ->
puddle:apply(Pool, Fxn, Timeout, fun gleam@function:identity/1).
-spec with_transaction(
gleam@erlang@process:subject(puddle:manager_message(sqlight:connection(), {ok,
KNG} |
{error, nil})),
integer(),
fun((sqlight:connection()) -> {ok, KNG} | {error, nil})
) -> {ok, KNG} | {error, nil}.
with_transaction(Pool, Timeout, Fxn) ->
Result = with_connection(
Pool,
Timeout,
fun(Connection) ->
gleam@result:'try'(
begin
_pipe = sqlight:exec(
<<"BEGIN TRANSACTION;"/utf8>>,
Connection
),
gleam@result:nil_error(_pipe)
end,
fun(_) -> case Fxn(Connection) of
{ok, Val} ->
_assert_subject = sqlight:exec(
<<"COMMIT TRANSACTION;"/utf8>>,
Connection
),
{ok, _} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"feather/pool"/utf8>>,
function => <<"with_transaction"/utf8>>,
line => 36})
end,
{ok, Val};
{error, nil} ->
_ = sqlight:exec(
<<"ROLLBACK TRANSACTION;"/utf8>>,
Connection
),
{error, nil}
end end
)
end
),
gleam@result:flatten(Result).