This commit is contained in:
parent
c30666ee26
commit
a2fa84255f
|
@ -238,12 +238,16 @@ 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(List) when is_list(List) -> [$', List, $'];
|
quote(Str) when is_list(Str);
|
||||||
quote(Bin) when is_binary(Bin) -> [$', Bin, $'];
|
is_binary(Str);
|
||||||
quote(Num) when is_number(Num) -> bin(Num);
|
is_atom(Str);
|
||||||
quote(Bool) when is_boolean(Bool) -> bin(Bool);
|
is_map(Str) ->
|
||||||
quote(Atom) when is_atom(Atom) -> [$', atom_to_binary(Atom, utf8), $'];
|
[$', escape_apo(bin(Str)), $'];
|
||||||
quote(Map) when is_map(Map) -> [$', emqx_json:encode(Map), $'].
|
quote(Val) ->
|
||||||
|
bin(Val).
|
||||||
|
|
||||||
|
escape_apo(Str) ->
|
||||||
|
re:replace(Str, <<"'">>, <<"\\\\'">>, [{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);
|
||||||
|
|
|
@ -116,3 +116,12 @@ t_preproc_sql3(_) ->
|
||||||
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',b:1,c:1.0,d:'{\"d1\":\"hi\"}'">>,
|
?assertEqual(<<"a:'1',b:1,c:1.0,d:'{\"d1\":\"hi\"}'">>,
|
||||||
emqx_rule_utils:proc_sql_param_str(ParamsTokens, Selected)).
|
emqx_rule_utils:proc_sql_param_str(ParamsTokens, Selected)).
|
||||||
|
|
||||||
|
t_preproc_sql4(_) ->
|
||||||
|
%% with apostrophes
|
||||||
|
%% https://github.com/emqx/emqx/issues/4135
|
||||||
|
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_sql_param_str(ParamsTokens, Selected)).
|
||||||
|
|
Loading…
Reference in New Issue