summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php')
-rw-r--r--vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php119
1 files changed, 119 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php b/vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php
new file mode 100644
index 0000000..8ec6f23
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php
@@ -0,0 +1,119 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Tools\Console\Command;
6
7use Doctrine\DBAL\Connection;
8use Doctrine\DBAL\Exception;
9use Doctrine\DBAL\Tools\Console\ConnectionProvider;
10use RuntimeException;
11use Symfony\Component\Console\Command\Command;
12use Symfony\Component\Console\Input\InputArgument;
13use Symfony\Component\Console\Input\InputInterface;
14use Symfony\Component\Console\Input\InputOption;
15use Symfony\Component\Console\Output\OutputInterface;
16use Symfony\Component\Console\Style\SymfonyStyle;
17
18use function array_keys;
19use function assert;
20use function is_bool;
21use function is_string;
22use function sprintf;
23use function stripos;
24
25/**
26 * Task for executing arbitrary SQL that can come from a file or directly from
27 * the command line.
28 */
29class RunSqlCommand extends Command
30{
31 public function __construct(private readonly ConnectionProvider $connectionProvider)
32 {
33 parent::__construct();
34 }
35
36 protected function configure(): void
37 {
38 $this
39 ->setName('dbal:run-sql')
40 ->setDescription('Executes arbitrary SQL directly from the command line.')
41 ->setDefinition([
42 new InputOption('connection', null, InputOption::VALUE_REQUIRED, 'The named database connection'),
43 new InputArgument('sql', InputArgument::REQUIRED, 'The SQL statement to execute.'),
44 new InputOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of result set (deprecated).'),
45 new InputOption('force-fetch', null, InputOption::VALUE_NONE, 'Forces fetching the result.'),
46 ])
47 ->setHelp(<<<'EOT'
48The <info>%command.name%</info> command executes the given SQL query and
49outputs the results:
50
51<info>php %command.full_name% "SELECT * FROM users"</info>
52EOT);
53 }
54
55 /**
56 * {@inheritDoc}
57 *
58 * @throws Exception
59 */
60 protected function execute(InputInterface $input, OutputInterface $output): int
61 {
62 $conn = $this->getConnection($input);
63 $io = new SymfonyStyle($input, $output);
64
65 $sql = $input->getArgument('sql');
66
67 if ($sql === null) {
68 throw new RuntimeException('Argument "sql" is required in order to execute this command correctly.');
69 }
70
71 assert(is_string($sql));
72
73 if ($input->getOption('depth') !== null) {
74 $io->warning('Parameter "depth" is deprecated and has no effect anymore.');
75 }
76
77 $forceFetch = $input->getOption('force-fetch');
78 assert(is_bool($forceFetch));
79
80 if (stripos($sql, 'select') === 0 || $forceFetch) {
81 $this->runQuery($io, $conn, $sql);
82 } else {
83 $this->runStatement($io, $conn, $sql);
84 }
85
86 return 0;
87 }
88
89 private function getConnection(InputInterface $input): Connection
90 {
91 $connectionName = $input->getOption('connection');
92 assert(is_string($connectionName) || $connectionName === null);
93
94 if ($connectionName !== null) {
95 return $this->connectionProvider->getConnection($connectionName);
96 }
97
98 return $this->connectionProvider->getDefaultConnection();
99 }
100
101 /** @throws Exception */
102 private function runQuery(SymfonyStyle $io, Connection $conn, string $sql): void
103 {
104 $resultSet = $conn->fetchAllAssociative($sql);
105 if ($resultSet === []) {
106 $io->success('The query yielded an empty result set.');
107
108 return;
109 }
110
111 $io->table(array_keys($resultSet[0]), $resultSet);
112 }
113
114 /** @throws Exception */
115 private function runStatement(SymfonyStyle $io, Connection $conn, string $sql): void
116 {
117 $io->success(sprintf('%d rows affected.', $conn->executeStatement($sql)));
118 }
119}