25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-24 17:29:28 +01:00

Fix MS SQL error caused by ORDER BY in subquery

'The ORDER BY clause is invalid in views, inline functions, derived
tables, subqueries, and common table expressions, unless TOP, OFFSET
or FOR XML is also specified.'

Omit the ORDER BY clause from subquery if the SELECT is not constrained
by TOP.
This commit is contained in:
Stu Tomlinson 2022-12-12 16:05:46 +00:00
parent 5e94fdcfd5
commit 19f2f1fa86

View File

@ -441,6 +441,11 @@ make_sql_query(User, LServer, MAMQuery, RSM, ExtraUsernames) ->
true ->
[]
end,
SubOrderClause = if LimitClause /= [], TopClause /= [] ->
<<" ORDER BY timestamp DESC ">>;
true ->
[]
end,
WithTextClause = if is_binary(WithText), WithText /= <<>> ->
[<<" and match (txt) against (">>,
ToString(WithText), <<")">>];
@ -528,7 +533,7 @@ make_sql_query(User, LServer, MAMQuery, RSM, ExtraUsernames) ->
% XEP-0059: Result Set Management
% 2.5 Requesting the Last Page in a Result Set
[<<"SELECT">>, UserSel, <<" timestamp, xml, peer, kind, nick FROM (">>,
Query, <<" ORDER BY timestamp DESC ">>,
Query, SubOrderClause,
LimitClause, <<") AS t ORDER BY timestamp ASC;">>];
_ ->
[Query, <<" ORDER BY timestamp ASC ">>,