Merge branch 'master' of github.com:processone/ejabberd

This commit is contained in:
Mickael Remond 2016-03-29 11:21:58 +02:00
commit 221d8e0e5d
4 changed files with 49 additions and 17 deletions

Binary file not shown.

View File

@ -269,6 +269,7 @@ connecting(connect, #state{host = Host} = State) ->
[mysql | Args] -> apply(fun mysql_connect/5, Args); [mysql | Args] -> apply(fun mysql_connect/5, Args);
[pgsql | Args] -> apply(fun pgsql_connect/5, Args); [pgsql | Args] -> apply(fun pgsql_connect/5, Args);
[sqlite | Args] -> apply(fun sqlite_connect/1, Args); [sqlite | Args] -> apply(fun sqlite_connect/1, Args);
[mssql | Args] -> apply(fun odbc_connect/1, Args);
[odbc | Args] -> apply(fun odbc_connect/1, Args) [odbc | Args] -> apply(fun odbc_connect/1, Args)
end, end,
{_, PendingRequests} = State#state.pending_requests, {_, PendingRequests} = State#state.pending_requests,
@ -505,6 +506,8 @@ sql_query_internal(#sql_query{} = Query) ->
case State#state.db_type of case State#state.db_type of
odbc -> odbc ->
generic_sql_query(Query); generic_sql_query(Query);
mssql ->
generic_sql_query(Query);
pgsql -> pgsql ->
Key = {?PREPARE_KEY, Query#sql_query.hash}, Key = {?PREPARE_KEY, Query#sql_query.hash},
case get(Key) of case get(Key) of
@ -554,7 +557,10 @@ sql_query_internal(Query) ->
?DEBUG("SQL: \"~s\"", [Query]), ?DEBUG("SQL: \"~s\"", [Query]),
Res = case State#state.db_type of Res = case State#state.db_type of
odbc -> odbc ->
to_odbc(odbc:sql_query(State#state.db_ref, Query, to_odbc(odbc:sql_query(State#state.db_ref, [Query],
(?TRANSACTION_TIMEOUT) - 1000));
mssql ->
to_odbc(odbc:sql_query(State#state.db_ref, [Query],
(?TRANSACTION_TIMEOUT) - 1000)); (?TRANSACTION_TIMEOUT) - 1000));
pgsql -> pgsql ->
pgsql_to_odbc(pgsql:squery(State#state.db_ref, Query)); pgsql_to_odbc(pgsql:squery(State#state.db_ref, Query));
@ -898,8 +904,8 @@ db_opts(Host) ->
<<"">>), <<"">>),
case Type of case Type of
mssql -> mssql ->
[odbc, <<"DSN=", Host/binary, ";UID=", User/binary, [mssql, <<"DSN=", Host/binary, ";UID=", User/binary,
";PWD=", Pass/binary>>]; ";PWD=", Pass/binary>>];
_ -> _ ->
[Type, Server, Port, DB, User, Pass] [Type, Server, Port, DB, User, Pass]
end end

View File

@ -1260,7 +1260,7 @@ make_matchspec(LUser, LServer, Start, End, none) ->
Msg Msg
end). end).
make_sql_query(User, _LServer, Start, End, With, RSM) -> make_sql_query(User, LServer, Start, End, With, RSM) ->
{Max, Direction, ID} = case RSM of {Max, Direction, ID} = case RSM of
#rsm_in{} -> #rsm_in{} ->
{RSM#rsm_in.max, {RSM#rsm_in.max,
@ -1269,11 +1269,19 @@ make_sql_query(User, _LServer, Start, End, With, RSM) ->
none -> none ->
{none, none, <<>>} {none, none, <<>>}
end, end,
LimitClause = if is_integer(Max), Max >= 0 -> ODBCType = ejabberd_config:get_option(
{odbc_type, LServer},
ejabberd_odbc:opt_type(odbc_type)),
LimitClause = if is_integer(Max), Max >= 0, ODBCType /= mssql ->
[<<" limit ">>, jlib:integer_to_binary(Max+1)]; [<<" limit ">>, jlib:integer_to_binary(Max+1)];
true -> true ->
[] []
end, end,
TopClause = if is_integer(Max), Max >= 0, ODBCType == mssql ->
[<<" TOP ">>, jlib:integer_to_binary(Max+1)];
true ->
[]
end,
WithClause = case With of WithClause = case With of
{text, <<>>} -> {text, <<>>} ->
[]; [];
@ -1320,7 +1328,7 @@ make_sql_query(User, _LServer, Start, End, With, RSM) ->
end, end,
SUser = ejabberd_odbc:escape(User), SUser = ejabberd_odbc:escape(User),
Query = [<<"SELECT timestamp, xml, peer, kind, nick" Query = [<<"SELECT ">>, TopClause, <<" timestamp, xml, peer, kind, nick"
" FROM archive WHERE username='">>, " FROM archive WHERE username='">>,
SUser, <<"'">>, WithClause, StartClause, EndClause, SUser, <<"'">>, WithClause, StartClause, EndClause,
PageClause], PageClause],

View File

@ -697,12 +697,21 @@ get_items(Nidx, _From,
{selected, [_], [[C]]} -> C; {selected, [_], [[C]]} -> C;
_ -> <<"0">> _ -> <<"0">>
end, end,
case catch Query = fun(mssql, _) ->
ejabberd_odbc:sql_query_t([<<"select itemid, publisher, creation, modification, payload " ejabberd_odbc:sql_query_t(
"from pubsub_item where nodeid='">>, Nidx, [<<"select top ">>, jlib:i2l(Max),
<<"' and ">>, AttrName, <<" ">>, Way, <<" ">>, Id, <<" order by ">>, <<" itemid, publisher, creation, modification, payload "
AttrName, <<" ">>, Order, <<" limit ">>, jlib:i2l(Max), <<" ;">>]) "from pubsub_item where nodeid='">>, Nidx,
of <<"' and ">>, AttrName, <<" ">>, Way, <<" ">>, Id, <<" order by ">>,
AttrName, <<" ">>, Order, <<";">>]);
(_, _) ->
ejabberd_odbc:sql_query_t(
[<<"select itemid, publisher, creation, modification, payload "
"from pubsub_item where nodeid='">>, Nidx,
<<"' and ">>, AttrName, <<" ">>, Way, <<" ">>, Id, <<" order by ">>,
AttrName, <<" ">>, Order, <<" limit ">>, jlib:i2l(Max), <<" ;">>])
end,
case catch ejabberd_odbc:sql_query_t(Query) of
{selected, {selected,
[<<"itemid">>, <<"publisher">>, <<"creation">>, <<"modification">>, <<"payload">>], RItems} -> [<<"itemid">>, <<"publisher">>, <<"creation">>, <<"modification">>, <<"payload">>], RItems} ->
case RItems of case RItems of
@ -760,11 +769,20 @@ get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId, RSM
end. end.
get_last_items(Nidx, _From, Count) -> get_last_items(Nidx, _From, Count) ->
case catch Limit = jlib:i2l(Count),
ejabberd_odbc:sql_query_t([<<"select itemid, publisher, creation, modification, payload " Query = fun(mssql, _) ->
"from pubsub_item where nodeid='">>, Nidx, ejabberd_odbc:sql_query_t(
<<"' order by modification desc limit ">>, jlib:i2l(Count), <<";">>]) [<<"select top ">>, Limit,
of <<" itemid, publisher, creation, modification, payload "
"from pubsub_item where nodeid='">>, Nidx,
<<"' order by modification desc ;">>]);
(_, _) ->
ejabberd_odbc:sql_query_t(
[<<"select itemid, publisher, creation, modification, payload "
"from pubsub_item where nodeid='">>, Nidx,
<<"' order by modification desc limit ">>, Limit, <<";">>])
end,
case catch ejabberd_odbc:sql_query_t(Query) of
{selected, {selected,
[<<"itemid">>, <<"publisher">>, <<"creation">>, <<"modification">>, <<"payload">>], RItems} -> [<<"itemid">>, <<"publisher">>, <<"creation">>, <<"modification">>, <<"payload">>], RItems} ->
{result, [raw_to_item(Nidx, RItem) || RItem <- RItems]}; {result, [raw_to_item(Nidx, RItem) || RItem <- RItems]};