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 ***************************************************************************/
22 // {{{ class PlSqlJoin
29 const MODE_LEFT
= 'LEFT';
30 const MODE_RIGHT
= 'RIGHT';
31 const MODE_INNER
= 'INNER';
33 private function __construct($mode, $params)
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");
42 $this->table
= $table;
43 $this->condition
= $condition;
46 public function mode()
51 public function table()
56 public function condition()
58 return $this->condition
;
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
66 public function replaceJoinMetas($key, $joinMetas = array())
68 $joinMetas['$ME'] = $key;
69 return str_replace(array_keys($joinMetas), $joinMetas, $this->condition
);
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.
76 public static function formatJoins(array $joins, array $joinMetas)
79 foreach ($joins as $key => $join) {
80 if (!($join instanceof PlSqlJoin
)) {
81 Platal
::page()->kill("Error: not a join: $join");
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) . ')';
95 * @param table The name of the table.
96 * @param condition The condition of the jointure
98 public static function left()
100 $params = func_get_args();
101 return new PlSqlJoin(self
::MODE_LEFT
, $params);
104 /** Build a right join
105 * @param table The name of the table.
106 * @param condition The condition of the jointure
108 public static function right()
110 $params = func_get_args();
111 return new PlSqlJoin(self
::MODE_RIGHT
, $params);
114 /** Build a inner join
115 * @param table The name of the table.
116 * @param condition The condition of the jointure
118 public static function inner()
120 $params = func_get_args();
121 return new PlSqlJoin(self
::MODE_INNER
, $params);
126 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: