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 | * SQL Transaction
|
---|
246 | * @access private
|
---|
247 | */
|
---|
248 | function sql_transaction($status = 'begin')
|
---|
249 | {
|
---|
250 | switch ($status)
|
---|
251 | {
|
---|
252 | case 'begin':
|
---|
253 | // If we are within a transaction we will not open another one, but enclose the current one to not loose data (prevening auto commit)
|
---|
254 | if ($this->transaction)
|
---|
255 | {
|
---|
256 | $this->transactions++;
|
---|
257 | return true;
|
---|
258 | }
|
---|
259 |
|
---|
260 | $result = $this->_sql_transaction('begin');
|
---|
261 |
|
---|
262 | if (!$result)
|
---|
263 | {
|
---|
264 | $this->sql_error();
|
---|
265 | }
|
---|
266 |
|
---|
267 | $this->transaction = true;
|
---|
268 | break;
|
---|
269 |
|
---|
270 | case 'commit':
|
---|
271 | // If there was a previously opened transaction we do not commit yet... but count back the number of inner transactions
|
---|
272 | if ($this->transaction && $this->transactions)
|
---|
273 | {
|
---|
274 | $this->transactions--;
|
---|
275 | return true;
|
---|
276 | }
|
---|
277 |
|
---|
278 | // Check if there is a transaction (no transaction can happen if there was an error, with a combined rollback and error returning enabled)
|
---|
279 | // This implies we have transaction always set for autocommit db's
|
---|
280 | if (!$this->transaction)
|
---|
281 | {
|
---|
282 | return false;
|
---|
283 | }
|
---|
284 |
|
---|
285 | $result = $this->_sql_transaction('commit');
|
---|
286 |
|
---|
287 | if (!$result)
|
---|
288 | {
|
---|
289 | $this->sql_error();
|
---|
290 | }
|
---|
291 |
|
---|
292 | $this->transaction = false;
|
---|
293 | $this->transactions = 0;
|
---|
294 | break;
|
---|
295 |
|
---|
296 | case 'rollback':
|
---|
297 | $result = $this->_sql_transaction('rollback');
|
---|
298 | $this->transaction = false;
|
---|
299 | $this->transactions = 0;
|
---|
300 | break;
|
---|
301 |
|
---|
302 | default:
|
---|
303 | $result = $this->_sql_transaction($status);
|
---|
304 | break;
|
---|
305 | }
|
---|
306 |
|
---|
307 | return $result;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Build sql statement from array for insert/update/select statements
|
---|
312 | *
|
---|
313 | * Idea for this from Ikonboard
|
---|
314 | * Possible query values: INSERT, INSERT_SELECT, UPDATE, SELECT
|
---|
315 | *
|
---|
316 | */
|
---|
317 | function sql_build_array($query, $assoc_ary = false)
|
---|
318 | {
|
---|
319 | if (!is_array($assoc_ary))
|
---|
320 | {
|
---|
321 | return false;
|
---|
322 | }
|
---|
323 |
|
---|
324 | $fields = $values = array();
|
---|
325 |
|
---|
326 | if ($query == 'INSERT' || $query == 'INSERT_SELECT')
|
---|
327 | {
|
---|
328 | foreach ($assoc_ary as $key => $var)
|
---|
329 | {
|
---|
330 | $fields[] = $key;
|
---|
331 |
|
---|
332 | if (is_array($var) && is_string($var[0]))
|
---|
333 | {
|
---|
334 | // This is used for INSERT_SELECT(s)
|
---|
335 | $values[] = $var[0];
|
---|
336 | }
|
---|
337 | else
|
---|
338 | {
|
---|
339 | $values[] = $this->_sql_validate_value($var);
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | $query = ($query == 'INSERT') ? ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')' : ' (' . implode(', ', $fields) . ') SELECT ' . implode(', ', $values) . ' ';
|
---|
344 | }
|
---|
345 | else if ($query == 'MULTI_INSERT')
|
---|
346 | {
|
---|
347 | trigger_error('The MULTI_INSERT query value is no longer supported. Please use sql_multi_insert() instead.', E_USER_ERROR);
|
---|
348 | }
|
---|
349 | else if ($query == 'UPDATE' || $query == 'SELECT')
|
---|
350 | {
|
---|
351 | $values = array();
|
---|
352 | foreach ($assoc_ary as $key => $var)
|
---|
353 | {
|
---|
354 | $values[] = "$key = " . $this->_sql_validate_value($var);
|
---|
355 | }
|
---|
356 | $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values);
|
---|
357 | }
|
---|
358 |
|
---|
359 | return $query;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Build IN or NOT IN sql comparison string, uses <> or = on single element
|
---|
364 | * arrays to improve comparison speed
|
---|
365 | *
|
---|
366 | * @access public
|
---|
367 | * @param string $field name of the sql column that shall be compared
|
---|
368 | * @param array $array array of values that are allowed (IN) or not allowed (NOT IN)
|
---|
369 | * @param bool $negate true for NOT IN (), false for IN () (default)
|
---|
370 | * @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.
|
---|
371 | */
|
---|
372 | function sql_in_set($field, $array, $negate = false, $allow_empty_set = false)
|
---|
373 | {
|
---|
374 | if (!sizeof($array))
|
---|
375 | {
|
---|
376 | if (!$allow_empty_set)
|
---|
377 | {
|
---|
378 | // Print the backtrace to help identifying the location of the problematic code
|
---|
379 | $this->sql_error('No values specified for SQL IN comparison');
|
---|
380 | }
|
---|
381 | else
|
---|
382 | {
|
---|
383 | // NOT IN () actually means everything so use a tautology
|
---|
384 | if ($negate)
|
---|
385 | {
|
---|
386 | return '1=1';
|
---|
387 | }
|
---|
388 | // IN () actually means nothing so use a contradiction
|
---|
389 | else
|
---|
390 | {
|
---|
391 | return '1=0';
|
---|
392 | }
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | if (!is_array($array))
|
---|
397 | {
|
---|
398 | $array = array($array);
|
---|
399 | }
|
---|
400 |
|
---|
401 | if (sizeof($array) == 1)
|
---|
402 | {
|
---|
403 | @reset($array);
|
---|
404 | $var = current($array);
|
---|
405 |
|
---|
406 | return $field . ($negate ? ' <> ' : ' = ') . $this->_sql_validate_value($var);
|
---|
407 | }
|
---|
408 | else
|
---|
409 | {
|
---|
410 | return $field . ($negate ? ' NOT IN ' : ' IN ') . '(' . implode(', ', array_map(array($this, '_sql_validate_value'), $array)) . ')';
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Run binary AND operator on DB column.
|
---|
416 | * Results in sql statement: "{$column_name} & (1 << {$bit}) {$compare}"
|
---|
417 | *
|
---|
418 | * @param string $column_name The column name to use
|
---|
419 | * @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
|
---|
420 | * @param string $compare Any custom SQL code after the check (for example "= 0")
|
---|
421 | */
|
---|
422 | function sql_bit_and($column_name, $bit, $compare = '')
|
---|
423 | {
|
---|
424 | if (method_exists($this, '_sql_bit_and'))
|
---|
425 | {
|
---|
426 | return $this->_sql_bit_and($column_name, $bit, $compare);
|
---|
427 | }
|
---|
428 |
|
---|
429 | return $column_name . ' & ' . (1 << $bit) . (($compare) ? ' ' . $compare : '');
|
---|
430 | }
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * Run binary OR operator on DB column.
|
---|
434 | * Results in sql statement: "{$column_name} | (1 << {$bit}) {$compare}"
|
---|
435 | *
|
---|
436 | * @param string $column_name The column name to use
|
---|
437 | * @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
|
---|
438 | * @param string $compare Any custom SQL code after the check (for example "= 0")
|
---|
439 | */
|
---|
440 | function sql_bit_or($column_name, $bit, $compare = '')
|
---|
441 | {
|
---|
442 | if (method_exists($this, '_sql_bit_or'))
|
---|
443 | {
|
---|
444 | return $this->_sql_bit_or($column_name, $bit, $compare);
|
---|
445 | }
|
---|
446 |
|
---|
447 | return $column_name . ' | ' . (1 << $bit) . (($compare) ? ' ' . $compare : '');
|
---|
448 | }
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Run more than one insert statement.
|
---|
452 | *
|
---|
453 | * @param string $table table name to run the statements on
|
---|
454 | * @param array &$sql_ary multi-dimensional array holding the statement data.
|
---|
455 | *
|
---|
456 | * @return bool false if no statements were executed.
|
---|
457 | * @access public
|
---|
458 | */
|
---|
459 | function sql_multi_insert($table, &$sql_ary)
|
---|
460 | {
|
---|
461 | if (!sizeof($sql_ary))
|
---|
462 | {
|
---|
463 | return false;
|
---|
464 | }
|
---|
465 |
|
---|
466 | if ($this->multi_insert)
|
---|
467 | {
|
---|
468 | $ary = array();
|
---|
469 | foreach ($sql_ary as $id => $_sql_ary)
|
---|
470 | {
|
---|
471 | // If by accident the sql array is only one-dimensional we build a normal insert statement
|
---|
472 | if (!is_array($_sql_ary))
|
---|
473 | {
|
---|
474 | return $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $sql_ary));
|
---|
475 | }
|
---|
476 |
|
---|
477 | $values = array();
|
---|
478 | foreach ($_sql_ary as $key => $var)
|
---|
479 | {
|
---|
480 | $values[] = $this->_sql_validate_value($var);
|
---|
481 | }
|
---|
482 | $ary[] = '(' . implode(', ', $values) . ')';
|
---|
483 | }
|
---|
484 |
|
---|
485 | return $this->sql_query('INSERT INTO ' . $table . ' ' . ' (' . implode(', ', array_keys($sql_ary[0])) . ') VALUES ' . implode(', ', $ary));
|
---|
486 | }
|
---|
487 | else
|
---|
488 | {
|
---|
489 | foreach ($sql_ary as $ary)
|
---|
490 | {
|
---|
491 | if (!is_array($ary))
|
---|
492 | {
|
---|
493 | return false;
|
---|
494 | }
|
---|
495 |
|
---|
496 | $result = $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $ary));
|
---|
497 |
|
---|
498 | if (!$result)
|
---|
499 | {
|
---|
500 | return false;
|
---|
501 | }
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 | return true;
|
---|
506 | }
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Function for validating values
|
---|
510 | * @access private
|
---|
511 | */
|
---|
512 | function _sql_validate_value($var)
|
---|
513 | {
|
---|
514 | if (is_null($var))
|
---|
515 | {
|
---|
516 | return 'NULL';
|
---|
517 | }
|
---|
518 | else if (is_string($var))
|
---|
519 | {
|
---|
520 | return "'" . $this->sql_escape($var) . "'";
|
---|
521 | }
|
---|
522 | else
|
---|
523 | {
|
---|
524 | return (is_bool($var)) ? intval($var) : $var;
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | /**
|
---|
529 | * Build sql statement from array for select and select distinct statements
|
---|
530 | *
|
---|
531 | * Possible query values: SELECT, SELECT_DISTINCT
|
---|
532 | */
|
---|
533 | function sql_build_query($query, $array)
|
---|
534 | {
|
---|
535 | $sql = '';
|
---|
536 | switch ($query)
|
---|
537 | {
|
---|
538 | case 'SELECT':
|
---|
539 | case 'SELECT_DISTINCT';
|
---|
540 |
|
---|
541 | $sql = str_replace('_', ' ', $query) . ' ' . $array['SELECT'] . ' FROM ';
|
---|
542 |
|
---|
543 | // Build table array. We also build an alias array for later checks.
|
---|
544 | $table_array = $aliases = array();
|
---|
545 | $used_multi_alias = false;
|
---|
546 |
|
---|
547 | foreach ($array['FROM'] as $table_name => $alias)
|
---|
548 | {
|
---|
549 | if (is_array($alias))
|
---|
550 | {
|
---|
551 | $used_multi_alias = true;
|
---|
552 |
|
---|
553 | foreach ($alias as $multi_alias)
|
---|
554 | {
|
---|
555 | $table_array[] = $table_name . ' ' . $multi_alias;
|
---|
556 | $aliases[] = $multi_alias;
|
---|
557 | }
|
---|
558 | }
|
---|
559 | else
|
---|
560 | {
|
---|
561 | $table_array[] = $table_name . ' ' . $alias;
|
---|
562 | $aliases[] = $alias;
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | // We run the following code to determine if we need to re-order the table array. ;)
|
---|
567 | // 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.
|
---|
568 | // DBMS who rely on this: Oracle, PostgreSQL and MSSQL. For all other DBMS it makes absolutely no difference in which order the table is.
|
---|
569 | if (!empty($array['LEFT_JOIN']) && sizeof($array['FROM']) > 1 && $used_multi_alias !== false)
|
---|
570 | {
|
---|
571 | // Take first LEFT JOIN
|
---|
572 | $join = current($array['LEFT_JOIN']);
|
---|
573 |
|
---|
574 | // Determine the table used there (even if there are more than one used, we only want to have one
|
---|
575 | preg_match('/(' . implode('|', $aliases) . ')\.[^\s]+/U', str_replace(array('(', ')', 'AND', 'OR', ' '), '', $join['ON']), $matches);
|
---|
576 |
|
---|
577 | // If there is a first join match, we need to make sure the table order is correct
|
---|
578 | if (!empty($matches[1]))
|
---|
579 | {
|
---|
580 | $first_join_match = trim($matches[1]);
|
---|
581 | $table_array = $last = array();
|
---|
582 |
|
---|
583 | foreach ($array['FROM'] as $table_name => $alias)
|
---|
584 | {
|
---|
585 | if (is_array($alias))
|
---|
586 | {
|
---|
587 | foreach ($alias as $multi_alias)
|
---|
588 | {
|
---|
589 | ($multi_alias === $first_join_match) ? $last[] = $table_name . ' ' . $multi_alias : $table_array[] = $table_name . ' ' . $multi_alias;
|
---|
590 | }
|
---|
591 | }
|
---|
592 | else
|
---|
593 | {
|
---|
594 | ($alias === $first_join_match) ? $last[] = $table_name . ' ' . $alias : $table_array[] = $table_name . ' ' . $alias;
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | $table_array = array_merge($table_array, $last);
|
---|
599 | }
|
---|
600 | }
|
---|
601 |
|
---|
602 | $sql .= $this->_sql_custom_build('FROM', implode(', ', $table_array));
|
---|
603 |
|
---|
604 | if (!empty($array['LEFT_JOIN']))
|
---|
605 | {
|
---|
606 | foreach ($array['LEFT_JOIN'] as $join)
|
---|
607 | {
|
---|
608 | $sql .= ' LEFT JOIN ' . key($join['FROM']) . ' ' . current($join['FROM']) . ' ON (' . $join['ON'] . ')';
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | if (!empty($array['WHERE']))
|
---|
613 | {
|
---|
614 | $sql .= ' WHERE ' . $this->_sql_custom_build('WHERE', $array['WHERE']);
|
---|
615 | }
|
---|
616 |
|
---|
617 | if (!empty($array['GROUP_BY']))
|
---|
618 | {
|
---|
619 | $sql .= ' GROUP BY ' . $array['GROUP_BY'];
|
---|
620 | }
|
---|
621 |
|
---|
622 | if (!empty($array['ORDER_BY']))
|
---|
623 | {
|
---|
624 | $sql .= ' ORDER BY ' . $array['ORDER_BY'];
|
---|
625 | }
|
---|
626 |
|
---|
627 | break;
|
---|
628 | }
|
---|
629 |
|
---|
630 | return $sql;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * display sql error page
|
---|
635 | */
|
---|
636 | function sql_error($sql = '')
|
---|
637 | {
|
---|
638 | global $auth, $user, $config;
|
---|
639 |
|
---|
640 | // Set var to retrieve errored status
|
---|
641 | $this->sql_error_triggered = true;
|
---|
642 | $this->sql_error_sql = $sql;
|
---|
643 |
|
---|
644 | $this->sql_error_returned = $this->_sql_error();
|
---|
645 |
|
---|
646 | if (!$this->return_on_error)
|
---|
647 | {
|
---|
648 | $message = 'SQL ERROR [ ' . $this->sql_layer . ' ]<br /><br />' . $this->sql_error_returned['message'] . ' [' . $this->sql_error_returned['code'] . ']';
|
---|
649 |
|
---|
650 | // Show complete SQL error and path to administrators only
|
---|
651 | // Additionally show complete error on installation or if extended debug mode is enabled
|
---|
652 | // The DEBUG_EXTRA constant is for development only!
|
---|
653 | if ((isset($auth) && $auth->acl_get('a_')) || defined('IN_INSTALL') || defined('DEBUG_EXTRA'))
|
---|
654 | {
|
---|
655 | // Print out a nice backtrace...
|
---|
656 | $backtrace = get_backtrace();
|
---|
657 |
|
---|
658 | $message .= ($sql) ? '<br /><br />SQL<br /><br />' . htmlspecialchars($sql) : '';
|
---|
659 | $message .= ($backtrace) ? '<br /><br />BACKTRACE<br />' . $backtrace : '';
|
---|
660 | $message .= '<br />';
|
---|
661 | }
|
---|
662 | else
|
---|
663 | {
|
---|
664 | // If error occurs in initiating the session we need to use a pre-defined language string
|
---|
665 | // This could happen if the connection could not be established for example (then we are not able to grab the default language)
|
---|
666 | if (!isset($user->lang['SQL_ERROR_OCCURRED']))
|
---|
667 | {
|
---|
668 | $message .= '<br /><br />An sql error occurred while fetching this page. Please contact an administrator if this problem persists.';
|
---|
669 | }
|
---|
670 | else
|
---|
671 | {
|
---|
672 | if (!empty($config['board_contact']))
|
---|
673 | {
|
---|
674 | $message .= '<br /><br />' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>');
|
---|
675 | }
|
---|
676 | else
|
---|
677 | {
|
---|
678 | $message .= '<br /><br />' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '', '');
|
---|
679 | }
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | if ($this->transaction)
|
---|
684 | {
|
---|
685 | $this->sql_transaction('rollback');
|
---|
686 | }
|
---|
687 |
|
---|
688 | if (strlen($message) > 1024)
|
---|
689 | {
|
---|
690 | // We need to define $msg_long_text here to circumvent text stripping.
|
---|
691 | global $msg_long_text;
|
---|
692 | $msg_long_text = $message;
|
---|
693 |
|
---|
694 | trigger_error(false, E_USER_ERROR);
|
---|
695 | }
|
---|
696 |
|
---|
697 | trigger_error($message, E_USER_ERROR);
|
---|
698 | }
|
---|
699 |
|
---|
700 | if ($this->transaction)
|
---|
701 | {
|
---|
702 | $this->sql_transaction('rollback');
|
---|
703 | }
|
---|
704 |
|
---|
705 | return $this->sql_error_returned;
|
---|
706 | }
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Explain queries
|
---|
710 | */
|
---|
711 | function sql_report($mode, $query = '')
|
---|
712 | {
|
---|
713 | global $cache, $starttime, $phpbb_root_path, $user;
|
---|
714 |
|
---|
715 | if (empty($_REQUEST['explain']))
|
---|
716 | {
|
---|
717 | return false;
|
---|
718 | }
|
---|
719 |
|
---|
720 | if (!$query && $this->query_hold != '')
|
---|
721 | {
|
---|
722 | $query = $this->query_hold;
|
---|
723 | }
|
---|
724 |
|
---|
725 | switch ($mode)
|
---|
726 | {
|
---|
727 | case 'display':
|
---|
728 | if (!empty($cache))
|
---|
729 | {
|
---|
730 | $cache->unload();
|
---|
731 | }
|
---|
732 | $this->sql_close();
|
---|
733 |
|
---|
734 | $mtime = explode(' ', microtime());
|
---|
735 | $totaltime = $mtime[0] + $mtime[1] - $starttime;
|
---|
736 |
|
---|
737 | echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
---|
738 | <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
|
---|
739 | <head>
|
---|
740 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
---|
741 | <meta http-equiv="Content-Style-Type" content="text/css" />
|
---|
742 | <meta http-equiv="imagetoolbar" content="no" />
|
---|
743 | <title>SQL Report</title>
|
---|
744 | <link href="' . $phpbb_root_path . 'adm/style/admin.css" rel="stylesheet" type="text/css" media="screen" />
|
---|
745 | </head>
|
---|
746 | <body id="errorpage">
|
---|
747 | <div id="wrap">
|
---|
748 | <div id="page-header">
|
---|
749 | <a href="' . build_url('explain') . '">Return to previous page</a>
|
---|
750 | </div>
|
---|
751 | <div id="page-body">
|
---|
752 | <div id="acp">
|
---|
753 | <div class="panel">
|
---|
754 | <span class="corners-top"><span></span></span>
|
---|
755 | <div id="content">
|
---|
756 | <h1>SQL Report</h1>
|
---|
757 | <br />
|
---|
758 | <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>
|
---|
759 |
|
---|
760 | <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>
|
---|
761 |
|
---|
762 | <br /><br />
|
---|
763 | ' . $this->sql_report . '
|
---|
764 | </div>
|
---|
765 | <span class="corners-bottom"><span></span></span>
|
---|
766 | </div>
|
---|
767 | </div>
|
---|
768 | </div>
|
---|
769 | <div id="page-footer">
|
---|
770 | Powered by phpBB © 2000, 2002, 2005, 2007 <a href="http://www.phpbb.com/">phpBB Group</a>
|
---|
771 | </div>
|
---|
772 | </div>
|
---|
773 | </body>
|
---|
774 | </html>';
|
---|
775 |
|
---|
776 | exit_handler();
|
---|
777 |
|
---|
778 | break;
|
---|
779 |
|
---|
780 | case 'stop':
|
---|
781 | $endtime = explode(' ', microtime());
|
---|
782 | $endtime = $endtime[0] + $endtime[1];
|
---|
783 |
|
---|
784 | $this->sql_report .= '
|
---|
785 |
|
---|
786 | <table cellspacing="1">
|
---|
787 | <thead>
|
---|
788 | <tr>
|
---|
789 | <th>Query #' . $this->num_queries['total'] . '</th>
|
---|
790 | </tr>
|
---|
791 | </thead>
|
---|
792 | <tbody>
|
---|
793 | <tr>
|
---|
794 | <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>
|
---|
795 | </tr>
|
---|
796 | </tbody>
|
---|
797 | </table>
|
---|
798 |
|
---|
799 | ' . $this->html_hold . '
|
---|
800 |
|
---|
801 | <p style="text-align: center;">
|
---|
802 | ';
|
---|
803 |
|
---|
804 | if ($this->query_result)
|
---|
805 | {
|
---|
806 | if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query))
|
---|
807 | {
|
---|
808 | $this->sql_report .= 'Affected rows: <b>' . $this->sql_affectedrows($this->query_result) . '</b> | ';
|
---|
809 | }
|
---|
810 | $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>';
|
---|
811 | }
|
---|
812 | else
|
---|
813 | {
|
---|
814 | $error = $this->sql_error();
|
---|
815 | $this->sql_report .= '<b style="color: red">FAILED</b> - ' . $this->sql_layer . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']);
|
---|
816 | }
|
---|
817 |
|
---|
818 | $this->sql_report .= '</p><br /><br />';
|
---|
819 |
|
---|
820 | $this->sql_time += $endtime - $this->curtime;
|
---|
821 | break;
|
---|
822 |
|
---|
823 | case 'start':
|
---|
824 | $this->query_hold = $query;
|
---|
825 | $this->html_hold = '';
|
---|
826 |
|
---|
827 | $this->_sql_report($mode, $query);
|
---|
828 |
|
---|
829 | $this->curtime = explode(' ', microtime());
|
---|
830 | $this->curtime = $this->curtime[0] + $this->curtime[1];
|
---|
831 |
|
---|
832 | break;
|
---|
833 |
|
---|
834 | case 'add_select_row':
|
---|
835 |
|
---|
836 | $html_table = func_get_arg(2);
|
---|
837 | $row = func_get_arg(3);
|
---|
838 |
|
---|
839 | if (!$html_table && sizeof($row))
|
---|
840 | {
|
---|
841 | $html_table = true;
|
---|
842 | $this->html_hold .= '<table cellspacing="1"><tr>';
|
---|
843 |
|
---|
844 | foreach (array_keys($row) as $val)
|
---|
845 | {
|
---|
846 | $this->html_hold .= '<th>' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '</th>';
|
---|
847 | }
|
---|
848 | $this->html_hold .= '</tr>';
|
---|
849 | }
|
---|
850 | $this->html_hold .= '<tr>';
|
---|
851 |
|
---|
852 | $class = 'row1';
|
---|
853 | foreach (array_values($row) as $val)
|
---|
854 | {
|
---|
855 | $class = ($class == 'row1') ? 'row2' : 'row1';
|
---|
856 | $this->html_hold .= '<td class="' . $class . '">' . (($val) ? $val : ' ') . '</td>';
|
---|
857 | }
|
---|
858 | $this->html_hold .= '</tr>';
|
---|
859 |
|
---|
860 | return $html_table;
|
---|
861 |
|
---|
862 | break;
|
---|
863 |
|
---|
864 | case 'fromcache':
|
---|
865 |
|
---|
866 | $this->_sql_report($mode, $query);
|
---|
867 |
|
---|
868 | break;
|
---|
869 |
|
---|
870 | case 'record_fromcache':
|
---|
871 |
|
---|
872 | $endtime = func_get_arg(2);
|
---|
873 | $splittime = func_get_arg(3);
|
---|
874 |
|
---|
875 | $time_cache = $endtime - $this->curtime;
|
---|
876 | $time_db = $splittime - $endtime;
|
---|
877 | $color = ($time_db > $time_cache) ? 'green' : 'red';
|
---|
878 |
|
---|
879 | $this->sql_report .= '<table cellspacing="1"><thead><tr><th>Query results obtained from the cache</th></tr></thead><tbody><tr>';
|
---|
880 | $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>';
|
---|
881 | $this->sql_report .= '<p style="text-align: center;">';
|
---|
882 | $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 />';
|
---|
883 |
|
---|
884 | // Pad the start time to not interfere with page timing
|
---|
885 | $starttime += $time_db;
|
---|
886 |
|
---|
887 | break;
|
---|
888 |
|
---|
889 | default:
|
---|
890 |
|
---|
891 | $this->_sql_report($mode, $query);
|
---|
892 |
|
---|
893 | break;
|
---|
894 | }
|
---|
895 |
|
---|
896 | return true;
|
---|
897 | }
|
---|
898 | }
|
---|
899 |
|
---|
900 | /**
|
---|
901 | * This variable holds the class name to use later
|
---|
902 | */
|
---|
903 | $sql_db = (!empty($dbms)) ? 'dbal_' . basename($dbms) : 'dbal';
|
---|
904 |
|
---|
905 | ?>
|
---|