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

Improve compatibility with CockroachDB (#3074)

This commit is contained in:
Alexey Shchepin 2019-12-11 17:48:53 +03:00
parent fbab446c24
commit f9120f75b0
2 changed files with 27 additions and 22 deletions

View File

@ -44,6 +44,7 @@
escape_like/1, escape_like/1,
escape_like_arg/1, escape_like_arg/1,
escape_like_arg_circumflex/1, escape_like_arg_circumflex/1,
to_string_literal/2,
to_bool/1, to_bool/1,
sqlite_db/1, sqlite_db/1,
sqlite_file/1, sqlite_file/1,
@ -251,6 +252,17 @@ to_array(EscapeFun, Val) ->
Escaped = lists:join(<<",">>, lists:map(EscapeFun, Val)), Escaped = lists:join(<<",">>, lists:map(EscapeFun, Val)),
[<<"{">>, Escaped, <<"}">>]. [<<"{">>, Escaped, <<"}">>].
to_string_literal(odbc, S) ->
<<"'", (escape(S))/binary, "'">>;
to_string_literal(mysql, S) ->
<<"'", (escape(S))/binary, "'">>;
to_string_literal(mssql, S) ->
<<"'", (standard_escape(S))/binary, "'">>;
to_string_literal(sqlite, S) ->
<<"'", (standard_escape(S))/binary, "'">>;
to_string_literal(pgsql, S) ->
<<"E'", (escape(S))/binary, "'">>.
encode_term(Term) -> encode_term(Term) ->
escape(list_to_binary( escape(list_to_binary(
erl_prettypr:format(erl_syntax:abstract(Term), erl_prettypr:format(erl_syntax:abstract(Term),

View File

@ -354,12 +354,7 @@ make_sql_query(User, LServer, MAMQuery, RSM, ExtraUsernames) ->
WithText = proplists:get_value(withtext, MAMQuery), WithText = proplists:get_value(withtext, MAMQuery),
{Max, Direction, ID} = get_max_direction_id(RSM), {Max, Direction, ID} = get_max_direction_id(RSM),
ODBCType = ejabberd_option:sql_type(LServer), ODBCType = ejabberd_option:sql_type(LServer),
Escape = ToString = fun(S) -> ejabberd_sql:to_string_literal(ODBCType, S) end,
case ODBCType of
mssql -> fun ejabberd_sql:standard_escape/1;
sqlite -> fun ejabberd_sql:standard_escape/1;
_ -> fun ejabberd_sql:escape/1
end,
LimitClause = if is_integer(Max), Max >= 0, ODBCType /= mssql -> LimitClause = if is_integer(Max), Max >= 0, ODBCType /= mssql ->
[<<" limit ">>, integer_to_binary(Max+1)]; [<<" limit ">>, integer_to_binary(Max+1)];
true -> true ->
@ -371,20 +366,18 @@ make_sql_query(User, LServer, MAMQuery, RSM, ExtraUsernames) ->
[] []
end, end,
WithTextClause = if is_binary(WithText), WithText /= <<>> -> WithTextClause = if is_binary(WithText), WithText /= <<>> ->
[<<" and match (txt) against ('">>, [<<" and match (txt) against (">>,
Escape(WithText), <<"')">>]; ToString(WithText), <<")">>];
true -> true ->
[] []
end, end,
WithClause = case catch jid:tolower(With) of WithClause = case catch jid:tolower(With) of
{_, _, <<>>} -> {_, _, <<>>} ->
[<<" and bare_peer='">>, [<<" and bare_peer=">>,
Escape(jid:encode(With)), ToString(jid:encode(With))];
<<"'">>];
{_, _, _} -> {_, _, _} ->
[<<" and peer='">>, [<<" and peer=">>,
Escape(jid:encode(With)), ToString(jid:encode(With))];
<<"'">>];
_ -> _ ->
[] []
end, end,
@ -415,23 +408,23 @@ make_sql_query(User, LServer, MAMQuery, RSM, ExtraUsernames) ->
_ -> _ ->
[] []
end, end,
SUser = Escape(User), SUser = ToString(User),
SServer = Escape(LServer), SServer = ToString(LServer),
HostMatch = case ejabberd_sql:use_new_schema() of HostMatch = case ejabberd_sql:use_new_schema() of
true -> true ->
[<<" and server_host='", SServer/binary, "'">>]; [<<" and server_host=", SServer/binary>>];
_ -> _ ->
<<"">> <<"">>
end, end,
{UserSel, UserWhere} = case ExtraUsernames of {UserSel, UserWhere} = case ExtraUsernames of
Users when is_list(Users) -> Users when is_list(Users) ->
EscUsers = [<<"'", (Escape(U))/binary, "'">> || U <- [User | Users]], EscUsers = [ToString(U) || U <- [User | Users]],
{<<" username,">>, {<<" username,">>,
[<<" username in (">>, str:join(EscUsers, <<",">>), <<")">>]}; [<<" username in (">>, str:join(EscUsers, <<",">>), <<")">>]};
subscribers_table -> subscribers_table ->
SJid = Escape(jid:encode({User, LServer, <<>>})), SJid = ToString(jid:encode({User, LServer, <<>>})),
RoomName = case ODBCType of RoomName = case ODBCType of
sqlite -> sqlite ->
<<"room || '@' || host">>; <<"room || '@' || host">>;
@ -439,11 +432,11 @@ make_sql_query(User, LServer, MAMQuery, RSM, ExtraUsernames) ->
<<"concat(room, '@', host)">> <<"concat(room, '@', host)">>
end, end,
{<<" username,">>, {<<" username,">>,
[<<" (username = '">>, SUser, <<"'">>, [<<" (username = ">>, SUser,
<<" or username in (select ">>, RoomName, <<" or username in (select ">>, RoomName,
<<" from muc_room_subscribers where jid='">>, SJid, <<"'">>, HostMatch, <<"))">>]}; <<" from muc_room_subscribers where jid=">>, SJid, HostMatch, <<"))">>]};
_ -> _ ->
{<<>>, [<<" username='">>, SUser, <<"'">>]} {<<>>, [<<" username=">>, SUser]}
end, end,
Query = [<<"SELECT ">>, TopClause, UserSel, Query = [<<"SELECT ">>, TopClause, UserSel,