./ct_report/coverage/mod_offline_chatmarkers_rdbms.COVER.html

1 %%%----------------------------------------------------------------------------
2 %%% @copyright (C) 2020, Erlang Solutions Ltd.
3 %%% @doc
4 %%% RDBMS backend for mod_offline_chatmarkers module.
5 %%% @end
6 %%%----------------------------------------------------------------------------
7
8 -module(mod_offline_chatmarkers_rdbms).
9 -behaviour(mod_offline_chatmarkers_backend).
10
11 -export([init/2,
12 get/2,
13 maybe_store/5,
14 remove_user/2]).
15
16 -spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
17 init(HostType, _Opts) ->
18 1 mongoose_rdbms:prepare(offline_chatmarkers_select, offline_markers, [jid],
19 <<"SELECT thread, room, timestamp FROM offline_markers WHERE jid = ?">>),
20 1 mongoose_rdbms:prepare(offline_chatmarkers_delete, offline_markers, [jid],
21 <<"DELETE FROM offline_markers WHERE jid = ?">>),
22 1 rdbms_queries:prepare_upsert(HostType, offline_chatmarkers_upsert, offline_markers,
23 [<<"jid">>, <<"thread">>, <<"room">>, <<"timestamp">>],
24 [],
25 [<<"jid">>, <<"thread">>, <<"room">>]),
26 1 ok.
27
28 -spec get(mongooseim:host_type(), jid:jid()) -> {ok, [{Thread :: undefined | binary(),
29 Room :: undefined | jid:jid(),
30 Timestamp :: integer()}]}.
31 get(HostType, Jid) ->
32 6 {selected, Rows} = execute_select(HostType, encode_jid(Jid)),
33 6 decode(Rows).
34
35 -spec execute_select(mongooseim:host_type(), binary()) -> mongoose_rdbms:query_result().
36 execute_select(HostType, Jid) ->
37 6 mongoose_rdbms:execute_successfully(HostType, offline_chatmarkers_select, [Jid]).
38
39 -spec execute_delete_user(mongooseim:host_type(), binary()) -> mongoose_rdbms:query_result().
40 execute_delete_user(HostType, Jid) ->
41 6 mongoose_rdbms:execute_successfully(HostType, offline_chatmarkers_delete, [Jid]).
42
43 -spec execute_maybe_store(mongooseim:host_type(),
44 Jid :: binary(),
45 Thread :: binary(),
46 Room :: binary(),
47 Timestamp :: integer()) ->
48 mongoose_rdbms:query_result().
49 execute_maybe_store(HostType, Jid, Thread, Room, Timestamp) ->
50 10 rdbms_queries:execute_upsert(HostType, offline_chatmarkers_upsert,
51 [Jid, Thread, Room, Timestamp],
52 [],
53 [Jid, Thread, Room]).
54 %%% @doc
55 %%% Jid, Thread, and Room parameters serve as a composite database key. If
56 %%% key is not available in the database, then it must be added with the
57 %%% corresponding timestamp. Otherwise this function does nothing, the stored
58 %%% timestamp for the composite key MUST remain unchanged!
59 %%% @end
60 -spec maybe_store(mongooseim:host_type(), Jid :: jid:jid(), Thread :: undefined | binary(),
61 Room :: undefined | jid:jid(), Timestamp :: integer()) -> ok.
62 maybe_store(HostType, Jid, Thread, Room, Timestamp) ->
63 10 execute_maybe_store(HostType, encode_jid(Jid), encode_thread(Thread),
64 encode_jid(Room), Timestamp),
65 10 ok.
66
67 -spec remove_user(mongooseim:host_type(), jid:jid()) -> ok.
68 remove_user(HostType, Jid) ->
69 6 execute_delete_user(HostType, encode_jid(Jid)),
70 6 ok.
71
72 5 encode_jid(undefined) -> <<>>;
73 27 encode_jid(JID) -> jid:to_bare_binary(JID).
74
75 6 encode_thread(undefined) -> <<>>;
76 4 encode_thread(Thread) -> Thread.
77
78 decode(Rows) ->
79 6 {ok, [decode_row(R) || R <- Rows]}.
80
81 decode_row({Thread, Room, TS}) ->
82 4 {decode_thread(Thread), decode_jid(Room), decode_timestamp(TS)}.
83
84 2 decode_jid(<<>>) -> undefined;
85 2 decode_jid(EncodedJID) -> jid:from_binary(EncodedJID).
86
87 2 decode_thread(<<>>) -> undefined;
88 2 decode_thread(EncodedThread) -> EncodedThread.
89
90 decode_timestamp(EncodedTS) ->
91 4 mongoose_rdbms:result_to_integer(EncodedTS).
Line Hits Source