1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Tools\Console\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; |
6
|
|
|
use Doctrine\DBAL\Tools\Dumper; |
7
|
|
|
use LogicException; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use function assert; |
15
|
|
|
use function is_bool; |
16
|
|
|
use function is_numeric; |
17
|
|
|
use function is_string; |
18
|
|
|
use function sprintf; |
19
|
|
|
use function stripos; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Task for executing arbitrary SQL that can come from a file or directly from |
23
|
|
|
* the command line. |
24
|
|
|
*/ |
25
|
|
|
class RunSqlCommand extends Command |
26
|
|
|
{ |
27
|
|
|
/** @return void */ |
28
|
|
|
protected function configure() |
29
|
|
|
{ |
30
|
|
|
$this |
31
|
|
|
->setName('dbal:run-sql') |
32
|
|
|
->setDescription('Executes arbitrary SQL directly from the command line.') |
33
|
|
|
->setDefinition([ |
34
|
|
|
new InputArgument('sql', InputArgument::REQUIRED, 'The SQL statement to execute.'), |
35
|
|
|
new InputOption('connection', null, InputOption::VALUE_REQUIRED, 'The database connection on which to execute the SQL statement', 'db'), |
36
|
|
|
new InputOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of result set.', 7), |
37
|
|
|
new InputOption('force-fetch', null, InputOption::VALUE_NONE, 'Forces fetching the result.'), |
38
|
|
|
]) |
39
|
|
|
->setHelp(<<<EOT |
40
|
|
|
Executes arbitrary SQL directly from the command line. |
41
|
|
|
EOT |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
|
$connection = $input->getOption('connection'); |
51
|
|
|
|
52
|
|
|
if (! is_string($connection)) { |
53
|
|
|
throw new LogicException("Option 'connection' must contain a string value"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$connHelper = $this->getHelper($connection); |
57
|
|
|
|
58
|
|
|
if (! $connHelper instanceof ConnectionHelper) { |
59
|
|
|
throw new LogicException(sprintf( |
60
|
|
|
"Helper '%s' must be a valid '%s' object.", |
61
|
|
|
$connection, |
62
|
|
|
ConnectionHelper::class |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$conn = $connHelper->getConnection(); |
67
|
|
|
|
68
|
|
|
$sql = $input->getArgument('sql'); |
69
|
|
|
|
70
|
|
|
if ($sql === null) { |
71
|
|
|
throw new RuntimeException("Argument 'SQL' is required in order to execute this command correctly."); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
assert(is_string($sql)); |
75
|
|
|
|
76
|
|
|
$depth = $input->getOption('depth'); |
77
|
|
|
|
78
|
|
|
if (! is_numeric($depth)) { |
79
|
|
|
throw new LogicException("Option 'depth' must contains an integer value"); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$forceFetch = $input->getOption('force-fetch'); |
83
|
|
|
assert(is_bool($forceFetch)); |
84
|
|
|
|
85
|
|
|
if (stripos($sql, 'select') === 0 || $forceFetch) { |
86
|
|
|
$resultSet = $conn->fetchAll($sql); |
87
|
|
|
} else { |
88
|
|
|
$resultSet = $conn->executeUpdate($sql); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$output->write(Dumper::dump($resultSet, (int) $depth)); |
92
|
|
|
|
93
|
|
|
return 0; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|