Commit | Line | Data |
---|---|---|
285fb262 RB |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2010 Polytechnique.org * | |
4 | * http://opensource.polytechnique.org/ * | |
5 | * * | |
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. * | |
10 | * * | |
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. * | |
15 | * * | |
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 * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
bd9b36fe RB |
22 | class PlLimit |
23 | { | |
24 | private $count = null; | |
25 | private $from = null; | |
26 | ||
27 | public function __construct($count = null, $from = null) | |
28 | { | |
29 | $this->count = $count; | |
30 | $this->from = $from; | |
31 | } | |
32 | ||
33 | public function getSql() | |
34 | { | |
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); | |
38 | } else { | |
39 | return XDB::format('LIMIT {?}', (int)$this->count); | |
40 | } | |
41 | } | |
42 | return ''; | |
43 | } | |
44 | } | |
45 | ||
285fb262 RB |
46 | class PlSqlJoin |
47 | { | |
48 | private $mode; | |
49 | private $table; | |
50 | private $condition; | |
51 | ||
52 | const MODE_LEFT = 'LEFT'; | |
53 | const MODE_RIGHT = 'RIGHT'; | |
54 | const MODE_INNER = 'INNER'; | |
55 | ||
56 | public function __construct($mode, $table, $condition) | |
57 | { | |
58 | if ($mode != self::MODE_LEFT && $mode != self::MODE_RIGHT && $mode != self::MODE_INNER) { | |
59 | Platal::page()->kill("Join mode error : unknown mode $mode"); | |
60 | return; | |
61 | } | |
62 | $this->mode = $mode; | |
63 | $this->table = $table; | |
64 | $this->condition = $condition; | |
65 | } | |
66 | ||
67 | public function mode() | |
68 | { | |
69 | return $this->mode; | |
70 | } | |
71 | ||
72 | public function table() | |
73 | { | |
74 | return $this->table; | |
75 | } | |
76 | ||
bd9b36fe | 77 | public function condition() |
285fb262 RB |
78 | { |
79 | return $this->condition; | |
80 | } | |
285fb262 | 81 | |
bd9b36fe RB |
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 | |
86 | */ | |
87 | public function replaceJoinMetas($key, $joinMetas = array()) | |
285fb262 | 88 | { |
bd9b36fe RB |
89 | $joinMetas['$ME'] = $key; |
90 | return str_replace(array_keys($joinMetas), $joinMetas, $this->condition); | |
285fb262 RB |
91 | } |
92 | ||
bd9b36fe RB |
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. | |
96 | */ | |
97 | public static function formatJoins(array $joins, array $joinMetas) | |
285fb262 RB |
98 | { |
99 | $str = ''; | |
100 | foreach ($joins as $key => $join) { | |
bd9b36fe RB |
101 | if (! ($join instanceof PlSqlJoin)) { |
102 | Platal::page()->kill("Error: not a join: $join"); | |
285fb262 RB |
103 | } |
104 | $mode = $join->mode(); | |
105 | $table = $join->table(); | |
106 | $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key; | |
107 | if ($join->condition() != null) { | |
bd9b36fe | 108 | $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')'; |
285fb262 RB |
109 | } |
110 | $str .= "\n"; | |
111 | } | |
112 | return $str; | |
113 | } | |
bd9b36fe RB |
114 | } |
115 | ||
f66f26e2 RB |
116 | abstract class PlFilterOrder |
117 | { | |
118 | protected $desc = false; | |
119 | public function __construct($desc = false) | |
120 | { | |
121 | $this->desc = $desc; | |
122 | } | |
123 | ||
124 | public function toggleDesc() | |
125 | { | |
126 | $this->desc = !$desc; | |
127 | } | |
128 | ||
129 | public function setDescending($desc = true) | |
130 | { | |
131 | $this->desc = $desc; | |
132 | } | |
133 | ||
134 | public function buildSort(PlFilter &$pf) | |
135 | { | |
136 | $sel = $this->getSortTokens($pf); | |
137 | if (!is_array($sel)) { | |
138 | $sel = array($sel); | |
139 | } | |
140 | if ($this->desc) { | |
141 | foreach ($sel as $k => $s) { | |
142 | $sel[$k] = $s . ' DESC'; | |
143 | } | |
144 | } | |
145 | return $sel; | |
146 | } | |
147 | ||
148 | // abstract protected function getSortTokens(&$pf); | |
149 | } | |
150 | ||
151 | // {{{ interface PlFilterCondition | |
152 | interface PlFilterCondition | |
153 | { | |
154 | const COND_TRUE = 'TRUE'; | |
155 | const COND_FALSE = 'FALSE'; | |
156 | ||
157 | public function buildCondition(PlFilter &$pf); | |
158 | } | |
159 | // }}} | |
160 | ||
161 | // {{{ class PFC_OneChild | |
162 | abstract class PFC_OneChild implements PlFilterCondition | |
163 | { | |
164 | protected $child; | |
165 | ||
166 | public function __construct(&$child = null) | |
167 | { | |
168 | if (!is_null($child) && ($child instanceof PlFilterCondition)) { | |
169 | $this->setChild($child); | |
170 | } | |
171 | } | |
172 | ||
173 | public function setChild(PlFilterCondition &$cond) | |
174 | { | |
175 | $this->child =& $cond; | |
176 | } | |
177 | } | |
178 | // }}} | |
179 | ||
180 | // {{{ class PFC_NChildren | |
181 | abstract class PFC_NChildren implements PlFilterCondition | |
182 | { | |
183 | protected $children = array(); | |
184 | ||
185 | public function __construct() | |
186 | { | |
187 | $children = func_get_args(); | |
188 | foreach ($children as &$child) { | |
189 | if (!is_null($child) && ($child instanceof PlFilterCondition)) { | |
190 | $this->addChild($child); | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | public function addChild(PlFilterCondition &$cond) | |
196 | { | |
197 | $this->children[] =& $cond; | |
198 | } | |
199 | ||
200 | protected function catConds(array $cond, $op, $fallback) | |
201 | { | |
202 | if (count($cond) == 0) { | |
203 | return $fallback; | |
204 | } else if (count($cond) == 1) { | |
205 | return $cond[0]; | |
206 | } else { | |
207 | return '(' . implode(') ' . $op . ' (', $cond) . ')'; | |
208 | } | |
209 | } | |
210 | } | |
211 | // }}} | |
212 | ||
213 | // {{{ class PFC_True | |
214 | class PFC_True implements PlFilterCondition | |
215 | { | |
216 | public function buildCondition(PlFilter &$uf) | |
217 | { | |
218 | return self::COND_TRUE; | |
219 | } | |
220 | } | |
221 | // }}} | |
222 | ||
223 | // {{{ class PFC_False | |
224 | class PFC_False implements PlFilterCondition | |
225 | { | |
226 | public function buildCondition(PlFilter &$uf) | |
227 | { | |
228 | return self::COND_FALSE; | |
229 | } | |
230 | } | |
231 | // }}} | |
232 | ||
233 | // {{{ class PFC_Not | |
234 | class PFC_Not extends PFC_OneChild | |
235 | { | |
236 | public function buildCondition(PlFilter &$uf) | |
237 | { | |
238 | $val = $this->child->buildCondition($uf); | |
239 | if ($val == self::COND_TRUE) { | |
240 | return self::COND_FALSE; | |
241 | } else if ($val == self::COND_FALSE) { | |
242 | return self::COND_TRUE; | |
243 | } else { | |
244 | return 'NOT (' . $val . ')'; | |
245 | } | |
246 | } | |
247 | } | |
248 | // }}} | |
249 | ||
250 | // {{{ class PFC_And | |
251 | class PFC_And extends PFC_NChildren | |
252 | { | |
253 | public function buildCondition(PlFilter &$uf) | |
254 | { | |
255 | if (empty($this->children)) { | |
256 | return self::COND_FALSE; | |
257 | } else { | |
258 | $true = self::COND_FALSE; | |
259 | $conds = array(); | |
260 | foreach ($this->children as &$child) { | |
261 | $val = $child->buildCondition($uf); | |
262 | if ($val == self::COND_TRUE) { | |
263 | $true = self::COND_TRUE; | |
264 | } else if ($val == self::COND_FALSE) { | |
265 | return self::COND_FALSE; | |
266 | } else { | |
267 | $conds[] = $val; | |
268 | } | |
269 | } | |
270 | return $this->catConds($conds, 'AND', $true); | |
271 | } | |
272 | } | |
273 | } | |
274 | // }}} | |
275 | ||
276 | // {{{ class PFC_Or | |
277 | class PFC_Or extends PFC_NChildren | |
278 | { | |
279 | public function buildCondition(PlFilter &$uf) | |
280 | { | |
281 | if (empty($this->children)) { | |
282 | return self::COND_TRUE; | |
283 | } else { | |
284 | $true = self::COND_TRUE; | |
285 | $conds = array(); | |
286 | foreach ($this->children as &$child) { | |
287 | $val = $child->buildCondition($uf); | |
288 | if ($val == self::COND_TRUE) { | |
289 | return self::COND_TRUE; | |
290 | } else if ($val == self::COND_FALSE) { | |
291 | $true = self::COND_FALSE; | |
292 | } else { | |
293 | $conds[] = $val; | |
294 | } | |
295 | } | |
296 | return $this->catConds($conds, 'OR', $true); | |
297 | } | |
298 | } | |
299 | } | |
300 | // }}} | |
301 | ||
302 | ||
bd9b36fe RB |
303 | abstract class PlFilter |
304 | { | |
305 | /** Filters objects matching the PlFilter | |
306 | * @param $objects The objects to filter | |
307 | * @param $limit The portion of the matching objects to show | |
308 | */ | |
309 | public abstract function filter(array $objects, PlLimit &$limit); | |
310 | ||
311 | public abstract function setCondition(PlFilterCondition &$cond); | |
312 | ||
313 | public abstract function addSort(PlFilterOrder &$sort); | |
314 | ||
315 | public abstract function getTotalCount(); | |
316 | ||
317 | /** Get objects, selecting only those within a limit | |
318 | * @param $limit The portion of the matching objects to select | |
319 | */ | |
320 | public abstract function get(PlLimit &$limit); | |
321 | ||
322 | /** PRIVATE FUNCTIONS | |
323 | */ | |
324 | ||
325 | /** List of metas to replace in joins: | |
326 | * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins' | |
327 | * | |
328 | * "$ME" => "joined table alias" is always added to these. | |
329 | */ | |
330 | private $joinMetas = array(); | |
285fb262 | 331 | |
bd9b36fe RB |
332 | /** Build the 'join' part of the query |
333 | * This function will call all methods declared in self::$joinMethods | |
334 | * to get an array of PlSqlJoin objects to merge | |
335 | */ | |
285fb262 RB |
336 | private function buildJoins() |
337 | { | |
338 | $joins = array(); | |
bd9b36fe | 339 | foreach ($this->$joinMethods as $method) { |
285fb262 RB |
340 | $joins = array_merge($joins, $this->$method()); |
341 | } | |
bd9b36fe | 342 | return PlSqlJoin::formatJoins($joins, $this->$joinMetas); |
285fb262 RB |
343 | } |
344 | ||
345 | } | |
346 | ||
347 | ?> |