Current section
Files
Jump to
Current section
Files
src/based_pg.erl
-module(based_pg).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([adapter/1]).
-export_type([config/0]).
-type config() :: {config, binary(), integer(), binary(), binary(), binary()}.
-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 => 81})
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 connect(config()) -> gleam@pgo:connection().
connect(Config) ->
{config, Host, Port, Database, User, Password} = Config,
Conn = gleam_pgo_ffi:connect(
erlang:setelement(
9,
erlang:setelement(
6,
erlang:setelement(
5,
erlang:setelement(
4,
erlang:setelement(
3,
erlang:setelement(
2,
gleam@pgo:default_config(),
Host
),
Port
),
Database
),
User
),
{some, Password}
),
5
)
),
Conn.
-spec with_connection(config(), fun((gleam@pgo:connection()) -> ILQ)) -> ILQ.
with_connection(Config, Callback) ->
Conn = connect(Config),
Result = Callback(Conn),
gleam_pgo_ffi:disconnect(Conn),
Result.
-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 adapter(config()) -> based:based_adapter(config(), gleam@pgo:connection(), any()).
adapter(Config) ->
{based_adapter, fun with_connection/2, Config, fun execute/2}.