1 |
|
-module(mod_caps_rdbms). |
2 |
|
-behaviour(mod_caps_backend). |
3 |
|
|
4 |
|
-export([init/2, |
5 |
|
read/2, |
6 |
|
write/3, |
7 |
|
delete_node/2]). |
8 |
|
|
9 |
|
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
10 |
|
init(HostType, _Opts) -> |
11 |
2 |
rdbms_queries:prepare_upsert(HostType, caps_upsert, caps, |
12 |
|
[<<"node">>, <<"sub_node">>, <<"features">>], |
13 |
|
[<<"features">>], |
14 |
|
[<<"node">>, <<"sub_node">>]), |
15 |
2 |
mongoose_rdbms:prepare(caps_delete, caps, |
16 |
|
[node, sub_node], |
17 |
|
<<"DELETE FROM caps WHERE node=? AND sub_node=?">>), |
18 |
2 |
mongoose_rdbms:prepare(caps_select, caps, |
19 |
|
[node, sub_node], |
20 |
|
<<"SELECT features FROM caps WHERE node=? AND sub_node=?">>), |
21 |
2 |
ok. |
22 |
|
|
23 |
|
-spec read(mongooseim:host_type(), mod_caps:node_pair()) -> |
24 |
|
{ok, mod_caps:maybe_pending_features()} | error. |
25 |
|
read(HostType, {Node, SubNode}) -> |
26 |
:-( |
case mongoose_rdbms:execute(HostType, caps_select, [Node, SubNode]) of |
27 |
:-( |
[{selected, [{Encoded}]}] -> {ok, jiffy:decode(Encoded)}; |
28 |
:-( |
_ -> error |
29 |
|
end. |
30 |
|
|
31 |
|
-spec write(mongooseim:host_type(), mod_caps:node_pair(), |
32 |
|
mod_caps:maybe_pending_features()) -> ok. |
33 |
|
write(HostType, {Node, SubNode}, Features) -> |
34 |
:-( |
Encoded = jiffy:encode(Features), |
35 |
:-( |
InsertParams = [Node, SubNode, Encoded], |
36 |
:-( |
UpdateParams = [Encoded], |
37 |
:-( |
UniqueKeyValues = [Node, SubNode], |
38 |
:-( |
rdbms_queries:execute_upsert(HostType, caps_upsert, |
39 |
|
InsertParams, UpdateParams, UniqueKeyValues), |
40 |
:-( |
ok. |
41 |
|
|
42 |
|
-spec delete_node(mongooseim:host_type(), mod_caps:node_pair()) -> ok. |
43 |
|
delete_node(HostType, {Node, SubNode}) -> |
44 |
:-( |
{updated, _} = mongoose_rdbms:execute(HostType, caps_delete, [Node, SubNode]), |
45 |
:-( |
ok. |