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
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
);
137 // {{{ interface PlFilterCondition
138 interface PlFilterCondition
140 const COND_TRUE
= 'TRUE';
141 const COND_FALSE
= 'FALSE';
143 public function buildCondition(PlFilter
&$pf);
147 // {{{ class PFC_OneChild
148 abstract class PFC_OneChild
implements PlFilterCondition
152 public function __construct(&$child = null
)
154 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
155 $this->setChild($child);
159 public function setChild(PlFilterCondition
&$cond)
161 $this->child
=& $cond;
166 // {{{ class PFC_NChildren
167 abstract class PFC_NChildren
implements PlFilterCondition
169 protected $children = array();
171 public function __construct()
173 $this->addChildren(pl_flatten(func_get_args()));
176 public function addChildren(array $conds)
178 foreach ($conds as &$cond) {
179 if (!is_null($cond) && ($cond instanceof PlFilterCondition
)) {
180 $this->addChild($cond);
185 public function addChild(PlFilterCondition
&$cond)
187 $this->children
[] =& $cond;
190 protected function catConds(array $cond, $op, $fallback)
192 if (count($cond) == 0) {
194 } else if (count($cond) == 1) {
197 return '(' . implode(') ' . $op . ' (', $cond) . ')';
203 // {{{ class PFC_True
204 class PFC_True
implements PlFilterCondition
206 public function buildCondition(PlFilter
&$uf)
208 return self
::COND_TRUE
;
213 // {{{ class PFC_False
214 class PFC_False
implements PlFilterCondition
216 public function buildCondition(PlFilter
&$uf)
218 return self
::COND_FALSE
;
224 class PFC_Not
extends PFC_OneChild
226 public function buildCondition(PlFilter
&$uf)
228 $val = $this->child
->buildCondition($uf);
229 if ($val == self
::COND_TRUE
) {
230 return self
::COND_FALSE
;
231 } else if ($val == self
::COND_FALSE
) {
232 return self
::COND_TRUE
;
234 return 'NOT (' . $val . ')';
241 class PFC_And
extends PFC_NChildren
243 public function buildCondition(PlFilter
&$uf)
245 if (empty($this->children
)) {
246 return self
::COND_FALSE
;
248 $true = self
::COND_FALSE
;
250 foreach ($this->children
as &$child) {
251 $val = $child->buildCondition($uf);
252 if ($val == self
::COND_TRUE
) {
253 $true = self
::COND_TRUE
;
254 } else if ($val == self
::COND_FALSE
) {
255 return self
::COND_FALSE
;
260 return $this->catConds($conds, 'AND', $true);
267 class PFC_Or
extends PFC_NChildren
269 public function buildCondition(PlFilter
&$uf)
271 if (empty($this->children
)) {
272 return self
::COND_TRUE
;
274 $true = self
::COND_TRUE
;
276 foreach ($this->children
as &$child) {
277 $val = $child->buildCondition($uf);
278 if ($val == self
::COND_TRUE
) {
279 return self
::COND_TRUE
;
280 } else if ($val == self
::COND_FALSE
) {
281 $true = self
::COND_FALSE
;
286 return $this->catConds($conds, 'OR', $true);
292 // {{{ class PlFilter
293 abstract class PlFilter
295 /** Filters objects matching the PlFilter
296 * @param $objects The objects to filter
297 * @param $limit The portion of the matching objects to show
299 public abstract function filter(array $objects, $limit = null
);
301 public abstract function setCondition(PlFilterCondition
&$cond);
303 public abstract function addSort(PlFilterOrder
&$sort);
305 public abstract function getTotalCount();
307 /** Whether this PlFilter can return grouped results through
308 * $this->getGroups();
310 public abstract function hasGroups();
312 /** Used to retrieve value/amount resulting from grouping by the first
315 public abstract function getGroups();
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($limit = null
);
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 protected $joinMetas = array();
332 protected $joinMethods = array();
334 /** Build the 'join' part of the query
335 * This function will call all methods declared in self::$joinMethods
336 * to get an array of PlSqlJoin objects to merge
338 protected function buildJoins()
341 foreach ($this->joinMethods
as $method) {
342 $joins = array_merge($joins, $this->$method());
344 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);
350 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: