fix: bad empty map

This commit is contained in:
DDDHuang 2022-04-21 21:26:35 +08:00
parent 75f612a449
commit 4b7a5bbf53
2 changed files with 15 additions and 11 deletions

View File

@ -51,7 +51,7 @@ description() ->
init(#{query := SQL} = Source0) -> init(#{query := SQL} = Source0) ->
{PrepareSQL, TmplToken} = emqx_authz_utils:parse_sql(SQL, '?', ?PLACEHOLDERS), {PrepareSQL, TmplToken} = emqx_authz_utils:parse_sql(SQL, '?', ?PLACEHOLDERS),
Source = Source0#{prepare_statement => #{?MODULE => PrepareSQL}}, Source = Source0#{prepare_statement => #{?PREPARE_KEY => PrepareSQL}},
case emqx_authz_utils:create_resource(emqx_connector_mysql, Source) of case emqx_authz_utils:create_resource(emqx_connector_mysql, Source) of
{error, Reason} -> {error, Reason} ->
error({load_config_error, Reason}); error({load_config_error, Reason});
@ -74,7 +74,7 @@ authorize(
} }
) -> ) ->
RenderParams = emqx_authz_utils:render_sql_params(TmplToken, Client), RenderParams = emqx_authz_utils:render_sql_params(TmplToken, Client),
case emqx_resource:query(ResourceID, {prepared_query, ?MODULE, RenderParams}) of case emqx_resource:query(ResourceID, {prepared_query, ?PREPARE_KEY, RenderParams}) of
{ok, _Columns, []} -> {ok, _Columns, []} ->
nomatch; nomatch;
{ok, Columns, Rows} -> {ok, Columns, Rows} ->

View File

@ -86,6 +86,7 @@ on_start(InstId, #{server := {Host, Port},
{pool_size, PoolSize}], {pool_size, PoolSize}],
PoolName = emqx_plugin_libs_pool:pool_name(InstId), PoolName = emqx_plugin_libs_pool:pool_name(InstId),
Prepares = maps:get(prepare_statement, Config, #{}), Prepares = maps:get(prepare_statement, Config, #{}),
io:format("Prepares ~p~n", [Prepares]),
State = init_prepare(#{poolname => PoolName, prepare_statement => Prepares}), State = init_prepare(#{poolname => PoolName, prepare_statement => Prepares}),
case emqx_plugin_libs_pool:start_pool(PoolName, ?MODULE, Options ++ SslOpts) of case emqx_plugin_libs_pool:start_pool(PoolName, ?MODULE, Options ++ SslOpts) of
ok -> {ok, State}; ok -> {ok, State};
@ -181,17 +182,20 @@ connect(Options) ->
to_server(Str) -> to_server(Str) ->
emqx_connector_schema_lib:parse_server(Str, ?MYSQL_HOST_OPTIONS). emqx_connector_schema_lib:parse_server(Str, ?MYSQL_HOST_OPTIONS).
init_prepare(State = #{prepare_statement := #{}}) ->
State;
init_prepare(State = #{prepare_statement := Prepares, poolname := PoolName}) -> init_prepare(State = #{prepare_statement := Prepares, poolname := PoolName}) ->
case prepare_sql(Prepares, PoolName) of case maps:size(Prepares) of
ok -> 0 ->
State; State;
{error, Reason} -> _ ->
LogMeta = #{msg => <<"MySQL init prepare statement failed">>, reason => Reason}, case prepare_sql(Prepares, PoolName) of
?SLOG(error, LogMeta), ok ->
%% mark the prepare_statement as failed State;
State#{prepare_statement => {error, Prepares}} {error, Reason} ->
LogMeta = #{msg => <<"MySQL init prepare statement failed">>, reason => Reason},
?SLOG(error, LogMeta),
%% mark the prepare_statement as failed
State#{prepare_statement => {error, Prepares}}
end
end. end.
prepare_sql(Prepares, PoolName) when is_map(Prepares) -> prepare_sql(Prepares, PoolName) when is_map(Prepares) ->