Make PlSet PHP-compatible...
[platal.git] / classes / plfilter.php
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
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
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
77 public function condition()
78 {
79 return $this->condition;
80 }
81
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())
88 {
89 $joinMetas['$ME'] = $key;
90 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition);
91 }
92
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)
98 {
99 $str = '';
100 foreach ($joins as $key => $join) {
101 if (! ($join instanceof PlSqlJoin)) {
102 Platal::page()->kill("Error: not a join: $join");
103 }
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) . ')';
109 }
110 $str .= "\n";
111 }
112 return $str;
113 }
114 }
115
116 abstract class PlFilter
117 {
118 /** Filters objects matching the PlFilter
119 * @param $objects The objects to filter
120 * @param $limit The portion of the matching objects to show
121 */
122 public abstract function filter(array $objects, PlLimit &$limit);
123
124 public abstract function setCondition(PlFilterCondition &$cond);
125
126 public abstract function addSort(PlFilterOrder &$sort);
127
128 public abstract function getTotalCount();
129
130 /** Get objects, selecting only those within a limit
131 * @param $limit The portion of the matching objects to select
132 */
133 public abstract function get(PlLimit &$limit);
134
135 /** PRIVATE FUNCTIONS
136 */
137
138 /** List of metas to replace in joins:
139 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
140 *
141 * "$ME" => "joined table alias" is always added to these.
142 */
143 private $joinMetas = array();
144
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
148 */
149 private function buildJoins()
150 {
151 $joins = array();
152 foreach ($this->$joinMethods as $method) {
153 $joins = array_merge($joins, $this->$method());
154 }
155 return PlSqlJoin::formatJoins($joins, $this->$joinMetas);
156 }
157
158 }
159
160 ?>