fix(tools): allow striker-access-database to process and execute DB subs

main
Tsu-ba-me 3 years ago
parent 3e8baf4b3e
commit c268d345fc
  1. 27
      tools/striker-access-database

@ -41,19 +41,36 @@ if (not $anvil->data->{sys}{database}{connections})
$anvil->nice_exit({ exit_code => 1 }); $anvil->nice_exit({ exit_code => 1 });
} }
my $db_uuid = $anvil->data->{switches}{'uuid'}; my $db_uuid = $anvil->data->{switches}{'uuid'};
my $sql_query = $anvil->data->{switches}{'query'}; my $db_sub_name = defined $anvil->data->{switches}{'sub'} ? $anvil->data->{switches}{'sub'} : "";
my $access_mode = defined $anvil->data->{switches}{'mode'} ? $anvil->data->{switches}{'mode'} : ""; my $db_sub_params = defined $anvil->data->{switches}{'sub-params'} ? $anvil->data->{switches}{'sub-params'} : "{}";
my $sql_query = $anvil->data->{switches}{'query'};
my $access_mode = defined $anvil->data->{switches}{'mode'} ? $anvil->data->{switches}{'mode'} : "";
if ($sql_query) if ($sql_query)
{ {
my $results = db_access({ db_uuid => $db_uuid, sql_query => $sql_query, access_mode => $access_mode }); my $results = db_access({ db_uuid => $db_uuid, sql_query => $sql_query, access_mode => $access_mode });
print JSON->new->utf8->encode($results)."\n"; print JSON->new->utf8->encode($results)."\n";
} }
elsif ($anvil->Database->can($db_sub_name))
{
my $decoded_db_sub_params;
my $is_decode_json_success = eval {
$decoded_db_sub_params = decode_json($db_sub_params);
};
if (not $is_decode_json_success)
{
print "error: failed to parse subroutine parameters\n";
$anvil->nice_exit({ exit_code => 1 });
}
my $results = $anvil->Database->${db_sub_name}($decoded_db_sub_params);
print JSON->new->utf8->encode({ sub_results => $results })."\n";
}
else else
{ {
print "error: missing --query and its parameter\n"; print "error: missing switches and perhaps their respective parameters; one of --query or --sub is required\n";
$anvil->nice_exit({ exit_code => 1 }); $anvil->nice_exit({ exit_code => 1 });
} }

Loading…
Cancel
Save