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 PlSqlJoin
57 const MODE_LEFT
= 'LEFT';
58 const MODE_RIGHT
= 'RIGHT';
59 const MODE_INNER
= 'INNER';
61 private function __construct($mode, $params)
63 $table = array_shift($params);
64 $condition = call_user_func_array(array('XDB', 'format'), $params);
65 if ($mode != self
::MODE_LEFT
&& $mode != self
::MODE_RIGHT
&& $mode != self
::MODE_INNER
) {
66 Platal
::page()->kill("Join mode error: unknown mode $mode");
70 $this->table
= $table;
71 $this->condition
= $condition;
74 public function mode()
79 public function table()
84 public function condition()
86 return $this->condition
;
89 /** Replace all "metas" in the condition with their translation.
90 * $ME always becomes the alias of the table
91 * @param $key The name the joined table will have in the final query
92 * @param $joinMetas An array of meta => conversion to apply to the condition
94 public function replaceJoinMetas($key, $joinMetas = array())
96 $joinMetas['$ME'] = $key;
97 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition
);
100 /** Create a join command from an array of PlSqlJoin
101 * @param $joins The list of 'join' to convert into an SQL query
102 * @param $joinMetas An array of ('$META' => 'conversion') to apply to the joins.
104 public static function formatJoins(array $joins, array $joinMetas)
107 foreach ($joins as $key => $join) {
108 if (!($join instanceof PlSqlJoin
)) {
109 Platal
::page()->kill("Error: not a join: $join");
111 $mode = $join->mode();
112 $table = $join->table();
113 $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key;
114 if ($join->condition() != null
) {
115 $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')';
122 /** Build a left join
123 * @param table The name of the table.
124 * @param condition The condition of the jointure
126 public static function left()
128 $params = func_get_args();
129 return new PlSqlJoin(self
::MODE_LEFT
, $params);
132 /** Build a right join
133 * @param table The name of the table.
134 * @param condition The condition of the jointure
136 public static function right()
138 $params = func_get_args();
139 return new PlSqlJoin(self
::MODE_RIGHT
, $params);
142 /** Build a inner join
143 * @param table The name of the table.
144 * @param condition The condition of the jointure
146 public static function inner()
148 $params = func_get_args();
149 return new PlSqlJoin(self
::MODE_INNER
, $params);
154 // {{{ class PlFilterOrder
155 /** Base class for ordering results of a query.
156 * Parameters for the ordering must be given to the constructor ($desc for a
158 * The getSortTokens function is used to get actual ordering part of the query.
160 abstract class PlFilterOrder
162 protected $desc = false
;
163 public function __construct($desc = false
)
166 $this->_tokens
= null
;
169 public function toggleDesc()
171 $this->desc
= !$this->desc
;
174 public function setDescending($desc = true
)
179 public function buildSort(PlFilter
&$pf)
181 $sel = $this->getSortTokens($pf);
182 $this->_tokens
= $sel;
183 if (!is_array($sel)) {
187 foreach ($sel as $k => $s) {
188 $sel[$k] = $s . ' DESC';
194 /** This function must return the tokens to use for ordering
195 * @param &$pf The PlFilter whose results must be ordered
196 * @return The name of the field to use for ordering results
198 abstract protected function getSortTokens(PlFilter
&$pf);
202 // {{{ class PlFilterGroupableOrder
203 /** Extension of a PlFilterOrder, for orders where the value on which ordering
204 * is done could be used for grouping results (promo, country, ...)
206 class PlFilterGroupableOrder
extends PlFilterOrder
208 /** This function will be called when trying to retrieve groups;
209 * the returned token will be used to group the values.
210 * It will always be called AFTER getSortTokens().
212 public function getGroupToken(PlFilter
&$pf)
214 return $this->_tokens
;
219 // {{{ class PFO_Random
220 class PFO_Random
extends PlFilterOrder
222 private $seed = null
;
224 public function __construct($seed = null
, $desc = false
)
226 parent
::__construct($desc);
230 protected function getSortTokens(PlFilter
&$pf)
232 if ($this->seed
== null
) {
235 return XDB
::format('RAND({?})', $this->seed
);
241 // {{{ interface PlFilterCondition
242 interface PlFilterCondition
244 const COND_TRUE
= 'TRUE';
245 const COND_FALSE
= 'FALSE';
247 public function buildCondition(PlFilter
&$pf);
251 // {{{ class PFC_OneChild
252 abstract class PFC_OneChild
implements PlFilterCondition
256 public function __construct(&$child = null
)
258 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
259 $this->setChild($child);
263 public function setChild(PlFilterCondition
&$cond)
265 $this->child
=& $cond;
270 // {{{ class PFC_NChildren
271 abstract class PFC_NChildren
implements PlFilterCondition
273 protected $children = array();
275 public function __construct()
277 $this->addChildren(pl_flatten(func_get_args()));
280 public function addChildren(array $conds)
282 foreach ($conds as &$cond) {
283 if (!is_null($cond) && ($cond instanceof PlFilterCondition
)) {
284 $this->addChild($cond);
289 public function addChild(PlFilterCondition
&$cond)
291 $this->children
[] =& $cond;
294 protected function catConds(array $cond, $op, $fallback)
296 if (count($cond) == 0) {
298 } else if (count($cond) == 1) {
301 return '(' . implode(') ' . $op . ' (', $cond) . ')';
307 // {{{ class PFC_True
308 class PFC_True
implements PlFilterCondition
310 public function buildCondition(PlFilter
&$uf)
312 return self
::COND_TRUE
;
317 // {{{ class PFC_False
318 class PFC_False
implements PlFilterCondition
320 public function buildCondition(PlFilter
&$uf)
322 return self
::COND_FALSE
;
328 class PFC_Not
extends PFC_OneChild
330 public function buildCondition(PlFilter
&$uf)
332 $val = $this->child
->buildCondition($uf);
333 if ($val == self
::COND_TRUE
) {
334 return self
::COND_FALSE
;
335 } else if ($val == self
::COND_FALSE
) {
336 return self
::COND_TRUE
;
338 return 'NOT (' . $val . ')';
345 class PFC_And
extends PFC_NChildren
347 public function buildCondition(PlFilter
&$uf)
349 if (empty($this->children
)) {
350 return self
::COND_FALSE
;
352 $true = self
::COND_FALSE
;
354 foreach ($this->children
as &$child) {
355 $val = $child->buildCondition($uf);
356 if ($val == self
::COND_TRUE
) {
357 $true = self
::COND_TRUE
;
358 } else if ($val == self
::COND_FALSE
) {
359 return self
::COND_FALSE
;
364 return $this->catConds($conds, 'AND', $true);
371 class PFC_Or
extends PFC_NChildren
373 public function buildCondition(PlFilter
&$uf)
375 if (empty($this->children
)) {
376 return self
::COND_TRUE
;
378 $true = self
::COND_TRUE
;
380 foreach ($this->children
as &$child) {
381 $val = $child->buildCondition($uf);
382 if ($val == self
::COND_TRUE
) {
383 return self
::COND_TRUE
;
384 } else if ($val == self
::COND_FALSE
) {
385 $true = self
::COND_FALSE
;
390 return $this->catConds($conds, 'OR', $true);
396 // {{{ class PlFilter
397 abstract class PlFilter
399 /** Filters objects matching the PlFilter
400 * @param $objects The objects to filter
401 * @param $limit The portion of the matching objects to show
403 public abstract function filter(array $objects, $limit = null
);
405 public abstract function setCondition(PlFilterCondition
&$cond);
407 public abstract function addSort(PlFilterOrder
&$sort);
409 public abstract function getTotalCount();
411 /** Whether this PlFilter can return grouped results through
412 * $this->getGroups();
414 public abstract function hasGroups();
416 /** Used to retrieve value/amount resulting from grouping by the first
419 public abstract function getGroups();
421 /** Get objects, selecting only those within a limit
422 * @param $limit The portion of the matching objects to select
424 public abstract function get($limit = null
);
426 /** PRIVATE FUNCTIONS
429 /** List of metas to replace in joins:
430 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
432 * "$ME" => "joined table alias" is always added to these.
434 protected $joinMetas = array();
436 protected $joinMethods = array();
438 /** Build the 'join' part of the query
439 * This function will call all methods declared in self::$joinMethods
440 * to get an array of PlSqlJoin objects to merge
442 protected function buildJoins()
445 foreach ($this->joinMethods
as $method) {
446 $joins = array_merge($joins, $this->$method());
448 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);
454 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: