feat(tools): add cli tool for accessing database with SQL query

main
Tsu-ba-me 3 years ago
parent b89953dbe8
commit e909a715a3
  1. 59
      tools/striker-access-database

@ -0,0 +1,59 @@
#!/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 $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";
}
else
{
print "error: missing --query and its parameter\n";
$anvil->nice_exit({ exit_code => 1 });
}
Loading…
Cancel
Save