25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-10-31 15:21:38 +01:00

Updated some mod_offline SQL queries to the new API

This commit is contained in:
Alexey Shchepin 2016-02-12 16:25:09 +03:00
parent ba35c1ed9d
commit 99255631dd
2 changed files with 50 additions and 74 deletions

View File

@ -25,6 +25,8 @@
-module(mod_offline). -module(mod_offline).
-compile([{parse_transform, ejabberd_sql_pt}]).
-author('alexey@process-one.net'). -author('alexey@process-one.net').
-protocol({xep, 13, '1.2'}). -protocol({xep, 13, '1.2'}).
@ -79,6 +81,8 @@
-include("mod_offline.hrl"). -include("mod_offline.hrl").
-include("ejabberd_sql_pt.hrl").
-define(PROCNAME, ejabberd_offline). -define(PROCNAME, ejabberd_offline).
-define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000). -define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000).
@ -686,13 +690,10 @@ pop_offline_messages(Ls, LUser, LServer, mnesia) ->
_ -> Ls _ -> Ls
end; end;
pop_offline_messages(Ls, LUser, LServer, odbc) -> pop_offline_messages(Ls, LUser, LServer, odbc) ->
EUser = ejabberd_odbc:escape(LUser), case odbc_queries:get_and_del_spool_msg_t(LServer, LUser) of
case odbc_queries:get_and_del_spool_msg_t(LServer, {atomic, {selected, Rs}} ->
EUser)
of
{atomic, {selected, [<<"username">>, <<"xml">>], Rs}} ->
Ls ++ Ls ++
lists:flatmap(fun ([_, XML]) -> lists:flatmap(fun ({_, XML}) ->
case fxml_stream:parse_element(XML) of case fxml_stream:parse_element(XML) of
{error, _Reason} -> {error, _Reason} ->
[]; [];
@ -811,8 +812,7 @@ remove_user(LUser, LServer, mnesia) ->
F = fun () -> mnesia:delete({offline_msg, US}) end, F = fun () -> mnesia:delete({offline_msg, US}) end,
mnesia:transaction(F); mnesia:transaction(F);
remove_user(LUser, LServer, odbc) -> remove_user(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser), odbc_queries:del_spool_msg(LServer, LUser);
odbc_queries:del_spool_msg(LServer, Username);
remove_user(LUser, LServer, riak) -> remove_user(LUser, LServer, riak) ->
{atomic, ejabberd_riak:delete_by_index(offline_msg, {atomic, ejabberd_riak:delete_by_index(offline_msg,
<<"us">>, {LUser, LServer})}. <<"us">>, {LUser, LServer})}.
@ -883,13 +883,13 @@ get_offline_els(LUser, LServer, DBType)
jlib:replace_from_to(From, To, Packet) jlib:replace_from_to(From, To, Packet)
end, Msgs); end, Msgs);
get_offline_els(LUser, LServer, odbc) -> get_offline_els(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser), case catch ejabberd_odbc:sql_query(
case catch ejabberd_odbc:sql_query(LServer, LServer,
[<<"select xml from spool where username='">>, ?SQL("select @(xml)s from spool where "
Username, <<"' order by seq;">>]) of "username=%(LUser)s order by seq")) of
{selected, [<<"xml">>], Rs} -> {selected, Rs} ->
lists:flatmap( lists:flatmap(
fun([XML]) -> fun({XML}) ->
case fxml_stream:parse_element(XML) of case fxml_stream:parse_element(XML) of
#xmlel{} = El -> #xmlel{} = El ->
case offline_msg_to_route(LServer, El) of case offline_msg_to_route(LServer, El) of
@ -1050,20 +1050,20 @@ read_all_msgs(LUser, LServer, riak) ->
[] []
end; end;
read_all_msgs(LUser, LServer, odbc) -> read_all_msgs(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser), case catch ejabberd_odbc:sql_query(
case catch ejabberd_odbc:sql_query(LServer, LServer,
[<<"select xml from spool where username='">>, ?SQL("select @(xml)s from spool where "
Username, <<"' order by seq;">>]) "username=%(LUser)s order by seq")) of
of {selected, Rs} ->
{selected, [<<"xml">>], Rs} -> lists:flatmap(
lists:flatmap(fun ([XML]) -> fun({XML}) ->
case fxml_stream:parse_element(XML) of case fxml_stream:parse_element(XML) of
{error, _Reason} -> []; {error, _Reason} -> [];
El -> [El] El -> [El]
end end
end, end,
Rs); Rs);
_ -> [] _ -> []
end. end.
format_user_queue(Msgs, DBType) when DBType == mnesia; DBType == riak -> format_user_queue(Msgs, DBType) when DBType == mnesia; DBType == riak ->
@ -1246,30 +1246,7 @@ us_to_list({User, Server}) ->
jid:to_string({User, Server, <<"">>}). jid:to_string({User, Server, <<"">>}).
get_queue_length(LUser, LServer) -> get_queue_length(LUser, LServer) ->
get_queue_length(LUser, LServer, count_offline_messages(LUser, LServer).
gen_mod:db_type(LServer, ?MODULE)).
get_queue_length(LUser, LServer, mnesia) ->
length(mnesia:dirty_read({offline_msg,
{LUser, LServer}}));
get_queue_length(LUser, LServer, riak) ->
case ejabberd_riak:count_by_index(offline_msg,
<<"us">>, {LUser, LServer}) of
{ok, N} ->
N;
_ ->
0
end;
get_queue_length(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser),
case catch ejabberd_odbc:sql_query(LServer,
[<<"select count(*) from spool where username='">>,
Username, <<"';">>])
of
{selected, [_], [[SCount]]} ->
jlib:binary_to_integer(SCount);
_ -> 0
end.
get_messages_subset(User, Host, MsgsAll, DBType) -> get_messages_subset(User, Host, MsgsAll, DBType) ->
Access = gen_mod:get_module_opt(Host, ?MODULE, access_max_user_messages, Access = gen_mod:get_module_opt(Host, ?MODULE, access_max_user_messages,
@ -1342,8 +1319,7 @@ delete_all_msgs(LUser, LServer, riak) ->
<<"us">>, {LUser, LServer}), <<"us">>, {LUser, LServer}),
{atomic, Res}; {atomic, Res};
delete_all_msgs(LUser, LServer, odbc) -> delete_all_msgs(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser), odbc_queries:del_spool_msg(LServer, LUser),
odbc_queries:del_spool_msg(LServer, Username),
{atomic, ok}. {atomic, ok}.
webadmin_user_parse_query(_, <<"removealloffline">>, webadmin_user_parse_query(_, <<"removealloffline">>,
@ -1379,15 +1355,13 @@ count_offline_messages(LUser, LServer, mnesia) ->
_ -> 0 _ -> 0
end; end;
count_offline_messages(LUser, LServer, odbc) -> count_offline_messages(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser), case catch ejabberd_odbc:sql_query(
case catch odbc_queries:count_records_where(LServer, LServer,
<<"spool">>, ?SQL("select @(count(*))d from spool "
<<"where username='", "where username=%(LUser)s")) of
Username/binary, "'">>) {selected, [{Res}]} ->
of Res;
{selected, [_], [[Res]]} -> _ -> 0
jlib:binary_to_integer(Res);
_ -> 0
end; end;
count_offline_messages(LUser, LServer, riak) -> count_offline_messages(LUser, LServer, riak) ->
case ejabberd_riak:count_by_index( case ejabberd_riak:count_by_index(

View File

@ -25,6 +25,8 @@
-module(odbc_queries). -module(odbc_queries).
-compile([{parse_transform, ejabberd_sql_pt}]).
-behaviour(ejabberd_config). -behaviour(ejabberd_config).
-author("mremond@process-one.net"). -author("mremond@process-one.net").
@ -60,6 +62,7 @@
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("logger.hrl"). -include("logger.hrl").
-include("ejabberd_sql_pt.hrl").
%% Almost a copy of string:join/2. %% Almost a copy of string:join/2.
%% We use this version because string:join/2 is relatively %% We use this version because string:join/2 is relatively
@ -291,23 +294,22 @@ add_spool_sql(Username, XML) ->
add_spool(LServer, Queries) -> add_spool(LServer, Queries) ->
ejabberd_odbc:sql_transaction(LServer, Queries). ejabberd_odbc:sql_transaction(LServer, Queries).
get_and_del_spool_msg_t(LServer, Username) -> get_and_del_spool_msg_t(LServer, LUser) ->
F = fun () -> F = fun () ->
Result = Result =
ejabberd_odbc:sql_query_t([<<"select username, xml from spool where " ejabberd_odbc:sql_query_t(
"username='">>, ?SQL("select @(username)s, @(xml)s from spool where "
Username, "username=%(LUser)s order by seq;")),
<<"' order by seq;">>]), ejabberd_odbc:sql_query_t(
ejabberd_odbc:sql_query_t([<<"delete from spool where username='">>, ?SQL("delete from spool where username=%(LUser)s;")),
Username, <<"';">>]),
Result Result
end, end,
ejabberd_odbc:sql_transaction(LServer, F). ejabberd_odbc:sql_transaction(LServer, F).
del_spool_msg(LServer, Username) -> del_spool_msg(LServer, LUser) ->
ejabberd_odbc:sql_query(LServer, ejabberd_odbc:sql_query(
[<<"delete from spool where username='">>, Username, LServer,
<<"';">>]). ?SQL("delete from spool where username=%(LUser)s")).
get_roster(LServer, Username) -> get_roster(LServer, Username) ->
ejabberd_odbc:sql_query(LServer, ejabberd_odbc:sql_query(LServer,