diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Tools/Console/Command/RunSqlCommand.php | 119 |
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 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Tools\Console\Command; | ||
6 | |||
7 | use Doctrine\DBAL\Connection; | ||
8 | use Doctrine\DBAL\Exception; | ||
9 | use Doctrine\DBAL\Tools\Console\ConnectionProvider; | ||
10 | use RuntimeException; | ||
11 | use Symfony\Component\Console\Command\Command; | ||
12 | use Symfony\Component\Console\Input\InputArgument; | ||
13 | use Symfony\Component\Console\Input\InputInterface; | ||
14 | use Symfony\Component\Console\Input\InputOption; | ||
15 | use Symfony\Component\Console\Output\OutputInterface; | ||
16 | use Symfony\Component\Console\Style\SymfonyStyle; | ||
17 | |||
18 | use function array_keys; | ||
19 | use function assert; | ||
20 | use function is_bool; | ||
21 | use function is_string; | ||
22 | use function sprintf; | ||
23 | use function stripos; | ||
24 | |||
25 | /** | ||
26 | * Task for executing arbitrary SQL that can come from a file or directly from | ||
27 | * the command line. | ||
28 | */ | ||
29 | class 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' | ||
48 | The <info>%command.name%</info> command executes the given SQL query and | ||
49 | outputs the results: | ||
50 | |||
51 | <info>php %command.full_name% "SELECT * FROM users"</info> | ||
52 | EOT); | ||
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 | } | ||