Skip to content

Commit

Permalink
Merge pull request #3074 from esl/prepared-queries-pubsub
Browse files Browse the repository at this point in the history
Prepared queries pubsub
  • Loading branch information
chrzaszcz authored Apr 8, 2021
2 parents a9e73b8 + ac21acf commit bf18df3
Show file tree
Hide file tree
Showing 3 changed files with 548 additions and 691 deletions.
24 changes: 7 additions & 17 deletions src/pubsub/mod_pubsub_cache_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ start(Host) ->
<<"created_lserver">>, <<"created_at">>, <<"payload">>],
UpdateFields = [<<"itemid">>, <<"created_luser">>, <<"created_lserver">>,
<<"created_at">>, <<"payload">>],
mongoose_rdbms:prepare(pubsub_get_last_item, pubsub_last_item, [nidx],
<<"SELECT nidx, itemid, created_luser, created_at, created_lserver, payload "
"FROM pubsub_last_item WHERE nidx = ?">>),
mongoose_rdbms:prepare(pubsub_delete_last_item, pubsub_last_item, [nidx],
<<"DELETE FROM pubsub_last_item WHERE nidx = ?">>),
rdbms_queries:prepare_upsert(Host, pubsub_last_item_upsert, pubsub_last_item,
InsertFields,
UpdateFields,
Expand Down Expand Up @@ -49,28 +54,16 @@ upsert_last_item(ServerHost, Nidx, ItemID, Publisher, Payload) ->
-spec delete_last_item(ServerHost :: binary(),
Nidx :: mod_pubsub:nodeIdx()) -> ok | {error, Reason :: term()}.
delete_last_item(ServerHost, Nidx) ->
DeleteQuerySQL = delete_pubsub_last_item(Nidx),
Res = mongoose_rdbms:sql_query(ServerHost, DeleteQuerySQL),
Res = mongoose_rdbms:execute_successfully(ServerHost, pubsub_delete_last_item, [Nidx]),
convert_rdbms_response(Res).

-spec get_last_item(ServerHost :: binary(),
Nidx :: mod_pubsub:nodeIdx()) ->
{ok, LastItem :: mod_pubsub:pubsubLastItem()} | {error, Reason :: term()}.
get_last_item(ServerHost, Nidx) ->
ReadQuerySQL = get_pubsub_last_item(Nidx),
Res = mongoose_rdbms:sql_query(ServerHost, ReadQuerySQL),
Res = mongoose_rdbms:execute_successfully(ServerHost, pubsub_get_last_item, [Nidx]),
convert_rdbms_response(Res).

-spec get_pubsub_last_item(mod_pubsub:nodeIdx()) -> iolist().
get_pubsub_last_item(Nidx) ->
["SELECT nidx, itemid, created_luser, created_at, created_lserver, payload FROM pubsub_last_item"
" WHERE nidx = ", esc_int(Nidx), ";"].

-spec delete_pubsub_last_item(mod_pubsub:nodeIdx()) -> iolist().
delete_pubsub_last_item(Nidx) ->
["DELETE FROM pubsub_last_item"
" WHERE nidx = ", esc_int(Nidx), ";"].

%%====================================================================
%% Helpers
%%====================================================================
Expand All @@ -86,9 +79,6 @@ convert_rdbms_response(Response) ->
?LOG_ERROR(#{what => pubsub_rdbms_cache_failed, reason => Response}),
{error, pubsub_rdbms_cache_failed}.

esc_int(Int) ->
mongoose_rdbms:use_escaped_integer(mongoose_rdbms:escape_integer(Int)).

prepare_upsert_params(Publisher, Payload) ->
PayloadXML = #xmlel{name = <<"item">>, children = Payload},
CreatedAt = os:system_time(microsecond),
Expand Down
Loading

0 comments on commit bf18df3

Please sign in to comment.