Current section
Files
Jump to
Current section
Files
src/postgleam@notifications.erl
-module(postgleam@notifications).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/postgleam/notifications.gleam").
-export([receive_notifications/2, listen/3, unlisten/3, notify/4]).
-export_type([notification/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 notification() :: {notification, binary(), binary(), integer()}.
-file("src/postgleam/notifications.gleam", 90).
-spec list_reverse_loop(list(JJO), list(JJO)) -> list(JJO).
list_reverse_loop(L, Acc) ->
case L of
[] ->
Acc;
[X | Rest] ->
list_reverse_loop(Rest, [X | Acc])
end.
-file("src/postgleam/notifications.gleam", 86).
-spec list_reverse(list(JJL)) -> list(JJL).
list_reverse(L) ->
list_reverse_loop(L, []).
-file("src/postgleam/notifications.gleam", 70).
-spec collect_notifications(
postgleam@connection:connection_state(),
integer(),
list(notification())
) -> {ok, {list(notification()), postgleam@connection:connection_state()}} |
{error, postgleam@error:error()}.
collect_notifications(State, _, Acc) ->
case postgleam@message:decode_backend(erlang:element(7, State)) of
{decoded, {notification_response, Pg_pid, Channel, Payload}, _} ->
Notif = {notification, Channel, Payload, Pg_pid},
State@1 = {connection_state,
erlang:element(2, State),
erlang:element(3, State),
erlang:element(4, State),
erlang:element(5, State),
erlang:element(6, State),
<<>>},
collect_notifications(State@1, 0, [Notif | Acc]);
_ ->
{ok, {list_reverse(Acc), State}}
end.
-file("src/postgleam/notifications.gleam", 58).
?DOC(
" Check for pending notifications.\n"
" Sends a lightweight query to flush any queued notifications from the server.\n"
" Returns a list of notifications that arrived, plus the updated state.\n"
).
-spec receive_notifications(postgleam@connection:connection_state(), integer()) -> {ok,
{list(notification()), postgleam@connection:connection_state()}} |
{error, postgleam@error:error()}.
receive_notifications(State, Timeout) ->
gleam@result:'try'(
postgleam@connection:simple_query(State, <<"SELECT 1"/utf8>>, Timeout),
fun(_use0) ->
{_, State@1} = _use0,
collect_notifications(State@1, Timeout, [])
end
).
-file("src/postgleam/notifications.gleam", 97).
-spec quote_identifier(binary()) -> binary().
quote_identifier(S) ->
<<<<"\""/utf8, S/binary>>/binary, "\""/utf8>>.
-file("src/postgleam/notifications.gleam", 15).
?DOC(" Listen on a channel\n").
-spec listen(postgleam@connection:connection_state(), binary(), integer()) -> {ok,
postgleam@connection:connection_state()} |
{error, postgleam@error:error()}.
listen(State, Channel, Timeout) ->
gleam@result:'try'(
postgleam@connection:simple_query(
State,
<<"LISTEN "/utf8, (quote_identifier(Channel))/binary>>,
Timeout
),
fun(_use0) ->
{_, State@1} = _use0,
{ok, State@1}
end
).
-file("src/postgleam/notifications.gleam", 27).
?DOC(" Stop listening on a channel\n").
-spec unlisten(postgleam@connection:connection_state(), binary(), integer()) -> {ok,
postgleam@connection:connection_state()} |
{error, postgleam@error:error()}.
unlisten(State, Channel, Timeout) ->
gleam@result:'try'(
postgleam@connection:simple_query(
State,
<<"UNLISTEN "/utf8, (quote_identifier(Channel))/binary>>,
Timeout
),
fun(_use0) ->
{_, State@1} = _use0,
{ok, State@1}
end
).
-file("src/postgleam/notifications.gleam", 101).
-spec quote_literal(binary()) -> binary().
quote_literal(S) ->
<<<<"'"/utf8, S/binary>>/binary, "'"/utf8>>.
-file("src/postgleam/notifications.gleam", 39).
?DOC(" Send a notification on a channel\n").
-spec notify(
postgleam@connection:connection_state(),
binary(),
binary(),
integer()
) -> {ok, postgleam@connection:connection_state()} |
{error, postgleam@error:error()}.
notify(State, Channel, Payload, Timeout) ->
Sql = <<<<<<<<"SELECT pg_notify("/utf8, (quote_literal(Channel))/binary>>/binary,
", "/utf8>>/binary,
(quote_literal(Payload))/binary>>/binary,
")"/utf8>>,
gleam@result:'try'(
postgleam@connection:simple_query(State, Sql, Timeout),
fun(_use0) ->
{_, State@1} = _use0,
{ok, State@1}
end
).