|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\ORM\Tools\Console\Command; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\Common\Util\Debug; |
|
23
|
|
|
use Symfony\Component\Console\Command\Command; |
|
24
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
26
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
28
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Command to execute DQL queries in a given EntityManager. |
|
32
|
|
|
* |
|
33
|
|
|
* @link www.doctrine-project.org |
|
34
|
|
|
* @since 2.0 |
|
35
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
36
|
|
|
* @author Guilherme Blanco <[email protected]> |
|
37
|
|
|
* @author Jonathan Wage <[email protected]> |
|
38
|
|
|
* @author Roman Borschel <[email protected]> |
|
39
|
|
|
*/ |
|
40
|
|
|
class RunDqlCommand extends Command |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
3 |
|
protected function configure() |
|
46
|
|
|
{ |
|
47
|
3 |
|
$this->setName('orm:run-dql') |
|
48
|
3 |
|
->setDescription('Executes arbitrary DQL directly from the command line') |
|
49
|
3 |
|
->addArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.') |
|
50
|
3 |
|
->addOption('hydrate', null, InputOption::VALUE_REQUIRED, 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.', 'object') |
|
51
|
3 |
|
->addOption('first-result', null, InputOption::VALUE_REQUIRED, 'The first result in the result set.') |
|
52
|
3 |
|
->addOption('max-result', null, InputOption::VALUE_REQUIRED, 'The maximum number of results in the result set.') |
|
53
|
3 |
|
->addOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of Entity graph.', 7) |
|
54
|
3 |
|
->addOption('show-sql', null, InputOption::VALUE_NONE, 'Dump generated SQL instead of executing query') |
|
55
|
3 |
|
->setHelp('Executes arbitrary DQL directly from the command line.'); |
|
56
|
3 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
62
|
|
|
{ |
|
63
|
2 |
|
$ui = new SymfonyStyle($input, $output); |
|
64
|
|
|
|
|
65
|
|
|
/* @var $em \Doctrine\ORM\EntityManagerInterface */ |
|
66
|
2 |
|
$em = $this->getHelper('em')->getEntityManager(); |
|
67
|
|
|
|
|
68
|
2 |
|
if (($dql = $input->getArgument('dql')) === null) { |
|
69
|
|
|
throw new \RuntimeException("Argument 'dql' is required in order to execute this command correctly."); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
$depth = $input->getOption('depth'); |
|
73
|
|
|
|
|
74
|
2 |
|
if ( ! is_numeric($depth)) { |
|
75
|
|
|
throw new \LogicException("Option 'depth' must contain an integer value"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
$hydrationModeName = $input->getOption('hydrate'); |
|
79
|
2 |
|
$hydrationMode = 'Doctrine\ORM\Query::HYDRATE_' . strtoupper(str_replace('-', '_', $hydrationModeName)); |
|
80
|
|
|
|
|
81
|
2 |
|
if ( ! defined($hydrationMode)) { |
|
82
|
|
|
throw new \RuntimeException( |
|
83
|
|
|
"Hydration mode '$hydrationModeName' does not exist. It should be either: object. array, scalar or single-scalar." |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
$query = $em->createQuery($dql); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
2 |
|
if (($firstResult = $input->getOption('first-result')) !== null) { |
|
90
|
|
|
if ( ! is_numeric($firstResult)) { |
|
91
|
|
|
throw new \LogicException("Option 'first-result' must contain an integer value"); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$query->setFirstResult((int) $firstResult); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
if (($maxResult = $input->getOption('max-result')) !== null) { |
|
98
|
|
|
if ( ! is_numeric($maxResult)) { |
|
99
|
|
|
throw new \LogicException("Option 'max-result' must contain an integer value"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$query->setMaxResults((int) $maxResult); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
if ($input->getOption('show-sql')) { |
|
106
|
1 |
|
$ui->text($query->getSQL()); |
|
107
|
1 |
|
return 0; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
$resultSet = $query->execute([], constant($hydrationMode)); |
|
111
|
|
|
|
|
112
|
1 |
|
$ui->text(Debug::dump($resultSet, $input->getOption('depth'), true, false)); |
|
113
|
|
|
|
|
114
|
1 |
|
return 0; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|