1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Util\Debug; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use LogicException; |
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
|
|
|
use function constant; |
18
|
|
|
use function defined; |
19
|
|
|
use function is_numeric; |
20
|
|
|
use function sprintf; |
21
|
|
|
use function str_replace; |
22
|
|
|
use function strtoupper; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Command to execute DQL queries in a given EntityManager. |
26
|
|
|
*/ |
27
|
|
|
class RunDqlCommand extends Command |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
3 |
|
protected function configure() |
33
|
|
|
{ |
34
|
3 |
|
$this->setName('orm:run-dql') |
35
|
3 |
|
->setDescription('Executes arbitrary DQL directly from the command line') |
36
|
3 |
|
->addArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.') |
37
|
3 |
|
->addOption('hydrate', null, InputOption::VALUE_REQUIRED, 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.', 'object') |
38
|
3 |
|
->addOption('first-result', null, InputOption::VALUE_REQUIRED, 'The first result in the result set.') |
39
|
3 |
|
->addOption('max-result', null, InputOption::VALUE_REQUIRED, 'The maximum number of results in the result set.') |
40
|
3 |
|
->addOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of Entity graph.', 7) |
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 |
|
$depth = $input->getOption('depth'); |
61
|
|
|
|
62
|
2 |
|
if (! is_numeric($depth)) { |
63
|
|
|
throw new LogicException("Option 'depth' must contain an integer value"); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$hydrationModeName = $input->getOption('hydrate'); |
67
|
2 |
|
$hydrationMode = 'Doctrine\ORM\Query::HYDRATE_' . strtoupper(str_replace('-', '_', $hydrationModeName)); |
68
|
|
|
|
69
|
2 |
|
if (! defined($hydrationMode)) { |
70
|
|
|
throw new RuntimeException(sprintf( |
71
|
|
|
"Hydration mode '%s' does not exist. It should be either: object. array, scalar or single-scalar.", |
72
|
|
|
$hydrationModeName |
73
|
|
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
$query = $em->createQuery($dql); |
77
|
2 |
|
$firstResult = $input->getOption('first-result'); |
78
|
|
|
|
79
|
2 |
|
if ($firstResult !== null) { |
80
|
|
|
if (! is_numeric($firstResult)) { |
81
|
|
|
throw new LogicException("Option 'first-result' must contain an integer value"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$query->setFirstResult((int) $firstResult); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
$maxResult = $input->getOption('max-result'); |
88
|
|
|
|
89
|
2 |
|
if ($maxResult !== null) { |
90
|
|
|
if (! is_numeric($maxResult)) { |
91
|
|
|
throw new LogicException("Option 'max-result' must contain an integer value"); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$query->setMaxResults((int) $maxResult); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
if ($input->getOption('show-sql')) { |
98
|
1 |
|
$ui->text($query->getSQL()); |
99
|
1 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$resultSet = $query->execute([], constant($hydrationMode)); |
103
|
|
|
|
104
|
1 |
|
$ui->text(Debug::dump($resultSet, $input->getOption('depth'), true, false)); |
105
|
1 |
|
} |
106
|
|
|
} |
107
|
|
|
|