# Source for file Abstract.php

Documentation is available at Abstract.php

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. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
27. * @category Object Relational Mapping
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 = array_merge($this->_params, $params);
69. } else {
70. $this->_params[] = $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($values[$k]);
90. } else {
91. $value = '?';
92. }
93. $a[] = $value;
94. }
96. $this->_params = array_merge($this->_params, $params);
98. $where = $expr . ' IN (' . implode(', ', $a) . ')';
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 = array_merge($this->_params, $params);
125. } else {
126. $this->_params[] = $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;
163. return $this;
164. }
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;
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;
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;
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. } else {
218. if ($params !== null) {
219. if (is_array($params)) {
220. $this->_params = array_merge($this->_params, $params);
221. } else {
222. $this->_params[] = $params;
223. }
224. }
225. return $this->parseQueryPart('set', $key . ' = ' . $value, true);
226. }
227. }
228. /**
229. * from
230. * sets the FROM part of the query
231. *
232. * @param string $from Query FROM part
233. * @return Doctrine_Query
234. */
235. public function from($from)
236. {
237. return $this->parseQueryPart('from', $from);
238. }
239. /**
240. * innerJoin
241. * appends an INNER JOIN to the FROM part of the query
242. *
243. * @param string $join Query INNER JOIN
244. * @return Doctrine_Query
245. */
246. public function innerJoin($join)
247. {
248. return $this->parseQueryPart('from', 'INNER JOIN ' . $join, true);
249. }
250. /**
251. * leftJoin
252. * appends a LEFT JOIN to the FROM part of the query
253. *
254. * @param string $join Query LEFT JOIN
255. * @return Doctrine_Query
256. */
257. public function leftJoin($join)
258. {
259. return $this->parseQueryPart('from', 'LEFT JOIN ' . $join, true);
260. }
261. /**
262. * groupBy
263. * sets the GROUP BY part of the query
264. *
265. * @param string $groupby Query GROUP BY part
266. * @return Doctrine_Query
267. */
268. public function groupBy($groupby)
269. {
270. return $this->parseQueryPart('groupby', $groupby);
271. }
272. /**
273. * where
274. * sets the WHERE part of the query
275. *
276. * @param string $join Query WHERE part
277. * @param mixed $params an array of parameters or a simple scalar
278. * @return Doctrine_Query
279. */
280. public function where($where, $params = array())
281. {
282. //$this->_params = array();
283. if (is_array($params)) {
284. $this->_params = $params;
285. } else {
286. $this->_params[] = $params;
287. }
289. return $this->parseQueryPart('where', $where);
290. }
291. /**
292. * having
293. * sets the HAVING part of the query
294. *
295. * @param string $having Query HAVING part
296. * @param mixed $params an array of parameters or a simple scalar
297. * @return Doctrine_Query
298. */
299. public function having($having, $params = array())
300. {
301. $this->_params = array();
302. if (is_array($params)) {
303. $this->_params = $params;
304. } else {
305. $this->_params[] = $params;
306. }
308. return $this->parseQueryPart('having', $having);
309. }
310. /**
311. * orderBy
312. * sets the ORDER BY part of the query
313. *
314. * @param string $orderby Query ORDER BY part
315. * @return Doctrine_Query
316. */
317. public function orderBy($orderby)
318. {
319. return $this->parseQueryPart('orderby', $orderby);
320. }
321. /**
322. * limit
323. * sets the Query query limit
324. *
325. * @param integer $limit limit to be used for limiting the query results
326. * @return Doctrine_Query
327. */
328. public function limit($limit)
329. {
330. return $this->parseQueryPart('limit', $limit);
331. }
332. /**
333. * offset
334. * sets the Query query offset
335. *
336. * @param integer $offset offset to be used for paginating the query
337. * @return Doctrine_Query
338. */
339. public function offset($offset)
340. {
341. return $this->parseQueryPart('offset', $offset);
342. }
344. /**
345. * parseQueryPart
346. * parses given DQL query part
347. *
348. * @param string $queryPartName the name of the query part
349. * @param string $queryPart query part to be parsed
350. * @param boolean $append whether or not to append the query part to its stack
351. * if false is given, this method will overwrite
352. * the given query part stack with $queryPart
353. * @return Doctrine_Query this object
354. */
355. abstract public function parseQueryPart($queryPartName, $queryPart, $append = false);
356. }