Current section

Files

Jump to
based_pg src based_pg.erl
Raw

src/based_pg.erl

-module(based_pg).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([adapter/1, default_config/0]).
-export_type([config/0, ip_version/0]).
-type config() :: {config,
binary(),
integer(),
binary(),
binary(),
gleam@option:option(binary()),
boolean(),
list({binary(), binary()}),
integer(),
integer(),
integer(),
integer(),
boolean(),
ip_version()}.
-type ip_version() :: ipv4 | ipv6.
-spec decode_error_message(list(gleam@dynamic:decode_error())) -> binary().
decode_error_message(Errors) ->
[{decode_error, Expected, Actual, Path} | _] = case Errors of
[{decode_error, _, _, _} | _] -> Errors;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"based_pg"/utf8>>,
function => <<"decode_error_message"/utf8>>,
line => 103})
end,
Path@1 = gleam@string:join(Path, <<"."/utf8>>),
<<<<<<<<<<"Decoder failed, expected "/utf8, Expected/binary>>/binary,
", got "/utf8>>/binary,
Actual/binary>>/binary,
" in "/utf8>>/binary,
Path@1/binary>>.
-spec to_based_error(gleam@pgo:query_error()) -> based:based_error().
to_based_error(Error) ->
case Error of
{constraint_violated, Msg, Constraint, _} ->
{based_error, <<"constraint_violated"/utf8>>, Constraint, Msg};
{postgresql_error, Code, Name, Message} ->
{based_error, Code, Name, Message};
{unexpected_argument_count, Expected, Got} ->
{based_error,
<<"unexpected_argument_count"/utf8>>,
<<""/utf8>>,
<<<<<<"Expected "/utf8, (gleam@int:to_string(Expected))/binary>>/binary,
", got "/utf8>>/binary,
(gleam@int:to_string(Got))/binary>>};
{unexpected_argument_type, Expected@1, Got@1} ->
{based_error,
<<"unexpected_argument_count"/utf8>>,
<<""/utf8>>,
<<<<<<"Expected "/utf8, Expected@1/binary>>/binary,
", got "/utf8>>/binary,
Got@1/binary>>};
{unexpected_result_type, Decode_errors} ->
{based_error,
<<"unexpected_result_type"/utf8>>,
<<""/utf8>>,
decode_error_message(Decode_errors)};
connection_unavailable ->
{based_error,
<<"connection_unavailable"/utf8>>,
<<""/utf8>>,
<<""/utf8>>}
end.
-spec to_pgo_values(list(based:value())) -> list(gleam@pgo:value()).
to_pgo_values(Values) ->
_pipe = Values,
gleam@list:map(_pipe, fun(Value) -> case Value of
{string, Val} ->
gleam_pgo_ffi:coerce(Val);
{int, Val@1} ->
gleam_pgo_ffi:coerce(Val@1);
{float, Val@2} ->
gleam_pgo_ffi:coerce(Val@2);
{bool, Val@3} ->
gleam_pgo_ffi:coerce(Val@3);
null ->
gleam_pgo_ffi:null()
end end).
-spec execute(based:'query'(), gleam@pgo:connection()) -> {ok,
list(gleam@dynamic:dynamic_())} |
{error, based:based_error()}.
execute(Query, Conn) ->
{'query', Sql, Args} = Query,
Values = begin
_pipe = Args,
to_pgo_values(_pipe)
end,
_pipe@1 = gleam@pgo:execute(Sql, Conn, Values, fun gleam@dynamic:dynamic/1),
_pipe@2 = gleam@result:map(
_pipe@1,
fun(Returned) ->
{returned, _, Rows} = Returned,
Rows
end
),
gleam@result:map_error(_pipe@2, fun to_based_error/1).
-spec to_pgo_ip_version(ip_version()) -> gleam@pgo:ip_version().
to_pgo_ip_version(Ip_version) ->
case Ip_version of
ipv4 ->
ipv4;
ipv6 ->
ipv6
end.
-spec to_pgo_config(config()) -> gleam@pgo:config().
to_pgo_config(Config) ->
{config,
Host,
Port,
Database,
User,
Password,
Ssl,
Connection_parameters,
Pool_size,
Queue_target,
Queue_interval,
Idle_interval,
Trace,
Ip_version} = Config,
{config,
Host,
Port,
Database,
User,
Password,
Ssl,
Connection_parameters,
Pool_size,
Queue_target,
Queue_interval,
Idle_interval,
Trace,
to_pgo_ip_version(Ip_version)}.
-spec with_connection(config(), fun((gleam@pgo:connection()) -> ILQ)) -> ILQ.
with_connection(Config, Callback) ->
Conn = begin
_pipe = Config,
_pipe@1 = to_pgo_config(_pipe),
gleam_pgo_ffi:connect(_pipe@1)
end,
Result = Callback(Conn),
gleam_pgo_ffi:disconnect(Conn),
Result.
-spec adapter(config()) -> based:based_adapter(config(), gleam@pgo:connection(), any()).
adapter(Config) ->
{based_adapter, fun with_connection/2, Config, fun execute/2}.
-spec from_pgo_ip_version(gleam@pgo:ip_version()) -> ip_version().
from_pgo_ip_version(Ip_version) ->
case Ip_version of
ipv4 ->
ipv4;
ipv6 ->
ipv6
end.
-spec from_pgo_config(gleam@pgo:config()) -> config().
from_pgo_config(Config) ->
{config,
Host,
Port,
Database,
User,
Password,
Ssl,
Connection_parameters,
Pool_size,
Queue_target,
Queue_interval,
Idle_interval,
Trace,
Ip_version} = Config,
{config,
Host,
Port,
Database,
User,
Password,
Ssl,
Connection_parameters,
Pool_size,
Queue_target,
Queue_interval,
Idle_interval,
Trace,
from_pgo_ip_version(Ip_version)}.
-spec default_config() -> config().
default_config() ->
_pipe = gleam@pgo:default_config(),
from_pgo_config(_pipe).