1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use LogicException; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
16
|
|
|
use Symfony\Component\VarDumper\Cloner\VarCloner; |
17
|
|
|
use Symfony\Component\VarDumper\Dumper\CliDumper; |
18
|
|
|
use function constant; |
19
|
|
|
use function defined; |
20
|
|
|
use function is_numeric; |
21
|
|
|
use function sprintf; |
22
|
|
|
use function str_replace; |
23
|
|
|
use function strtoupper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Command to execute DQL queries in a given EntityManager. |
27
|
|
|
*/ |
28
|
|
|
class RunDqlCommand extends Command |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
3 |
|
protected function configure() |
34
|
|
|
{ |
35
|
3 |
|
$this->setName('orm:run-dql') |
36
|
3 |
|
->setDescription('Executes arbitrary DQL directly from the command line') |
37
|
3 |
|
->addArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.') |
38
|
3 |
|
->addOption('hydrate', null, InputOption::VALUE_REQUIRED, 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.', 'object') |
39
|
3 |
|
->addOption('first-result', null, InputOption::VALUE_REQUIRED, 'The first result in the result set.') |
40
|
3 |
|
->addOption('max-result', null, InputOption::VALUE_REQUIRED, 'The maximum number of results in the result set.') |
41
|
3 |
|
->addOption('show-sql', null, InputOption::VALUE_NONE, 'Dump generated SQL instead of executing query') |
42
|
3 |
|
->setHelp('Executes arbitrary DQL directly from the command line.'); |
43
|
3 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
2 |
|
$ui = new SymfonyStyle($input, $output); |
51
|
|
|
|
52
|
|
|
/** @var EntityManagerInterface $em */ |
53
|
2 |
|
$em = $this->getHelper('em')->getEntityManager(); |
54
|
2 |
|
$dql = $input->getArgument('dql'); |
55
|
|
|
|
56
|
2 |
|
if ($dql === null) { |
57
|
|
|
throw new RuntimeException("Argument 'dql' is required in order to execute this command correctly."); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$hydrationModeName = $input->getOption('hydrate'); |
61
|
2 |
|
$hydrationMode = 'Doctrine\ORM\Query::HYDRATE_' . strtoupper(str_replace('-', '_', $hydrationModeName)); |
62
|
|
|
|
63
|
2 |
|
if (! defined($hydrationMode)) { |
64
|
|
|
throw new RuntimeException(sprintf( |
65
|
|
|
"Hydration mode '%s' does not exist. It should be either: object. array, scalar or single-scalar.", |
66
|
|
|
$hydrationModeName |
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
$query = $em->createQuery($dql); |
71
|
2 |
|
$firstResult = $input->getOption('first-result'); |
72
|
|
|
|
73
|
2 |
|
if ($firstResult !== null) { |
74
|
|
|
if (! is_numeric($firstResult)) { |
75
|
|
|
throw new LogicException("Option 'first-result' must contain an integer value"); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$query->setFirstResult((int) $firstResult); |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
$maxResult = $input->getOption('max-result'); |
82
|
|
|
|
83
|
2 |
|
if ($maxResult !== null) { |
84
|
|
|
if (! is_numeric($maxResult)) { |
85
|
|
|
throw new LogicException("Option 'max-result' must contain an integer value"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$query->setMaxResults((int) $maxResult); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
if ($input->getOption('show-sql')) { |
92
|
1 |
|
$ui->text($query->getSQL()); |
93
|
1 |
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$resultSet = $query->execute([], constant($hydrationMode)); |
97
|
|
|
|
98
|
|
|
$dumper = new CliDumper(static function (string $payload) use ($output) : void { |
99
|
1 |
|
$output->write($payload); |
100
|
1 |
|
}); |
101
|
1 |
|
$dumper->dump((new VarCloner())->cloneVar($resultSet)); |
102
|
1 |
|
} |
103
|
|
|
} |
104
|
|
|
|