php-spark/plugins/com.noccy.pdo.shell/PdoShellCommand.php
Christopher Vagnetoft 0c7fc0196a Multiple fixes
* PDO shell improvements: .query command, -r and --db on command
  line to read commands from file or preselect database.
* Updated build scripts and readme
2021-12-16 16:01:17 +01:00

47 lines
1.4 KiB
PHP

<?php
namespace SparkPlug\Com\Noccy\Pdo\Shell;
use Spark\Commands\Command;
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 PdoShellCommand extends Command {
protected function configure() {
$this->setName("pdo:shell");
$this->setDescription("Launch an interactive PDO shell");
$this->addOption("db", null, InputOption::VALUE_REQUIRED, "Select database resource", "db");
$this->addOption("read", "r", InputOption::VALUE_REQUIRED, "Read commands to execute from file");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$shell = new Shell\PdoShell($output);
$db = $input->getOption("db");
$read = $input->getOption("read");
if ($read) {
if (!file_exists($read)) {
$output->writeln("<error>File not found: {$read}</>");
return Command::FAILURE;
}
$file = file($read, FILE_IGNORE_NEW_LINES);
$shell->runCommands($file);
} else {
$shell->runCommands([ ".select {$db}" ]);
$shell->run();
}
return Command::SUCCESS;
}
}