25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-28 16:34:13 +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}) ->
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) -> (_Host, _R) ->
[] []
end}, end},
%% REVIEW:
%% * From the mnesia table, the #pubsub_state.items is not used in ODBC
%% * Right now AFFILIATION is the first letter of Affiliation
%% * Right now SUBSCRIPTIONS expects only one Subscription
%% * Right now SUBSCRIPTIONS letter is the first letter of Subscription
{pubsub_state, {pubsub_state,
fun(_Host, #pubsub_state{stateid = {Jid, Stateid}, fun(_Host, #pubsub_state{stateid = {JID, Nidx},
%nodeidx = Nodeidx,
items = _Items,
affiliation = Affiliation, affiliation = Affiliation,
subscriptions = Subscriptions}) -> subscriptions = Subscriptions}) ->
STATEID = list_to_binary(integer_to_list(Stateid)), J = jid:encode(JID),
JID = ejabberd_sql:escape(jid:encode(Jid)), S = node_flat_sql:encode_subscriptions(Subscriptions),
NODEID = <<"unknown">>, %% TODO: integer_to_list(Nodeidx), A = node_flat_sql:encode_affiliation(Affiliation),
AFFILIATION = list_to_binary(string:substr(atom_to_list(Affiliation),1,1)), [?SQL("insert into pubsub_state(nodeid,jid,affiliation,subscriptions)"
SUBSCRIPTIONS = list_to_binary(parse_subscriptions(Subscriptions)), " values (%(Nidx)d, %(J)s, %(A)s, %(S)s);")];
[?SQL("delete from pubsub_state where stateid=%(STATEID)s;"),
?SQL("insert into pubsub_state(stateid,jid,nodeid,affiliation,subscriptions)\n"
" values (%(STATEID)s, %(JID)s, %(NODEID)s, %(AFFILIATION)s, %(SUBSCRIPTIONS)s);")];
(_Host, _R) -> (_Host, _R) ->
[] []
end}, end},
{pubsub_item,
%% REVIEW: fun(_Host, #pubsub_item{itemid = {ItemId, Nidx},
%% * Parents is not migrated to PARENTs creation = {C, _},
%% * Probably some option VALs are not correctly represented in mysql modification = {M, JID},
{pubsub_node, payload = Payload}) ->
fun(_Host, #pubsub_node{nodeid = {Hostid, Nodeid}, P = jid:encode(JID),
id = Id, XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>),
parents = _Parents, SM = encode_now(M),
type = Type, SC = encode_now(C),
owners = Owners, [?SQL("insert into pubsub_item(itemid,nodeid,creation,modification,publisher,payload)"
options = Options}) -> " values (%(ItemId)s, %(Nidx)d, %(SC)s, %(SM)s, %(P)s, %(XML)s);")];
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) -> (_Host, _R) ->
[] []
end}]. end}].
parse_subscriptions([]) -> encode_now({T1, T2, T3}) ->
""; <<(misc:i2l(T1, 6))/binary, ":",
parse_subscriptions([{State, Item}]) -> (misc:i2l(T2, 6))/binary, ":",
STATE = case State of (misc:i2l(T3, 6))/binary>>.
subscribed -> "s"
end,
string:join([STATE, Item],":").