php-spark/plugins/com.noccy.pdo/Commands/PdoExecCommand.php

37 lines
1.2 KiB
PHP
Raw Normal View History

2021-12-08 23:58:28 +00:00
<?php
namespace SparkPlug\Com\Noccy\Pdo\Commands;
2021-12-08 23:58:28 +00:00
use Spark\Commands\Command;
use Symfony\Component\Console\Helper\Table;
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 PdoExecCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output)
{
$source = $input->getOption("res");
$sourcePdo = get_resource($source)->getPDO();
if (!$sourcePdo) {
$output->writeln("<error>Invalid resource: {$source}</>");
return Command::INVALID;
}
$query = $input->getArgument('query');
$stmt = $sourcePdo->prepare($query);
$stmt->execute();
return Command::SUCCESS;
}
protected function configure() {
$this->setName("pdo:exec");
$this->setDescription("Run a query without returning data");
$this->addOption("res", "r", InputOption::VALUE_REQUIRED, "Resource to query", "db");
$this->addArgument("query", InputArgument::REQUIRED, "SQL query to execute");
}
}