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 export()
67 throw new Exception("This instance is not exportable");
70 public function toggleDesc()
72 $this->desc
= !$this->desc
;
75 public function setDescending($desc = true
)
80 public function buildSort(PlFilter
&$pf)
82 $sel = $this->getSortTokens($pf);
83 $this->_tokens
= $sel;
84 if (!is_array($sel)) {
88 foreach ($sel as $k => $s) {
89 $sel[$k] = $s . ' DESC';
95 /** This function must return the tokens to use for ordering
96 * @param &$pf The PlFilter whose results must be ordered
97 * @return The name of the field to use for ordering results
99 abstract protected function getSortTokens(PlFilter
&$pf);
103 // {{{ class PlFilterGroupableOrder
104 /** Extension of a PlFilterOrder, for orders where the value on which ordering
105 * is done could be used for grouping results (promo, country, ...)
107 abstract class PlFilterGroupableOrder
extends PlFilterOrder
109 /** This function will be called when trying to retrieve groups;
110 * the returned token will be used to group the values.
111 * It will always be called AFTER getSortTokens().
113 public function getGroupToken(PlFilter
&$pf)
115 return $this->_tokens
;
120 // {{{ class PFO_Random
121 class PFO_Random
extends PlFilterOrder
123 private $seed = null
;
125 public function __construct($seed = null
, $desc = false
)
127 parent
::__construct($desc);
131 protected function getSortTokens(PlFilter
&$pf)
133 if ($this->seed
== null
) {
136 return XDB
::format('RAND({?})', $this->seed
);
140 public function export()
142 $export = array('type' => 'random',);
143 if ($this->seed
!== null
)
144 $export['seed'] = $this->seed
;
150 // {{{ interface PlFilterCondition
151 interface PlFilterCondition
extends PlExportable
153 const COND_TRUE
= 'TRUE';
154 const COND_FALSE
= 'FALSE';
156 public function buildCondition(PlFilter
&$pf);
160 // {{{ class PFC_OneChild
161 abstract class PFC_OneChild
implements PlFilterCondition
165 public function __construct(&$child = null
)
167 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
168 $this->setChild($child);
172 public function setChild(PlFilterCondition
&$cond)
174 $this->child
=& $cond;
177 public function export()
179 return array('child' => $child->export());
184 // {{{ class PFC_NChildren
185 abstract class PFC_NChildren
implements PlFilterCondition
187 protected $children = array();
189 public function __construct()
191 $this->addChildren(pl_flatten(func_get_args()));
194 public function addChildren(array $conds)
196 foreach ($conds as &$cond) {
197 if (!is_null($cond) && ($cond instanceof PlFilterCondition
)) {
198 $this->addChild($cond);
203 public function addChild(PlFilterCondition
&$cond)
205 $this->children
[] =& $cond;
208 protected function catConds(array $cond, $op, $fallback)
210 if (count($cond) == 0) {
212 } else if (count($cond) == 1) {
215 return '(' . implode(') ' . $op . ' (', $cond) . ')';
219 public function export()
222 foreach ($this->children
as $child)
223 $export[] = $child->export();
224 return array('children' => $export);
229 // {{{ class PFC_True
230 class PFC_True
implements PlFilterCondition
232 public function buildCondition(PlFilter
&$uf)
234 return self
::COND_TRUE
;
237 public function export()
239 return array('type' => 'true');
244 // {{{ class PFC_False
245 class PFC_False
implements PlFilterCondition
247 public function buildCondition(PlFilter
&$uf)
249 return self
::COND_FALSE
;
252 public function export()
254 return array('type' => 'false');
260 class PFC_Not
extends PFC_OneChild
262 public function buildCondition(PlFilter
&$uf)
264 $val = $this->child
->buildCondition($uf);
265 if ($val == self
::COND_TRUE
) {
266 return self
::COND_FALSE
;
267 } else if ($val == self
::COND_FALSE
) {
268 return self
::COND_TRUE
;
270 return 'NOT (' . $val . ')';
274 public function export()
276 $export = parent
::export();
277 $export['type'] = 'not';
284 class PFC_And
extends PFC_NChildren
286 public function buildCondition(PlFilter
&$uf)
288 if (empty($this->children
)) {
289 return self
::COND_FALSE
;
291 $true = self
::COND_FALSE
;
293 foreach ($this->children
as &$child) {
294 $val = $child->buildCondition($uf);
295 if ($val == self
::COND_TRUE
) {
296 $true = self
::COND_TRUE
;
297 } else if ($val == self
::COND_FALSE
) {
298 return self
::COND_FALSE
;
303 return $this->catConds($conds, 'AND', $true);
307 public function export() {
308 $export = parent
::export();
309 $export['type'] = 'and';
316 class PFC_Or
extends PFC_NChildren
318 public function buildCondition(PlFilter
&$uf)
320 if (empty($this->children
)) {
321 return self
::COND_TRUE
;
323 $true = self
::COND_TRUE
;
325 foreach ($this->children
as &$child) {
326 $val = $child->buildCondition($uf);
327 if ($val == self
::COND_TRUE
) {
328 return self
::COND_TRUE
;
329 } else if ($val == self
::COND_FALSE
) {
330 $true = self
::COND_FALSE
;
335 return $this->catConds($conds, 'OR', $true);
339 public function export() {
340 $export = parent
::export();
341 $export['type'] = 'or';
347 // {{{ class PlFilter
348 abstract class PlFilter
implements PlExportable
350 /** Filters objects matching the PlFilter
351 * @param $objects The objects to filter
352 * @param $limit The portion of the matching objects to show
354 public abstract function filter(array $objects, $limit = null
);
356 public abstract function setCondition(PlFilterCondition
&$cond);
358 public abstract function addSort(PlFilterOrder
&$sort);
360 public abstract function getTotalCount();
362 /** Whether this PlFilter can return grouped results through
363 * $this->getGroups();
365 public abstract function hasGroups();
367 /** Used to retrieve value/amount resulting from grouping by the first
370 public abstract function getGroups();
372 /** Get objects, selecting only those within a limit
373 * @param $limit The portion of the matching objects to select
375 public abstract function get($limit = null
);
377 /** PRIVATE FUNCTIONS
380 /** List of metas to replace in joins:
381 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
383 * "$ME" => "joined table alias" is always added to these.
385 protected $joinMetas = array();
387 protected $joinMethods = array();
389 /** Build the 'join' part of the query
390 * This function will call all methods declared in self::$joinMethods
391 * to get an array of PlSqlJoin objects to merge
393 protected function buildJoins()
396 foreach ($this->joinMethods
as $method) {
397 $joins = array_merge($joins, $this->$method());
399 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);
405 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: