fix(rulesql): proc cassandra cql with apostrophes failed
This commit is contained in:
parent
a2fa84255f
commit
70a490114d
|
@ -27,6 +27,7 @@
|
||||||
, preproc_sql/2
|
, preproc_sql/2
|
||||||
, proc_sql/2
|
, proc_sql/2
|
||||||
, proc_sql_param_str/2
|
, proc_sql_param_str/2
|
||||||
|
, proc_cql_param_str/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
%% type converting
|
%% type converting
|
||||||
|
@ -145,8 +146,15 @@ proc_sql(Tokens, Data) ->
|
||||||
|
|
||||||
-spec(proc_sql_param_str(tmpl_token(), map()) -> binary()).
|
-spec(proc_sql_param_str(tmpl_token(), map()) -> binary()).
|
||||||
proc_sql_param_str(Tokens, Data) ->
|
proc_sql_param_str(Tokens, Data) ->
|
||||||
|
proc_param_str(Tokens, Data, fun quote_sql/1).
|
||||||
|
|
||||||
|
-spec(proc_cql_param_str(tmpl_token(), map()) -> binary()).
|
||||||
|
proc_cql_param_str(Tokens, Data) ->
|
||||||
|
proc_param_str(Tokens, Data, fun quote_cql/1).
|
||||||
|
|
||||||
|
proc_param_str(Tokens, Data, Quote) ->
|
||||||
iolist_to_binary(
|
iolist_to_binary(
|
||||||
proc_tmpl(Tokens, Data, #{return => rawlist, var_trans => fun quote/1})).
|
proc_tmpl(Tokens, Data, #{return => rawlist, var_trans => Quote})).
|
||||||
|
|
||||||
%% backward compatibility for hot upgrading from =< e4.2.1
|
%% backward compatibility for hot upgrading from =< e4.2.1
|
||||||
get_phld_var(Fun, Data) when is_function(Fun) ->
|
get_phld_var(Fun, Data) when is_function(Fun) ->
|
||||||
|
@ -238,16 +246,23 @@ sql_data(Bool) when is_boolean(Bool) -> Bool;
|
||||||
sql_data(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
|
sql_data(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
|
||||||
sql_data(Map) when is_map(Map) -> emqx_json:encode(Map).
|
sql_data(Map) when is_map(Map) -> emqx_json:encode(Map).
|
||||||
|
|
||||||
quote(Str) when is_list(Str);
|
quote_sql(Str) ->
|
||||||
is_binary(Str);
|
quote(Str, <<"\\\\'">>).
|
||||||
is_atom(Str);
|
|
||||||
is_map(Str) ->
|
quote_cql(Str) ->
|
||||||
[$', escape_apo(bin(Str)), $'];
|
quote(Str, <<"''">>).
|
||||||
quote(Val) ->
|
|
||||||
|
quote(Str, ReplaceWith) when
|
||||||
|
is_list(Str);
|
||||||
|
is_binary(Str);
|
||||||
|
is_atom(Str);
|
||||||
|
is_map(Str) ->
|
||||||
|
[$', escape_apo(bin(Str), ReplaceWith), $'];
|
||||||
|
quote(Val, _) ->
|
||||||
bin(Val).
|
bin(Val).
|
||||||
|
|
||||||
escape_apo(Str) ->
|
escape_apo(Str, ReplaceWith) ->
|
||||||
re:replace(Str, <<"'">>, <<"\\\\'">>, [{return, binary}, global]).
|
re:replace(Str, <<"'">>, ReplaceWith, [{return, binary}, global]).
|
||||||
|
|
||||||
str(Bin) when is_binary(Bin) -> binary_to_list(Bin);
|
str(Bin) when is_binary(Bin) -> binary_to_list(Bin);
|
||||||
str(Num) when is_number(Num) -> number_to_list(Num);
|
str(Num) when is_number(Num) -> number_to_list(Num);
|
||||||
|
|
|
@ -125,3 +125,12 @@ t_preproc_sql4(_) ->
|
||||||
ParamsTokens = emqx_rule_utils:preproc_tmpl(<<"a:${a},b:${b},c:${c},d:${d}">>),
|
ParamsTokens = emqx_rule_utils:preproc_tmpl(<<"a:${a},b:${b},c:${c},d:${d}">>),
|
||||||
?assertEqual(<<"a:'1\\'\\'2',b:1,c:1.0,d:'{\"d1\":\"someone\\'s phone\"}'">>,
|
?assertEqual(<<"a:'1\\'\\'2',b:1,c:1.0,d:'{\"d1\":\"someone\\'s phone\"}'">>,
|
||||||
emqx_rule_utils:proc_sql_param_str(ParamsTokens, Selected)).
|
emqx_rule_utils:proc_sql_param_str(ParamsTokens, Selected)).
|
||||||
|
|
||||||
|
t_preproc_sql5(_) ->
|
||||||
|
%% with apostrophes for cassandra
|
||||||
|
%% https://github.com/emqx/emqx/issues/4148
|
||||||
|
Selected = #{a => <<"1''2">>, b => 1, c => 1.0,
|
||||||
|
d => #{d1 => <<"someone's phone">>}},
|
||||||
|
ParamsTokens = emqx_rule_utils:preproc_tmpl(<<"a:${a},b:${b},c:${c},d:${d}">>),
|
||||||
|
?assertEqual(<<"a:'1''''2',b:1,c:1.0,d:'{\"d1\":\"someone''s phone\"}'">>,
|
||||||
|
emqx_rule_utils:proc_cql_param_str(ParamsTokens, Selected)).
|
||||||
|
|
Loading…
Reference in New Issue