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, inline]).
-define(FILEPATH, "src/based/pg.gleam").
-export([application/2, host/2, port/2, username/2, password/2, database/2, connection_parameter/3, ssl/2, rows_as_dict/2, ip_version/2, pool_size/2, idle_interval/2, queue_target/2, from_url/1, new/1, start/1, supervised/1, db/1, transaction/2, 'begin'/1, commit/1, rollback/1]).
-export_type([ip_version/0, ssl/0, db/0, connection/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.
-type ip_version() :: ipv4 | ipv6.
-type ssl() :: ssl_disabled | ssl_verified | ssl_unverified.
-opaque db() :: {db, pgl:db()}.
-opaque connection() :: {connection, pgl:connection()}.
-file("src/based/pg.gleam", 34).
?DOC(" Name of the application connecting to the database.\n").
-spec application(pgl:config(), binary()) -> pgl:config().
application(Config, Application) ->
pgl:application(Config, Application).
-file("src/based/pg.gleam", 39).
?DOC(" The database server hostname.\n").
-spec host(pgl:config(), binary()) -> pgl:config().
host(Config, Host) ->
pgl:host(Config, Host).
-file("src/based/pg.gleam", 44).
?DOC(" The port on which the database server is listening.\n").
-spec port(pgl:config(), integer()) -> pgl:config().
port(Config, Port) ->
pgl:port(Config, Port).
-file("src/based/pg.gleam", 49).
?DOC(" The username to connect to the database as.\n").
-spec username(pgl:config(), binary()) -> pgl:config().
username(Config, Username) ->
pgl:username(Config, Username).
-file("src/based/pg.gleam", 54).
?DOC(" The password of the user.\n").
-spec password(pgl:config(), binary()) -> pgl:config().
password(Config, Password) ->
pgl:password(Config, Password).
-file("src/based/pg.gleam", 59).
?DOC(" The name of the database to use.\n").
-spec database(pgl:config(), binary()) -> pgl:config().
database(Config, Database) ->
pgl:database(Config, Database).
-file("src/based/pg.gleam", 64).
?DOC(" Sets other postgres connection parameters.\n").
-spec connection_parameter(pgl:config(), binary(), binary()) -> pgl:config().
connection_parameter(Config, Name, Value) ->
pgl:connection_parameter(Config, Name, Value).
-file("src/based/pg.gleam", 73).
?DOC(" Whether SSL should be used.\n").
-spec ssl(pgl:config(), ssl()) -> pgl:config().
ssl(Config, Ssl) ->
Ssl@1 = case Ssl of
ssl_disabled ->
ssl_disabled;
ssl_verified ->
ssl_verified;
ssl_unverified ->
ssl_unverified
end,
pgl:ssl(Config, Ssl@1).
-file("src/based/pg.gleam", 84).
?DOC(" Configures rows to be returned as `Dict` rather than n-tuples.\n").
-spec rows_as_dict(pgl:config(), boolean()) -> pgl:config().
rows_as_dict(Config, Rows_as_dict) ->
pgl:rows_as_dict(Config, Rows_as_dict).
-file("src/based/pg.gleam", 89).
?DOC(" Which IP version to use\n").
-spec ip_version(pgl:config(), ip_version()) -> pgl:config().
ip_version(Config, Ip_version) ->
Ip_version@1 = case Ip_version of
ipv4 ->
ipv4;
ipv6 ->
ipv6
end,
pgl:ip_version(Config, Ip_version@1).
-file("src/based/pg.gleam", 99).
?DOC(" Sets the size of the connection pool.\n").
-spec pool_size(pgl:config(), integer()) -> pgl:config().
pool_size(Config, Pool_size) ->
pgl:pool_size(Config, Pool_size).
-file("src/based/pg.gleam", 104).
?DOC(" How often idle connections should ping the database server.\n").
-spec idle_interval(pgl:config(), integer()) -> pgl:config().
idle_interval(Config, Idle_interval) ->
pgl:idle_interval(Config, Idle_interval).
-file("src/based/pg.gleam", 109).
?DOC(" How long it should take to check out a connection from the connection pool.\n").
-spec queue_target(pgl:config(), integer()) -> pgl:config().
queue_target(Config, Queue_target) ->
pgl:queue_target(Config, Queue_target).
-file("src/based/pg.gleam", 117).
?DOC(
" Build a `Config` from a PostgreSQL connection URL.\n"
"\n"
" Supports `postgres://` and `postgresql://` schemes.\n"
" Returns `Error(Nil)` if the URL is invalid or uses an unsupported scheme.\n"
).
-spec from_url(binary()) -> {ok, pgl:config()} | {error, nil}.
from_url(Url) ->
pgl:from_url(Url).
-file("src/based/pg.gleam", 121).
-spec adapter() -> based@sql:adapter(pg_value:value()).
adapter() ->
_pipe = based@sql:adapter(),
_pipe@1 = based@sql:on_placeholder(
_pipe,
fun(I) -> <<"$"/utf8, (erlang:integer_to_binary(I))/binary>> end
),
_pipe@2 = based@sql:on_identifier(
_pipe@1,
fun(Ident) -> <<<<"\""/utf8, Ident/binary>>/binary, "\""/utf8>> end
),
_pipe@3 = based@sql:on_value(_pipe@2, fun pg_value:to_string/1),
_pipe@4 = based@sql:on_int(_pipe@3, fun pg_value:int/1),
_pipe@5 = based@sql:on_text(_pipe@4, fun pg_value:text/1),
based@sql:on_null(_pipe@5, fun() -> null end).
-file("src/based/pg.gleam", 137).
?DOC(
" Creates a new database instance from a config. Call `start` to open\n"
" the connection pool before using it.\n"
).
-spec new(pgl:config()) -> db().
new(Config) ->
{db, pgl:new(Config)}.
-file("src/based/pg.gleam", 142).
?DOC(" Starts the connection pool for the given database under a new supervisor.\n").
-spec start(db()) -> {ok,
gleam@otp@actor:started(gleam@otp@static_supervisor:supervisor())} |
{error, gleam@otp@actor:start_error()}.
start(Db) ->
pgl:start(erlang:element(2, Db)).
-file("src/based/pg.gleam", 148).
?DOC(
" Returns a child specification for starting the connection pool under an\n"
" existing supervision tree.\n"
).
-spec supervised(db()) -> gleam@otp@supervision:child_specification(gleam@otp@static_supervisor:supervisor()).
supervised(Db) ->
pgl:supervised(erlang:element(2, Db)).
-file("src/based/pg.gleam", 205).
-spec handle_postgres_error(
binary(),
binary(),
binary(),
gleam@dict:dict(pgl:field(), binary())
) -> based:database_error().
handle_postgres_error(Code, Name, Message, Fields) ->
case gleam@dict:has_key(Fields, constraint) of
true ->
{constraint_error, Code, Name, Message};
false ->
case Code of
<<"23000"/utf8>> ->
{constraint_error, Code, Name, Message};
<<"23001"/utf8>> ->
{constraint_error, Code, Name, Message};
<<"23502"/utf8>> ->
{constraint_error, Code, Name, Message};
<<"23503"/utf8>> ->
{constraint_error, Code, Name, Message};
<<"23505"/utf8>> ->
{constraint_error, Code, Name, Message};
<<"23514"/utf8>> ->
{constraint_error, Code, Name, Message};
<<"23P01"/utf8>> ->
{constraint_error, Code, Name, Message};
<<"42601"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42846"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42803"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P20"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P19"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42830"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42602"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42622"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42939"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42804"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P18"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P21"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P22"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42809"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"428C9"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42703"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42883"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P01"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P02"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42704"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42701"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P03"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P04"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42723"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P05"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P06"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P07"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42712"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42710"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42702"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42725"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P08"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P09"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P10"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42611"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P11"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P12"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P13"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P14"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P15"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P16"/utf8>> ->
{syntax_error, Code, Name, Message};
<<"42P17"/utf8>> ->
{syntax_error, Code, Name, Message};
_ ->
{database_error, Code, Name, Message}
end
end.
-file("src/based/pg.gleam", 193).
-spec handle_error(pgl:pgl_error()) -> based:based_error().
handle_error(Err) ->
case Err of
{postgres_error, Code, Name, Message, Fields} ->
_pipe = handle_postgres_error(Code, Name, Message, Fields),
{db_error, _pipe};
{connection_error, Message@1} ->
_pipe@1 = {connection_error, Message@1},
{db_error, _pipe@1};
connection_timeout ->
_pipe@2 = connection_timeout,
{db_error, _pipe@2};
Pgl_error ->
{based_error, pgl:error_to_string(Pgl_error)}
end.
-file("src/based/pg.gleam", 188).
-spec db_query_to_pg_query(based@sql:'query'(pg_value:value())) -> pgl:'query'().
db_query_to_pg_query(Query) ->
_pipe = pgl:sql(erlang:element(2, Query)),
pgl:values(_pipe, erlang:element(3, Query)).
-file("src/based/pg.gleam", 170).
-spec batch(list(based@sql:'query'(pg_value:value())), connection()) -> {ok,
list(based:queried())} |
{error, based:based_error()}.
batch(Queries, Conn) ->
_pipe = Queries,
_pipe@1 = gleam@list:map(_pipe, fun db_query_to_pg_query/1),
_pipe@2 = pgl:batch(_pipe@1, erlang:element(2, Conn)),
_pipe@3 = gleam@result:map_error(_pipe@2, fun handle_error/1),
gleam@result:map(_pipe@3, fun(Queried) -> _pipe@4 = Queried,
gleam@list:map(
_pipe@4,
fun(Pgl_queried) ->
{queried, Count, Fields, Rows} = Pgl_queried,
{queried, Count, Fields, Rows}
end
) end).
-file("src/based/pg.gleam", 166).
-spec execute(binary(), connection()) -> {ok, integer()} |
{error, based:based_error()}.
execute(Sql, Conn) ->
_pipe = pgl:execute(Sql, erlang:element(2, Conn)),
gleam@result:map_error(_pipe, fun handle_error/1).
-file("src/based/pg.gleam", 267).
-spec 'query'(based@sql:'query'(pg_value:value()), connection()) -> {ok,
based:queried()} |
{error, based:based_error()}.
'query'(Query, Conn) ->
_pipe = Query,
_pipe@1 = db_query_to_pg_query(_pipe),
_pipe@2 = pgl:'query'(_pipe@1, erlang:element(2, Conn)),
_pipe@3 = gleam@result:map_error(_pipe@2, fun handle_error/1),
gleam@result:map(
_pipe@3,
fun(Pgl_queried) ->
{queried, Count, Fields, Rows} = Pgl_queried,
{queried, Count, Fields, Rows}
end
).
-file("src/based/pg.gleam", 158).
?DOC(
" Returns a `based.Db` that can be used with `based.query`, `based.execute`,\n"
" `based.all`, and other `based` functions.\n"
).
-spec db(db()) -> based:db(pg_value:value(), connection()).
db(Db) ->
_pipe = erlang:element(2, Db),
_pipe@1 = pgl:connection(_pipe),
_pipe@2 = {connection, _pipe@1},
_pipe@3 = based:driver(_pipe@2, fun 'query'/2, fun execute/2, fun batch/2),
based:new(_pipe@3, adapter()).
-file("src/based/pg.gleam", 305).
-spec pgl_tx_err_to_db_tx_err(pgl:transaction_error(RZX)) -> based:transaction_error(RZX).
pgl_tx_err_to_db_tx_err(Err) ->
case Err of
{rollback_error, Cause} ->
{rollback, Cause};
not_in_transaction ->
not_in_transaction;
{transaction_error, Message} ->
{transaction_error, Message}
end.
-file("src/based/pg.gleam", 285).
?DOC(
" Executes a function within a database transaction. If `next` returns\n"
" `Ok`, the transaction is committed. If it returns `Error` or panics,\n"
" the transaction is rolled back.\n"
).
-spec transaction(connection(), fun((connection()) -> {ok, RZM} | {error, RZN})) -> {ok,
RZM} |
{error, based:transaction_error(RZN)}.
transaction(Conn, Next) ->
_pipe = pgl:transaction(
erlang:element(2, Conn),
fun(Pgl_tx) ->
Tx_conn = {connection, Pgl_tx},
Next(Tx_conn)
end
),
gleam@result:map_error(_pipe, fun pgl_tx_err_to_db_tx_err/1).
-file("src/based/pg.gleam", 333).
-spec to_transaction_error(pgl:transaction_error(pgl:pgl_error())) -> based:transaction_error(any()).
to_transaction_error(Err) ->
case pgl_tx_err_to_db_tx_err(Err) of
not_in_transaction ->
not_in_transaction;
{rollback, Cause} ->
_pipe = Cause,
_pipe@1 = handle_error(_pipe),
_pipe@2 = based:error_to_string(_pipe@1),
{transaction_error, _pipe@2};
{transaction_error, Message} ->
{transaction_error, Message}
end.
-file("src/based/pg.gleam", 297).
?DOC(" Manually begins a transaction on the given connection.\n").
-spec 'begin'(connection()) -> {ok, connection()} |
{error, based:transaction_error(any())}.
'begin'(Conn) ->
_pipe = pgl:'begin'(erlang:element(2, Conn)),
_pipe@1 = gleam@result:map(
_pipe,
fun(Pgl_conn) -> {connection, Pgl_conn} end
),
gleam@result:map_error(_pipe@1, fun to_transaction_error/1).
-file("src/based/pg.gleam", 316).
?DOC(" Commits the current transaction on the given connection.\n").
-spec commit(connection()) -> {ok, connection()} |
{error, based:transaction_error(any())}.
commit(Conn) ->
_pipe = pgl:commit(erlang:element(2, Conn)),
_pipe@1 = gleam@result:map(
_pipe,
fun(Pgl_conn) -> {connection, Pgl_conn} end
),
gleam@result:map_error(_pipe@1, fun to_transaction_error/1).
-file("src/based/pg.gleam", 325).
?DOC(" Rolls back the current transaction on the given connection.\n").
-spec rollback(connection()) -> {ok, connection()} |
{error, based:transaction_error(any())}.
rollback(Conn) ->
_pipe = pgl:rollback(erlang:element(2, Conn)),
_pipe@1 = gleam@result:map(
_pipe,
fun(Pgl_conn) -> {connection, Pgl_conn} end
),
gleam@result:map_error(_pipe@1, fun to_transaction_error/1).