|
1 <?php |
|
2 /** |
|
3 * |
|
4 * @package dbal |
|
5 * @version $Id$ |
|
6 * @copyright (c) 2005 phpBB Group |
|
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
8 * |
|
9 */ |
|
10 |
|
11 /** |
|
12 * @ignore |
|
13 */ |
|
14 if (!defined('IN_PHPBB')) |
|
15 { |
|
16 exit; |
|
17 } |
|
18 |
|
19 /** |
|
20 * Database Abstraction Layer |
|
21 * @package dbal |
|
22 */ |
|
23 class dbal |
|
24 { |
|
25 var $db_connect_id; |
|
26 var $query_result; |
|
27 var $return_on_error = false; |
|
28 var $transaction = false; |
|
29 var $sql_time = 0; |
|
30 var $num_queries = array(); |
|
31 var $open_queries = array(); |
|
32 |
|
33 var $curtime = 0; |
|
34 var $query_hold = ''; |
|
35 var $html_hold = ''; |
|
36 var $sql_report = ''; |
|
37 |
|
38 var $persistency = false; |
|
39 var $user = ''; |
|
40 var $server = ''; |
|
41 var $dbname = ''; |
|
42 |
|
43 // Set to true if error triggered |
|
44 var $sql_error_triggered = false; |
|
45 |
|
46 // Holding the last sql query on sql error |
|
47 var $sql_error_sql = ''; |
|
48 // Holding the error information - only populated if sql_error_triggered is set |
|
49 var $sql_error_returned = array(); |
|
50 |
|
51 // Holding transaction count |
|
52 var $transactions = 0; |
|
53 |
|
54 // Supports multi inserts? |
|
55 var $multi_insert = false; |
|
56 |
|
57 /** |
|
58 * Current sql layer |
|
59 */ |
|
60 var $sql_layer = ''; |
|
61 |
|
62 /** |
|
63 * Wildcards for matching any (%) or exactly one (_) character within LIKE expressions |
|
64 */ |
|
65 var $any_char; |
|
66 var $one_char; |
|
67 |
|
68 /** |
|
69 * Exact version of the DBAL, directly queried |
|
70 */ |
|
71 var $sql_server_version = false; |
|
72 |
|
73 /** |
|
74 * Constructor |
|
75 */ |
|
76 function dbal() |
|
77 { |
|
78 $this->num_queries = array( |
|
79 'cached' => 0, |
|
80 'normal' => 0, |
|
81 'total' => 0, |
|
82 ); |
|
83 |
|
84 // Fill default sql layer based on the class being called. |
|
85 // This can be changed by the specified layer itself later if needed. |
|
86 $this->sql_layer = substr(get_class($this), 5); |
|
87 |
|
88 // Do not change this please! This variable is used to easy the use of it - and is hardcoded. |
|
89 $this->any_char = chr(0) . '%'; |
|
90 $this->one_char = chr(0) . '_'; |
|
91 } |
|
92 |
|
93 /** |
|
94 * return on error or display error message |
|
95 */ |
|
96 function sql_return_on_error($fail = false) |
|
97 { |
|
98 $this->sql_error_triggered = false; |
|
99 $this->sql_error_sql = ''; |
|
100 |
|
101 $this->return_on_error = $fail; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Return number of sql queries and cached sql queries used |
|
106 */ |
|
107 function sql_num_queries($cached = false) |
|
108 { |
|
109 return ($cached) ? $this->num_queries['cached'] : $this->num_queries['normal']; |
|
110 } |
|
111 |
|
112 /** |
|
113 * Add to query count |
|
114 */ |
|
115 function sql_add_num_queries($cached = false) |
|
116 { |
|
117 $this->num_queries['cached'] += ($cached !== false) ? 1 : 0; |
|
118 $this->num_queries['normal'] += ($cached !== false) ? 0 : 1; |
|
119 $this->num_queries['total'] += 1; |
|
120 } |
|
121 |
|
122 /** |
|
123 * DBAL garbage collection, close sql connection |
|
124 */ |
|
125 function sql_close() |
|
126 { |
|
127 if (!$this->db_connect_id) |
|
128 { |
|
129 return false; |
|
130 } |
|
131 |
|
132 if ($this->transaction) |
|
133 { |
|
134 do |
|
135 { |
|
136 $this->sql_transaction('commit'); |
|
137 } |
|
138 while ($this->transaction); |
|
139 } |
|
140 |
|
141 foreach ($this->open_queries as $query_id) |
|
142 { |
|
143 $this->sql_freeresult($query_id); |
|
144 } |
|
145 |
|
146 // Connection closed correctly. Set db_connect_id to false to prevent errors |
|
147 if ($result = $this->_sql_close()) |
|
148 { |
|
149 $this->db_connect_id = false; |
|
150 } |
|
151 |
|
152 return $result; |
|
153 } |
|
154 |
|
155 /** |
|
156 * Build LIMIT query |
|
157 * Doing some validation here. |
|
158 */ |
|
159 function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) |
|
160 { |
|
161 if (empty($query)) |
|
162 { |
|
163 return false; |
|
164 } |
|
165 |
|
166 // Never use a negative total or offset |
|
167 $total = ($total < 0) ? 0 : $total; |
|
168 $offset = ($offset < 0) ? 0 : $offset; |
|
169 |
|
170 return $this->_sql_query_limit($query, $total, $offset, $cache_ttl); |
|
171 } |
|
172 |
|
173 /** |
|
174 * Fetch all rows |
|
175 */ |
|
176 function sql_fetchrowset($query_id = false) |
|
177 { |
|
178 if ($query_id === false) |
|
179 { |
|
180 $query_id = $this->query_result; |
|
181 } |
|
182 |
|
183 if ($query_id !== false) |
|
184 { |
|
185 $result = array(); |
|
186 while ($row = $this->sql_fetchrow($query_id)) |
|
187 { |
|
188 $result[] = $row; |
|
189 } |
|
190 |
|
191 return $result; |
|
192 } |
|
193 |
|
194 return false; |
|
195 } |
|
196 |
|
197 /** |
|
198 * Fetch field |
|
199 * if rownum is false, the current row is used, else it is pointing to the row (zero-based) |
|
200 */ |
|
201 function sql_fetchfield($field, $rownum = false, $query_id = false) |
|
202 { |
|
203 global $cache; |
|
204 |
|
205 if ($query_id === false) |
|
206 { |
|
207 $query_id = $this->query_result; |
|
208 } |
|
209 |
|
210 if ($query_id !== false) |
|
211 { |
|
212 if ($rownum !== false) |
|
213 { |
|
214 $this->sql_rowseek($rownum, $query_id); |
|
215 } |
|
216 |
|
217 if (!is_object($query_id) && isset($cache->sql_rowset[$query_id])) |
|
218 { |
|
219 return $cache->sql_fetchfield($query_id, $field); |
|
220 } |
|
221 |
|
222 $row = $this->sql_fetchrow($query_id); |
|
223 return (isset($row[$field])) ? $row[$field] : false; |
|
224 } |
|
225 |
|
226 return false; |
|
227 } |
|
228 |
|
229 /** |
|
230 * Correctly adjust LIKE expression for special characters |
|
231 * Some DBMS are handling them in a different way |
|
232 * |
|
233 * @param string $expression The expression to use. Every wildcard is escaped, except $this->any_char and $this->one_char |
|
234 * @return string LIKE expression including the keyword! |
|
235 */ |
|
236 function sql_like_expression($expression) |
|
237 { |
|
238 $expression = utf8_str_replace(array('_', '%'), array("\_", "\%"), $expression); |
|
239 $expression = utf8_str_replace(array(chr(0) . "\_", chr(0) . "\%"), array('_', '%'), $expression); |
|
240 |
|
241 return $this->_sql_like_expression('LIKE \'' . $this->sql_escape($expression) . '\''); |
|
242 } |
|
243 |
|
244 /** |
|
245 * Returns whether results of a query need to be buffered to run a transaction while iterating over them. |
|
246 * |
|
247 * @return bool Whether buffering is required. |
|
248 */ |
|
249 function sql_buffer_nested_transactions() |
|
250 { |
|
251 return false; |
|
252 } |
|
253 |
|
254 /** |
|
255 * SQL Transaction |
|
256 * @access private |
|
257 */ |
|
258 function sql_transaction($status = 'begin') |
|
259 { |
|
260 switch ($status) |
|
261 { |
|
262 case 'begin': |
|
263 // If we are within a transaction we will not open another one, but enclose the current one to not loose data (prevening auto commit) |
|
264 if ($this->transaction) |
|
265 { |
|
266 $this->transactions++; |
|
267 return true; |
|
268 } |
|
269 |
|
270 $result = $this->_sql_transaction('begin'); |
|
271 |
|
272 if (!$result) |
|
273 { |
|
274 $this->sql_error(); |
|
275 } |
|
276 |
|
277 $this->transaction = true; |
|
278 break; |
|
279 |
|
280 case 'commit': |
|
281 // If there was a previously opened transaction we do not commit yet... but count back the number of inner transactions |
|
282 if ($this->transaction && $this->transactions) |
|
283 { |
|
284 $this->transactions--; |
|
285 return true; |
|
286 } |
|
287 |
|
288 // Check if there is a transaction (no transaction can happen if there was an error, with a combined rollback and error returning enabled) |
|
289 // This implies we have transaction always set for autocommit db's |
|
290 if (!$this->transaction) |
|
291 { |
|
292 return false; |
|
293 } |
|
294 |
|
295 $result = $this->_sql_transaction('commit'); |
|
296 |
|
297 if (!$result) |
|
298 { |
|
299 $this->sql_error(); |
|
300 } |
|
301 |
|
302 $this->transaction = false; |
|
303 $this->transactions = 0; |
|
304 break; |
|
305 |
|
306 case 'rollback': |
|
307 $result = $this->_sql_transaction('rollback'); |
|
308 $this->transaction = false; |
|
309 $this->transactions = 0; |
|
310 break; |
|
311 |
|
312 default: |
|
313 $result = $this->_sql_transaction($status); |
|
314 break; |
|
315 } |
|
316 |
|
317 return $result; |
|
318 } |
|
319 |
|
320 /** |
|
321 * Build sql statement from array for insert/update/select statements |
|
322 * |
|
323 * Idea for this from Ikonboard |
|
324 * Possible query values: INSERT, INSERT_SELECT, UPDATE, SELECT |
|
325 * |
|
326 */ |
|
327 function sql_build_array($query, $assoc_ary = false) |
|
328 { |
|
329 if (!is_array($assoc_ary)) |
|
330 { |
|
331 return false; |
|
332 } |
|
333 |
|
334 $fields = $values = array(); |
|
335 |
|
336 if ($query == 'INSERT' || $query == 'INSERT_SELECT') |
|
337 { |
|
338 foreach ($assoc_ary as $key => $var) |
|
339 { |
|
340 $fields[] = $key; |
|
341 |
|
342 if (is_array($var) && is_string($var[0])) |
|
343 { |
|
344 // This is used for INSERT_SELECT(s) |
|
345 $values[] = $var[0]; |
|
346 } |
|
347 else |
|
348 { |
|
349 $values[] = $this->_sql_validate_value($var); |
|
350 } |
|
351 } |
|
352 |
|
353 $query = ($query == 'INSERT') ? ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')' : ' (' . implode(', ', $fields) . ') SELECT ' . implode(', ', $values) . ' '; |
|
354 } |
|
355 else if ($query == 'MULTI_INSERT') |
|
356 { |
|
357 trigger_error('The MULTI_INSERT query value is no longer supported. Please use sql_multi_insert() instead.', E_USER_ERROR); |
|
358 } |
|
359 else if ($query == 'UPDATE' || $query == 'SELECT') |
|
360 { |
|
361 $values = array(); |
|
362 foreach ($assoc_ary as $key => $var) |
|
363 { |
|
364 $values[] = "$key = " . $this->_sql_validate_value($var); |
|
365 } |
|
366 $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); |
|
367 } |
|
368 |
|
369 return $query; |
|
370 } |
|
371 |
|
372 /** |
|
373 * Build IN or NOT IN sql comparison string, uses <> or = on single element |
|
374 * arrays to improve comparison speed |
|
375 * |
|
376 * @access public |
|
377 * @param string $field name of the sql column that shall be compared |
|
378 * @param array $array array of values that are allowed (IN) or not allowed (NOT IN) |
|
379 * @param bool $negate true for NOT IN (), false for IN () (default) |
|
380 * @param bool $allow_empty_set If true, allow $array to be empty, this function will return 1=1 or 1=0 then. Default to false. |
|
381 */ |
|
382 function sql_in_set($field, $array, $negate = false, $allow_empty_set = false) |
|
383 { |
|
384 if (!sizeof($array)) |
|
385 { |
|
386 if (!$allow_empty_set) |
|
387 { |
|
388 // Print the backtrace to help identifying the location of the problematic code |
|
389 $this->sql_error('No values specified for SQL IN comparison'); |
|
390 } |
|
391 else |
|
392 { |
|
393 // NOT IN () actually means everything so use a tautology |
|
394 if ($negate) |
|
395 { |
|
396 return '1=1'; |
|
397 } |
|
398 // IN () actually means nothing so use a contradiction |
|
399 else |
|
400 { |
|
401 return '1=0'; |
|
402 } |
|
403 } |
|
404 } |
|
405 |
|
406 if (!is_array($array)) |
|
407 { |
|
408 $array = array($array); |
|
409 } |
|
410 |
|
411 if (sizeof($array) == 1) |
|
412 { |
|
413 @reset($array); |
|
414 $var = current($array); |
|
415 |
|
416 return $field . ($negate ? ' <> ' : ' = ') . $this->_sql_validate_value($var); |
|
417 } |
|
418 else |
|
419 { |
|
420 return $field . ($negate ? ' NOT IN ' : ' IN ') . '(' . implode(', ', array_map(array($this, '_sql_validate_value'), $array)) . ')'; |
|
421 } |
|
422 } |
|
423 |
|
424 /** |
|
425 * Run binary AND operator on DB column. |
|
426 * Results in sql statement: "{$column_name} & (1 << {$bit}) {$compare}" |
|
427 * |
|
428 * @param string $column_name The column name to use |
|
429 * @param int $bit The value to use for the AND operator, will be converted to (1 << $bit). Is used by options, using the number schema... 0, 1, 2...29 |
|
430 * @param string $compare Any custom SQL code after the check (for example "= 0") |
|
431 */ |
|
432 function sql_bit_and($column_name, $bit, $compare = '') |
|
433 { |
|
434 if (method_exists($this, '_sql_bit_and')) |
|
435 { |
|
436 return $this->_sql_bit_and($column_name, $bit, $compare); |
|
437 } |
|
438 |
|
439 return $column_name . ' & ' . (1 << $bit) . (($compare) ? ' ' . $compare : ''); |
|
440 } |
|
441 |
|
442 /** |
|
443 * Run binary OR operator on DB column. |
|
444 * Results in sql statement: "{$column_name} | (1 << {$bit}) {$compare}" |
|
445 * |
|
446 * @param string $column_name The column name to use |
|
447 * @param int $bit The value to use for the OR operator, will be converted to (1 << $bit). Is used by options, using the number schema... 0, 1, 2...29 |
|
448 * @param string $compare Any custom SQL code after the check (for example "= 0") |
|
449 */ |
|
450 function sql_bit_or($column_name, $bit, $compare = '') |
|
451 { |
|
452 if (method_exists($this, '_sql_bit_or')) |
|
453 { |
|
454 return $this->_sql_bit_or($column_name, $bit, $compare); |
|
455 } |
|
456 |
|
457 return $column_name . ' | ' . (1 << $bit) . (($compare) ? ' ' . $compare : ''); |
|
458 } |
|
459 |
|
460 /** |
|
461 * Run more than one insert statement. |
|
462 * |
|
463 * @param string $table table name to run the statements on |
|
464 * @param array &$sql_ary multi-dimensional array holding the statement data. |
|
465 * |
|
466 * @return bool false if no statements were executed. |
|
467 * @access public |
|
468 */ |
|
469 function sql_multi_insert($table, &$sql_ary) |
|
470 { |
|
471 if (!sizeof($sql_ary)) |
|
472 { |
|
473 return false; |
|
474 } |
|
475 |
|
476 if ($this->multi_insert) |
|
477 { |
|
478 $ary = array(); |
|
479 foreach ($sql_ary as $id => $_sql_ary) |
|
480 { |
|
481 // If by accident the sql array is only one-dimensional we build a normal insert statement |
|
482 if (!is_array($_sql_ary)) |
|
483 { |
|
484 return $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $sql_ary)); |
|
485 } |
|
486 |
|
487 $values = array(); |
|
488 foreach ($_sql_ary as $key => $var) |
|
489 { |
|
490 $values[] = $this->_sql_validate_value($var); |
|
491 } |
|
492 $ary[] = '(' . implode(', ', $values) . ')'; |
|
493 } |
|
494 |
|
495 return $this->sql_query('INSERT INTO ' . $table . ' ' . ' (' . implode(', ', array_keys($sql_ary[0])) . ') VALUES ' . implode(', ', $ary)); |
|
496 } |
|
497 else |
|
498 { |
|
499 foreach ($sql_ary as $ary) |
|
500 { |
|
501 if (!is_array($ary)) |
|
502 { |
|
503 return false; |
|
504 } |
|
505 |
|
506 $result = $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $ary)); |
|
507 |
|
508 if (!$result) |
|
509 { |
|
510 return false; |
|
511 } |
|
512 } |
|
513 } |
|
514 |
|
515 return true; |
|
516 } |
|
517 |
|
518 /** |
|
519 * Function for validating values |
|
520 * @access private |
|
521 */ |
|
522 function _sql_validate_value($var) |
|
523 { |
|
524 if (is_null($var)) |
|
525 { |
|
526 return 'NULL'; |
|
527 } |
|
528 else if (is_string($var)) |
|
529 { |
|
530 return "'" . $this->sql_escape($var) . "'"; |
|
531 } |
|
532 else |
|
533 { |
|
534 return (is_bool($var)) ? intval($var) : $var; |
|
535 } |
|
536 } |
|
537 |
|
538 /** |
|
539 * Build sql statement from array for select and select distinct statements |
|
540 * |
|
541 * Possible query values: SELECT, SELECT_DISTINCT |
|
542 */ |
|
543 function sql_build_query($query, $array) |
|
544 { |
|
545 $sql = ''; |
|
546 switch ($query) |
|
547 { |
|
548 case 'SELECT': |
|
549 case 'SELECT_DISTINCT'; |
|
550 |
|
551 $sql = str_replace('_', ' ', $query) . ' ' . $array['SELECT'] . ' FROM '; |
|
552 |
|
553 // Build table array. We also build an alias array for later checks. |
|
554 $table_array = $aliases = array(); |
|
555 $used_multi_alias = false; |
|
556 |
|
557 foreach ($array['FROM'] as $table_name => $alias) |
|
558 { |
|
559 if (is_array($alias)) |
|
560 { |
|
561 $used_multi_alias = true; |
|
562 |
|
563 foreach ($alias as $multi_alias) |
|
564 { |
|
565 $table_array[] = $table_name . ' ' . $multi_alias; |
|
566 $aliases[] = $multi_alias; |
|
567 } |
|
568 } |
|
569 else |
|
570 { |
|
571 $table_array[] = $table_name . ' ' . $alias; |
|
572 $aliases[] = $alias; |
|
573 } |
|
574 } |
|
575 |
|
576 // We run the following code to determine if we need to re-order the table array. ;) |
|
577 // The reason for this is that for multi-aliased tables (two equal tables) in the FROM statement the last table need to match the first comparison. |
|
578 // DBMS who rely on this: Oracle, PostgreSQL and MSSQL. For all other DBMS it makes absolutely no difference in which order the table is. |
|
579 if (!empty($array['LEFT_JOIN']) && sizeof($array['FROM']) > 1 && $used_multi_alias !== false) |
|
580 { |
|
581 // Take first LEFT JOIN |
|
582 $join = current($array['LEFT_JOIN']); |
|
583 |
|
584 // Determine the table used there (even if there are more than one used, we only want to have one |
|
585 preg_match('/(' . implode('|', $aliases) . ')\.[^\s]+/U', str_replace(array('(', ')', 'AND', 'OR', ' '), '', $join['ON']), $matches); |
|
586 |
|
587 // If there is a first join match, we need to make sure the table order is correct |
|
588 if (!empty($matches[1])) |
|
589 { |
|
590 $first_join_match = trim($matches[1]); |
|
591 $table_array = $last = array(); |
|
592 |
|
593 foreach ($array['FROM'] as $table_name => $alias) |
|
594 { |
|
595 if (is_array($alias)) |
|
596 { |
|
597 foreach ($alias as $multi_alias) |
|
598 { |
|
599 ($multi_alias === $first_join_match) ? $last[] = $table_name . ' ' . $multi_alias : $table_array[] = $table_name . ' ' . $multi_alias; |
|
600 } |
|
601 } |
|
602 else |
|
603 { |
|
604 ($alias === $first_join_match) ? $last[] = $table_name . ' ' . $alias : $table_array[] = $table_name . ' ' . $alias; |
|
605 } |
|
606 } |
|
607 |
|
608 $table_array = array_merge($table_array, $last); |
|
609 } |
|
610 } |
|
611 |
|
612 $sql .= $this->_sql_custom_build('FROM', implode(' CROSS JOIN ', $table_array)); |
|
613 |
|
614 if (!empty($array['LEFT_JOIN'])) |
|
615 { |
|
616 foreach ($array['LEFT_JOIN'] as $join) |
|
617 { |
|
618 $sql .= ' LEFT JOIN ' . key($join['FROM']) . ' ' . current($join['FROM']) . ' ON (' . $join['ON'] . ')'; |
|
619 } |
|
620 } |
|
621 |
|
622 if (!empty($array['WHERE'])) |
|
623 { |
|
624 $sql .= ' WHERE ' . $this->_sql_custom_build('WHERE', $array['WHERE']); |
|
625 } |
|
626 |
|
627 if (!empty($array['GROUP_BY'])) |
|
628 { |
|
629 $sql .= ' GROUP BY ' . $array['GROUP_BY']; |
|
630 } |
|
631 |
|
632 if (!empty($array['ORDER_BY'])) |
|
633 { |
|
634 $sql .= ' ORDER BY ' . $array['ORDER_BY']; |
|
635 } |
|
636 |
|
637 break; |
|
638 } |
|
639 |
|
640 return $sql; |
|
641 } |
|
642 |
|
643 /** |
|
644 * display sql error page |
|
645 */ |
|
646 function sql_error($sql = '') |
|
647 { |
|
648 global $auth, $user, $config; |
|
649 |
|
650 // Set var to retrieve errored status |
|
651 $this->sql_error_triggered = true; |
|
652 $this->sql_error_sql = $sql; |
|
653 |
|
654 $this->sql_error_returned = $this->_sql_error(); |
|
655 |
|
656 if (!$this->return_on_error) |
|
657 { |
|
658 $message = 'SQL ERROR [ ' . $this->sql_layer . ' ]<br /><br />' . $this->sql_error_returned['message'] . ' [' . $this->sql_error_returned['code'] . ']'; |
|
659 |
|
660 // Show complete SQL error and path to administrators only |
|
661 // Additionally show complete error on installation or if extended debug mode is enabled |
|
662 // The DEBUG_EXTRA constant is for development only! |
|
663 if ((isset($auth) && $auth->acl_get('a_')) || defined('IN_INSTALL') || defined('DEBUG_EXTRA')) |
|
664 { |
|
665 // Print out a nice backtrace... |
|
666 $backtrace = get_backtrace(); |
|
667 |
|
668 $message .= ($sql) ? '<br /><br />SQL<br /><br />' . htmlspecialchars($sql) : ''; |
|
669 $message .= ($backtrace) ? '<br /><br />BACKTRACE<br />' . $backtrace : ''; |
|
670 $message .= '<br />'; |
|
671 } |
|
672 else |
|
673 { |
|
674 // If error occurs in initiating the session we need to use a pre-defined language string |
|
675 // This could happen if the connection could not be established for example (then we are not able to grab the default language) |
|
676 if (!isset($user->lang['SQL_ERROR_OCCURRED'])) |
|
677 { |
|
678 $message .= '<br /><br />An sql error occurred while fetching this page. Please contact an administrator if this problem persists.'; |
|
679 } |
|
680 else |
|
681 { |
|
682 if (!empty($config['board_contact'])) |
|
683 { |
|
684 $message .= '<br /><br />' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>'); |
|
685 } |
|
686 else |
|
687 { |
|
688 $message .= '<br /><br />' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '', ''); |
|
689 } |
|
690 } |
|
691 } |
|
692 |
|
693 if ($this->transaction) |
|
694 { |
|
695 $this->sql_transaction('rollback'); |
|
696 } |
|
697 |
|
698 if (strlen($message) > 1024) |
|
699 { |
|
700 // We need to define $msg_long_text here to circumvent text stripping. |
|
701 global $msg_long_text; |
|
702 $msg_long_text = $message; |
|
703 |
|
704 trigger_error(false, E_USER_ERROR); |
|
705 } |
|
706 |
|
707 trigger_error($message, E_USER_ERROR); |
|
708 } |
|
709 |
|
710 if ($this->transaction) |
|
711 { |
|
712 $this->sql_transaction('rollback'); |
|
713 } |
|
714 |
|
715 return $this->sql_error_returned; |
|
716 } |
|
717 |
|
718 /** |
|
719 * Explain queries |
|
720 */ |
|
721 function sql_report($mode, $query = '') |
|
722 { |
|
723 global $cache, $starttime, $phpbb_root_path, $user; |
|
724 |
|
725 if (empty($_REQUEST['explain'])) |
|
726 { |
|
727 return false; |
|
728 } |
|
729 |
|
730 if (!$query && $this->query_hold != '') |
|
731 { |
|
732 $query = $this->query_hold; |
|
733 } |
|
734 |
|
735 switch ($mode) |
|
736 { |
|
737 case 'display': |
|
738 if (!empty($cache)) |
|
739 { |
|
740 $cache->unload(); |
|
741 } |
|
742 $this->sql_close(); |
|
743 |
|
744 $mtime = explode(' ', microtime()); |
|
745 $totaltime = $mtime[0] + $mtime[1] - $starttime; |
|
746 |
|
747 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
748 <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> |
|
749 <head> |
|
750 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> |
|
751 <meta http-equiv="Content-Style-Type" content="text/css" /> |
|
752 <meta http-equiv="imagetoolbar" content="no" /> |
|
753 <title>SQL Report</title> |
|
754 <link href="' . $phpbb_root_path . 'adm/style/admin.css" rel="stylesheet" type="text/css" media="screen" /> |
|
755 </head> |
|
756 <body id="errorpage"> |
|
757 <div id="wrap"> |
|
758 <div id="page-header"> |
|
759 <a href="' . build_url('explain') . '">Return to previous page</a> |
|
760 </div> |
|
761 <div id="page-body"> |
|
762 <div id="acp"> |
|
763 <div class="panel"> |
|
764 <span class="corners-top"><span></span></span> |
|
765 <div id="content"> |
|
766 <h1>SQL Report</h1> |
|
767 <br /> |
|
768 <p><b>Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries['normal']} queries" . (($this->num_queries['cached']) ? " + {$this->num_queries['cached']} " . (($this->num_queries['cached'] == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '</b></p> |
|
769 |
|
770 <p>Time spent on ' . $this->sql_layer . ' queries: <b>' . round($this->sql_time, 5) . 's</b> | Time spent on PHP: <b>' . round($totaltime - $this->sql_time, 5) . 's</b></p> |
|
771 |
|
772 <br /><br /> |
|
773 ' . $this->sql_report . ' |
|
774 </div> |
|
775 <span class="corners-bottom"><span></span></span> |
|
776 </div> |
|
777 </div> |
|
778 </div> |
|
779 <div id="page-footer"> |
|
780 Powered by <a href="http://www.phpbb.com/">phpBB</a>® Forum Software © phpBB Group |
|
781 </div> |
|
782 </div> |
|
783 </body> |
|
784 </html>'; |
|
785 |
|
786 exit_handler(); |
|
787 |
|
788 break; |
|
789 |
|
790 case 'stop': |
|
791 $endtime = explode(' ', microtime()); |
|
792 $endtime = $endtime[0] + $endtime[1]; |
|
793 |
|
794 $this->sql_report .= ' |
|
795 |
|
796 <table cellspacing="1"> |
|
797 <thead> |
|
798 <tr> |
|
799 <th>Query #' . $this->num_queries['total'] . '</th> |
|
800 </tr> |
|
801 </thead> |
|
802 <tbody> |
|
803 <tr> |
|
804 <td class="row3"><textarea style="font-family:\'Courier New\',monospace;width:99%" rows="5" cols="10">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td> |
|
805 </tr> |
|
806 </tbody> |
|
807 </table> |
|
808 |
|
809 ' . $this->html_hold . ' |
|
810 |
|
811 <p style="text-align: center;"> |
|
812 '; |
|
813 |
|
814 if ($this->query_result) |
|
815 { |
|
816 if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) |
|
817 { |
|
818 $this->sql_report .= 'Affected rows: <b>' . $this->sql_affectedrows($this->query_result) . '</b> | '; |
|
819 } |
|
820 $this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: <b>' . sprintf('%.5f', $endtime - $this->curtime) . 's</b>'; |
|
821 } |
|
822 else |
|
823 { |
|
824 $error = $this->sql_error(); |
|
825 $this->sql_report .= '<b style="color: red">FAILED</b> - ' . $this->sql_layer . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); |
|
826 } |
|
827 |
|
828 $this->sql_report .= '</p><br /><br />'; |
|
829 |
|
830 $this->sql_time += $endtime - $this->curtime; |
|
831 break; |
|
832 |
|
833 case 'start': |
|
834 $this->query_hold = $query; |
|
835 $this->html_hold = ''; |
|
836 |
|
837 $this->_sql_report($mode, $query); |
|
838 |
|
839 $this->curtime = explode(' ', microtime()); |
|
840 $this->curtime = $this->curtime[0] + $this->curtime[1]; |
|
841 |
|
842 break; |
|
843 |
|
844 case 'add_select_row': |
|
845 |
|
846 $html_table = func_get_arg(2); |
|
847 $row = func_get_arg(3); |
|
848 |
|
849 if (!$html_table && sizeof($row)) |
|
850 { |
|
851 $html_table = true; |
|
852 $this->html_hold .= '<table cellspacing="1"><tr>'; |
|
853 |
|
854 foreach (array_keys($row) as $val) |
|
855 { |
|
856 $this->html_hold .= '<th>' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '</th>'; |
|
857 } |
|
858 $this->html_hold .= '</tr>'; |
|
859 } |
|
860 $this->html_hold .= '<tr>'; |
|
861 |
|
862 $class = 'row1'; |
|
863 foreach (array_values($row) as $val) |
|
864 { |
|
865 $class = ($class == 'row1') ? 'row2' : 'row1'; |
|
866 $this->html_hold .= '<td class="' . $class . '">' . (($val) ? $val : ' ') . '</td>'; |
|
867 } |
|
868 $this->html_hold .= '</tr>'; |
|
869 |
|
870 return $html_table; |
|
871 |
|
872 break; |
|
873 |
|
874 case 'fromcache': |
|
875 |
|
876 $this->_sql_report($mode, $query); |
|
877 |
|
878 break; |
|
879 |
|
880 case 'record_fromcache': |
|
881 |
|
882 $endtime = func_get_arg(2); |
|
883 $splittime = func_get_arg(3); |
|
884 |
|
885 $time_cache = $endtime - $this->curtime; |
|
886 $time_db = $splittime - $endtime; |
|
887 $color = ($time_db > $time_cache) ? 'green' : 'red'; |
|
888 |
|
889 $this->sql_report .= '<table cellspacing="1"><thead><tr><th>Query results obtained from the cache</th></tr></thead><tbody><tr>'; |
|
890 $this->sql_report .= '<td class="row3"><textarea style="font-family:\'Courier New\',monospace;width:99%" rows="5" cols="10">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></tbody></table>'; |
|
891 $this->sql_report .= '<p style="text-align: center;">'; |
|
892 $this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p><br /><br />'; |
|
893 |
|
894 // Pad the start time to not interfere with page timing |
|
895 $starttime += $time_db; |
|
896 |
|
897 break; |
|
898 |
|
899 default: |
|
900 |
|
901 $this->_sql_report($mode, $query); |
|
902 |
|
903 break; |
|
904 } |
|
905 |
|
906 return true; |
|
907 } |
|
908 } |
|
909 |
|
910 /** |
|
911 * This variable holds the class name to use later |
|
912 */ |
|
913 $sql_db = (!empty($dbms)) ? 'dbal_' . basename($dbms) : 'dbal'; |
|
914 |
|
915 ?> |