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 ***************************************************************************/
24 private $count = null
;
27 public function __construct($count = null
, $from = null
)
29 $this->count
= $count;
33 public function getSql()
35 if (!is_null($this->count
)) {
36 if (!is_null($this->from
) && $this->from
!= 0) {
37 return XDB
::format('LIMIT {?}, {?}', (int)$this->from
, (int)$this->count
);
39 return XDB
::format('LIMIT {?}', (int)$this->count
);
52 const MODE_LEFT
= 'LEFT';
53 const MODE_RIGHT
= 'RIGHT';
54 const MODE_INNER
= 'INNER';
56 public function __construct($mode, $table, $condition)
58 if ($mode != self
::MODE_LEFT
&& $mode != self
::MODE_RIGHT
&& $mode != self
::MODE_INNER
) {
59 Platal
::page()->kill("Join mode error : unknown mode $mode");
63 $this->table
= $table;
64 $this->condition
= $condition;
67 public function mode()
72 public function table()
77 public function condition()
79 return $this->condition
;
82 /** Replace all "metas" in the condition with their translation.
83 * $ME always becomes the alias of the table
84 * @param $key The name the joined table will have in the final query
85 * @param $joinMetas An array of meta => conversion to apply to the condition
87 public function replaceJoinMetas($key, $joinMetas = array())
89 $joinMetas['$ME'] = $key;
90 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition
);
93 /** Create a join command from an array of PlSqlJoin
94 * @param $joins The list of 'join' to convert into an SQL query
95 * @param $joinMetas An array of ('$META' => 'conversion') to apply to the joins.
97 public static function formatJoins(array $joins, array $joinMetas)
100 foreach ($joins as $key => $join) {
101 if (! ($join instanceof PlSqlJoin
)) {
102 Platal
::page()->kill("Error: not a join: $join");
104 $mode = $join->mode();
105 $table = $join->table();
106 $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key;
107 if ($join->condition() != null
) {
108 $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')';
116 abstract class PlFilterOrder
118 protected $desc = false
;
119 public function __construct($desc = false
)
124 public function toggleDesc()
126 $this->desc
= !$desc;
129 public function setDescending($desc = true
)
134 public function buildSort(PlFilter
&$pf)
136 $sel = $this->getSortTokens($pf);
137 if (!is_array($sel)) {
141 foreach ($sel as $k => $s) {
142 $sel[$k] = $s . ' DESC';
148 // abstract protected function getSortTokens(&$pf);
151 // {{{ interface PlFilterCondition
152 interface PlFilterCondition
154 const COND_TRUE
= 'TRUE';
155 const COND_FALSE
= 'FALSE';
157 public function buildCondition(PlFilter
&$pf);
161 // {{{ class PFC_OneChild
162 abstract class PFC_OneChild
implements PlFilterCondition
166 public function __construct(&$child = null
)
168 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
169 $this->setChild($child);
173 public function setChild(PlFilterCondition
&$cond)
175 $this->child
=& $cond;
180 // {{{ class PFC_NChildren
181 abstract class PFC_NChildren
implements PlFilterCondition
183 protected $children = array();
185 public function __construct()
187 $children = func_get_args();
188 foreach ($children as &$child) {
189 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
190 $this->addChild($child);
195 public function addChild(PlFilterCondition
&$cond)
197 $this->children
[] =& $cond;
200 protected function catConds(array $cond, $op, $fallback)
202 if (count($cond) == 0) {
204 } else if (count($cond) == 1) {
207 return '(' . implode(') ' . $op . ' (', $cond) . ')';
213 // {{{ class PFC_True
214 class PFC_True
implements PlFilterCondition
216 public function buildCondition(PlFilter
&$uf)
218 return self
::COND_TRUE
;
223 // {{{ class PFC_False
224 class PFC_False
implements PlFilterCondition
226 public function buildCondition(PlFilter
&$uf)
228 return self
::COND_FALSE
;
234 class PFC_Not
extends PFC_OneChild
236 public function buildCondition(PlFilter
&$uf)
238 $val = $this->child
->buildCondition($uf);
239 if ($val == self
::COND_TRUE
) {
240 return self
::COND_FALSE
;
241 } else if ($val == self
::COND_FALSE
) {
242 return self
::COND_TRUE
;
244 return 'NOT (' . $val . ')';
251 class PFC_And
extends PFC_NChildren
253 public function buildCondition(PlFilter
&$uf)
255 if (empty($this->children
)) {
256 return self
::COND_FALSE
;
258 $true = self
::COND_FALSE
;
260 foreach ($this->children
as &$child) {
261 $val = $child->buildCondition($uf);
262 if ($val == self
::COND_TRUE
) {
263 $true = self
::COND_TRUE
;
264 } else if ($val == self
::COND_FALSE
) {
265 return self
::COND_FALSE
;
270 return $this->catConds($conds, 'AND', $true);
277 class PFC_Or
extends PFC_NChildren
279 public function buildCondition(PlFilter
&$uf)
281 if (empty($this->children
)) {
282 return self
::COND_TRUE
;
284 $true = self
::COND_TRUE
;
286 foreach ($this->children
as &$child) {
287 $val = $child->buildCondition($uf);
288 if ($val == self
::COND_TRUE
) {
289 return self
::COND_TRUE
;
290 } else if ($val == self
::COND_FALSE
) {
291 $true = self
::COND_FALSE
;
296 return $this->catConds($conds, 'OR', $true);
303 abstract class PlFilter
305 /** Filters objects matching the PlFilter
306 * @param $objects The objects to filter
307 * @param $limit The portion of the matching objects to show
309 public abstract function filter(array $objects, PlLimit
&$limit);
311 public abstract function setCondition(PlFilterCondition
&$cond);
313 public abstract function addSort(PlFilterOrder
&$sort);
315 public abstract function getTotalCount();
317 /** Get objects, selecting only those within a limit
318 * @param $limit The portion of the matching objects to select
320 public abstract function get(PlLimit
&$limit);
322 /** PRIVATE FUNCTIONS
325 /** List of metas to replace in joins:
326 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
328 * "$ME" => "joined table alias" is always added to these.
330 private $joinMetas = array();
332 /** Build the 'join' part of the query
333 * This function will call all methods declared in self::$joinMethods
334 * to get an array of PlSqlJoin objects to merge
336 private function buildJoins()
339 foreach ($this->$joinMethods as $method) {
340 $joins = array_merge($joins, $this->$method());
342 return PlSqlJoin
::formatJoins($joins, $this->$joinMetas);