Development

/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineDqlTask.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineDqlTask.class.php

Revision 24625, 4.8 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] updated doctrine:dql task to render NULL for null values when in table mode (closes #7680)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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$
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->aliases = array('doctrine-dql');
43     $this->namespace = 'doctrine';
44     $this->name = 'dql';
45     $this->briefDescription = 'Execute a DQL query and view the results';
46
47     $this->detailedDescription = <<<EOF
48 The [doctrine:dql|INFO] task executes a DQL query and displays the formatted
49 results:
50
51   [./symfony doctrine:dql "FROM User"|INFO]
52
53 You can show the SQL that would be executed by using the [--show-sql|COMMENT] option:
54
55   [./symfony doctrine:dql --show-sql "FROM User"|INFO]
56
57 Provide query parameters as additional arguments:
58
59   [./symfony doctrine:dql "FROM User WHERE email LIKE ?" "%symfony-project.com"|INFO]
60 EOF;
61   }
62
63   /**
64    * @see sfTask
65    */
66   protected function execute($arguments = array(), $options = array())
67   {
68     $databaseManager = new sfDatabaseManager($this->configuration);
69
70     $dql = $arguments['dql_query'];
71
72     $q = Doctrine_Query::create()
73       ->parseDqlQuery($dql);
74
75     $this->logSection('doctrine', 'executing dql query');
76     $this->log(sprintf('DQL: %s', $dql));
77
78     if ($options['show-sql'])
79     {
80       $this->log(sprintf('SQL: %s', $q->getSqlQuery($arguments['parameter'])));
81     }
82
83     $count = $q->count($arguments['parameter']);
84
85     if ($count)
86     {
87       if (!$options['table'])
88       {
89         $results = $q->fetchArray($arguments['parameter']);
90
91         $this->log(array(
92           sprintf('found %s results', number_format($count)),
93           sfYaml::dump($results, 4),
94         ));
95       }
96       else
97       {
98         $results = $q->execute($arguments['parameter'], Doctrine_Core::HYDRATE_SCALAR);
99
100         $headers = array();
101
102         // calculate lengths
103         foreach ($results as $result)
104         {
105           foreach ($result as $field => $value)
106           {
107             if (!isset($headers[$field]))
108             {
109               $headers[$field] = 0;
110             }
111
112             $headers[$field] = max($headers[$field], strlen($this->renderValue($value)));
113           }
114         }
115
116         // print header
117         $hdr = '|';
118         $div = '+';
119
120         foreach ($headers as $field => & $length)
121         {
122           if ($length < strlen($field))
123           {
124             $length = strlen($field);
125           }
126
127           $hdr .= ' '.str_pad($field, $length).' |';
128           $div .= str_repeat('-', $length + 2).'+';
129         }
130
131         $this->log(array($div, $hdr, $div));
132
133         // print results
134         foreach ($results as $result)
135         {
136           $line = '|';
137           foreach ($result as $field => $value)
138           {
139             $line .= ' '.str_pad($this->renderValue($value), $headers[$field]).' |';
140           }
141           $this->log($line);
142         }
143
144         $this->log($div);
145
146         // find profiler
147         if ($profiler = $q->getConnection()->getListener()->get('symfony_profiler'))
148         {
149           $events = $profiler->getQueryExecutionEvents();
150           $event = array_pop($events);
151           $this->log(sprintf('%s results (%s sec)', number_format($count), number_format($event->getElapsedSecs(), 2)));
152         }
153         else
154         {
155           $this->log(sprintf('%s results', number_format($count)));
156         }
157
158         $this->log('');
159       }
160     }
161     else
162     {
163       $this->logSection('doctrine', 'no results found');
164     }
165   }
166
167   /**
168    * Renders the supplied value.
169    *
170    * @param string|null $value
171    *
172    * @return string
173    */
174   protected function renderValue($value)
175   {
176     return null === $value ? 'NULL' : $value;
177   }
178 }
179
Note: See TracBrowser for help on using the browser.