Fix SUID.
[platal.git] / classes / plsqljoin.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 PlSqlJoin
23 class PlSqlJoin
24 {
25 private $mode;
26 private $table;
27 private $condition;
28
29 const MODE_LEFT = 'LEFT';
30 const MODE_RIGHT = 'RIGHT';
31 const MODE_INNER = 'INNER';
32
33 private function __construct($mode, $params)
34 {
35 $table = array_shift($params);
36 $condition = XDB::prepare($params);
37 if ($mode != self::MODE_LEFT && $mode != self::MODE_RIGHT && $mode != self::MODE_INNER) {
38 Platal::page()->kill("Join mode error: unknown mode $mode");
39 return;
40 }
41 $this->mode = $mode;
42 $this->table = $table;
43 $this->condition = $condition;
44 }
45
46 public function mode()
47 {
48 return $this->mode;
49 }
50
51 public function table()
52 {
53 return $this->table;
54 }
55
56 public function condition()
57 {
58 return $this->condition;
59 }
60
61 /** Replace all "metas" in the condition with their translation.
62 * $ME always becomes the alias of the table
63 * @param $key The name the joined table will have in the final query
64 * @param $joinMetas An array of meta => conversion to apply to the condition
65 */
66 public function replaceJoinMetas($key, $joinMetas = array())
67 {
68 $joinMetas['$ME'] = $key;
69 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition);
70 }
71
72 /** Create a join command from an array of PlSqlJoin
73 * @param $joins The list of 'join' to convert into an SQL query
74 * @param $joinMetas An array of ('$META' => 'conversion') to apply to the joins.
75 */
76 public static function formatJoins(array $joins, array $joinMetas)
77 {
78 $str = '';
79 foreach ($joins as $key => $join) {
80 if (!($join instanceof PlSqlJoin)) {
81 Platal::page()->kill("Error: not a join: $join");
82 }
83 $mode = $join->mode();
84 $table = $join->table();
85 $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key;
86 if ($join->condition() != null) {
87 $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')';
88 }
89 $str .= "\n";
90 }
91 return $str;
92 }
93
94 /** Build a left join
95 * @param table The name of the table.
96 * @param condition The condition of the jointure
97 */
98 public static function left()
99 {
100 $params = func_get_args();
101 return new PlSqlJoin(self::MODE_LEFT, $params);
102 }
103
104 /** Build a right join
105 * @param table The name of the table.
106 * @param condition The condition of the jointure
107 */
108 public static function right()
109 {
110 $params = func_get_args();
111 return new PlSqlJoin(self::MODE_RIGHT, $params);
112 }
113
114 /** Build a inner join
115 * @param table The name of the table.
116 * @param condition The condition of the jointure
117 */
118 public static function inner()
119 {
120 $params = func_get_args();
121 return new PlSqlJoin(self::MODE_INNER, $params);
122 }
123 }
124 // }}}
125
126 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
127 ?>