5b29a2f5d0befcab50873685ab281bdcf0b97fb7
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 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
;
129 // {{{ class PFO_Random
130 class PFO_Random
extends PlFilterOrder
132 private $seed = null
;
134 public function __construct($seed = null
, $desc = false
)
136 parent
::__construct($desc);
140 protected function getSortTokens(PlFilter
$pf)
142 if ($this->seed
== null
) {
145 return XDB
::format('RAND({?})', $this->seed
);
149 public function export()
151 $export = array('type' => 'random',);
152 if ($this->seed
!== null
)
153 $export['seed'] = $this->seed
;
159 // {{{ interface PlFilterCondition
160 interface PlFilterCondition
extends PlExportable
162 const COND_TRUE
= 'TRUE';
163 const COND_FALSE
= 'FALSE';
165 public function buildCondition(PlFilter
$pf);
169 // {{{ class PFC_OneChild
170 abstract class PFC_OneChild
implements PlFilterCondition
174 public function __construct($child = null
)
176 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
177 $this->setChild($child);
181 public function setChild(PlFilterCondition
$cond)
183 $this->child
= $cond;
186 public function export()
188 return array('child' => $child->export());
193 // {{{ class PFC_NChildren
194 abstract class PFC_NChildren
implements PlFilterCondition
196 protected $children = array();
198 public function __construct()
200 $this->addChildren(pl_flatten(func_get_args()));
203 public function addChildren(array $conds)
205 foreach ($conds as $cond) {
206 if (!is_null($cond) && ($cond instanceof PlFilterCondition
)) {
207 $this->addChild($cond);
212 public function addChild(PlFilterCondition
$cond)
214 $this->children
[] = $cond;
217 protected function catConds(array $cond, $op, $fallback)
219 if (count($cond) == 0) {
221 } else if (count($cond) == 1) {
224 return '(' . implode(') ' . $op . ' (', $cond) . ')';
228 public function export()
231 foreach ($this->children
as $child) {
232 $export[] = $child->export();
234 return array('children' => $export);
239 // {{{ class PFC_True
240 class PFC_True
implements PlFilterCondition
242 public function buildCondition(PlFilter
$uf)
244 return self
::COND_TRUE
;
247 public function export()
249 return array('type' => 'true');
254 // {{{ class PFC_False
255 class PFC_False
implements PlFilterCondition
257 public function buildCondition(PlFilter
$uf)
259 return self
::COND_FALSE
;
262 public function export()
264 return array('type' => 'false');
270 class PFC_Not
extends PFC_OneChild
272 public function buildCondition(PlFilter
$uf)
274 $val = $this->child
->buildCondition($uf);
275 if ($val == self
::COND_TRUE
) {
276 return self
::COND_FALSE
;
277 } else if ($val == self
::COND_FALSE
) {
278 return self
::COND_TRUE
;
280 return 'NOT (' . $val . ')';
284 public function export()
286 $export = parent
::export();
287 $export['type'] = 'not';
294 class PFC_And
extends PFC_NChildren
296 public function buildCondition(PlFilter
$uf)
298 if (empty($this->children
)) {
299 return self
::COND_FALSE
;
301 $true = self
::COND_FALSE
;
303 foreach ($this->children
as $child) {
304 $val = $child->buildCondition($uf);
305 if ($val == self
::COND_TRUE
) {
306 $true = self
::COND_TRUE
;
307 } else if ($val == self
::COND_FALSE
) {
308 return self
::COND_FALSE
;
313 return $this->catConds($conds, 'AND', $true);
317 public function export() {
318 $export = parent
::export();
319 $export['type'] = 'and';
326 class PFC_Or
extends PFC_NChildren
328 public function buildCondition(PlFilter
$uf)
330 if (empty($this->children
)) {
331 return self
::COND_TRUE
;
333 $true = self
::COND_TRUE
;
335 foreach ($this->children
as $child) {
336 $val = $child->buildCondition($uf);
337 if ($val == self
::COND_TRUE
) {
338 return self
::COND_TRUE
;
339 } else if ($val == self
::COND_FALSE
) {
340 $true = self
::COND_FALSE
;
345 return $this->catConds($conds, 'OR', $true);
349 public function export() {
350 $export = parent
::export();
351 $export['type'] = 'or';
357 // {{{ class PlFilter
358 abstract class PlFilter
implements PlExportable
360 /** Filters objects matching the PlFilter
361 * @param $objects The objects to filter
362 * @param $limit The portion of the matching objects to show
364 public abstract function filter(array $objects, $limit = null
);
366 public abstract function setCondition(PlFilterCondition
$cond);
368 public abstract function addSort(PlFilterOrder
$sort);
370 public abstract function getTotalCount();
372 /** Whether this PlFilter can return grouped results through
373 * $this->getGroups();
375 public abstract function hasGroups();
377 /** Used to retrieve value/amount resulting from grouping by the first
380 public abstract function getGroups();
382 /** Get objects, selecting only those within a limit
383 * @param $limit The portion of the matching objects to select
385 public abstract function get($limit = null
);
387 /** Get ids, selecting only those within a limit
388 * @param $limit The portion of the matching objects to select
390 public function getIds($limit = null
)
392 return $this->get($limit);
395 /** PRIVATE FUNCTIONS
398 /** List of metas to replace in joins:
399 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
401 * "$ME" => "joined table alias" is always added to these.
403 protected $joinMetas = array();
405 protected $joinMethods = array();
407 /** Build the 'join' part of the query
408 * This function will call all methods declared in self::$joinMethods
409 * to get an array of PlSqlJoin objects to merge
411 protected function buildJoins()
414 foreach ($this->joinMethods
as $method) {
415 $joins = array_merge($joins, $this->$method());
417 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);
423 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: