php-spark/plugins/com.noccy.pdo.shell/Shell/PdoShell.php

438 lines
13 KiB
PHP
Raw Normal View History

2021-12-16 01:39:52 +00:00
<?php
namespace SparkPlug\Com\Noccy\Pdo\Shell\Shell;
use Attribute;
2021-12-16 01:39:52 +00:00
use SparkPlug\Com\Noccy\Pdo\PdoResource;
use Spark\Commands\Command;
use Spark\SparkApplication;
2021-12-16 01:39:52 +00:00
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class PdoShell {
private OutputInterface $output;
private string $resource = "db";
private ?PdoResource $db = null;
private array $vars = [];
private bool $running = false;
private array $options = [];
private ?array $lastQuery = null;
private ShellState $state;
#[EnumSetting('output', [ 'table', 'vertical', 'dump' ])]
#[EnumSetting('table.style', [ 'box', 'compact', 'borderless' ])]
2021-12-16 01:39:52 +00:00
private array $defaultOptions = [
'output' => 'table',
'table.maxwidth' => 40,
'table.style' => 'box',
'capture' => false,
'syntax' => 'none',
];
private array $validOptions = [
'output' => [ 'table', 'vertical', 'dump' ],
'table.maxwidth' => 'int',
'table.style' => [ 'box', 'compact', 'borderless' ],
'capture' => 'bool',
'syntax' => [ 'none', 'mysql', 'sqlite' ]
2021-12-16 01:39:52 +00:00
];
public function __construct(OutputInterface $output)
{
$this->output = $output;
$this->state = new ShellState();
2021-12-16 01:39:52 +00:00
$this->options = $this->defaultOptions;
}
public function getState(): ShellState
{
return $this->state;
}
2021-12-16 01:39:52 +00:00
private function promptForCommand()
{
$info = $this->resource . ($this->db?"":"?");
if ($this->getOption('capture')) {
$info .= '][#' . $this->vars['capture_index'];
}
$prompt = sprintf("PDO:[%s]> ", $info);
2021-12-16 01:39:52 +00:00
$input = readline($prompt);
return $input;
}
private function parseCommand(string $input): array
{
$parsed = str_getcsv($input, ' ', '"');
$parsed = array_map([$this,"expand"], $parsed);
$command = array_shift($parsed);
return [
strtolower($command),
$parsed
];
}
private function getOption(string $name): mixed
{
return $this->options[$name] ?? null;
}
2021-12-16 01:39:52 +00:00
private function expand(string $string): string
{
return $string;
}
public function run()
{
if ($this->running == true) return;
$this->running = true;
while ($this->running) {
$input = $this->promptForCommand();
if (!trim($input)) {
continue;
}
readline_add_history($input);
if (str_starts_with($input, ".")) {
[$cmd,$args] = $this->parseCommand($input);
$this->handleCommand($cmd, $args);
} else {
$this->doQuery($input, []);
}
}
}
public function runCommands(array $commands)
{
foreach ($commands as $input) {
if (str_starts_with($input, ".")) {
[$cmd,$args] = $this->parseCommand($input);
$this->handleCommand($cmd, $args);
} else {
$this->doQuery($input, []);
}
}
}
2021-12-16 01:39:52 +00:00
private function handleCommand(string $command, array $args)
{
switch ($command) {
case '.save':
$this->doSaveCommand($args);
break;
case '.show':
$this->doShowCommand($args);
break;
2021-12-16 01:39:52 +00:00
case '.select':
$this->doSelectCommand($args);
break;
case '.set':
$this->doSetCommand($args);
break;
case '.var':
$this->doVarCommand($args);
break;
case '.query':
$this->doQueryCommand($args);
break;
2021-12-16 01:39:52 +00:00
case '.help':
$this->doHelpCommand($args);
break;
case '.capture':
$this->doSetCommand(['capture', 1]);
$this->doVarCommand(['capture_index', 0]);
break;
case '.quit':
2021-12-16 01:39:52 +00:00
case '.exit':
$this->running = false;
break;;
default:
$this->output->writeln("<error>Bad command. Try .help</>");
}
}
private function doHelpCommand(array $args)
{
$cmds = [
'.help' => "Show this help",
'.save FILE' => "Save the last query and result to a .json file",
2021-12-16 01:39:52 +00:00
'.select RES' => "Select the database resource to query",
'.show [FILE]' => "Show the last query, or one saved to file",
2021-12-16 01:39:52 +00:00
'.set [KEY [VALUE]]' => "Set a configuration value",
'.var [NAME [VALUE]]' => "Set a variable, or show variable value",
'.exit|.quit' => "Exit the shell",
2021-12-16 01:39:52 +00:00
'SQL' => "Run SQL against the database",
'.query SQL [PARAM..]' => "Escape and run a query using ? as placeholder",
2021-12-16 01:39:52 +00:00
];
foreach ($cmds as $cmd=>$info) {
$this->output->writeln(" <options=bold>{$cmd}</> - <info>{$info}</>");
}
}
private function doSaveCommand(array $args)
{
if (empty($this->lastQuery)) {
$this->output->writeln("<error>No last query to save!</>");
return;
}
$filename = array_shift($args);
if (empty($filename)) {
for($n=0;$n<999;$n++) {
$filename = sprintf("query.%03d.json", $n);
if (!file_exists($filename)) break;
}
}
file_put_contents($filename, json_encode($this->lastQuery, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
$this->output->writeln("<info>Wrote {$filename}</>");
}
private function doShowCommand(array $args)
{
$file = array_shift($args);
if ($file) {
if (!file_exists($file)) {
$this->output->writeln("<error>File not found: {$file}</>");
return;
}
$json = json_decode(file_get_contents($file), true);
$query = $json['query']??'?';
$res = $json['result']??[];
} else {
if (!$this->lastQuery) {
return;
}
$query = $this->lastQuery['query'];
$res = $this->lastQuery['result'];
}
$this->output->writeln("<comment>{$query}</>");
switch ($this->options['output']) {
case 'table':
$this->dumpQueryTable($res);
break;
case 'vertical':
$this->dumpQueryVertical($res);
break;
default:
print_r($res);
}
}
2021-12-16 01:39:52 +00:00
private function doSetCommand(array $args)
{
$varname = array_shift($args);
$value = array_shift($args);
if (empty($varname)) {
foreach ($this->options as $var=>$value) {
if (!array_key_exists($var, $this->validOptions)) {
$info = "";
} elseif (is_array($this->validOptions[$var])) {
$info = "[<fg=cyan>".join("</>,<fg=cyan>", $this->validOptions[$var])."</>]";
} else {
$info = "<<fg=magenta>".$this->validOptions[$var]."</>>";
}
$this->output->writeln("<info>{$var}</>: <comment>".var_export($value,true)."</> ".$info);
2021-12-16 01:39:52 +00:00
}
return;
}
if ($value !== null) {
if (!array_key_exists($varname, $this->options)) {
$this->output->writeln("<error>No such option {$varname}</>");
return;
}
if (array_key_exists($varname, $this->validOptions)) {
$v = $this->validOptions[$varname];
if (is_array($v)) {
if (!in_array($value, $v)) {
$this->output->writeln("<error>Bad value for {$varname}</> Valid values: ".join(", ",$v)."</>");
return;
}
} elseif (is_string($v)) {
switch ($v) {
case 'int':
if (!ctype_digit($value)) {
$this->output->writeln("<error>Bad value for {$varname}</> Expected integer</>");
return;
}
break;
case 'bool':
$true = in_array($value, [ 1, "true", "yes", "on" ]);
$false = in_array($value, [ 0, "false", "no", "off" ]);
if (!$true && !$false) {
$this->output->writeln("<error>Bad value for {$varname}</> Expected boolean</>");
return;
}
$value = $true;
break;
}
}
}
$this->options[$varname] = $value;
2021-12-16 01:39:52 +00:00
} else {
$this->output->writeln(var_export($this->options[$varname]??null,true));
}
}
private function doSelectCommand(array $args)
{
$name = array_shift($args);
if (!$name) {
$app = SparkApplication::$instance;
$resources = $app->getResourceManager();
$named = $resources->getAllNamedResources();
foreach ($named as $name=>$resource) {
if (!($resource instanceof PdoResource)) {
continue;
}
$this->output->writeln(
sprintf(" <comment>%s</><info>%s</>", ($name==$this->resource?"*":" "), $name)
);
}
return;
}
2021-12-16 01:39:52 +00:00
$res = get_resource($name);
if ($res instanceof PdoResource) {
$this->db = $res;
$this->resource = $name;
$this->output->writeln("<fg=green>** Selected {$name}</>");
2021-12-16 01:39:52 +00:00
} else {
$this->output->writeln("<error>Invalid resource {$name}</>");
}
}
private function doVarCommand(array $args)
{
$varname = array_shift($args);
if (empty($varname)) {
foreach ($this->vars as $var=>$value) {
$this->output->writeln("<info>{$var}</>=<comment>".var_export($value,true)."</>");
}
return;
}
if (count($args) > 1) {
$this->vars[$varname] = $args;
} elseif (count($args) > 0) {
$this->vars[$varname] = array_shift($args);
} else {
$this->output->writeln(var_export($this->vars[$varname]??null,true));
}
}
private function doQueryCommand(array $args)
{
$query = array_shift($args);
$this->doQuery($query, $args);
}
2021-12-16 01:39:52 +00:00
private function doQuery(string $query, array $params=[])
{
if (!$query) {
return;
}
2021-12-16 01:39:52 +00:00
if (!$this->db) {
$this->output->writeln("<error>No database resource selected</>");
return;
}
try {
$pdo = $this->db->getPDO();
2021-12-16 01:39:52 +00:00
$stmt = $pdo->prepare($query);
$stmt->execute($params);
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\PDOException $e) {
$this->output->writeln("<error>{$e->getMessage()}</>");
return;
}
$this->lastQuery = [
'timestamp' => microtime(true),
'resource' => $this->resource,
'query' => $query,
'params' => $params,
'result' => $res
];
2021-12-16 01:39:52 +00:00
switch ($this->options['output']) {
case 'table':
$this->dumpQueryTable($res);
break;
case 'vertical':
$this->dumpQueryVertical($res);
break;
default:
print_r($res);
}
}
private function dumpQueryTable(array $res)
{
if (count($res) == 0) return;
$table = new Table($this->output);
$table->setHeaders(array_keys(reset($res)));
$table->setStyle($this->options['table.style']);
2021-12-16 01:39:52 +00:00
$max = $this->options['table.maxwidth'];
foreach ($res as $row) {
$table->addRow(array_map(function ($v) use ($max) {
return strlen($v)>$max ? substr($v, 0, $max)."..." : $v;
}, $row));
}
$table->render();
}
private function dumpQueryVertical(array $res)
{
if (count($res) == 0) return;
$names = array_keys(reset($res));
$nameWidth = max(array_map("strlen", $names)) + 4;
foreach ($res as $row) {
foreach ($row as $k=>$v) {
$this->output->writeln(
sprintf("<info>%{$nameWidth}s</>: %s",$k, $v)
);
}
if ($row != end($res)) {
$this->output->writeln(str_repeat("-", $nameWidth));
}
}
}
2021-12-16 01:39:52 +00:00
}
#[Attribute(Attribute::TARGET_PROPERTY)]
class EnumSetting
{
public function __construct(string $property, array $valid)
{
}
}