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