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

https://github.com/bheneka/gitta · PHP · 177 lines · 112 code · 31 blank · 34 comment · 6 complexity · d230e7578e6fe1dd2db8a736cf1bdb1c MD5 · raw file

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