2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
27 private $count = null
;
30 public function __construct($count = null
, $from = null
)
32 $this->count
= $count;
36 public function getSql()
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
);
42 return XDB
::format('LIMIT {?}', (int)$this->count
);
50 // {{{ class PlSqlJoin
57 const MODE_LEFT
= 'LEFT';
58 const MODE_RIGHT
= 'RIGHT';
59 const MODE_INNER
= 'INNER';
61 private function __construct($mode, $params)
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");
70 $this->table
= $table;
71 $this->condition
= $condition;
74 public function mode()
79 public function table()
84 public function condition()
86 return $this->condition
;
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
94 public function replaceJoinMetas($key, $joinMetas = array())
96 $joinMetas['$ME'] = $key;
97 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition
);
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.
104 public static function formatJoins(array $joins, array $joinMetas)
107 foreach ($joins as $key => $join) {
108 if (!($join instanceof PlSqlJoin
)) {
109 Platal
::page()->kill("Error: not a join: $join");
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) . ')';
122 /** Build a left join
123 * @param table The name of the table.
124 * @param condition The condition of the jointure
126 public static function left()
128 $params = func_get_args();
129 return new PlSqlJoin(self
::MODE_LEFT
, $params);
132 /** Build a right join
133 * @param table The name of the table.
134 * @param condition The condition of the jointure
136 public static function right()
138 $params = func_get_args();
139 return new PlSqlJoin(self
::MODE_RIGHT
, $params);
142 /** Build a inner join
143 * @param table The name of the table.
144 * @param condition The condition of the jointure
146 public static function inner()
148 $params = func_get_args();
149 return new PlSqlJoin(self
::MODE_INNER
, $params);
154 // {{{ class PlFilterOrder
155 abstract class PlFilterOrder
157 protected $desc = false
;
158 public function __construct($desc = false
)
163 public function toggleDesc()
165 $this->desc
= !$this->desc
;
168 public function setDescending($desc = true
)
173 public function buildSort(PlFilter
&$pf)
175 $sel = $this->getSortTokens($pf);
176 if (!is_array($sel)) {
180 foreach ($sel as $k => $s) {
181 $sel[$k] = $s . ' DESC';
187 abstract protected function getSortTokens(PlFilter
&$pf);
191 // {{{ class PFO_Random
192 class PFO_Random
extends PlFilterOrder
194 private $seed = null
;
196 public function __construct($seed = null
, $desc = false
)
198 parent
::__construct($desc);
202 protected function getSortTokens(PlFilter
&$pf)
204 if ($this->seed
== null
) {
207 return XDB
::format('RAND({?})', $this->seed
);
213 // {{{ interface PlFilterCondition
214 interface PlFilterCondition
216 const COND_TRUE
= 'TRUE';
217 const COND_FALSE
= 'FALSE';
219 public function buildCondition(PlFilter
&$pf);
223 // {{{ class PFC_OneChild
224 abstract class PFC_OneChild
implements PlFilterCondition
228 public function __construct(&$child = null
)
230 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
231 $this->setChild($child);
235 public function setChild(PlFilterCondition
&$cond)
237 $this->child
=& $cond;
242 // {{{ class PFC_NChildren
243 abstract class PFC_NChildren
implements PlFilterCondition
245 protected $children = array();
247 public function __construct()
249 $this->addChildren(pl_flatten(func_get_args()));
252 public function addChildren(array $conds)
254 foreach ($conds as &$cond) {
255 if (!is_null($cond) && ($cond instanceof PlFilterCondition
)) {
256 $this->addChild($cond);
261 public function addChild(PlFilterCondition
&$cond)
263 $this->children
[] =& $cond;
266 protected function catConds(array $cond, $op, $fallback)
268 if (count($cond) == 0) {
270 } else if (count($cond) == 1) {
273 return '(' . implode(') ' . $op . ' (', $cond) . ')';
279 // {{{ class PFC_True
280 class PFC_True
implements PlFilterCondition
282 public function buildCondition(PlFilter
&$uf)
284 return self
::COND_TRUE
;
289 // {{{ class PFC_False
290 class PFC_False
implements PlFilterCondition
292 public function buildCondition(PlFilter
&$uf)
294 return self
::COND_FALSE
;
300 class PFC_Not
extends PFC_OneChild
302 public function buildCondition(PlFilter
&$uf)
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
;
310 return 'NOT (' . $val . ')';
317 class PFC_And
extends PFC_NChildren
319 public function buildCondition(PlFilter
&$uf)
321 if (empty($this->children
)) {
322 return self
::COND_FALSE
;
324 $true = self
::COND_FALSE
;
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
;
336 return $this->catConds($conds, 'AND', $true);
343 class PFC_Or
extends PFC_NChildren
345 public function buildCondition(PlFilter
&$uf)
347 if (empty($this->children
)) {
348 return self
::COND_TRUE
;
350 $true = self
::COND_TRUE
;
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
;
362 return $this->catConds($conds, 'OR', $true);
368 // {{{ class PlFilter
369 abstract class PlFilter
371 /** Filters objects matching the PlFilter
372 * @param $objects The objects to filter
373 * @param $limit The portion of the matching objects to show
375 public abstract function filter(array $objects, $limit = null
);
377 public abstract function setCondition(PlFilterCondition
&$cond);
379 public abstract function addSort(PlFilterOrder
&$sort);
381 public abstract function getTotalCount();
383 /** Get objects, selecting only those within a limit
384 * @param $limit The portion of the matching objects to select
386 public abstract function get($limit = null
);
388 /** PRIVATE FUNCTIONS
391 /** List of metas to replace in joins:
392 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
394 * "$ME" => "joined table alias" is always added to these.
396 protected $joinMetas = array();
398 protected $joinMethods = array();
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
404 protected function buildJoins()
407 foreach ($this->joinMethods
as $method) {
408 $joins = array_merge($joins, $this->$method());
410 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);
416 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: