| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Orderby.php 7490 2010-03-29 19:53:27Z jwage $
|
|
|
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 |
/**
|
|
|
23 |
* Doctrine_Query_Orderby
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Query
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision: 7490 $
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_Query_Orderby extends Doctrine_Query_Part
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* DQL ORDER BY PARSER
|
|
|
37 |
* parses the order by part of the query string
|
|
|
38 |
*
|
|
|
39 |
* @param string $clause
|
|
|
40 |
* @return void
|
|
|
41 |
*/
|
|
|
42 |
public function parse($clause, $append = false)
|
|
|
43 |
{
|
|
|
44 |
$terms = $this->_tokenizer->clauseExplode($clause, array(' ', ',', '+', '-', '*', '/', '<', '>', '=', '>=', '<='));
|
|
|
45 |
$str = '';
|
|
|
46 |
|
|
|
47 |
foreach ($terms as $term) {
|
|
|
48 |
$pos = strpos($term[0], '(');
|
|
|
49 |
$hasComma = false;
|
|
|
50 |
|
|
|
51 |
if ($pos !== false) {
|
|
|
52 |
$name = substr($term[0], 0, $pos);
|
|
|
53 |
|
|
|
54 |
$term[0] = $this->query->parseFunctionExpression($term[0], array($this, 'parse'));
|
|
|
55 |
} else {
|
|
|
56 |
if (substr($term[0], 0, 1) !== "'" && substr($term[0], -1) !== "'") {
|
|
|
57 |
|
|
|
58 |
if (strpos($term[0], '.') !== false) {
|
|
|
59 |
if ( ! is_numeric($term[0])) {
|
|
|
60 |
$e = explode('.', $term[0]);
|
|
|
61 |
|
|
|
62 |
$field = array_pop($e);
|
|
|
63 |
|
|
|
64 |
// Check if field name still has comma
|
|
|
65 |
if (($pos = strpos($field, ',')) !== false) {
|
|
|
66 |
$field = substr($field, 0, $pos);
|
|
|
67 |
$hasComma = true;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
// Grab query connection
|
|
|
71 |
$conn = $this->query->getConnection();
|
|
|
72 |
|
|
|
73 |
if ($this->query->getType() === Doctrine_Query::SELECT) {
|
|
|
74 |
$componentAlias = implode('.', $e);
|
|
|
75 |
|
|
|
76 |
if (empty($componentAlias)) {
|
|
|
77 |
$componentAlias = $this->query->getRootAlias();
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$this->query->load($componentAlias);
|
|
|
81 |
|
|
|
82 |
// check the existence of the component alias
|
|
|
83 |
$queryComponent = $this->query->getQueryComponent($componentAlias);
|
|
|
84 |
|
|
|
85 |
$table = $queryComponent['table'];
|
|
|
86 |
|
|
|
87 |
$def = $table->getDefinitionOf($field);
|
|
|
88 |
|
|
|
89 |
// get the actual field name from alias
|
|
|
90 |
$field = $table->getColumnName($field);
|
|
|
91 |
|
|
|
92 |
// check column existence
|
|
|
93 |
if ( ! $def) {
|
|
|
94 |
throw new Doctrine_Query_Exception('Unknown column ' . $field);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if (isset($def['owner'])) {
|
|
|
98 |
$componentAlias = $componentAlias . '.' . $def['owner'];
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$tableAlias = $this->query->getSqlTableAlias($componentAlias);
|
|
|
102 |
|
|
|
103 |
// build sql expression
|
|
|
104 |
$term[0] = $conn->quoteIdentifier($tableAlias) . '.' . $conn->quoteIdentifier($field);
|
|
|
105 |
|
|
|
106 |
// driver specific modifications
|
|
|
107 |
$term[0] = method_exists($conn, 'modifyOrderByColumn') ? $conn->modifyOrderByColumn($table, $field, $term[0]) : $term[0];
|
|
|
108 |
} else {
|
|
|
109 |
// build sql expression
|
|
|
110 |
$field = $this->query->getRoot()->getColumnName($field);
|
|
|
111 |
$term[0] = $conn->quoteIdentifier($field);
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
} else {
|
|
|
115 |
if ( ! empty($term[0]) &&
|
|
|
116 |
! is_numeric($term[0]) &&
|
|
|
117 |
$term[0] !== '?' && substr($term[0], 0, 1) !== ':') {
|
|
|
118 |
|
|
|
119 |
$componentAlias = $this->query->getRootAlias();
|
|
|
120 |
|
|
|
121 |
$found = false;
|
|
|
122 |
|
|
|
123 |
// Check if field name still has comma
|
|
|
124 |
if (($pos = strpos($term[0], ',')) !== false) {
|
|
|
125 |
$term[0] = substr($term[0], 0, $pos);
|
|
|
126 |
$hasComma = true;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
if ($componentAlias !== false &&
|
|
|
130 |
$componentAlias !== null) {
|
|
|
131 |
$queryComponent = $this->query->getQueryComponent($componentAlias);
|
|
|
132 |
|
|
|
133 |
$table = $queryComponent['table'];
|
|
|
134 |
|
|
|
135 |
// check column existence
|
|
|
136 |
if ($table->hasField($term[0])) {
|
|
|
137 |
$found = true;
|
|
|
138 |
|
|
|
139 |
$def = $table->getDefinitionOf($term[0]);
|
|
|
140 |
|
|
|
141 |
// get the actual column name from field name
|
|
|
142 |
$field = $table->getColumnName($term[0]);
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
if (isset($def['owner'])) {
|
|
|
146 |
$componentAlias = $componentAlias . '.' . $def['owner'];
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
$tableAlias = $this->query->getSqlTableAlias($componentAlias);
|
|
|
150 |
$conn = $this->query->getConnection();
|
|
|
151 |
|
|
|
152 |
if ($this->query->getType() === Doctrine_Query::SELECT) {
|
|
|
153 |
// build sql expression
|
|
|
154 |
$term[0] = $conn->quoteIdentifier($tableAlias)
|
|
|
155 |
. '.' . $conn->quoteIdentifier($field);
|
|
|
156 |
} else {
|
|
|
157 |
// build sql expression
|
|
|
158 |
$term[0] = $conn->quoteIdentifier($field);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
// driver specific modifications
|
|
|
162 |
$term[0] = method_exists($conn, 'modifyOrderByColumn') ? $conn->modifyOrderByColumn($table, $field, $term[0]) : $term[0];
|
|
|
163 |
} else {
|
|
|
164 |
$found = false;
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
if ( ! $found) {
|
|
|
169 |
$tmp = strtoupper(trim($term[0], ', '));
|
|
|
170 |
|
|
|
171 |
if ($tmp !== 'DESC' && $tmp !== 'ASC') {
|
|
|
172 |
$term[0] = $this->query->getSqlAggregateAlias($term[0]);
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
$str .= $term[0] . ($hasComma ? ',' : '') . $term[1];
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
return $str;
|
|
|
184 |
}
|
|
|
185 |
}
|