Skip to content
Permalink
Browse files

Fix resources never destroyed

  • Loading branch information...
terry-xiaoyu committed May 31, 2019
1 parent b00fad4 commit 58a1ce45e1cf96cf05481d8ed076febef0d41976
Showing with 23 additions and 23 deletions.
  1. +2 −2 src/emqx_rule_engine.erl
  2. +21 −21 src/emqx_rule_utils.erl
@@ -335,11 +335,11 @@ clear_resource(_Module, undefined, ResId) ->
ok = emqx_rule_registry:remove_resource_params(ResId),
ok;
clear_resource(Module, Destroy, ResId) ->
ok = emqx_rule_registry:remove_resource_params(ResId),
case emqx_rule_registry:find_resource_params(ResId) of
{ok, #resource_params{params = Params}} ->
?RAISE(Module:Destroy(ResId, Params),
{{destroy_resource_failure, node()}, {{Module, Destroy}, _REASON_}});
{{destroy_resource_failure, node()}, {{Module, Destroy}, _REASON_}}),
ok = emqx_rule_registry:remove_resource_params(ResId);
not_found ->
ok
end.
@@ -65,33 +65,33 @@ proc_tmpl(Tokens, Data) ->
preproc_sql(Sql) ->
preproc_sql(Sql, '?').

-spec(preproc_sql(Sql::binary(), '?' | '$n') -> {prepare_statement(), prepare_params()}).
-spec(preproc_sql(Sql::binary(), ReplaceWith :: '?' | '$n') -> {prepare_statement(), prepare_params()}).
preproc_sql(Sql, ReplaceWith) ->
case re:run(Sql, ?EX_PLACE_HOLDER, [{capture, all_but_first, binary}, global]) of
{match, PlaceHolders} ->
NewSql =
case ReplaceWith of
'?' ->
re:replace(Sql, ?EX_PLACE_HOLDER, "?", [{return, binary}, global]);
'$n' ->
Parts = re:split(Sql, ?EX_PLACE_HOLDER, [{return,binary}, trim, group]),
{Sql1, _} =
lists:foldl(fun([Tkn, _Phld], {Acc, Seq}) ->
Seq1 = erlang:integer_to_binary(Seq),
{<<Acc/binary, Tkn/binary, "$", Seq1/binary>>, Seq + 1};
([Tkn], {Acc, Seq}) ->
{<<Acc/binary, Tkn/binary>>, Seq}
end, {<<>>, 1}, Parts),
binary_to_list(Sql1)
end,
{NewSql, fun(Data) ->
[maps:get(atom_key(Key), Data, undefined)
|| Key <- [var(hd(PH)) || PH <- PlaceHolders]]
end};
{repalce_with(Sql, ReplaceWith),
fun(Data) ->
[maps:get(atom_key(Key), Data, undefined)
|| Key <- [var(hd(PH)) || PH <- PlaceHolders]]
end};
nomatch ->
{Sql, []}
{Sql, fun(_) -> [] end}
end.

repalce_with(Tmpl, '?') ->
re:replace(Tmpl, ?EX_PLACE_HOLDER, "?", [{return, binary}, global]);
repalce_with(Tmpl, '$n') ->
Parts = re:split(Tmpl, ?EX_PLACE_HOLDER, [{return, binary}, trim, group]),
{Res, _} =
lists:foldl(
fun([Tkn, _Phld], {Acc, Seq}) ->
Seq1 = erlang:integer_to_binary(Seq),
{<<Acc/binary, Tkn/binary, "$", Seq1/binary>>, Seq + 1};
([Tkn], {Acc, Seq}) ->
{<<Acc/binary, Tkn/binary>>, Seq}
end, {<<>>, 1}, Parts),
Res.

atom_key(Key) when is_atom(Key) ->
Key;
atom_key(Key) when is_list(Key) ->

0 comments on commit 58a1ce4

Please sign in to comment.
You can’t perform that action at this time.