Current section
Files
Jump to
Current section
Files
src/glite@internal@connection_actor.erl
-module(glite@internal@connection_actor).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glite/internal/connection_actor.gleam").
-export([start/1]).
-export_type([message/0, actor_state/0]).
-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(false).
-type message() :: {exec,
binary(),
list(glite@value:value()),
gleam@erlang@process:subject({ok, integer()} |
{error, glite@error:error()})} |
{'query',
binary(),
list(glite@value:value()),
gleam@erlang@process:subject({ok, glite@connection:query_result()} |
{error, glite@error:error()})} |
{exec_many,
binary(),
list(list(glite@value:value())),
gleam@erlang@process:subject({ok, integer()} |
{error, glite@error:error()})} |
{exec_sql,
binary(),
gleam@erlang@process:subject({ok, nil} | {error, glite@error:error()})} |
{last_insert_rowid, gleam@erlang@process:subject(integer())} |
{disconnect, gleam@erlang@process:subject(nil)}.
-type actor_state() :: {actor_state,
glite@connection:db_handle(),
glite@config:config()}.
-file("src/glite/internal/connection_actor.gleam", 58).
?DOC(false).
-spec handle_message(actor_state(), message()) -> gleam@otp@actor:next(actor_state(), message()).
handle_message(State, Msg) ->
case Msg of
{exec, Sql, Params, Reply} ->
Result = glite@connection:execute(
erlang:element(2, State),
Sql,
Params
),
gleam@erlang@process:send(Reply, Result),
gleam@otp@actor:continue(State);
{'query', Sql@1, Params@1, Reply@1} ->
Result@1 = glite@connection:'query'(
erlang:element(2, State),
Sql@1,
Params@1
),
gleam@erlang@process:send(Reply@1, Result@1),
gleam@otp@actor:continue(State);
{exec_many, Sql@2, Params_list, Reply@2} ->
Result@2 = glite@connection:execute_many(
erlang:element(2, State),
Sql@2,
Params_list
),
gleam@erlang@process:send(Reply@2, Result@2),
gleam@otp@actor:continue(State);
{exec_sql, Sql@3, Reply@3} ->
Result@3 = glite@connection:exec_sql(
erlang:element(2, State),
Sql@3
),
gleam@erlang@process:send(Reply@3, Result@3),
gleam@otp@actor:continue(State);
{last_insert_rowid, Reply@4} ->
Rowid = glite@connection:last_insert_rowid(erlang:element(2, State)),
gleam@erlang@process:send(Reply@4, Rowid),
gleam@otp@actor:continue(State);
{disconnect, Reply@5} ->
_ = glite@connection:close(erlang:element(2, State)),
gleam@erlang@process:send(Reply@5, nil),
gleam@otp@actor:stop()
end.
-file("src/glite/internal/connection_actor.gleam", 101).
?DOC(false).
-spec error_to_string(glite@error:error()) -> binary().
error_to_string(Err) ->
case Err of
{sqlite_error, _, Msg} ->
<<"SQLite error: "/utf8, Msg/binary>>;
{connection_error, Msg@1} ->
<<"Connection error: "/utf8, Msg@1/binary>>;
{encode_error, Msg@2} ->
<<"Encode error: "/utf8, Msg@2/binary>>;
{decode_error, Msg@3} ->
<<"Decode error: "/utf8, Msg@3/binary>>;
busy_error ->
<<"Database busy"/utf8>>;
{constraint_error, Msg@4} ->
<<"Constraint error: "/utf8, Msg@4/binary>>;
timeout_error ->
<<"Timeout"/utf8>>
end.
-file("src/glite/internal/connection_actor.gleam", 40).
?DOC(false).
-spec start(glite@config:config()) -> {ok,
gleam@otp@actor:started(gleam@erlang@process:subject(message()))} |
{error, gleam@otp@actor:start_error()}.
start(Config) ->
_pipe@2 = gleam@otp@actor:new_with_initialiser(
erlang:element(3, Config) + 1000,
fun(Subject) -> case glite@connection:open(Config) of
{ok, Db} ->
State = {actor_state, Db, Config},
_pipe = gleam@otp@actor:initialised(State),
_pipe@1 = gleam@otp@actor:returning(_pipe, Subject),
{ok, _pipe@1};
{error, E} ->
{error, error_to_string(E)}
end end
),
_pipe@3 = gleam@otp@actor:on_message(_pipe@2, fun handle_message/2),
gleam@otp@actor:start(_pipe@3).