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
= !$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 $children = func_get_args();
250 foreach ($children as &$child) {
251 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
252 $this->addChild($child);
257 public function addChild(PlFilterCondition
&$cond)
259 $this->children
[] =& $cond;
262 protected function catConds(array $cond, $op, $fallback)
264 if (count($cond) == 0) {
266 } else if (count($cond) == 1) {
269 return '(' . implode(') ' . $op . ' (', $cond) . ')';
275 // {{{ class PFC_True
276 class PFC_True
implements PlFilterCondition
278 public function buildCondition(PlFilter
&$uf)
280 return self
::COND_TRUE
;
285 // {{{ class PFC_False
286 class PFC_False
implements PlFilterCondition
288 public function buildCondition(PlFilter
&$uf)
290 return self
::COND_FALSE
;
296 class PFC_Not
extends PFC_OneChild
298 public function buildCondition(PlFilter
&$uf)
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
;
306 return 'NOT (' . $val . ')';
313 class PFC_And
extends PFC_NChildren
315 public function buildCondition(PlFilter
&$uf)
317 if (empty($this->children
)) {
318 return self
::COND_FALSE
;
320 $true = self
::COND_FALSE
;
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
;
332 return $this->catConds($conds, 'AND', $true);
339 class PFC_Or
extends PFC_NChildren
341 public function buildCondition(PlFilter
&$uf)
343 if (empty($this->children
)) {
344 return self
::COND_TRUE
;
346 $true = self
::COND_TRUE
;
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
;
358 return $this->catConds($conds, 'OR', $true);
364 // {{{ class PlFilter
365 abstract class PlFilter
367 /** Filters objects matching the PlFilter
368 * @param $objects The objects to filter
369 * @param $limit The portion of the matching objects to show
371 public abstract function filter(array $objects, $limit = null
);
373 public abstract function setCondition(PlFilterCondition
&$cond);
375 public abstract function addSort(PlFilterOrder
&$sort);
377 public abstract function getTotalCount();
379 /** Get objects, selecting only those within a limit
380 * @param $limit The portion of the matching objects to select
382 public abstract function get($limit = null
);
384 /** PRIVATE FUNCTIONS
387 /** List of metas to replace in joins:
388 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
390 * "$ME" => "joined table alias" is always added to these.
392 protected $joinMetas = array();
394 protected $joinMethods = array();
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
400 protected function buildJoins()
403 foreach ($this->joinMethods
as $method) {
404 $joins = array_merge($joins, $this->$method());
406 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);
412 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: