2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 protected function buildExport($type)
67 $export = array('type' => $type);
69 $export['order'] = 'desc';
74 public function export()
76 throw new Exception("This instance is not exportable");
79 public function toggleDesc()
81 $this->desc
= !$this->desc
;
84 public function setDescending($desc = true
)
89 public function buildSort(PlFilter
$pf)
91 $sel = $this->getSortTokens($pf);
92 $this->_tokens
= $sel;
93 if (!is_array($sel)) {
97 foreach ($sel as $k => $s) {
98 $sel[$k] = $s . ' DESC';
104 /** This function must return the tokens to use for ordering
105 * @param $pf The PlFilter whose results must be ordered
106 * @return The name of the field to use for ordering results
108 abstract protected function getSortTokens(PlFilter
$pf);
112 // {{{ class PlFilterGroupableOrder
113 /** Extension of a PlFilterOrder, for orders where the value on which ordering
114 * is done could be used for grouping results (promo, country, ...)
116 abstract class PlFilterGroupableOrder
extends PlFilterOrder
118 /** This function will be called when trying to retrieve groups;
119 * the returned token will be used to group the values.
120 * It will always be called AFTER getSortTokens().
122 public function getGroupToken(PlFilter
$pf)
124 return $this->_tokens
;
127 /** This function is called when trying to restrict to one of the
128 * specific values of the group token.
130 * @return A PlFilterCondition.
132 public function getCondition($group_value)
139 // {{{ class PFO_Random
140 class PFO_Random
extends PlFilterOrder
142 private $seed = null
;
144 public function __construct($seed = null
, $desc = false
)
146 parent
::__construct($desc);
150 protected function getSortTokens(PlFilter
$pf)
152 if ($this->seed
== null
) {
155 return XDB
::format('RAND({?})', $this->seed
);
159 public function export()
161 $export = array('type' => 'random',);
162 if ($this->seed
!== null
)
163 $export['seed'] = $this->seed
;
169 // {{{ interface PlFilterCondition
170 interface PlFilterCondition
extends PlExportable
172 const COND_TRUE
= 'TRUE';
173 const COND_FALSE
= 'FALSE';
175 public function buildCondition(PlFilter
$pf);
179 // {{{ class PFC_OneChild
180 abstract class PFC_OneChild
implements PlFilterCondition
184 public function __construct($child = null
)
186 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
187 $this->setChild($child);
191 public function setChild(PlFilterCondition
$cond)
193 $this->child
= $cond;
196 public function export()
198 return array('child' => $this->child
->export());
203 // {{{ class PFC_NChildren
204 abstract class PFC_NChildren
implements PlFilterCondition
206 protected $children = array();
208 public function __construct()
210 $this->addChildren(pl_flatten(func_get_args()));
213 public function addChildren(array $conds)
215 foreach ($conds as $cond) {
216 if (!is_null($cond) && ($cond instanceof PlFilterCondition
)) {
217 $this->addChild($cond);
222 public function addChild(PlFilterCondition
$cond)
224 $this->children
[] = $cond;
227 protected function catConds(array $cond, $op, $fallback)
229 if (count($cond) == 0) {
231 } else if (count($cond) == 1) {
234 return '(' . implode(') ' . $op . ' (', $cond) . ')';
238 public function export()
241 foreach ($this->children
as $child) {
242 $export[] = $child->export();
244 return array('children' => $export);
249 // {{{ class PFC_True
250 class PFC_True
implements PlFilterCondition
252 public function buildCondition(PlFilter
$uf)
254 return self
::COND_TRUE
;
257 public function export()
259 return array('type' => 'true');
264 // {{{ class PFC_False
265 class PFC_False
implements PlFilterCondition
267 public function buildCondition(PlFilter
$uf)
269 return self
::COND_FALSE
;
272 public function export()
274 return array('type' => 'false');
280 class PFC_Not
extends PFC_OneChild
282 public function buildCondition(PlFilter
$uf)
284 $val = $this->child
->buildCondition($uf);
285 if ($val == self
::COND_TRUE
) {
286 return self
::COND_FALSE
;
287 } else if ($val == self
::COND_FALSE
) {
288 return self
::COND_TRUE
;
290 return 'NOT (' . $val . ')';
294 public function export()
296 $export = parent
::export();
297 $export['type'] = 'not';
304 class PFC_And
extends PFC_NChildren
306 public function buildCondition(PlFilter
$uf)
308 if (empty($this->children
)) {
309 return self
::COND_FALSE
;
311 $true = self
::COND_FALSE
;
313 foreach ($this->children
as $child) {
314 $val = $child->buildCondition($uf);
315 if ($val == self
::COND_TRUE
) {
316 $true = self
::COND_TRUE
;
317 } else if ($val == self
::COND_FALSE
) {
318 return self
::COND_FALSE
;
323 return $this->catConds($conds, 'AND', $true);
327 public function export() {
328 $export = parent
::export();
329 $export['type'] = 'and';
336 class PFC_Or
extends PFC_NChildren
338 public function buildCondition(PlFilter
$uf)
340 if (empty($this->children
)) {
341 return self
::COND_TRUE
;
343 $true = self
::COND_TRUE
;
345 foreach ($this->children
as $child) {
346 $val = $child->buildCondition($uf);
347 if ($val == self
::COND_TRUE
) {
348 return self
::COND_TRUE
;
349 } else if ($val == self
::COND_FALSE
) {
350 $true = self
::COND_FALSE
;
355 return $this->catConds($conds, 'OR', $true);
359 public function export() {
360 $export = parent
::export();
361 $export['type'] = 'or';
367 // {{{ class PlFilter
368 abstract class PlFilter
implements PlExportable
370 /** Filters objects matching the PlFilter
371 * @param $objects The objects to filter
372 * @param $limit The portion of the matching objects to show
374 public abstract function filter(array $objects, $limit = null
);
376 public abstract function setCondition(PlFilterCondition
$cond);
378 public abstract function addSort(PlFilterOrder
$sort);
380 public abstract function getTotalCount();
382 /** Whether this PlFilter can return grouped results through
383 * $this->getGroups();
385 public abstract function hasGroups();
387 /** Used to retrieve value/amount resulting from grouping by the first
390 public abstract function getGroups();
392 /** Get objects, selecting only those within a limit
393 * @param $limit The portion of the matching objects to select
395 public abstract function get($limit = null
);
397 /** Get ids, selecting only those within a limit
398 * @param $limit The portion of the matching objects to select
400 public function getIds($limit = null
)
402 return $this->get($limit);
405 /** PRIVATE FUNCTIONS
408 /** List of metas to replace in joins:
409 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
411 * "$ME" => "joined table alias" is always added to these.
413 protected $joinMetas = array();
415 protected $joinMethods = array();
417 /** Build the 'join' part of the query
418 * This function will call all methods declared in self::$joinMethods
419 * to get an array of PlSqlJoin objects to merge
421 protected function buildJoins()
424 foreach ($this->joinMethods
as $method) {
425 $joins = array_merge($joins, $this->$method());
427 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);
433 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: