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 ***************************************************************************/
24 private $count = null
;
27 public function __construct($count = null
, $from = null
)
29 $this->count
= $count;
33 public function getSql()
35 if (!is_null($this->count
)) {
36 if (!is_null($this->from
) && $this->from
!= 0) {
37 return XDB
::format('LIMIT {?}, {?}', (int)$this->from
, (int)$this->count
);
39 return XDB
::format('LIMIT {?}', (int)$this->count
);
52 const MODE_LEFT
= 'LEFT';
53 const MODE_RIGHT
= 'RIGHT';
54 const MODE_INNER
= 'INNER';
56 public function __construct($mode, $table, $condition)
58 if ($mode != self
::MODE_LEFT
&& $mode != self
::MODE_RIGHT
&& $mode != self
::MODE_INNER
) {
59 Platal
::page()->kill("Join mode error : unknown mode $mode");
63 $this->table
= $table;
64 $this->condition
= $condition;
67 public function mode()
72 public function table()
77 public function condition()
79 return $this->condition
;
82 /** Replace all "metas" in the condition with their translation.
83 * $ME always becomes the alias of the table
84 * @param $key The name the joined table will have in the final query
85 * @param $joinMetas An array of meta => conversion to apply to the condition
87 public function replaceJoinMetas($key, $joinMetas = array())
89 $joinMetas['$ME'] = $key;
90 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition
);
93 /** Create a join command from an array of PlSqlJoin
94 * @param $joins The list of 'join' to convert into an SQL query
95 * @param $joinMetas An array of ('$META' => 'conversion') to apply to the joins.
97 public static function formatJoins(array $joins, array $joinMetas)
100 foreach ($joins as $key => $join) {
101 if (! ($join instanceof PlSqlJoin
)) {
102 Platal
::page()->kill("Error: not a join: $join");
104 $mode = $join->mode();
105 $table = $join->table();
106 $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key;
107 if ($join->condition() != null
) {
108 $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')';
116 abstract class PlFilter
118 /** Filters objects matching the PlFilter
119 * @param $objects The objects to filter
120 * @param $limit The portion of the matching objects to show
122 public abstract function filter(array $objects, PlLimit
&$limit);
124 public abstract function setCondition(PlFilterCondition
&$cond);
126 public abstract function addSort(PlFilterOrder
&$sort);
128 public abstract function getTotalCount();
130 /** Get objects, selecting only those within a limit
131 * @param $limit The portion of the matching objects to select
133 public abstract function get(PlLimit
&$limit);
135 /** PRIVATE FUNCTIONS
138 /** List of metas to replace in joins:
139 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
141 * "$ME" => "joined table alias" is always added to these.
143 private $joinMetas = array();
145 /** Build the 'join' part of the query
146 * This function will call all methods declared in self::$joinMethods
147 * to get an array of PlSqlJoin objects to merge
149 private function buildJoins()
152 foreach ($this->$joinMethods as $method) {
153 $joins = array_merge($joins, $this->$method());
155 return PlSqlJoin
::formatJoins($joins, $this->$joinMetas);