From aeb5b2a2ff071cfe9150f91c1d31408e5d5543df Mon Sep 17 00:00:00 2001 From: firest Date: Mon, 29 Aug 2022 22:16:28 +0800 Subject: [PATCH] fix(placeholder): allow placeholders in sql to be wrapped by quotes --- apps/emqx_plugin_libs/src/emqx_placeholder.erl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/emqx_plugin_libs/src/emqx_placeholder.erl b/apps/emqx_plugin_libs/src/emqx_placeholder.erl index f63f5eefc..70a1e41a5 100644 --- a/apps/emqx_plugin_libs/src/emqx_placeholder.erl +++ b/apps/emqx_plugin_libs/src/emqx_placeholder.erl @@ -39,7 +39,7 @@ sql_data/1 ]). --define(EX_PLACE_HOLDER, "(\\$\\{[a-zA-Z0-9\\._]+\\})"). +-define(EX_PLACE_HOLDER, "(\\$\\{[a-zA-Z0-9\\._]+\\}|\"\\$\\{[a-zA-Z0-9\\._]+\\}\")"). %% Space and CRLF -define(EX_WITHE_CHARS, "\\s"). @@ -235,7 +235,9 @@ get_phld_var(Phld, Data) -> emqx_rule_maps:nested_get(Phld, Data). preproc_var_re(#{placeholders := PHs}) -> - "(" ++ string:join([ph_to_re(PH) || PH <- PHs], "|") ++ ")"; + Res = [ph_to_re(PH) || PH <- PHs], + QuoteRes = ["\"" ++ Re ++ "\"" || Re <- Res], + "(" ++ string:join(Res ++ QuoteRes, "|") ++ ")"; preproc_var_re(#{}) -> ?EX_PLACE_HOLDER. @@ -292,7 +294,9 @@ parse_nested(Attr) -> end. unwrap(<<"${", Val/binary>>) -> - binary:part(Val, {0, byte_size(Val) - 1}). + binary:part(Val, {0, byte_size(Val) - 1}); +unwrap(<<"\"${", Val/binary>>) -> + binary:part(Val, {0, byte_size(Val) - 2}). quote_sql(Str) -> quote(Str, <<"\\\\'">>).