aboutsummaryrefslogtreecommitdiff
path: root/src/pubsub_db_sql.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/pubsub_db_sql.erl')
-rw-r--r--src/pubsub_db_sql.erl88
1 files changed, 75 insertions, 13 deletions
diff --git a/src/pubsub_db_sql.erl b/src/pubsub_db_sql.erl
index b910a5e7d..4df6a8695 100644
--- a/src/pubsub_db_sql.erl
+++ b/src/pubsub_db_sql.erl
@@ -5,7 +5,7 @@
%%% Created : 7 Aug 2009 by Pablo Polvorin <pablo.polvorin@process-one.net>
%%%
%%%
-%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
+%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
@@ -25,12 +25,15 @@
-module(pubsub_db_sql).
+
-author("pablo.polvorin@process-one.net").
-include("pubsub.hrl").
+-include("ejabberd_sql_pt.hrl").
-export([add_subscription/1, read_subscription/1,
delete_subscription/1, update_subscription/1]).
+-export([export/1]).
%% TODO: Those -spec lines produce errors in old Erlang versions.
%% They can be enabled again in ejabberd 3.0 because it uses R12B or higher.
@@ -75,27 +78,27 @@ add_subscription(#pubsub_subscription{subid = SubId, options = Opts}) ->
Opts),
ok.
-subscription_opt_from_sql({<<"DELIVER">>, Value}) ->
+subscription_opt_from_sql([<<"DELIVER">>, Value]) ->
{deliver, sql_to_boolean(Value)};
-subscription_opt_from_sql({<<"DIGEST">>, Value}) ->
+subscription_opt_from_sql([<<"DIGEST">>, Value]) ->
{digest, sql_to_boolean(Value)};
-subscription_opt_from_sql({<<"DIGEST_FREQUENCY">>, Value}) ->
+subscription_opt_from_sql([<<"DIGEST_FREQUENCY">>, Value]) ->
{digest_frequency, sql_to_integer(Value)};
-subscription_opt_from_sql({<<"EXPIRE">>, Value}) ->
+subscription_opt_from_sql([<<"EXPIRE">>, Value]) ->
{expire, sql_to_timestamp(Value)};
-subscription_opt_from_sql({<<"INCLUDE_BODY">>, Value}) ->
+subscription_opt_from_sql([<<"INCLUDE_BODY">>, Value]) ->
{include_body, sql_to_boolean(Value)};
%%TODO: might be > than 1 show_values value??.
%% need to use compact all in only 1 opt.
-subscription_opt_from_sql({<<"SHOW_VALUES">>, Value}) ->
+subscription_opt_from_sql([<<"SHOW_VALUES">>, Value]) ->
{show_values, Value};
-subscription_opt_from_sql({<<"SUBSCRIPTION_TYPE">>, Value}) ->
+subscription_opt_from_sql([<<"SUBSCRIPTION_TYPE">>, Value]) ->
{subscription_type,
case Value of
<<"items">> -> items;
<<"nodes">> -> nodes
end};
-subscription_opt_from_sql({<<"SUBSCRIPTION_DEPTH">>, Value}) ->
+subscription_opt_from_sql([<<"SUBSCRIPTION_DEPTH">>, Value]) ->
{subscription_depth,
case Value of
<<"all">> -> all;
@@ -127,15 +130,74 @@ subscription_opt_to_sql({subscription_depth, Depth}) ->
N -> integer_to_sql(N)
end}.
-integer_to_sql(N) -> iolist_to_binary(integer_to_list(N)).
+integer_to_sql(N) -> integer_to_binary(N).
boolean_to_sql(true) -> <<"1">>;
boolean_to_sql(false) -> <<"0">>.
-timestamp_to_sql(T) -> jlib:now_to_utc_string(T).
+timestamp_to_sql(T) -> xmpp_util:encode_timestamp(T).
-sql_to_integer(N) -> jlib:binary_to_integer(N).
+sql_to_integer(N) -> binary_to_integer(N).
sql_to_boolean(B) -> B == <<"1">>.
-sql_to_timestamp(T) -> jlib:datetime_string_to_timestamp(T).
+sql_to_timestamp(T) -> xmpp_util:decode_timestamp(T).
+
+export(_Server) ->
+ [{pubsub_node,
+ fun(_Host, #pubsub_node{nodeid = {Host, Node}, id = Nidx,
+ parents = Parents, type = Type,
+ options = Options}) ->
+ H = node_flat_sql:encode_host(Host),
+ Parent = case Parents of
+ [] -> <<>>;
+ [First | _] -> First
+ end,
+ [?SQL("delete from pubsub_node where nodeid=%(Nidx)d;"),
+ ?SQL("delete from pubsub_node_option where nodeid=%(Nidx)d;"),
+ ?SQL("delete from pubsub_node_owner where nodeid=%(Nidx)d;"),
+ ?SQL("delete from pubsub_state where nodeid=%(Nidx)d;"),
+ ?SQL("delete from pubsub_item where nodeid=%(Nidx)d;"),
+ ?SQL("insert into pubsub_node(host,node,nodeid,parent,plugin)"
+ " values (%(H)s, %(Node)s, %(Nidx)d, %(Parent)s, %(Type)s);")]
+ ++ lists:map(
+ fun ({Key, Value}) ->
+ SKey = iolist_to_binary(atom_to_list(Key)),
+ SValue = misc:term_to_expr(Value),
+ ?SQL("insert into pubsub_node_option(nodeid,name,val)"
+ " values (%(Nidx)d, %(SKey)s, %(SValue)s);")
+ end, Options);
+ (_Host, _R) ->
+ []
+ end},
+ {pubsub_state,
+ fun(_Host, #pubsub_state{stateid = {JID, Nidx},
+ affiliation = Affiliation,
+ subscriptions = Subscriptions}) ->
+ J = jid:encode(JID),
+ S = node_flat_sql:encode_subscriptions(Subscriptions),
+ A = node_flat_sql:encode_affiliation(Affiliation),
+ [?SQL("insert into pubsub_state(nodeid,jid,affiliation,subscriptions)"
+ " values (%(Nidx)d, %(J)s, %(A)s, %(S)s);")];
+ (_Host, _R) ->
+ []
+ end},
+ {pubsub_item,
+ fun(_Host, #pubsub_item{itemid = {ItemId, Nidx},
+ creation = {C, _},
+ modification = {M, JID},
+ payload = Payload}) ->
+ P = jid:encode(JID),
+ XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>),
+ SM = encode_now(M),
+ SC = encode_now(C),
+ [?SQL("insert into pubsub_item(itemid,nodeid,creation,modification,publisher,payload)"
+ " values (%(ItemId)s, %(Nidx)d, %(SC)s, %(SM)s, %(P)s, %(XML)s);")];
+ (_Host, _R) ->
+ []
+ end}].
+
+encode_now({T1, T2, T3}) ->
+ <<(misc:i2l(T1, 6))/binary, ":",
+ (misc:i2l(T2, 6))/binary, ":",
+ (misc:i2l(T3, 6))/binary>>.