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