Add pl_core_include to build the path to a core include file.
[platal.git] / classes / xdb.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
0337d704 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
08cce2ff 22class XDB
0337d704 23{
32d9ae72 24 private static $mysqli = null;
25
821744e0 26 public static function connect()
32d9ae72 27 {
821744e0 28 global $globals;
29 XDB::$mysqli = new mysqli($globals->dbhost, $globals->dbuser, $globals->dbpwd, $globals->dbdb);
81e9c63f 30 if ($globals->debug & DEBUG_BT) {
d3f26be9 31 $bt = new PlBacktrace('MySQL');
32 if (mysqli_connect_errno()) {
33 $bt->newEvent("MySQLI connection", 0, mysqli_connect_error());
34 return false;
35 }
32d9ae72 36 }
37 XDB::$mysqli->autocommit(true);
821744e0 38 XDB::$mysqli->set_charset($globals->dbcharset);
32d9ae72 39 return true;
40 }
f1ca33de 41
ed3f4d3e 42 public static function _prepare($args)
43 {
f62bd784 44 $query = array_map(Array('XDB', 'escape'), $args);
0337d704 45 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
46 return call_user_func_array('sprintf', $query);
47 }
13a25546 48
6995a9b9 49 public static function _reformatQuery($query)
7c571120 50 {
a4f89886 51 $query = preg_split("/\n\\s*/", trim($query));
7c571120 52 $length = 0;
d3c52d30 53 foreach ($query as $key=>$line) {
54 $local = -2;
a14159bf 55 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches)
85d3b330 56 && $matches[1] != 'AND' && $matches[1] != 'OR')
57 {
d3c52d30 58 $local = strlen($matches[1]);
59 $line = $matches[1] . ' ' . $matches[2];
60 $length = max($length, $local);
7c571120 61 }
d3c52d30 62 $query[$key] = array($line, $local);
7c571120 63 }
64 $res = '';
d3c52d30 65 foreach ($query as $array) {
66 list($line, $local) = $array;
9630c649 67 $local = max(0, $length - $local);
7c571120 68 $res .= str_repeat(' ', $local) . $line . "\n";
69 $length += 2 * (substr_count($line, '(') - substr_count($line, ')'));
70 }
71 return $res;
72 }
73
ed3f4d3e 74 public static function _query($query)
75 {
f1ca33de 76 global $globals;
77
821744e0 78 if (!XDB::$mysqli && !XDB::connect()) {
084a60da
FB
79 Platal::page()->kill('Impossible de se connecter à la base de données.');
80 exit;
821744e0 81 }
82
81e9c63f 83 if ($globals->debug & DEBUG_BT) {
f1ca33de 84 $explain = array();
5fb22b39 85 if (strpos($query, 'FOUND_ROWS()') === false) {
32d9ae72 86 $res = XDB::$mysqli->query("EXPLAIN $query");
87 if ($res) {
88 while ($row = $res->fetch_assoc()) {
5fb22b39 89 $explain[] = $row;
90 }
32d9ae72 91 $res->free();
5fb22b39 92 }
f1ca33de 93 }
d3f26be9 94 PlBacktrace::$bt['MySQL']->start(XDB::_reformatQuery($query));
f1ca33de 95 }
96
32d9ae72 97 $res = XDB::$mysqli->query($query);
0381e170 98
81e9c63f 99 if ($globals->debug & DEBUG_BT) {
d3f26be9 100 PlBacktrace::$bt['MySQL']->stop(@$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows,
101 XDB::$mysqli->error,
102 $explain);
f1ca33de 103 }
084a60da
FB
104
105 if ($res === false) {
106 if (strpos($query, 'INSERT') === false && strpos($query, 'UPDATE') === false
107 && strpos($query, 'REPLACE') === false && strpos($query, 'DELETE') === false) {
108 Platal::page()->kill('Erreur lors de l\'interrogation de la base de données');
109 } else {
110 Platal::page()->kill('Erreur lors de l\'écriture dans la base de données');
111 }
112 exit;
113 }
f1ca33de 114 return $res;
115 }
116
6995a9b9 117 public static function query()
0337d704 118 {
08cce2ff 119 return new XOrgDBResult(XDB::_prepare(func_get_args()));
0337d704 120 }
121
6995a9b9 122 public static function execute()
f1ca33de 123 {
fe556813
FB
124 global $globals;
125 $args = func_get_args();
126 if ($globals->mode != 'rw' && !strpos($args[0], 'logger')) {
127 return;
128 }
129 return XDB::_query(XDB::_prepare($args));
0337d704 130 }
13a25546 131
6995a9b9 132 public static function iterator()
0337d704 133 {
08cce2ff 134 return new XOrgDBIterator(XDB::_prepare(func_get_args()));
0337d704 135 }
13a25546 136
6995a9b9 137 public static function iterRow()
0337d704 138 {
08cce2ff 139 return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM);
0337d704 140 }
13a25546 141
6995a9b9 142 public static function insertId()
13a25546 143 {
32d9ae72 144 return XDB::$mysqli->insert_id;
13a25546 145 }
146
0380bf85 147 public static function errno()
148 {
0380bf85 149 return XDB::$mysqli->errno;
150 }
151
152 public static function error()
834fd0f6 153 {
0380bf85 154 return XDB::$mysqli->error;
155 }
156
157 public static function affectedRows()
158 {
159 return XDB::$mysqli->affected_rows;
160 }
161
f62bd784 162 public static function escape($var)
0337d704 163 {
164 switch (gettype($var)) {
13a25546 165 case 'boolean':
166 return $var ? 1 : 0;
167
168 case 'integer':
169 case 'double':
170 case 'float':
171 return $var;
172
173 case 'string':
174 return "'".addslashes($var)."'";
175
176 case 'NULL':
177 return 'NULL';
178
179 case 'object':
113f6de8 180 if ($var instanceof PlFlagSet) {
04c1b2eb
FB
181 return "'" . addslashes($var->flags()) . "'";
182 }
13a25546 183 case 'array':
184 return "'".addslashes(serialize($var))."'";
185
186 default:
187 die(var_export($var, true).' is not a valid for a database entry');
0337d704 188 }
189 }
0337d704 190}
191
0337d704 192class XOrgDBResult
193{
0337d704 194
32d9ae72 195 private $_res;
0337d704 196
0381e170 197 public function __construct($query)
0337d704 198 {
755abda6 199 $this->_res = XDB::_query($query);
0337d704 200 }
201
0381e170 202 public function free()
0337d704 203 {
0381e170 204 if ($this->_res) {
205 $this->_res->free();
206 }
0337d704 207 unset($this);
208 }
209
0381e170 210 protected function _fetchRow()
0337d704 211 {
0381e170 212 return $this->_res ? $this->_res->fetch_row() : null;
0337d704 213 }
214
0381e170 215 protected function _fetchAssoc()
0337d704 216 {
0381e170 217 return $this->_res ? $this->_res->fetch_assoc() : null;
0337d704 218 }
219
0381e170 220 public function fetchAllRow()
0337d704 221 {
222 $result = Array();
0381e170 223 if (!$this->_res) {
224 return $result;
225 }
32d9ae72 226 while ($result[] = $this->_res->fetch_row());
0337d704 227 array_pop($result);
228 $this->free();
229 return $result;
230 }
231
0381e170 232 public function fetchAllAssoc()
0337d704 233 {
234 $result = Array();
0381e170 235 if (!$this->_res) {
236 return $result;
237 }
32d9ae72 238 while ($result[] = $this->_res->fetch_assoc());
0337d704 239 array_pop($result);
240 $this->free();
241 return $result;
242 }
243
0381e170 244 public function fetchOneAssoc()
0337d704 245 {
246 $tmp = $this->_fetchAssoc();
247 $this->free();
248 return $tmp;
249 }
250
0381e170 251 public function fetchOneRow()
0337d704 252 {
253 $tmp = $this->_fetchRow();
254 $this->free();
255 return $tmp;
256 }
257
0381e170 258 public function fetchOneCell()
0337d704 259 {
260 $tmp = $this->_fetchRow();
261 $this->free();
262 return $tmp[0];
263 }
264
0381e170 265 public function fetchColumn($key = 0)
0337d704 266 {
267 $res = Array();
268 if (is_numeric($key)) {
269 while($tmp = $this->_fetchRow()) {
270 $res[] = $tmp[$key];
271 }
272 } else {
273 while($tmp = $this->_fetchAssoc()) {
274 $res[] = $tmp[$key];
275 }
276 }
277 $this->free();
278 return $res;
279 }
280
0381e170 281 public function fetchOneField()
0380bf85 282 {
0381e170 283 return $this->_res ? $this->_res->fetch_field() : null;
0380bf85 284 }
285
0381e170 286 public function fetchFields()
0380bf85 287 {
288 $res = array();
289 while ($res[] = $this->fetchOneField());
290 return $res;
291 }
292
0381e170 293 public function numRows()
0337d704 294 {
0381e170 295 return $this->_res ? $this->_res->num_rows : 0;
0337d704 296 }
0380bf85 297
0381e170 298 public function fieldCount()
0380bf85 299 {
0381e170 300 return $this->_res ? $this->_res->field_count : 0;
0380bf85 301 }
0337d704 302}
303
2b1ee50b 304require_once dirname(__FILE__) . '/pliterator.php';
305
0381e170 306class XOrgDBIterator extends XOrgDBResult implements PlIterator
0337d704 307{
85d3b330 308 private $_result;
309 private $_pos;
310 private $_total;
0380bf85 311 private $_fpos;
312 private $_fields;
85d3b330 313 private $_mode = MYSQL_ASSOC;
0337d704 314
0381e170 315 public function __construct($query, $mode = MYSQL_ASSOC)
0337d704 316 {
0381e170 317 parent::__construct($query);
0337d704 318 $this->_pos = 0;
0381e170 319 $this->_total = $this->numRows();
0380bf85 320 $this->_fpost = 0;
0381e170 321 $this->_fields = $this->fieldCount();
0337d704 322 $this->_mode = $mode;
323 }
324
0381e170 325 public function next()
0337d704 326 {
327 $this->_pos ++;
328 if ($this->_pos > $this->_total) {
0381e170 329 $this->free();
0337d704 330 unset($this);
331 return null;
332 }
0381e170 333 return $this->_mode != MYSQL_ASSOC ? $this->_fetchRow() : $this->_fetchAssoc();
0337d704 334 }
335
0381e170 336 public function first()
0337d704 337 {
338 return $this->_pos == 1;
339 }
340
0381e170 341 public function last()
0337d704 342 {
0380bf85 343 return $this->_pos == $this->_total;
0337d704 344 }
345
0381e170 346 public function total()
0337d704 347 {
348 return $this->_total;
349 }
0380bf85 350
0381e170 351 public function nextField()
0380bf85 352 {
353 $this->_fpos++;
354 if ($this->_fpos > $this->_fields) {
355 return null;
356 }
0381e170 357 return $this->fetchOneField();
0380bf85 358 }
359
0381e170 360 public function firstField()
0380bf85 361 {
362 return $this->_fpos == 1;
363 }
364
0381e170 365 public function lastField()
0380bf85 366 {
367 return $this->_fpos == $this->_fields;
368 }
369
0381e170 370 public function totalFields()
0380bf85 371 {
372 return $this->_fields;
373 }
0337d704 374}
375
a7de4ef7 376// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 377?>