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 ***************************************************************************/
25 private $count = null
;
28 public function __construct($count = null
, $from = null
)
30 $this->count
= $count;
34 public function getSql()
36 if (!is_null($this->count
) && $this->count
!= 0) {
37 if (!is_null($this->from
) && $this->from
!= 0) {
38 return XDB
::format('LIMIT {?}, {?}', (int)$this->from
, (int)$this->count
);
40 return XDB
::format('LIMIT {?}', (int)$this->count
);
48 // {{{ class PlSqlJoin
55 const MODE_LEFT
= 'LEFT';
56 const MODE_RIGHT
= 'RIGHT';
57 const MODE_INNER
= 'INNER';
59 private function __construct($mode, $params)
61 $table = array_shift($params);
62 $condition = call_user_func_array(array('XDB', 'format'), $params);
63 if ($mode != self
::MODE_LEFT
&& $mode != self
::MODE_RIGHT
&& $mode != self
::MODE_INNER
) {
64 Platal
::page()->kill("Join mode error: unknown mode $mode");
68 $this->table
= $table;
69 $this->condition
= $condition;
72 public function mode()
77 public function table()
82 public function condition()
84 return $this->condition
;
87 /** Replace all "metas" in the condition with their translation.
88 * $ME always becomes the alias of the table
89 * @param $key The name the joined table will have in the final query
90 * @param $joinMetas An array of meta => conversion to apply to the condition
92 public function replaceJoinMetas($key, $joinMetas = array())
94 $joinMetas['$ME'] = $key;
95 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition
);
98 /** Create a join command from an array of PlSqlJoin
99 * @param $joins The list of 'join' to convert into an SQL query
100 * @param $joinMetas An array of ('$META' => 'conversion') to apply to the joins.
102 public static function formatJoins(array $joins, array $joinMetas)
105 foreach ($joins as $key => $join) {
106 if (!($join instanceof PlSqlJoin
)) {
107 Platal
::page()->kill("Error: not a join: $join");
109 $mode = $join->mode();
110 $table = $join->table();
111 $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key;
112 if ($join->condition() != null
) {
113 $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')';
120 /** Build a left join
121 * @param table The name of the table.
122 * @param condition The condition of the jointure
124 public static function left()
126 $params = func_get_args();
127 return new PlSqlJoin(self
::MODE_LEFT
, $params);
130 /** Build a right join
131 * @param table The name of the table.
132 * @param condition The condition of the jointure
134 public static function right()
136 $params = func_get_args();
137 return new PlSqlJoin(self
::MODE_RIGHT
, $params);
140 /** Build a inner join
141 * @param table The name of the table.
142 * @param condition The condition of the jointure
144 public static function inner()
146 $params = func_get_args();
147 return new PlSqlJoin(self
::MODE_INNER
, $params);
152 // {{{ class PlFilterOrder
153 abstract class PlFilterOrder
155 protected $desc = false
;
156 public function __construct($desc = false
)
161 public function toggleDesc()
163 $this->desc
= !$desc;
166 public function setDescending($desc = true
)
171 public function buildSort(PlFilter
&$pf)
173 $sel = $this->getSortTokens($pf);
174 if (!is_array($sel)) {
178 foreach ($sel as $k => $s) {
179 $sel[$k] = $s . ' DESC';
185 abstract protected function getSortTokens(PlFilter
&$pf);
189 // {{{ class PFO_Random
190 class PFO_Random
extends PlFilterOrder
192 private $seed = null
;
194 public function __construct($seed = null
, $desc = false
)
196 parent
::__construct($desc);
200 protected function getSortTokens(PlFilter
&$pf)
202 if ($this->seed
== null
) {
205 return XDB
::format('RAND({?})', $this->seed
);
211 // {{{ interface PlFilterCondition
212 interface PlFilterCondition
214 const COND_TRUE
= 'TRUE';
215 const COND_FALSE
= 'FALSE';
217 public function buildCondition(PlFilter
&$pf);
221 // {{{ class PFC_OneChild
222 abstract class PFC_OneChild
implements PlFilterCondition
226 public function __construct(&$child = null
)
228 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
229 $this->setChild($child);
233 public function setChild(PlFilterCondition
&$cond)
235 $this->child
=& $cond;
240 // {{{ class PFC_NChildren
241 abstract class PFC_NChildren
implements PlFilterCondition
243 protected $children = array();
245 public function __construct()
247 $children = func_get_args();
248 foreach ($children as &$child) {
249 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
250 $this->addChild($child);
255 public function addChild(PlFilterCondition
&$cond)
257 $this->children
[] =& $cond;
260 protected function catConds(array $cond, $op, $fallback)
262 if (count($cond) == 0) {
264 } else if (count($cond) == 1) {
267 return '(' . implode(') ' . $op . ' (', $cond) . ')';
273 // {{{ class PFC_True
274 class PFC_True
implements PlFilterCondition
276 public function buildCondition(PlFilter
&$uf)
278 return self
::COND_TRUE
;
283 // {{{ class PFC_False
284 class PFC_False
implements PlFilterCondition
286 public function buildCondition(PlFilter
&$uf)
288 return self
::COND_FALSE
;
294 class PFC_Not
extends PFC_OneChild
296 public function buildCondition(PlFilter
&$uf)
298 $val = $this->child
->buildCondition($uf);
299 if ($val == self
::COND_TRUE
) {
300 return self
::COND_FALSE
;
301 } else if ($val == self
::COND_FALSE
) {
302 return self
::COND_TRUE
;
304 return 'NOT (' . $val . ')';
311 class PFC_And
extends PFC_NChildren
313 public function buildCondition(PlFilter
&$uf)
315 if (empty($this->children
)) {
316 return self
::COND_FALSE
;
318 $true = self
::COND_FALSE
;
320 foreach ($this->children
as &$child) {
321 $val = $child->buildCondition($uf);
322 if ($val == self
::COND_TRUE
) {
323 $true = self
::COND_TRUE
;
324 } else if ($val == self
::COND_FALSE
) {
325 return self
::COND_FALSE
;
330 return $this->catConds($conds, 'AND', $true);
337 class PFC_Or
extends PFC_NChildren
339 public function buildCondition(PlFilter
&$uf)
341 if (empty($this->children
)) {
342 return self
::COND_TRUE
;
344 $true = self
::COND_TRUE
;
346 foreach ($this->children
as &$child) {
347 $val = $child->buildCondition($uf);
348 if ($val == self
::COND_TRUE
) {
349 return self
::COND_TRUE
;
350 } else if ($val == self
::COND_FALSE
) {
351 $true = self
::COND_FALSE
;
356 return $this->catConds($conds, 'OR', $true);
362 // {{{ class PlFilter
363 abstract class PlFilter
365 /** Filters objects matching the PlFilter
366 * @param $objects The objects to filter
367 * @param $limit The portion of the matching objects to show
369 public abstract function filter(array $objects, $limit = null
);
371 public abstract function setCondition(PlFilterCondition
&$cond);
373 public abstract function addSort(PlFilterOrder
&$sort);
375 public abstract function getTotalCount();
377 /** Get objects, selecting only those within a limit
378 * @param $limit The portion of the matching objects to select
380 public abstract function get($limit = null
);
382 /** PRIVATE FUNCTIONS
385 /** List of metas to replace in joins:
386 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
388 * "$ME" => "joined table alias" is always added to these.
390 protected $joinMetas = array();
392 protected $joinMethods = array();
394 /** Build the 'join' part of the query
395 * This function will call all methods declared in self::$joinMethods
396 * to get an array of PlSqlJoin objects to merge
398 protected function buildJoins()
401 foreach ($this->joinMethods
as $method) {
402 $joins = array_merge($joins, $this->$method());
404 return PlSqlJoin
::formatJoins($joins, $this->joinMetas
);
410 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: