25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Rewrite pubsub export to sql (#1571)

This commit is contained in:
Christophe Romain 2017-11-13 17:34:37 +01:00
parent 9daf2dd925
commit daed6a05a6

View File

@ -144,92 +144,61 @@ sql_to_boolean(B) -> B == <<"1">>.
sql_to_timestamp(T) -> xmpp_util:decode_timestamp(T). sql_to_timestamp(T) -> xmpp_util:decode_timestamp(T).
%% REVIEW:
%% * this code takes NODEID from Itemid2, and forgets about Nodeidx
%% * this code assumes Payload only contains one xmlelement()
%% * PUBLISHER is taken from Creation
export(_Server) -> export(_Server) ->
[{pubsub_item, [{pubsub_node,
fun(_Host, #pubsub_item{itemid = {Itemid1, NODEID}, fun(_Host, #pubsub_node{nodeid = {Host, Node}, id = Nidx,
%nodeidx = _Nodeidx, parents = Parents, type = Type,
creation = {{C1, C2, C3}, Cusr}, options = Options}) ->
modification = {{M1, M2, M3}, _Musr}, H = node_flat_sql:encode_host(Host),
payload = Payload}) -> Parent = case Parents of
ITEMID = ejabberd_sql:escape(Itemid1), [] -> <<>>;
CREATION = ejabberd_sql:escape(list_to_binary( [First | _] -> First
string:join([string:right(integer_to_list(I),6,$0)||I<-[C1,C2,C3]],":"))), end,
MODIFICATION = ejabberd_sql:escape(list_to_binary( [?SQL("delete from pubsub_node where nodeid=%(Nidx)d;"),
string:join([string:right(integer_to_list(I),6,$0)||I<-[M1,M2,M3]],":"))), ?SQL("delete from pubsub_node_option where nodeid=%(Nidx)d;"),
PUBLISHER = ejabberd_sql:escape(jid:encode(Cusr)), ?SQL("delete from pubsub_node_owner where nodeid=%(Nidx)d;"),
[PayloadEl] = [El || {xmlel,_,_,_} = El <- Payload], ?SQL("delete from pubsub_state where nodeid=%(Nidx)d;"),
PAYLOAD = ejabberd_sql:escape(fxml:element_to_binary(PayloadEl)), ?SQL("delete from pubsub_item where nodeid=%(Nidx)d;"),
[?SQL("delete from pubsub_item where itemid=%(ITEMID)s;"), ?SQL("insert into pubsub_node(host,node,nodeid,parent,type)"
?SQL("insert into pubsub_item(itemid,nodeid,creation,modification,publisher,payload) \n" " values (%(H)s, %(Node)s, %(Nidx)d, %(Parent)s, %(Type)s);")]
" values (%(ITEMID)s, %(NODEID)d, %(CREATION)s, ++ lists:map(
%(MODIFICATION)s, %(PUBLISHER)s, %(PAYLOAD)s);")]; fun ({Key, Value}) ->
(_Host, _R) -> SKey = iolist_to_binary(atom_to_list(Key)),
[] SValue = misc:term_to_expr(Value),
end}, ?SQL("insert into pubsub_node_option(nodeid,name,val)"
%% REVIEW: " values (%(Nidx)d, %(SKey)s, %(SValue)s);")
%% * From the mnesia table, the #pubsub_state.items is not used in ODBC end, Options);
%% * Right now AFFILIATION is the first letter of Affiliation (_Host, _R) ->
%% * Right now SUBSCRIPTIONS expects only one Subscription []
%% * Right now SUBSCRIPTIONS letter is the first letter of Subscription end},
{pubsub_state, {pubsub_state,
fun(_Host, #pubsub_state{stateid = {Jid, Stateid}, fun(_Host, #pubsub_state{stateid = {JID, Nidx},
%nodeidx = Nodeidx, affiliation = Affiliation,
items = _Items, subscriptions = Subscriptions}) ->
affiliation = Affiliation, J = jid:encode(JID),
subscriptions = Subscriptions}) -> S = node_flat_sql:encode_subscriptions(Subscriptions),
STATEID = list_to_binary(integer_to_list(Stateid)), A = node_flat_sql:encode_affiliation(Affiliation),
JID = ejabberd_sql:escape(jid:encode(Jid)), [?SQL("insert into pubsub_state(nodeid,jid,affiliation,subscriptions)"
NODEID = <<"unknown">>, %% TODO: integer_to_list(Nodeidx), " values (%(Nidx)d, %(J)s, %(A)s, %(S)s);")];
AFFILIATION = list_to_binary(string:substr(atom_to_list(Affiliation),1,1)), (_Host, _R) ->
SUBSCRIPTIONS = list_to_binary(parse_subscriptions(Subscriptions)), []
[?SQL("delete from pubsub_state where stateid=%(STATEID)s;"), end},
?SQL("insert into pubsub_state(stateid,jid,nodeid,affiliation,subscriptions)\n" {pubsub_item,
" values (%(STATEID)s, %(JID)s, %(NODEID)s, %(AFFILIATION)s, %(SUBSCRIPTIONS)s);")]; fun(_Host, #pubsub_item{itemid = {ItemId, Nidx},
(_Host, _R) -> creation = {C, _},
[] modification = {M, JID},
end}, payload = Payload}) ->
P = jid:encode(JID),
%% REVIEW: XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>),
%% * Parents is not migrated to PARENTs SM = encode_now(M),
%% * Probably some option VALs are not correctly represented in mysql SC = encode_now(C),
{pubsub_node, [?SQL("insert into pubsub_item(itemid,nodeid,creation,modification,publisher,payload)"
fun(_Host, #pubsub_node{nodeid = {Hostid, Nodeid}, " values (%(ItemId)s, %(Nidx)d, %(SC)s, %(SM)s, %(P)s, %(XML)s);")];
id = Id, (_Host, _R) ->
parents = _Parents, []
type = Type, end}].
owners = Owners,
options = Options}) ->
HOST = case Hostid of
{U,S,R} -> ejabberd_sql:escape(jid:encode({U,S,R}));
_ -> ejabberd_sql:escape(Hostid)
end,
NODE = ejabberd_sql:escape(Nodeid),
PARENT = <<"">>,
IdB = integer_to_binary(Id),
TYPE = ejabberd_sql:escape(<<Type/binary, "_odbc">>),
[?SQL("delete from pubsub_node where nodeid=%(Id)d;"),
?SQL("insert into pubsub_node(host,node,nodeid,parent,type) \n"
" values (%(HOST)s, %(NODE)s, %(Id)d, %(PARENT)s, %(TYPE)s);"),
?SQL("delete from pubsub_node_option where nodeid=%(Id)d;"),
[["insert into pubsub_node_option(nodeid,name,val)\n"
" values (", IdB, ", '", atom_to_list(Name), "', '",
io_lib:format("~p", [Val]), "');\n"] || {Name,Val} <- Options],
?SQL("delete from pubsub_node_owner where nodeid=%(Id)d;"),
[["insert into pubsub_node_owner(nodeid,owner)\n"
" values (", IdB, ", '", jid:encode(Usr), "');\n"] || Usr <- Owners],"\n"];
(_Host, _R) ->
[]
end}].
parse_subscriptions([]) ->
"";
parse_subscriptions([{State, Item}]) ->
STATE = case State of
subscribed -> "s"
end,
string:join([STATE, Item],":").
encode_now({T1, T2, T3}) ->
<<(misc:i2l(T1, 6))/binary, ":",
(misc:i2l(T2, 6))/binary, ":",
(misc:i2l(T3, 6))/binary>>.