feat(tpl): support `:n` SQL parameters

This commit is contained in:
Andrew Mayorov 2023-04-28 12:03:55 +03:00
parent dfb7faf6a8
commit 7bb995f0c6
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
2 changed files with 11 additions and 2 deletions

View File

@ -34,7 +34,7 @@
-type values() :: [emqx_connector_sql:value()].
-type parse_opts() :: #{
parameters => '$n' | '?',
parameters => '$n' | ':n' | '?',
% Inherited from `emqx_connector_template:parse_opts()`
strip_double_quote => boolean()
}.
@ -116,7 +116,9 @@ mk_prepared_statement(Template, Opts) ->
mk_replace('?', Acc) ->
{"?", Acc};
mk_replace('$n', N) ->
{"$" ++ integer_to_list(N), N + 1}.
{"$" ++ integer_to_list(N), N + 1};
mk_replace(':n', N) ->
{":" ++ integer_to_list(N), N + 1}.
%% @doc Render a row template into a list of SQL values.
%% An _SQL value_ is a vaguely defined concept here, it is something that's considered

View File

@ -188,6 +188,13 @@ t_parse_sql_prepstmt_n(_) ->
emqx_connector_template_sql:render_prepstmt_strict(RowTemplate, Bindings)
).
t_parse_sql_prepstmt_colon(_) ->
{PrepareStatement, _RowTemplate} =
emqx_connector_template_sql:parse_prepstmt(<<"a=${a},b=${b},c=${c},d=${d}">>, #{
parameters => ':n'
}),
?assertEqual(<<"a=:1,b=:2,c=:3,d=:4">>, bin(PrepareStatement)).
t_parse_sql_prepstmt_partial_ph(_) ->
Bindings = #{a => <<"1">>, b => 1, c => 1.0, d => #{d1 => <<"hi">>}},
{PrepareStatement, RowTemplate} =