From c268d345fc6e9b619eb28cd264ab60f879ec6c61 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 7 Dec 2021 16:53:54 -0500 Subject: [PATCH] fix(tools): allow striker-access-database to process and execute DB subs --- tools/striker-access-database | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tools/striker-access-database b/tools/striker-access-database index d1cbadb4..3aa82f9f 100755 --- a/tools/striker-access-database +++ b/tools/striker-access-database @@ -41,19 +41,36 @@ if (not $anvil->data->{sys}{database}{connections}) $anvil->nice_exit({ exit_code => 1 }); } -my $db_uuid = $anvil->data->{switches}{'uuid'}; -my $sql_query = $anvil->data->{switches}{'query'}; -my $access_mode = defined $anvil->data->{switches}{'mode'} ? $anvil->data->{switches}{'mode'} : ""; +my $db_uuid = $anvil->data->{switches}{'uuid'}; +my $db_sub_name = defined $anvil->data->{switches}{'sub'} ? $anvil->data->{switches}{'sub'} : ""; +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) { my $results = db_access({ db_uuid => $db_uuid, sql_query => $sql_query, access_mode => $access_mode }); - 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 { - 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 }); }