Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Tools/Console/Command/RunSqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Tools\Console\Command;

use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\DBAL\Tools\Dumper;
use LogicException;
use RuntimeException;
Expand All @@ -14,6 +15,7 @@
use function is_bool;
use function is_numeric;
use function is_string;
use function sprintf;
use function stripos;

/**
Expand All @@ -30,6 +32,7 @@ protected function configure()
->setDescription('Executes arbitrary SQL directly from the command line.')
->setDefinition([
new InputArgument('sql', InputArgument::REQUIRED, 'The SQL statement to execute.'),
new InputOption('connection', null, InputOption::VALUE_REQUIRED, 'The database connection on which to execute the SQL statement', 'db'),
new InputOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of result set.', 7),
new InputOption('force-fetch', null, InputOption::VALUE_NONE, 'Forces fetching the result.'),
])
Expand All @@ -44,7 +47,23 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$conn = $this->getHelper('db')->getConnection();
$connection = $input->getOption('connection');

if (! is_string($connection)) {
throw new LogicException("Option 'connection' must contain a string value");
}

$connHelper = $this->getHelper($connection);

if (! $connHelper instanceof ConnectionHelper) {
throw new LogicException(sprintf(
"Helper '%s' must be a valid '%s' object.",
$connection,
ConnectionHelper::class
));
}

$conn = $connHelper->getConnection();

$sql = $input->getArgument('sql');

Expand Down