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 ***************************************************************************/
25 private $count = null
;
28 public function __construct($count = null
, $from = null
)
30 $this->count
= $count;
34 public function getSql()
36 if (!is_null($this->count
) && $this->count
!= 0) {
37 if (!is_null($this->from
) && $this->from
!= 0) {
38 return XDB
::format('LIMIT {?}, {?}', (int)$this->from
, (int)$this->count
);
40 return XDB
::format('LIMIT {?}', (int)$this->count
);
48 // {{{ class PlSqlJoin
55 const MODE_LEFT
= 'LEFT';
56 const MODE_RIGHT
= 'RIGHT';
57 const MODE_INNER
= 'INNER';
59 public function __construct($mode, $table, $condition)
61 if ($mode != self
::MODE_LEFT
&& $mode != self
::MODE_RIGHT
&& $mode != self
::MODE_INNER
) {
62 Platal
::page()->kill("Join mode error: unknown mode $mode");
66 $this->table
= $table;
67 $this->condition
= $condition;
70 public function mode()
75 public function table()
80 public function condition()
82 return $this->condition
;
85 /** Replace all "metas" in the condition with their translation.
86 * $ME always becomes the alias of the table
87 * @param $key The name the joined table will have in the final query
88 * @param $joinMetas An array of meta => conversion to apply to the condition
90 public function replaceJoinMetas($key, $joinMetas = array())
92 $joinMetas['$ME'] = $key;
93 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition
);
96 /** Create a join command from an array of PlSqlJoin
97 * @param $joins The list of 'join' to convert into an SQL query
98 * @param $joinMetas An array of ('$META' => 'conversion') to apply to the joins.
100 public static function formatJoins(array $joins, array $joinMetas)
103 foreach ($joins as $key => $join) {
104 if (!($join instanceof PlSqlJoin
)) {
105 Platal
::page()->kill("Error: not a join: $join");
107 $mode = $join->mode();
108 $table = $join->table();
109 $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key;
110 if ($join->condition() != null
) {
111 $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')';
120 // {{{ class PlFilterOrder
121 abstract class PlFilterOrder
123 protected $desc = false
;
124 public function __construct($desc = false
)
129 public function toggleDesc()
131 $this->desc
= !$desc;
134 public function setDescending($desc = true
)
139 public function buildSort(PlFilter
&$pf)
141 $sel = $this->getSortTokens($pf);
142 if (!is_array($sel)) {
146 foreach ($sel as $k => $s) {
147 $sel[$k] = $s . ' DESC';
153 abstract protected function getSortTokens(PlFilter
&$pf);
157 // {{{ class PFO_Random
158 class PFO_Random
extends PlFilterOrder
160 private $seed = null
;
162 public function __construct($seed = null
, $desc = false
)
164 parent
::__construct($desc);
168 protected function getSortTokens(PlFilter
&$pf)
170 if ($this->seed
== null
) {
173 return XDB
::format('RAND({?})', $this->seed
);
179 // {{{ interface PlFilterCondition
180 interface PlFilterCondition
182 const COND_TRUE
= 'TRUE';
183 const COND_FALSE
= 'FALSE';
185 public function buildCondition(PlFilter
&$pf);
189 // {{{ class PFC_OneChild
190 abstract class PFC_OneChild
implements PlFilterCondition
194 public function __construct(&$child = null
)
196 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
197 $this->setChild($child);
201 public function setChild(PlFilterCondition
&$cond)
203 $this->child
=& $cond;
208 // {{{ class PFC_NChildren
209 abstract class PFC_NChildren
implements PlFilterCondition
211 protected $children = array();
213 public function __construct()
215 $children = func_get_args();
216 foreach ($children as &$child) {
217 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
218 $this->addChild($child);
223 public function addChild(PlFilterCondition
&$cond)
225 $this->children
[] =& $cond;
228 protected function catConds(array $cond, $op, $fallback)
230 if (count($cond) == 0) {
232 } else if (count($cond) == 1) {
235 return '(' . implode(') ' . $op . ' (', $cond) . ')';
241 // {{{ class PFC_True
242 class PFC_True
implements PlFilterCondition
244 public function buildCondition(PlFilter
&$uf)
246 return self
::COND_TRUE
;
251 // {{{ class PFC_False
252 class PFC_False
implements PlFilterCondition
254 public function buildCondition(PlFilter
&$uf)
256 return self
::COND_FALSE
;
262 class PFC_Not
extends PFC_OneChild
264 public function buildCondition(PlFilter
&$uf)
266 $val = $this->child
->buildCondition($uf);
267 if ($val == self
::COND_TRUE
) {
268 return self
::COND_FALSE
;
269 } else if ($val == self
::COND_FALSE
) {
270 return self
::COND_TRUE
;
272 return 'NOT (' . $val . ')';
279 class PFC_And
extends PFC_NChildren
281 public function buildCondition(PlFilter
&$uf)
283 if (empty($this->children
)) {
284 return self
::COND_FALSE
;
286 $true = self
::COND_FALSE
;
288 foreach ($this->children
as &$child) {
289 $val = $child->buildCondition($uf);
290 if ($val == self
::COND_TRUE
) {
291 $true = self
::COND_TRUE
;
292 } else if ($val == self
::COND_FALSE
) {
293 return self
::COND_FALSE
;
298 return $this->catConds($conds, 'AND', $true);
305 class PFC_Or
extends PFC_NChildren
307 public function buildCondition(PlFilter
&$uf)
309 if (empty($this->children
)) {
310 return self
::COND_TRUE
;
312 $true = self
::COND_TRUE
;
314 foreach ($this->children
as &$child) {
315 $val = $child->buildCondition($uf);
316 if ($val == self
::COND_TRUE
) {
317 return self
::COND_TRUE
;
318 } else if ($val == self
::COND_FALSE
) {
319 $true = self
::COND_FALSE
;
324 return $this->catConds($conds, 'OR', $true);
330 // {{{ class PlFilter
331 abstract class PlFilter
333 /** Filters objects matching the PlFilter
334 * @param $objects The objects to filter
335 * @param $limit The portion of the matching objects to show
337 public abstract function filter(array $objects, $limit = null
);
339 public abstract function setCondition(PlFilterCondition
&$cond);
341 public abstract function addSort(PlFilterOrder
&$sort);
343 public abstract function getTotalCount();
345 /** Get objects, selecting only those within a limit
346 * @param $limit The portion of the matching objects to select
348 public abstract function get($limit = null
);
350 /** PRIVATE FUNCTIONS
353 /** List of metas to replace in joins:
354 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
356 * "$ME" => "joined table alias" is always added to these.
358 protected $joinMetas = array();
360 protected $joinMethods = array();
362 /** Build the 'join' part of the query
363 * This function will call all methods declared in self::$joinMethods
364 * to get an array of PlSqlJoin objects to merge
366 protected function buildJoins()
369 foreach ($this->joinMethods
as $method) {
370 $joins = array_merge($joins, $this->$method());
372 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);