Non-fatal SQL errors when running unit tests.
[platal.git] / classes / plfilter.php
CommitLineData
285fb262
RB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
857a2b96
FB
22__autoload('xdb');
23
0d78a96b 24// {{{ class PlLimit
bd9b36fe
RB
25class PlLimit
26{
27 private $count = null;
28 private $from = null;
29
30 public function __construct($count = null, $from = null)
31 {
32 $this->count = $count;
33 $this->from = $from;
34 }
35
36 public function getSql()
37 {
4aa4899b 38 if (!is_null($this->count) && $this->count != 0) {
bd9b36fe
RB
39 if (!is_null($this->from) && $this->from != 0) {
40 return XDB::format('LIMIT {?}, {?}', (int)$this->from, (int)$this->count);
41 } else {
42 return XDB::format('LIMIT {?}', (int)$this->count);
43 }
44 }
45 return '';
46 }
47}
0d78a96b 48// }}}
bd9b36fe 49
0d78a96b 50// {{{ class PlSqlJoin
285fb262
RB
51class PlSqlJoin
52{
53 private $mode;
54 private $table;
55 private $condition;
56
57 const MODE_LEFT = 'LEFT';
58 const MODE_RIGHT = 'RIGHT';
59 const MODE_INNER = 'INNER';
60
ae3effcc 61 private function __construct($mode, $params)
285fb262 62 {
ae3effcc
FB
63 $table = array_shift($params);
64 $condition = call_user_func_array(array('XDB', 'format'), $params);
285fb262 65 if ($mode != self::MODE_LEFT && $mode != self::MODE_RIGHT && $mode != self::MODE_INNER) {
4aa4899b 66 Platal::page()->kill("Join mode error: unknown mode $mode");
285fb262
RB
67 return;
68 }
69 $this->mode = $mode;
70 $this->table = $table;
71 $this->condition = $condition;
72 }
73
74 public function mode()
75 {
76 return $this->mode;
77 }
78
79 public function table()
80 {
81 return $this->table;
82 }
83
bd9b36fe 84 public function condition()
285fb262
RB
85 {
86 return $this->condition;
87 }
285fb262 88
bd9b36fe
RB
89 /** Replace all "metas" in the condition with their translation.
90 * $ME always becomes the alias of the table
91 * @param $key The name the joined table will have in the final query
92 * @param $joinMetas An array of meta => conversion to apply to the condition
93 */
94 public function replaceJoinMetas($key, $joinMetas = array())
285fb262 95 {
bd9b36fe
RB
96 $joinMetas['$ME'] = $key;
97 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition);
285fb262
RB
98 }
99
bd9b36fe
RB
100 /** Create a join command from an array of PlSqlJoin
101 * @param $joins The list of 'join' to convert into an SQL query
102 * @param $joinMetas An array of ('$META' => 'conversion') to apply to the joins.
103 */
104 public static function formatJoins(array $joins, array $joinMetas)
285fb262
RB
105 {
106 $str = '';
107 foreach ($joins as $key => $join) {
4aa4899b 108 if (!($join instanceof PlSqlJoin)) {
bd9b36fe 109 Platal::page()->kill("Error: not a join: $join");
285fb262
RB
110 }
111 $mode = $join->mode();
112 $table = $join->table();
113 $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key;
114 if ($join->condition() != null) {
bd9b36fe 115 $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')';
285fb262
RB
116 }
117 $str .= "\n";
118 }
119 return $str;
120 }
ae3effcc
FB
121
122 /** Build a left join
123 * @param table The name of the table.
124 * @param condition The condition of the jointure
125 */
126 public static function left()
127 {
128 $params = func_get_args();
129 return new PlSqlJoin(self::MODE_LEFT, $params);
130 }
131
132 /** Build a right join
133 * @param table The name of the table.
134 * @param condition The condition of the jointure
135 */
136 public static function right()
137 {
138 $params = func_get_args();
139 return new PlSqlJoin(self::MODE_RIGHT, $params);
140 }
141
142 /** Build a inner join
143 * @param table The name of the table.
144 * @param condition The condition of the jointure
145 */
146 public static function inner()
147 {
148 $params = func_get_args();
149 return new PlSqlJoin(self::MODE_INNER, $params);
150 }
bd9b36fe 151}
0d78a96b 152// }}}
bd9b36fe 153
0d78a96b 154// {{{ class PlFilterOrder
f66f26e2
RB
155abstract class PlFilterOrder
156{
157 protected $desc = false;
158 public function __construct($desc = false)
159 {
160 $this->desc = $desc;
161 }
162
163 public function toggleDesc()
164 {
165 $this->desc = !$desc;
166 }
167
168 public function setDescending($desc = true)
169 {
170 $this->desc = $desc;
171 }
172
173 public function buildSort(PlFilter &$pf)
174 {
175 $sel = $this->getSortTokens($pf);
176 if (!is_array($sel)) {
177 $sel = array($sel);
178 }
179 if ($this->desc) {
180 foreach ($sel as $k => $s) {
181 $sel[$k] = $s . ' DESC';
182 }
183 }
184 return $sel;
185 }
186
0d78a96b 187 abstract protected function getSortTokens(PlFilter &$pf);
f66f26e2 188}
0d78a96b
RB
189// }}}
190
191// {{{ class PFO_Random
192class PFO_Random extends PlFilterOrder
193{
194 private $seed = null;
195
196 public function __construct($seed = null, $desc = false)
197 {
198 parent::__construct($desc);
199 $this->seed = $seed;
200 }
201
202 protected function getSortTokens(PlFilter &$pf)
203 {
204 if ($this->seed == null) {
205 return 'RAND()';
206 } else {
207 return XDB::format('RAND({?})', $this->seed);
208 }
209 }
210}
211// }}}
f66f26e2
RB
212
213// {{{ interface PlFilterCondition
214interface PlFilterCondition
215{
216 const COND_TRUE = 'TRUE';
217 const COND_FALSE = 'FALSE';
218
219 public function buildCondition(PlFilter &$pf);
220}
221// }}}
222
223// {{{ class PFC_OneChild
224abstract class PFC_OneChild implements PlFilterCondition
225{
226 protected $child;
227
228 public function __construct(&$child = null)
229 {
230 if (!is_null($child) && ($child instanceof PlFilterCondition)) {
231 $this->setChild($child);
232 }
233 }
234
235 public function setChild(PlFilterCondition &$cond)
236 {
237 $this->child =& $cond;
238 }
239}
240// }}}
241
242// {{{ class PFC_NChildren
243abstract class PFC_NChildren implements PlFilterCondition
244{
245 protected $children = array();
246
247 public function __construct()
248 {
249 $children = func_get_args();
250 foreach ($children as &$child) {
251 if (!is_null($child) && ($child instanceof PlFilterCondition)) {
252 $this->addChild($child);
253 }
254 }
255 }
256
257 public function addChild(PlFilterCondition &$cond)
258 {
259 $this->children[] =& $cond;
260 }
261
262 protected function catConds(array $cond, $op, $fallback)
263 {
264 if (count($cond) == 0) {
265 return $fallback;
266 } else if (count($cond) == 1) {
267 return $cond[0];
268 } else {
269 return '(' . implode(') ' . $op . ' (', $cond) . ')';
270 }
271 }
272}
273// }}}
274
275// {{{ class PFC_True
276class PFC_True implements PlFilterCondition
277{
278 public function buildCondition(PlFilter &$uf)
279 {
280 return self::COND_TRUE;
281 }
282}
283// }}}
284
285// {{{ class PFC_False
286class PFC_False implements PlFilterCondition
287{
288 public function buildCondition(PlFilter &$uf)
289 {
290 return self::COND_FALSE;
291 }
292}
293// }}}
294
295// {{{ class PFC_Not
296class PFC_Not extends PFC_OneChild
297{
298 public function buildCondition(PlFilter &$uf)
299 {
300 $val = $this->child->buildCondition($uf);
301 if ($val == self::COND_TRUE) {
302 return self::COND_FALSE;
303 } else if ($val == self::COND_FALSE) {
304 return self::COND_TRUE;
305 } else {
306 return 'NOT (' . $val . ')';
307 }
308 }
309}
310// }}}
311
312// {{{ class PFC_And
313class PFC_And extends PFC_NChildren
314{
315 public function buildCondition(PlFilter &$uf)
316 {
317 if (empty($this->children)) {
318 return self::COND_FALSE;
319 } else {
320 $true = self::COND_FALSE;
321 $conds = array();
322 foreach ($this->children as &$child) {
323 $val = $child->buildCondition($uf);
324 if ($val == self::COND_TRUE) {
325 $true = self::COND_TRUE;
326 } else if ($val == self::COND_FALSE) {
327 return self::COND_FALSE;
328 } else {
329 $conds[] = $val;
330 }
331 }
332 return $this->catConds($conds, 'AND', $true);
333 }
334 }
335}
336// }}}
337
338// {{{ class PFC_Or
339class PFC_Or extends PFC_NChildren
340{
341 public function buildCondition(PlFilter &$uf)
342 {
343 if (empty($this->children)) {
344 return self::COND_TRUE;
345 } else {
346 $true = self::COND_TRUE;
347 $conds = array();
348 foreach ($this->children as &$child) {
349 $val = $child->buildCondition($uf);
350 if ($val == self::COND_TRUE) {
351 return self::COND_TRUE;
352 } else if ($val == self::COND_FALSE) {
353 $true = self::COND_FALSE;
354 } else {
355 $conds[] = $val;
356 }
357 }
358 return $this->catConds($conds, 'OR', $true);
359 }
360 }
361}
362// }}}
363
0d78a96b 364// {{{ class PlFilter
bd9b36fe
RB
365abstract class PlFilter
366{
367 /** Filters objects matching the PlFilter
368 * @param $objects The objects to filter
369 * @param $limit The portion of the matching objects to show
370 */
e1746810 371 public abstract function filter(array $objects, $limit = null);
bd9b36fe
RB
372
373 public abstract function setCondition(PlFilterCondition &$cond);
374
375 public abstract function addSort(PlFilterOrder &$sort);
376
377 public abstract function getTotalCount();
378
379 /** Get objects, selecting only those within a limit
380 * @param $limit The portion of the matching objects to select
381 */
e1746810 382 public abstract function get($limit = null);
bd9b36fe
RB
383
384 /** PRIVATE FUNCTIONS
385 */
386
387 /** List of metas to replace in joins:
388 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
389 *
390 * "$ME" => "joined table alias" is always added to these.
391 */
9a122520
RB
392 protected $joinMetas = array();
393
394 protected $joinMethods = array();
285fb262 395
bd9b36fe
RB
396 /** Build the 'join' part of the query
397 * This function will call all methods declared in self::$joinMethods
398 * to get an array of PlSqlJoin objects to merge
399 */
9a122520 400 protected function buildJoins()
285fb262
RB
401 {
402 $joins = array();
9a122520 403 foreach ($this->joinMethods as $method) {
285fb262
RB
404 $joins = array_merge($joins, $this->$method());
405 }
9a122520 406 return PlSqlJoin::formatJoins($joins, $this->joinMetas);
285fb262
RB
407 }
408
409}
0d78a96b 410// }}}
285fb262 411
ae3effcc 412// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
285fb262 413?>