|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Anvil::Tools;
|
|
|
|
use JSON;
|
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
|
|
|
my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0];
|
|
|
|
my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0];
|
|
|
|
if (($running_directory =~ /^\./) && ($ENV{PWD}))
|
|
|
|
{
|
|
|
|
$running_directory =~ s/^\./$ENV{PWD}/;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $anvil = Anvil::Tools->new();
|
|
|
|
|
|
|
|
sub db_access
|
|
|
|
{
|
|
|
|
my $parameters = shift;
|
|
|
|
my $db_uuid = $parameters->{db_uuid};
|
|
|
|
my $sql_query = $parameters->{sql_query};
|
|
|
|
my $access_mode = $parameters->{access_mode};
|
|
|
|
|
|
|
|
my $access_parameters = { query => $sql_query, uuid => $db_uuid, source => $THIS_FILE, line => __LINE__ };
|
|
|
|
|
|
|
|
return ($access_mode eq "write")
|
|
|
|
? { write_code => $anvil->Database->write($access_parameters) }
|
|
|
|
: $anvil->Database->query($access_parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
$anvil->Get->switches;
|
|
|
|
|
|
|
|
$anvil->Database->connect;
|
|
|
|
$anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132" });
|
|
|
|
if (not $anvil->data->{sys}{database}{connections})
|
|
|
|
{
|
|
|
|
# No databases, exit.
|
|
|
|
$anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003" });
|
|
|
|
$anvil->nice_exit({ exit_code => 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
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 switches and perhaps their respective parameters; one of --query or --sub is required\n";
|
|
|
|
$anvil->nice_exit({ exit_code => 1 });
|
|
|
|
}
|
|
|
|
|