PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/rokcommon/Doctrine/Query/Orderby.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 179 lines | 95 code | 35 blank | 49 comment | 24 complexity | 8e97b2cf75312c2beeb645e9cb563149 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Orderby.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_Orderby
  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_Orderby extends Doctrine_Query_Part
  33. {
  34. /**
  35. * DQL ORDER BY PARSER
  36. * parses the order by part of the query string
  37. *
  38. * @param string $clause
  39. * @return void
  40. */
  41. public function parse($clause, $append = false)
  42. {
  43. $terms = $this->_tokenizer->clauseExplode($clause, array(' ', ',', '+', '-', '*', '/', '<', '>', '=', '>=', '<='));
  44. $str = '';
  45. foreach ($terms as $term) {
  46. $pos = strpos($term[0], '(');
  47. $hasComma = false;
  48. if ($pos !== false) {
  49. $name = substr($term[0], 0, $pos);
  50. $term[0] = $this->query->parseFunctionExpression($term[0]);
  51. } else {
  52. if (substr($term[0], 0, 1) !== "'" && substr($term[0], -1) !== "'") {
  53. if (strpos($term[0], '.') !== false) {
  54. if ( ! is_numeric($term[0])) {
  55. $e = explode('.', $term[0]);
  56. $field = array_pop($e);
  57. // Check if field name still has comma
  58. if (($pos = strpos($field, ',')) !== false) {
  59. $field = substr($field, 0, $pos);
  60. $hasComma = true;
  61. }
  62. // Grab query connection
  63. $conn = $this->query->getConnection();
  64. if ($this->query->getType() === Doctrine_Query::SELECT) {
  65. $componentAlias = implode('.', $e);
  66. if (empty($componentAlias)) {
  67. $componentAlias = $this->query->getRootAlias();
  68. }
  69. $this->query->load($componentAlias);
  70. // check the existence of the component alias
  71. $queryComponent = $this->query->getQueryComponent($componentAlias);
  72. $table = $queryComponent['table'];
  73. $def = $table->getDefinitionOf($field);
  74. // get the actual field name from alias
  75. $field = $table->getColumnName($field);
  76. // check column existence
  77. if ( ! $def) {
  78. throw new Doctrine_Query_Exception('Unknown column ' . $field);
  79. }
  80. if (isset($def['owner'])) {
  81. $componentAlias = $componentAlias . '.' . $def['owner'];
  82. }
  83. $tableAlias = $this->query->getSqlTableAlias($componentAlias);
  84. // build sql expression
  85. $term[0] = $conn->quoteIdentifier($tableAlias) . '.' . $conn->quoteIdentifier($field);
  86. } else {
  87. // build sql expression
  88. $field = $this->query->getRoot()->getColumnName($field);
  89. $term[0] = $conn->quoteIdentifier($field);
  90. }
  91. }
  92. } else {
  93. if ( ! empty($term[0]) &&
  94. ! is_numeric($term[0]) &&
  95. $term[0] !== '?' && substr($term[0], 0, 1) !== ':') {
  96. $componentAlias = $this->query->getRootAlias();
  97. $found = false;
  98. // Check if field name still has comma
  99. if (($pos = strpos($term[0], ',')) !== false) {
  100. $term[0] = substr($term[0], 0, $pos);
  101. $hasComma = true;
  102. }
  103. if ($componentAlias !== false &&
  104. $componentAlias !== null) {
  105. $queryComponent = $this->query->getQueryComponent($componentAlias);
  106. $table = $queryComponent['table'];
  107. // check column existence
  108. if ($table->hasField($term[0])) {
  109. $found = true;
  110. $def = $table->getDefinitionOf($term[0]);
  111. // get the actual column name from field name
  112. $term[0] = $table->getColumnName($term[0]);
  113. if (isset($def['owner'])) {
  114. $componentAlias = $componentAlias . '.' . $def['owner'];
  115. }
  116. $tableAlias = $this->query->getSqlTableAlias($componentAlias);
  117. $conn = $this->query->getConnection();
  118. if ($this->query->getType() === Doctrine_Query::SELECT) {
  119. // build sql expression
  120. $term[0] = $conn->quoteIdentifier($tableAlias)
  121. . '.' . $conn->quoteIdentifier($term[0]);
  122. } else {
  123. // build sql expression
  124. $term[0] = $conn->quoteIdentifier($term[0]);
  125. }
  126. } else {
  127. $found = false;
  128. }
  129. }
  130. if ( ! $found) {
  131. $tmp = strtoupper(trim($term[0], ', '));
  132. if ($tmp !== 'DESC' && $tmp !== 'ASC') {
  133. $term[0] = $this->query->getSqlAggregateAlias($term[0]);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. $str .= $term[0] . ($hasComma ? ',' : '') . $term[1];
  141. }
  142. return $str;
  143. }
  144. }