PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/rokcommon/Doctrine/Query/Set.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 106 lines | 53 code | 21 blank | 32 comment | 7 complexity | df0847b8adaf5f3cce8cb74b75905e3e MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Set.php 48519 2012-02-03 23:18:52Z btowles $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. /**
  22. * Doctrine_Query
  23. *
  24. * @package Doctrine
  25. * @subpackage Query
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.doctrine-project.org
  28. * @since 1.0
  29. * @version $Revision: 7490 $
  30. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  31. */
  32. class Doctrine_Query_Set extends Doctrine_Query_Part
  33. {
  34. public function parse($dql)
  35. {
  36. $terms = $this->_tokenizer->sqlExplode($dql, ' ');
  37. $termsTranslation = array();
  38. foreach ($terms as $term) {
  39. $termOriginal = $term;
  40. // We need to check for agg functions here
  41. $matches = array();
  42. $hasAggExpression = $this->_processPossibleAggExpression($term, $matches);
  43. $lftExpr = (($hasAggExpression) ? $matches[1] . '(' : '');
  44. $rgtExpr = (($hasAggExpression) ? $matches[3] . ')' : '');
  45. preg_match_all("/^([a-zA-Z0-9_]+[\.[a-zA-Z0-9_]+]*)(\sAS\s[a-zA-Z0-9_]+)?/i", $term, $m, PREG_SET_ORDER);
  46. if (isset($m[0])) {
  47. $processed = array();
  48. foreach ($m as $piece) {
  49. $part = $piece[1];
  50. $e = explode('.', trim($part));
  51. $fieldName = array_pop($e);
  52. $reference = (count($e) > 0) ? implode('.', $e) : $this->query->getRootAlias();
  53. $aliasMap = $this->query->getQueryComponent($reference);
  54. if ($aliasMap['table']->hasField($fieldName)) {
  55. $columnName = $aliasMap['table']->getColumnName($fieldName);
  56. $columnName = $aliasMap['table']->getConnection()->quoteIdentifier($columnName);
  57. $part = $columnName;
  58. }
  59. $processed[] = $part . (isset($piece[2]) ? $piece[2] : '');
  60. }
  61. $termsTranslation[$termOriginal] = $lftExpr . implode(' ', $processed) . $rgtExpr;
  62. }
  63. }
  64. return strtr($dql, $termsTranslation);
  65. }
  66. protected function _processPossibleAggExpression(& $expr, & $matches = array())
  67. {
  68. $hasAggExpr = preg_match('/(.*[^\s\(\=])\(([^\)]*)\)(.*)/', $expr, $matches);
  69. if ($hasAggExpr) {
  70. $expr = $matches[2];
  71. // We need to process possible comma separated items
  72. if (substr(trim($matches[3]), 0, 1) == ',') {
  73. $xplod = $this->_tokenizer->sqlExplode(trim($matches[3], ' )'), ',');
  74. $matches[3] = array();
  75. foreach ($xplod as $part) {
  76. if ($part != '') {
  77. $matches[3][] = $this->parseLiteralValue($part);
  78. }
  79. }
  80. $matches[3] = '), ' . implode(', ', $matches[3]);
  81. }
  82. }
  83. return $hasAggExpr;
  84. }
  85. }