Prenvents '##' to be escaped to '' in SQL querries (Closes #1156).
[platal.git] / classes / plfilter.php
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
22 __autoload('xdb');
23
24 // {{{ class PlLimit
25 class 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 {
38 if (!is_null($this->count) && $this->count != 0) {
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 }
48 // }}}
49
50 // {{{ class PlSqlJoin
51 class 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
61 private function __construct($mode, $params)
62 {
63 $table = array_shift($params);
64 $condition = call_user_func_array(array('XDB', 'format'), $params);
65 if ($mode != self::MODE_LEFT && $mode != self::MODE_RIGHT && $mode != self::MODE_INNER) {
66 Platal::page()->kill("Join mode error: unknown mode $mode");
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
84 public function condition()
85 {
86 return $this->condition;
87 }
88
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())
95 {
96 $joinMetas['$ME'] = $key;
97 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition);
98 }
99
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)
105 {
106 $str = '';
107 foreach ($joins as $key => $join) {
108 if (!($join instanceof PlSqlJoin)) {
109 Platal::page()->kill("Error: not a join: $join");
110 }
111 $mode = $join->mode();
112 $table = $join->table();
113 $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key;
114 if ($join->condition() != null) {
115 $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')';
116 }
117 $str .= "\n";
118 }
119 return $str;
120 }
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 }
151 }
152 // }}}
153
154 // {{{ class PlFilterOrder
155 abstract 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 = !$this->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
187 abstract protected function getSortTokens(PlFilter &$pf);
188 }
189 // }}}
190
191 // {{{ class PFO_Random
192 class 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 // }}}
212
213 // {{{ interface PlFilterCondition
214 interface 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
224 abstract 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
243 abstract class PFC_NChildren implements PlFilterCondition
244 {
245 protected $children = array();
246
247 public function __construct()
248 {
249 $this->addChildren(pl_flatten(func_get_args()));
250 }
251
252 public function addChildren(array $conds)
253 {
254 foreach ($conds as &$cond) {
255 if (!is_null($cond) && ($cond instanceof PlFilterCondition)) {
256 $this->addChild($cond);
257 }
258 }
259 }
260
261 public function addChild(PlFilterCondition &$cond)
262 {
263 $this->children[] =& $cond;
264 }
265
266 protected function catConds(array $cond, $op, $fallback)
267 {
268 if (count($cond) == 0) {
269 return $fallback;
270 } else if (count($cond) == 1) {
271 return $cond[0];
272 } else {
273 return '(' . implode(') ' . $op . ' (', $cond) . ')';
274 }
275 }
276 }
277 // }}}
278
279 // {{{ class PFC_True
280 class PFC_True implements PlFilterCondition
281 {
282 public function buildCondition(PlFilter &$uf)
283 {
284 return self::COND_TRUE;
285 }
286 }
287 // }}}
288
289 // {{{ class PFC_False
290 class PFC_False implements PlFilterCondition
291 {
292 public function buildCondition(PlFilter &$uf)
293 {
294 return self::COND_FALSE;
295 }
296 }
297 // }}}
298
299 // {{{ class PFC_Not
300 class PFC_Not extends PFC_OneChild
301 {
302 public function buildCondition(PlFilter &$uf)
303 {
304 $val = $this->child->buildCondition($uf);
305 if ($val == self::COND_TRUE) {
306 return self::COND_FALSE;
307 } else if ($val == self::COND_FALSE) {
308 return self::COND_TRUE;
309 } else {
310 return 'NOT (' . $val . ')';
311 }
312 }
313 }
314 // }}}
315
316 // {{{ class PFC_And
317 class PFC_And extends PFC_NChildren
318 {
319 public function buildCondition(PlFilter &$uf)
320 {
321 if (empty($this->children)) {
322 return self::COND_FALSE;
323 } else {
324 $true = self::COND_FALSE;
325 $conds = array();
326 foreach ($this->children as &$child) {
327 $val = $child->buildCondition($uf);
328 if ($val == self::COND_TRUE) {
329 $true = self::COND_TRUE;
330 } else if ($val == self::COND_FALSE) {
331 return self::COND_FALSE;
332 } else {
333 $conds[] = $val;
334 }
335 }
336 return $this->catConds($conds, 'AND', $true);
337 }
338 }
339 }
340 // }}}
341
342 // {{{ class PFC_Or
343 class PFC_Or extends PFC_NChildren
344 {
345 public function buildCondition(PlFilter &$uf)
346 {
347 if (empty($this->children)) {
348 return self::COND_TRUE;
349 } else {
350 $true = self::COND_TRUE;
351 $conds = array();
352 foreach ($this->children as &$child) {
353 $val = $child->buildCondition($uf);
354 if ($val == self::COND_TRUE) {
355 return self::COND_TRUE;
356 } else if ($val == self::COND_FALSE) {
357 $true = self::COND_FALSE;
358 } else {
359 $conds[] = $val;
360 }
361 }
362 return $this->catConds($conds, 'OR', $true);
363 }
364 }
365 }
366 // }}}
367
368 // {{{ class PlFilter
369 abstract class PlFilter
370 {
371 /** Filters objects matching the PlFilter
372 * @param $objects The objects to filter
373 * @param $limit The portion of the matching objects to show
374 */
375 public abstract function filter(array $objects, $limit = null);
376
377 public abstract function setCondition(PlFilterCondition &$cond);
378
379 public abstract function addSort(PlFilterOrder &$sort);
380
381 public abstract function getTotalCount();
382
383 /** Get objects, selecting only those within a limit
384 * @param $limit The portion of the matching objects to select
385 */
386 public abstract function get($limit = null);
387
388 /** PRIVATE FUNCTIONS
389 */
390
391 /** List of metas to replace in joins:
392 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
393 *
394 * "$ME" => "joined table alias" is always added to these.
395 */
396 protected $joinMetas = array();
397
398 protected $joinMethods = array();
399
400 /** Build the 'join' part of the query
401 * This function will call all methods declared in self::$joinMethods
402 * to get an array of PlSqlJoin objects to merge
403 */
404 protected function buildJoins()
405 {
406 $joins = array();
407 foreach ($this->joinMethods as $method) {
408 $joins = array_merge($joins, $this->$method());
409 }
410 return PlSqlJoin::formatJoins($joins, $this->joinMetas);
411 }
412
413 }
414 // }}}
415
416 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
417 ?>