1 |
<?php
|
2 |
/*
|
3 |
* $Id: Query.php 1393 2007-05-19 17:49:16Z zYne $
|
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.phpdoctrine.com>.
|
20 |
*/
|
21 |
Doctrine::autoload('Doctrine_Hydrate');
|
22 |
/**
|
23 |
* Doctrine_Query_Abstract
|
24 |
*
|
25 |
* @package Doctrine
|
26 |
* @subpackage Query
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
28 |
* @link www.phpdoctrine.com
|
29 |
* @since 1.0
|
30 |
* @version $Revision: 1393 $
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
32 |
*/
|
33 |
abstract class Doctrine_Query_Abstract extends Doctrine_Hydrate
|
34 |
{
|
35 |
/**
|
36 |
* addSelect
|
37 |
* adds fields to the SELECT part of the query
|
38 |
*
|
39 |
* @param string $select Query SELECT part
|
40 |
* @return Doctrine_Query
|
41 |
*/
|
42 |
public function addSelect($select)
|
43 |
{
|
44 |
return $this->parseQueryPart('select', $select, true);
|
45 |
}
|
46 |
/**
|
47 |
* addFrom
|
48 |
* adds fields to the FROM part of the query
|
49 |
*
|
50 |
* @param string $from Query FROM part
|
51 |
* @return Doctrine_Query
|
52 |
*/
|
53 |
public function addFrom($from)
|
54 |
{
|
55 |
return $this->parseQueryPart('from', $from, true);
|
56 |
}
|
57 |
/**
|
58 |
* addWhere
|
59 |
* adds conditions to the WHERE part of the query
|
60 |
*
|
61 |
* @param string $where Query WHERE part
|
62 |
* @param mixed $params an array of parameters or a simple scalar
|
63 |
* @return Doctrine_Query
|
64 |
*/
|
65 |
public function addWhere($where, $params = array())
|
66 |
{
|
67 |
if (is_array($params)) {
|
68 |
$this->_params['where'] = array_merge($this->_params['where'], $params);
|
69 |
} else {
|
70 |
$this->_params['where'][] = $params;
|
71 |
}
|
72 |
return $this->parseQueryPart('where', $where, true);
|
73 |
}
|
74 |
/**
|
75 |
* whereIn
|
76 |
* adds IN condition to the query WHERE part
|
77 |
*
|
78 |
* @param string $expr
|
79 |
* @param mixed $params an array of parameters or a simple scalar
|
80 |
* @return Doctrine_Query
|
81 |
*/
|
82 |
public function whereIn($expr, $params = array())
|
83 |
{
|
84 |
$params = (array) $params;
|
85 |
$a = array();
|
86 |
foreach ($params as $k => $value) {
|
87 |
if ($value instanceof Doctrine_Expression) {
|
88 |
$value = $value->getSql();
|
89 |
unset($params[$k]);
|
90 |
} else {
|
91 |
$value = '?';
|
92 |
}
|
93 |
$a[] = $value;
|
94 |
}
|
95 |
|
96 |
$this->_params['where'] = array_merge($this->_params['where'], $params);
|
97 |
|
98 |
$where = $expr . ' IN (' . implode(', ', $a) . ')';
|
99 |
|
100 |
return $this->parseQueryPart('where', $where, true);
|
101 |
}
|
102 |
/**
|
103 |
* addGroupBy
|
104 |
* adds fields to the GROUP BY part of the query
|
105 |
*
|
106 |
* @param string $groupby Query GROUP BY part
|
107 |
* @return Doctrine_Query
|
108 |
*/
|
109 |
public function addGroupBy($groupby)
|
110 |
{
|
111 |
return $this->parseQueryPart('groupby', $groupby, true);
|
112 |
}
|
113 |
/**
|
114 |
* addHaving
|
115 |
* adds conditions to the HAVING part of the query
|
116 |
*
|
117 |
* @param string $having Query HAVING part
|
118 |
* @param mixed $params an array of parameters or a simple scalar
|
119 |
* @return Doctrine_Query
|
120 |
*/
|
121 |
public function addHaving($having, $params = array())
|
122 |
{
|
123 |
if (is_array($params)) {
|
124 |
$this->_params['having'] = array_merge($this->_params['having'], $params);
|
125 |
} else {
|
126 |
$this->_params['having'][] = $params;
|
127 |
}
|
128 |
return $this->parseQueryPart('having', $having, true);
|
129 |
}
|
130 |
/**
|
131 |
* addOrderBy
|
132 |
* adds fields to the ORDER BY part of the query
|
133 |
*
|
134 |
* @param string $orderby Query ORDER BY part
|
135 |
* @return Doctrine_Query
|
136 |
*/
|
137 |
public function addOrderBy($orderby)
|
138 |
{
|
139 |
return $this->parseQueryPart('orderby', $orderby, true);
|
140 |
}
|
141 |
/**
|
142 |
* select
|
143 |
* sets the SELECT part of the query
|
144 |
*
|
145 |
* @param string $select Query SELECT part
|
146 |
* @return Doctrine_Query
|
147 |
*/
|
148 |
public function select($select)
|
149 |
{
|
150 |
return $this->parseQueryPart('select', $select);
|
151 |
}
|
152 |
/**
|
153 |
* distinct
|
154 |
* Makes the query SELECT DISTINCT.
|
155 |
*
|
156 |
* @param bool $flag Whether or not the SELECT is DISTINCT (default true).
|
157 |
* @return Doctrine_Query
|
158 |
*/
|
159 |
public function distinct($flag = true)
|
160 |
{
|
161 |
$this->parts['distinct'] = (bool) $flag;
|
162 |
|
163 |
return $this;
|
164 |
}
|
165 |
|
166 |
/**
|
167 |
* forUpdate
|
168 |
* Makes the query SELECT FOR UPDATE.
|
169 |
*
|
170 |
* @param bool $flag Whether or not the SELECT is FOR UPDATE (default true).
|
171 |
* @return Doctrine_Query
|
172 |
*/
|
173 |
public function forUpdate($flag = true)
|
174 |
{
|
175 |
$this->parts[self::FOR_UPDATE] = (bool) $flag;
|
176 |
|
177 |
return $this;
|
178 |
}
|
179 |
/**
|
180 |
* delete
|
181 |
* sets the query type to DELETE
|
182 |
*
|
183 |
* @return Doctrine_Query
|
184 |
*/
|
185 |
public function delete()
|
186 |
{
|
187 |
$this->type = self::DELETE;
|
188 |
|
189 |
return $this;
|
190 |
}
|
191 |
/**
|
192 |
* update
|
193 |
* sets the UPDATE part of the query
|
194 |
*
|
195 |
* @param string $update Query UPDATE part
|
196 |
* @return Doctrine_Query
|
197 |
*/
|
198 |
public function update($update)
|
199 |
{
|
200 |
$this->type = self::UPDATE;
|
201 |
|
202 |
return $this->parseQueryPart('from', $update);
|
203 |
}
|
204 |
/**
|
205 |
* set
|
206 |
* sets the SET part of the query
|
207 |
*
|
208 |
* @param string $update Query UPDATE part
|
209 |
* @return Doctrine_Query
|
210 |
*/
|
211 |
public function set($key, $value, $params = null)
|
212 |
{
|
213 |
if (is_array($key)) {
|
214 |
foreach ($key as $k => $v) {
|
215 |
$this->set($k, '?', array($v));
|
216 |
}
|
217 |
return $this;
|
218 |
} else {
|
219 |
if ($params !== null) {
|
220 |
if (is_array($params)) {
|
221 |
$this->_params['set'] = array_merge($this->_params['set'], $params);
|
222 |
} else {
|
223 |
$this->_params['set'][] = $params;
|
224 |
}
|
225 |
}
|
226 |
return $this->parseQueryPart('set', $key . ' = ' . $value, true);
|
227 |
}
|
228 |
}
|
229 |
/**
|
230 |
* from
|
231 |
* sets the FROM part of the query
|
232 |
*
|
233 |
* @param string $from Query FROM part
|
234 |
* @return Doctrine_Query
|
235 |
*/
|
236 |
public function from($from)
|
237 |
{
|
238 |
return $this->parseQueryPart('from', $from);
|
239 |
}
|
240 |
/**
|
241 |
* innerJoin
|
242 |
* appends an INNER JOIN to the FROM part of the query
|
243 |
*
|
244 |
* @param string $join Query INNER JOIN
|
245 |
* @return Doctrine_Query
|
246 |
*/
|
247 |
public function innerJoin($join)
|
248 |
{
|
249 |
return $this->parseQueryPart('from', 'INNER JOIN ' . $join, true);
|
250 |
}
|
251 |
/**
|
252 |
* leftJoin
|
253 |
* appends a LEFT JOIN to the FROM part of the query
|
254 |
*
|
255 |
* @param string $join Query LEFT JOIN
|
256 |
* @return Doctrine_Query
|
257 |
*/
|
258 |
public function leftJoin($join)
|
259 |
{
|
260 |
return $this->parseQueryPart('from', 'LEFT JOIN ' . $join, true);
|
261 |
}
|
262 |
/**
|
263 |
* groupBy
|
264 |
* sets the GROUP BY part of the query
|
265 |
*
|
266 |
* @param string $groupby Query GROUP BY part
|
267 |
* @return Doctrine_Query
|
268 |
*/
|
269 |
public function groupBy($groupby)
|
270 |
{
|
271 |
return $this->parseQueryPart('groupby', $groupby);
|
272 |
}
|
273 |
/**
|
274 |
* where
|
275 |
* sets the WHERE part of the query
|
276 |
*
|
277 |
* @param string $join Query WHERE part
|
278 |
* @param mixed $params an array of parameters or a simple scalar
|
279 |
* @return Doctrine_Query
|
280 |
*/
|
281 |
public function where($where, $params = array())
|
282 |
{
|
283 |
$this->_params['where'] = array();
|
284 |
if (is_array($params)) {
|
285 |
$this->_params['where'] = $params;
|
286 |
} else {
|
287 |
$this->_params['where'][] = $params;
|
288 |
}
|
289 |
|
290 |
return $this->parseQueryPart('where', $where);
|
291 |
}
|
292 |
/**
|
293 |
* having
|
294 |
* sets the HAVING part of the query
|
295 |
*
|
296 |
* @param string $having Query HAVING part
|
297 |
* @param mixed $params an array of parameters or a simple scalar
|
298 |
* @return Doctrine_Query
|
299 |
*/
|
300 |
public function having($having, $params = array())
|
301 |
{
|
302 |
$this->_params['having'] = array();
|
303 |
if (is_array($params)) {
|
304 |
$this->_params['having'] = $params;
|
305 |
} else {
|
306 |
$this->_params['having'][] = $params;
|
307 |
}
|
308 |
|
309 |
return $this->parseQueryPart('having', $having);
|
310 |
}
|
311 |
/**
|
312 |
* orderBy
|
313 |
* sets the ORDER BY part of the query
|
314 |
*
|
315 |
* @param string $orderby Query ORDER BY part
|
316 |
* @return Doctrine_Query
|
317 |
*/
|
318 |
public function orderBy($orderby)
|
319 |
{
|
320 |
return $this->parseQueryPart('orderby', $orderby);
|
321 |
}
|
322 |
/**
|
323 |
* limit
|
324 |
* sets the Query query limit
|
325 |
*
|
326 |
* @param integer $limit limit to be used for limiting the query results
|
327 |
* @return Doctrine_Query
|
328 |
*/
|
329 |
public function limit($limit)
|
330 |
{
|
331 |
return $this->parseQueryPart('limit', $limit);
|
332 |
}
|
333 |
/**
|
334 |
* offset
|
335 |
* sets the Query query offset
|
336 |
*
|
337 |
* @param integer $offset offset to be used for paginating the query
|
338 |
* @return Doctrine_Query
|
339 |
*/
|
340 |
public function offset($offset)
|
341 |
{
|
342 |
return $this->parseQueryPart('offset', $offset);
|
343 |
}
|
344 |
|
345 |
/**
|
346 |
* parseQueryPart
|
347 |
* parses given DQL query part
|
348 |
*
|
349 |
* @param string $queryPartName the name of the query part
|
350 |
* @param string $queryPart query part to be parsed
|
351 |
* @param boolean $append whether or not to append the query part to its stack
|
352 |
* if false is given, this method will overwrite
|
353 |
* the given query part stack with $queryPart
|
354 |
* @return Doctrine_Query this object
|
355 |
*/
|
356 |
abstract public function parseQueryPart($queryPartName, $queryPart, $append = false);
|
357 |
} |