| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
* (c) Jonathan H. Wage <jonwage@gmail.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Creates database for current model.
|
|
|
16 |
*
|
|
|
17 |
* @package symfony
|
|
|
18 |
* @subpackage doctrine
|
|
|
19 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
20 |
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
21 |
* @version SVN: $Id: sfDoctrineDqlTask.class.php 24625 2009-12-01 00:05:40Z Kris.Wallsmith $
|
|
|
22 |
*/
|
|
|
23 |
class sfDoctrineDqlTask extends sfDoctrineBaseTask
|
|
|
24 |
{
|
|
|
25 |
/**
|
|
|
26 |
* @see sfTask
|
|
|
27 |
*/
|
|
|
28 |
protected function configure()
|
|
|
29 |
{
|
|
|
30 |
$this->addArguments(array(
|
|
|
31 |
new sfCommandArgument('dql_query', sfCommandArgument::REQUIRED, 'The DQL query to execute', null),
|
|
|
32 |
new sfCommandArgument('parameter', sfCommandArgument::OPTIONAL | sfCommandArgument::IS_ARRAY, 'Query parameter'),
|
|
|
33 |
));
|
|
|
34 |
|
|
|
35 |
$this->addOptions(array(
|
|
|
36 |
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
|
|
|
37 |
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
|
|
|
38 |
new sfCommandOption('show-sql', null, sfCommandOption::PARAMETER_NONE, 'Show the sql that would be executed'),
|
|
|
39 |
new sfCommandOption('table', null, sfCommandOption::PARAMETER_NONE, 'Return results in table format'),
|
|
|
40 |
));
|
|
|
41 |
|
|
|
42 |
$this->namespace = 'doctrine';
|
|
|
43 |
$this->name = 'dql';
|
|
|
44 |
$this->briefDescription = 'Execute a DQL query and view the results';
|
|
|
45 |
|
|
|
46 |
$this->detailedDescription = <<<EOF
|
|
|
47 |
The [doctrine:dql|INFO] task executes a DQL query and displays the formatted
|
|
|
48 |
results:
|
|
|
49 |
|
|
|
50 |
[./symfony doctrine:dql "FROM User"|INFO]
|
|
|
51 |
|
|
|
52 |
You can show the SQL that would be executed by using the [--show-sql|COMMENT] option:
|
|
|
53 |
|
|
|
54 |
[./symfony doctrine:dql --show-sql "FROM User"|INFO]
|
|
|
55 |
|
|
|
56 |
Provide query parameters as additional arguments:
|
|
|
57 |
|
|
|
58 |
[./symfony doctrine:dql "FROM User WHERE email LIKE ?" "%symfony-project.com"|INFO]
|
|
|
59 |
EOF;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* @see sfTask
|
|
|
64 |
*/
|
|
|
65 |
protected function execute($arguments = array(), $options = array())
|
|
|
66 |
{
|
|
|
67 |
$databaseManager = new sfDatabaseManager($this->configuration);
|
|
|
68 |
|
|
|
69 |
$dql = $arguments['dql_query'];
|
|
|
70 |
|
|
|
71 |
$q = Doctrine_Query::create()
|
|
|
72 |
->parseDqlQuery($dql);
|
|
|
73 |
|
|
|
74 |
$this->logSection('doctrine', 'executing dql query');
|
|
|
75 |
$this->log(sprintf('DQL: %s', $dql));
|
|
|
76 |
|
|
|
77 |
if ($options['show-sql'])
|
|
|
78 |
{
|
|
|
79 |
$this->log(sprintf('SQL: %s', $q->getSqlQuery($arguments['parameter'])));
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$count = $q->count($arguments['parameter']);
|
|
|
83 |
|
|
|
84 |
if ($count)
|
|
|
85 |
{
|
|
|
86 |
if (!$options['table'])
|
|
|
87 |
{
|
|
|
88 |
$results = $q->fetchArray($arguments['parameter']);
|
|
|
89 |
|
|
|
90 |
$this->log(array(
|
|
|
91 |
sprintf('found %s results', number_format($count)),
|
|
|
92 |
sfYaml::dump($results, 4),
|
|
|
93 |
));
|
|
|
94 |
}
|
|
|
95 |
else
|
|
|
96 |
{
|
|
|
97 |
$results = $q->execute($arguments['parameter'], Doctrine_Core::HYDRATE_SCALAR);
|
|
|
98 |
|
|
|
99 |
$headers = array();
|
|
|
100 |
|
|
|
101 |
// calculate lengths
|
|
|
102 |
foreach ($results as $result)
|
|
|
103 |
{
|
|
|
104 |
foreach ($result as $field => $value)
|
|
|
105 |
{
|
|
|
106 |
if (!isset($headers[$field]))
|
|
|
107 |
{
|
|
|
108 |
$headers[$field] = 0;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$headers[$field] = max($headers[$field], strlen($this->renderValue($value)));
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// print header
|
|
|
116 |
$hdr = '|';
|
|
|
117 |
$div = '+';
|
|
|
118 |
|
|
|
119 |
foreach ($headers as $field => & $length)
|
|
|
120 |
{
|
|
|
121 |
if ($length < strlen($field))
|
|
|
122 |
{
|
|
|
123 |
$length = strlen($field);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
$hdr .= ' '.str_pad($field, $length).' |';
|
|
|
127 |
$div .= str_repeat('-', $length + 2).'+';
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$this->log(array($div, $hdr, $div));
|
|
|
131 |
|
|
|
132 |
// print results
|
|
|
133 |
foreach ($results as $result)
|
|
|
134 |
{
|
|
|
135 |
$line = '|';
|
|
|
136 |
foreach ($result as $field => $value)
|
|
|
137 |
{
|
|
|
138 |
$line .= ' '.str_pad($this->renderValue($value), $headers[$field]).' |';
|
|
|
139 |
}
|
|
|
140 |
$this->log($line);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
$this->log($div);
|
|
|
144 |
|
|
|
145 |
// find profiler
|
|
|
146 |
if ($profiler = $q->getConnection()->getListener()->get('symfony_profiler'))
|
|
|
147 |
{
|
|
|
148 |
$events = $profiler->getQueryExecutionEvents();
|
|
|
149 |
$event = array_pop($events);
|
|
|
150 |
$this->log(sprintf('%s results (%s sec)', number_format($count), number_format($event->getElapsedSecs(), 2)));
|
|
|
151 |
}
|
|
|
152 |
else
|
|
|
153 |
{
|
|
|
154 |
$this->log(sprintf('%s results', number_format($count)));
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
$this->log('');
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
else
|
|
|
161 |
{
|
|
|
162 |
$this->logSection('doctrine', 'no results found');
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Renders the supplied value.
|
|
|
168 |
*
|
|
|
169 |
* @param string|null $value
|
|
|
170 |
*
|
|
|
171 |
* @return string
|
|
|
172 |
*/
|
|
|
173 |
protected function renderValue($value)
|
|
|
174 |
{
|
|
|
175 |
return null === $value ? 'NULL' : $value;
|
|
|
176 |
}
|
|
|
177 |
}
|