You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.5 KiB
79 lines
2.5 KiB
3 years ago
|
#!/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 });
|
||
|
}
|
||
|
|
||
3 years ago
|
my $db_uuid = $anvil->data->{switches}{'uuid'};
|
||
|
my $sub_name = defined $anvil->data->{switches}{'sub'} ? $anvil->data->{switches}{'sub'} : "";
|
||
|
my $sub_module_name = defined $anvil->data->{switches}{'sub-module'} ? $anvil->data->{switches}{'sub-module'} : "Database";
|
||
|
my $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'} : "";
|
||
3 years ago
|
|
||
|
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";
|
||
|
}
|
||
3 years ago
|
elsif ($anvil->${sub_module_name}->can($sub_name))
|
||
3 years ago
|
{
|
||
3 years ago
|
my $decoded_sub_params;
|
||
3 years ago
|
my $is_decode_json_success = eval {
|
||
3 years ago
|
$decoded_sub_params = decode_json($sub_params);
|
||
3 years ago
|
};
|
||
|
|
||
|
if (not $is_decode_json_success)
|
||
|
{
|
||
|
print "error: failed to parse subroutine parameters\n";
|
||
|
$anvil->nice_exit({ exit_code => 1 });
|
||
|
}
|
||
|
|
||
3 years ago
|
my $results = $anvil->${sub_module_name}->${sub_name}($decoded_sub_params);
|
||
3 years ago
|
print JSON->new->utf8->encode({ sub_results => $results })."\n";
|
||
|
}
|
||
3 years ago
|
else
|
||
|
{
|
||
3 years ago
|
print "error: missing switches and perhaps their respective parameters; one of --query or --sub is required\n";
|
||
3 years ago
|
$anvil->nice_exit({ exit_code => 1 });
|
||
|
}
|
||
|
|
||
2 years ago
|
$anvil->nice_exit({ exit_code => 0 });
|