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 PlFilterOrder
51 /** Base class for ordering results of a query.
52 * Parameters for the ordering must be given to the constructor ($desc for a
54 * The getSortTokens function is used to get actual ordering part of the query.
56 abstract class PlFilterOrder
implements PlExportable
58 protected $desc = false
;
59 public function __construct($desc = false
)
62 $this->_tokens
= null
;
65 public function toggleDesc()
67 $this->desc
= !$this->desc
;
70 public function setDescending($desc = true
)
75 public function buildSort(PlFilter
&$pf)
77 $sel = $this->getSortTokens($pf);
78 $this->_tokens
= $sel;
79 if (!is_array($sel)) {
83 foreach ($sel as $k => $s) {
84 $sel[$k] = $s . ' DESC';
90 /** This function must return the tokens to use for ordering
91 * @param &$pf The PlFilter whose results must be ordered
92 * @return The name of the field to use for ordering results
94 abstract protected function getSortTokens(PlFilter
&$pf);
98 // {{{ class PlFilterGroupableOrder
99 /** Extension of a PlFilterOrder, for orders where the value on which ordering
100 * is done could be used for grouping results (promo, country, ...)
102 abstract class PlFilterGroupableOrder
extends PlFilterOrder
104 /** This function will be called when trying to retrieve groups;
105 * the returned token will be used to group the values.
106 * It will always be called AFTER getSortTokens().
108 public function getGroupToken(PlFilter
&$pf)
110 return $this->_tokens
;
115 // {{{ class PFO_Random
116 class PFO_Random
extends PlFilterOrder
118 private $seed = null
;
120 public function __construct($seed = null
, $desc = false
)
122 parent
::__construct($desc);
126 protected function getSortTokens(PlFilter
&$pf)
128 if ($this->seed
== null
) {
131 return XDB
::format('RAND({?})', $this->seed
);
135 public function export()
137 $export = array('type' => 'random',);
138 if ($this->seed
!== null
)
139 $export['seed'] = $this->seed
;
145 // {{{ interface PlFilterCondition
146 interface PlFilterCondition
extends PlExportable
148 const COND_TRUE
= 'TRUE';
149 const COND_FALSE
= 'FALSE';
151 public function buildCondition(PlFilter
&$pf);
155 // {{{ class PFC_OneChild
156 abstract class PFC_OneChild
implements PlFilterCondition
160 public function __construct(&$child = null
)
162 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
163 $this->setChild($child);
167 public function setChild(PlFilterCondition
&$cond)
169 $this->child
=& $cond;
172 public function export()
174 return array('child' => $child->export());
179 // {{{ class PFC_NChildren
180 abstract class PFC_NChildren
implements PlFilterCondition
182 protected $children = array();
184 public function __construct()
186 $this->addChildren(pl_flatten(func_get_args()));
189 public function addChildren(array $conds)
191 foreach ($conds as &$cond) {
192 if (!is_null($cond) && ($cond instanceof PlFilterCondition
)) {
193 $this->addChild($cond);
198 public function addChild(PlFilterCondition
&$cond)
200 $this->children
[] =& $cond;
203 protected function catConds(array $cond, $op, $fallback)
205 if (count($cond) == 0) {
207 } else if (count($cond) == 1) {
210 return '(' . implode(') ' . $op . ' (', $cond) . ')';
214 public function export()
217 foreach ($this->children
as $child)
218 $export[] = $child->export();
219 return array('children' => $export);
224 // {{{ class PFC_True
225 class PFC_True
implements PlFilterCondition
227 public function buildCondition(PlFilter
&$uf)
229 return self
::COND_TRUE
;
232 public function export()
234 return array('type' => 'true');
239 // {{{ class PFC_False
240 class PFC_False
implements PlFilterCondition
242 public function buildCondition(PlFilter
&$uf)
244 return self
::COND_FALSE
;
247 public function export()
249 return array('type' => 'false');
255 class PFC_Not
extends PFC_OneChild
257 public function buildCondition(PlFilter
&$uf)
259 $val = $this->child
->buildCondition($uf);
260 if ($val == self
::COND_TRUE
) {
261 return self
::COND_FALSE
;
262 } else if ($val == self
::COND_FALSE
) {
263 return self
::COND_TRUE
;
265 return 'NOT (' . $val . ')';
269 public function export()
271 $export = parent
::export();
272 $export['type'] = 'not';
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);
302 public function export() {
303 $export = parent
::export();
304 $export['type'] = 'and';
311 class PFC_Or
extends PFC_NChildren
313 public function buildCondition(PlFilter
&$uf)
315 if (empty($this->children
)) {
316 return self
::COND_TRUE
;
318 $true = self
::COND_TRUE
;
320 foreach ($this->children
as &$child) {
321 $val = $child->buildCondition($uf);
322 if ($val == self
::COND_TRUE
) {
323 return self
::COND_TRUE
;
324 } else if ($val == self
::COND_FALSE
) {
325 $true = self
::COND_FALSE
;
330 return $this->catConds($conds, 'OR', $true);
334 public function export() {
335 $export = parent
::export();
336 $export['type'] = 'or';
342 // {{{ class PlFilter
343 abstract class PlFilter
implements PlExportable
345 /** Filters objects matching the PlFilter
346 * @param $objects The objects to filter
347 * @param $limit The portion of the matching objects to show
349 public abstract function filter(array $objects, $limit = null
);
351 public abstract function setCondition(PlFilterCondition
&$cond);
353 public abstract function addSort(PlFilterOrder
&$sort);
355 public abstract function getTotalCount();
357 /** Whether this PlFilter can return grouped results through
358 * $this->getGroups();
360 public abstract function hasGroups();
362 /** Used to retrieve value/amount resulting from grouping by the first
365 public abstract function getGroups();
367 /** Get objects, selecting only those within a limit
368 * @param $limit The portion of the matching objects to select
370 public abstract function get($limit = null
);
372 /** PRIVATE FUNCTIONS
375 /** List of metas to replace in joins:
376 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
378 * "$ME" => "joined table alias" is always added to these.
380 protected $joinMetas = array();
382 protected $joinMethods = array();
384 /** Build the 'join' part of the query
385 * This function will call all methods declared in self::$joinMethods
386 * to get an array of PlSqlJoin objects to merge
388 protected function buildJoins()
391 foreach ($this->joinMethods
as $method) {
392 $joins = array_merge($joins, $this->$method());
394 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);
400 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: