Commit | Line | Data |
---|---|---|
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 | 22 | class XDB |
0337d704 | 23 | { |
32d9ae72 | 24 | private static $mysqli = null; |
25 | ||
821744e0 | 26 | public static function connect() |
32d9ae72 | 27 | { |
821744e0 | 28 | global $globals; |
e4f6c7d0 | 29 | self::$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 | } |
e4f6c7d0 FB |
37 | self::$mysqli->autocommit(true); |
38 | self::$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 | ||
e4f6c7d0 | 78 | if (!self::$mysqli && !self::connect()) { |
12ccfec7 | 79 | header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); |
084a60da FB |
80 | Platal::page()->kill('Impossible de se connecter à la base de données.'); |
81 | exit; | |
821744e0 | 82 | } |
83 | ||
81e9c63f | 84 | if ($globals->debug & DEBUG_BT) { |
f1ca33de | 85 | $explain = array(); |
5fb22b39 | 86 | if (strpos($query, 'FOUND_ROWS()') === false) { |
e4f6c7d0 | 87 | $res = self::$mysqli->query("EXPLAIN $query"); |
32d9ae72 | 88 | if ($res) { |
89 | while ($row = $res->fetch_assoc()) { | |
5fb22b39 | 90 | $explain[] = $row; |
91 | } | |
32d9ae72 | 92 | $res->free(); |
5fb22b39 | 93 | } |
f1ca33de | 94 | } |
d3f26be9 | 95 | PlBacktrace::$bt['MySQL']->start(XDB::_reformatQuery($query)); |
f1ca33de | 96 | } |
97 | ||
32d9ae72 | 98 | $res = XDB::$mysqli->query($query); |
0381e170 | 99 | |
81e9c63f | 100 | if ($globals->debug & DEBUG_BT) { |
e4f6c7d0 FB |
101 | PlBacktrace::$bt['MySQL']->stop(@$res->num_rows ? $res->num_rows : self::$mysqli->affected_rows, |
102 | self::$mysqli->error, | |
d3f26be9 | 103 | $explain); |
f1ca33de | 104 | } |
084a60da FB |
105 | |
106 | if ($res === false) { | |
12ccfec7 | 107 | header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); |
084a60da FB |
108 | if (strpos($query, 'INSERT') === false && strpos($query, 'UPDATE') === false |
109 | && strpos($query, 'REPLACE') === false && strpos($query, 'DELETE') === false) { | |
7f10bc61 | 110 | $text = 'Erreur lors de l\'interrogation de la base de données'; |
084a60da | 111 | } else { |
7f10bc61 | 112 | $text = 'Erreur lors de l\'écriture dans la base de données'; |
084a60da | 113 | } |
7f10bc61 FB |
114 | if ($globals->debug) { |
115 | $text .= '<pre>' . pl_entities(XDB::_reformatQuery($query)) . '</pre>'; | |
f8eaef22 FB |
116 | } else { |
117 | $file = fopen($globals->spoolroot . '/spool/tmp/query_errors', 'a'); | |
118 | fwrite($file, '<pre>' . pl_entities(XDB::_reformatQuery($query)) . '</pre>' | |
119 | . '<pre>' . XDB::$mysqli->error . '</pre>' . "\n"); | |
120 | fclose($file); | |
7f10bc61 FB |
121 | } |
122 | Platal::page()->kill($text); | |
084a60da FB |
123 | exit; |
124 | } | |
f1ca33de | 125 | return $res; |
126 | } | |
127 | ||
e4f6c7d0 FB |
128 | private static function queryv($query) |
129 | { | |
130 | return new XOrgDBResult(self::_prepare($query)); | |
131 | } | |
132 | ||
6995a9b9 | 133 | public static function query() |
0337d704 | 134 | { |
e4f6c7d0 | 135 | return self::queryv(func_get_args()); |
0337d704 | 136 | } |
137 | ||
20973bf8 FB |
138 | public static function format() |
139 | { | |
e4f6c7d0 | 140 | return self::_prepare(func_get_args()); |
20973bf8 FB |
141 | } |
142 | ||
6995a9b9 | 143 | public static function execute() |
f1ca33de | 144 | { |
fe556813 FB |
145 | global $globals; |
146 | $args = func_get_args(); | |
147 | if ($globals->mode != 'rw' && !strpos($args[0], 'logger')) { | |
148 | return; | |
149 | } | |
e4f6c7d0 | 150 | return self::_query(XDB::_prepare($args)); |
0337d704 | 151 | } |
13a25546 | 152 | |
6995a9b9 | 153 | public static function iterator() |
0337d704 | 154 | { |
e4f6c7d0 | 155 | return new XOrgDBIterator(self::_prepare(func_get_args())); |
0337d704 | 156 | } |
13a25546 | 157 | |
6995a9b9 | 158 | public static function iterRow() |
0337d704 | 159 | { |
e4f6c7d0 FB |
160 | return new XOrgDBIterator(self::_prepare(func_get_args()), MYSQL_NUM); |
161 | } | |
162 | ||
163 | private static function findQuery($params, $default = array()) | |
164 | { | |
165 | for ($i = 0 ; $i < count($default) ; ++$i) { | |
166 | $is_query = false; | |
167 | foreach (array('insert', 'select', 'replace', 'delete', 'update') as $kwd) { | |
168 | if (stripos($params[0], $kwd) !== false) { | |
169 | $is_query = true; | |
170 | break; | |
171 | } | |
172 | } | |
173 | if ($is_query) { | |
174 | break; | |
175 | } else { | |
176 | $default[$i] = array_shift($params); | |
177 | } | |
178 | } | |
179 | return array($default, $params); | |
180 | } | |
181 | ||
182 | /** Fetch all rows returned by the given query. | |
183 | * This functions can take 2 optional arguments (cf XOrgDBResult::fetchAllRow()). | |
184 | * Optional arguments are given *before* the query. | |
185 | */ | |
186 | public static function fetchAllRow() | |
187 | { | |
188 | list($args, $query) = self::findQuery(func_get_args(), array(false, false)); | |
189 | return self::queryv($query)->fetchAllRow($args[0], $args[1]); | |
190 | } | |
191 | ||
192 | /** Fetch all rows returned by the given query. | |
193 | * This functions can take 2 optional arguments (cf XOrgDBResult::fetchAllAssoc()). | |
194 | * Optional arguments are given *before* the query. | |
195 | */ | |
196 | public static function fetchAllAssoc() | |
197 | { | |
198 | list($args, $query) = self::findQuery(func_get_args(), array(false, false)); | |
199 | return self::queryv($query)->fetchAllAssoc($args[0], $args[1]); | |
200 | } | |
201 | ||
202 | public static function fetchOneCell() | |
203 | { | |
204 | list($args, $query) = self::findQuery(func_get_args()); | |
205 | return self::queryv($query)->fetchOneCell(); | |
206 | } | |
207 | ||
208 | public static function fetchOneRow() | |
209 | { | |
210 | list($args, $query) = self::findQuery(func_get_args()); | |
211 | return self::queryv($query)->fetchOneRow(); | |
212 | } | |
213 | ||
214 | public static function fetchOneAssoc() | |
215 | { | |
216 | list($args, $query) = self::findQuery(func_get_args()); | |
217 | return self::queryv($query)->fetchOneAssoc(); | |
218 | } | |
219 | ||
220 | /** Fetch a column from the result of the given query. | |
221 | * This functions can take 1 optional arguments (cf XOrgDBResult::fetchColumn()). | |
222 | * Optional arguments are given *before* the query. | |
223 | */ | |
224 | public static function fetchColumn() | |
225 | { | |
226 | list($args, $query) = self::findQuery(func_get_args(), array(0)); | |
227 | return self::queryv($query)->fetchColumn(); | |
0337d704 | 228 | } |
13a25546 | 229 | |
6995a9b9 | 230 | public static function insertId() |
13a25546 | 231 | { |
e4f6c7d0 | 232 | return self::$mysqli->insert_id; |
13a25546 | 233 | } |
234 | ||
0380bf85 | 235 | public static function errno() |
236 | { | |
e4f6c7d0 | 237 | return self::$mysqli->errno; |
0380bf85 | 238 | } |
239 | ||
240 | public static function error() | |
834fd0f6 | 241 | { |
e4f6c7d0 | 242 | return self::$mysqli->error; |
0380bf85 | 243 | } |
244 | ||
245 | public static function affectedRows() | |
246 | { | |
e4f6c7d0 | 247 | return self::$mysqli->affected_rows; |
0380bf85 | 248 | } |
249 | ||
f62bd784 | 250 | public static function escape($var) |
0337d704 | 251 | { |
252 | switch (gettype($var)) { | |
13a25546 | 253 | case 'boolean': |
254 | return $var ? 1 : 0; | |
255 | ||
256 | case 'integer': | |
257 | case 'double': | |
258 | case 'float': | |
259 | return $var; | |
260 | ||
261 | case 'string': | |
262 | return "'".addslashes($var)."'"; | |
263 | ||
264 | case 'NULL': | |
265 | return 'NULL'; | |
266 | ||
267 | case 'object': | |
113f6de8 | 268 | if ($var instanceof PlFlagSet) { |
04c1b2eb FB |
269 | return "'" . addslashes($var->flags()) . "'"; |
270 | } | |
13a25546 | 271 | case 'array': |
272 | return "'".addslashes(serialize($var))."'"; | |
273 | ||
274 | default: | |
275 | die(var_export($var, true).' is not a valid for a database entry'); | |
0337d704 | 276 | } |
277 | } | |
0337d704 | 278 | } |
279 | ||
0337d704 | 280 | class XOrgDBResult |
281 | { | |
0337d704 | 282 | |
32d9ae72 | 283 | private $_res; |
0337d704 | 284 | |
0381e170 | 285 | public function __construct($query) |
0337d704 | 286 | { |
755abda6 | 287 | $this->_res = XDB::_query($query); |
0337d704 | 288 | } |
289 | ||
0381e170 | 290 | public function free() |
0337d704 | 291 | { |
0381e170 | 292 | if ($this->_res) { |
293 | $this->_res->free(); | |
294 | } | |
0337d704 | 295 | unset($this); |
296 | } | |
297 | ||
0381e170 | 298 | protected function _fetchRow() |
0337d704 | 299 | { |
0381e170 | 300 | return $this->_res ? $this->_res->fetch_row() : null; |
0337d704 | 301 | } |
302 | ||
0381e170 | 303 | protected function _fetchAssoc() |
0337d704 | 304 | { |
0381e170 | 305 | return $this->_res ? $this->_res->fetch_assoc() : null; |
0337d704 | 306 | } |
307 | ||
e4f6c7d0 | 308 | public function fetchAllRow($id = false, $keep_array = false) |
0337d704 | 309 | { |
310 | $result = Array(); | |
0381e170 | 311 | if (!$this->_res) { |
312 | return $result; | |
313 | } | |
e4f6c7d0 FB |
314 | while (($data = $this->_res->fetch_row())) { |
315 | if ($id !== false) { | |
316 | $key = $data[$id]; | |
317 | unset($data[$id]); | |
318 | if (!$keep_array && count($data) == 1) { | |
319 | reset($data); | |
320 | $result[$key] = current($data); | |
321 | } else { | |
322 | $result[$key] = $data; | |
323 | } | |
324 | } else { | |
325 | $result[] = $data; | |
326 | } | |
327 | } | |
0337d704 | 328 | $this->free(); |
329 | return $result; | |
330 | } | |
331 | ||
e4f6c7d0 | 332 | public function fetchAllAssoc($id = false, $keep_array = false) |
0337d704 | 333 | { |
334 | $result = Array(); | |
0381e170 | 335 | if (!$this->_res) { |
336 | return $result; | |
337 | } | |
e4f6c7d0 FB |
338 | while (($data = $this->_res->fetch_assoc())) { |
339 | if ($id !== false) { | |
340 | $key = $data[$id]; | |
341 | unset($data[$id]); | |
342 | if (!$keep_array && count($data) == 1) { | |
343 | reset($data); | |
344 | $result[$key] = current($data); | |
345 | } else { | |
346 | $result[$key] = $data; | |
347 | } | |
348 | } else { | |
349 | $result[] = $data; | |
350 | } | |
351 | } | |
0337d704 | 352 | $this->free(); |
353 | return $result; | |
354 | } | |
355 | ||
0381e170 | 356 | public function fetchOneAssoc() |
0337d704 | 357 | { |
358 | $tmp = $this->_fetchAssoc(); | |
359 | $this->free(); | |
360 | return $tmp; | |
361 | } | |
362 | ||
0381e170 | 363 | public function fetchOneRow() |
0337d704 | 364 | { |
365 | $tmp = $this->_fetchRow(); | |
366 | $this->free(); | |
367 | return $tmp; | |
368 | } | |
369 | ||
0381e170 | 370 | public function fetchOneCell() |
0337d704 | 371 | { |
372 | $tmp = $this->_fetchRow(); | |
373 | $this->free(); | |
374 | return $tmp[0]; | |
375 | } | |
376 | ||
0381e170 | 377 | public function fetchColumn($key = 0) |
0337d704 | 378 | { |
379 | $res = Array(); | |
380 | if (is_numeric($key)) { | |
381 | while($tmp = $this->_fetchRow()) { | |
382 | $res[] = $tmp[$key]; | |
383 | } | |
384 | } else { | |
385 | while($tmp = $this->_fetchAssoc()) { | |
386 | $res[] = $tmp[$key]; | |
387 | } | |
388 | } | |
389 | $this->free(); | |
390 | return $res; | |
391 | } | |
392 | ||
0381e170 | 393 | public function fetchOneField() |
0380bf85 | 394 | { |
0381e170 | 395 | return $this->_res ? $this->_res->fetch_field() : null; |
0380bf85 | 396 | } |
397 | ||
0381e170 | 398 | public function fetchFields() |
0380bf85 | 399 | { |
400 | $res = array(); | |
401 | while ($res[] = $this->fetchOneField()); | |
402 | return $res; | |
403 | } | |
404 | ||
0381e170 | 405 | public function numRows() |
0337d704 | 406 | { |
0381e170 | 407 | return $this->_res ? $this->_res->num_rows : 0; |
0337d704 | 408 | } |
0380bf85 | 409 | |
0381e170 | 410 | public function fieldCount() |
0380bf85 | 411 | { |
0381e170 | 412 | return $this->_res ? $this->_res->field_count : 0; |
0380bf85 | 413 | } |
0337d704 | 414 | } |
415 | ||
2b1ee50b | 416 | require_once dirname(__FILE__) . '/pliterator.php'; |
417 | ||
0381e170 | 418 | class XOrgDBIterator extends XOrgDBResult implements PlIterator |
0337d704 | 419 | { |
85d3b330 | 420 | private $_result; |
421 | private $_pos; | |
422 | private $_total; | |
0380bf85 | 423 | private $_fpos; |
424 | private $_fields; | |
85d3b330 | 425 | private $_mode = MYSQL_ASSOC; |
0337d704 | 426 | |
0381e170 | 427 | public function __construct($query, $mode = MYSQL_ASSOC) |
0337d704 | 428 | { |
0381e170 | 429 | parent::__construct($query); |
0337d704 | 430 | $this->_pos = 0; |
0381e170 | 431 | $this->_total = $this->numRows(); |
0380bf85 | 432 | $this->_fpost = 0; |
0381e170 | 433 | $this->_fields = $this->fieldCount(); |
0337d704 | 434 | $this->_mode = $mode; |
435 | } | |
436 | ||
0381e170 | 437 | public function next() |
0337d704 | 438 | { |
439 | $this->_pos ++; | |
440 | if ($this->_pos > $this->_total) { | |
0381e170 | 441 | $this->free(); |
0337d704 | 442 | unset($this); |
443 | return null; | |
444 | } | |
0381e170 | 445 | return $this->_mode != MYSQL_ASSOC ? $this->_fetchRow() : $this->_fetchAssoc(); |
0337d704 | 446 | } |
447 | ||
0381e170 | 448 | public function first() |
0337d704 | 449 | { |
450 | return $this->_pos == 1; | |
451 | } | |
452 | ||
0381e170 | 453 | public function last() |
0337d704 | 454 | { |
0380bf85 | 455 | return $this->_pos == $this->_total; |
0337d704 | 456 | } |
457 | ||
0381e170 | 458 | public function total() |
0337d704 | 459 | { |
460 | return $this->_total; | |
461 | } | |
0380bf85 | 462 | |
0381e170 | 463 | public function nextField() |
0380bf85 | 464 | { |
465 | $this->_fpos++; | |
466 | if ($this->_fpos > $this->_fields) { | |
467 | return null; | |
468 | } | |
0381e170 | 469 | return $this->fetchOneField(); |
0380bf85 | 470 | } |
471 | ||
0381e170 | 472 | public function firstField() |
0380bf85 | 473 | { |
474 | return $this->_fpos == 1; | |
475 | } | |
476 | ||
0381e170 | 477 | public function lastField() |
0380bf85 | 478 | { |
479 | return $this->_fpos == $this->_fields; | |
480 | } | |
481 | ||
0381e170 | 482 | public function totalFields() |
0380bf85 | 483 | { |
484 | return $this->_fields; | |
485 | } | |
0337d704 | 486 | } |
487 | ||
a7de4ef7 | 488 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 489 | ?> |