Adjust some files to support new methods...
[phpbb.git] / phpBB / includes / db / mysql.php
blobff45a403e938cb332a2442a636a20e9bf5334e02
1 <?php
2 /**
4 * @package dbal
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 include_once(PHPBB_ROOT_PATH . 'includes/db/dbal.' . PHP_EXT);
21 /**
22 * MySQL Database Abstraction Layer
23 * Compatible with:
24 * MySQL 4.1+
25 * MySQL 5.0+
26 * @package dbal
28 class dbal_mysql extends dbal
30 var $multi_insert = true;
32 // Supports multiple table deletion
33 var $multi_table_deletion = true;
35 var $dbms_type = 'mysql';
37 /**
38 * Connect to server
39 * @access public
41 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
43 $this->persistency = $persistency;
44 $this->user = $sqluser;
45 $this->server = $sqlserver . (($port) ? ':' . $port : '');
46 $this->dbname = $database;
48 $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mysql_connect($this->server, $this->user, $sqlpassword, $new_link);
50 if ($this->db_connect_id && $this->dbname != '')
52 if (@mysql_select_db($this->dbname, $this->db_connect_id))
54 @mysql_query("SET NAMES 'utf8'", $this->db_connect_id);
56 // enforce strict mode on databases that support it
57 if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
59 if (!in_array('STRICT_ALL_TABLES', $modes))
61 $modes[] = 'STRICT_ALL_TABLES';
64 if (!in_array('STRICT_TRANS_TABLES', $modes))
66 $modes[] = 'STRICT_TRANS_TABLES';
69 $mode = implode(',', $modes);
70 @mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id);
73 return $this->db_connect_id;
77 return $this->sql_error('');
80 /**
81 * Version information about used database
82 * @param bool $raw if true, only return the fetched sql_server_version
83 * @return string sql server version
85 function sql_server_info($raw = false)
87 global $cache;
89 if (empty($cache) || ($this->sql_server_version = $cache->get('mysql_version')) === false)
91 $result = @mysql_query('SELECT VERSION() AS version', $this->db_connect_id);
92 $row = @mysql_fetch_assoc($result);
93 @mysql_free_result($result);
95 $this->sql_server_version = $row['version'];
97 if (!empty($cache))
99 $cache->put('mysql_version', $this->sql_server_version);
103 return ($raw) ? $this->sql_server_version : 'MySQL ' . $this->sql_server_version;
107 * SQL Transaction
108 * @access private
110 function _sql_transaction($status = 'begin')
112 switch ($status)
114 case 'begin':
115 return @mysql_query('BEGIN', $this->db_connect_id);
116 break;
118 case 'commit':
119 return @mysql_query('COMMIT', $this->db_connect_id);
120 break;
122 case 'rollback':
123 return @mysql_query('ROLLBACK', $this->db_connect_id);
124 break;
127 return true;
131 * Base query method
133 * @param string $query Contains the SQL query which shall be executed
134 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
135 * @return mixed When casted to bool the returned value returns true on success and false on failure
137 * @access public
139 function sql_query($query = '', $cache_ttl = 0)
141 if ($query != '')
143 global $cache;
145 // EXPLAIN only in extra debug mode
146 if (defined('DEBUG_EXTRA'))
148 $this->sql_report('start', $query);
151 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
152 $this->sql_add_num_queries($this->query_result);
154 if ($this->query_result === false)
156 if (($this->query_result = @mysql_query($query, $this->db_connect_id)) === false)
158 $this->sql_error($query);
161 if (defined('DEBUG_EXTRA'))
163 $this->sql_report('stop', $query);
166 if ($cache_ttl && method_exists($cache, 'sql_save'))
168 $this->open_queries[(int) $this->query_result] = $this->query_result;
169 $cache->sql_save($query, $this->query_result, $cache_ttl);
171 else if (strpos($query, 'SELECT') === 0 && $this->query_result)
173 $this->open_queries[(int) $this->query_result] = $this->query_result;
176 else if (defined('DEBUG_EXTRA'))
178 $this->sql_report('fromcache', $query);
181 else
183 return false;
186 return $this->query_result;
190 * Build LIMIT query
192 function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
194 $this->query_result = false;
196 // if $total is set to 0 we do not want to limit the number of rows
197 if ($total == 0)
199 // Having a value of -1 was always a bug
200 $total = '18446744073709551615';
203 $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
205 return $this->sql_query($query, $cache_ttl);
209 * Return number of affected rows
211 function sql_affectedrows()
213 return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false;
217 * Fetch current row
219 function sql_fetchrow($query_id = false)
221 global $cache;
223 if ($query_id === false)
225 $query_id = $this->query_result;
228 if (isset($cache->sql_rowset[$query_id]))
230 return $cache->sql_fetchrow($query_id);
233 return ($query_id !== false) ? @mysql_fetch_assoc($query_id) : false;
237 * Get last inserted id after insert statement
239 function sql_nextid()
241 return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false;
245 * Free sql result
247 function sql_freeresult($query_id = false)
249 global $cache;
251 if ($query_id === false)
253 $query_id = $this->query_result;
256 if (isset($cache->sql_rowset[$query_id]))
258 return $cache->sql_freeresult($query_id);
261 if (isset($this->open_queries[(int) $query_id]))
263 unset($this->open_queries[(int) $query_id]);
264 return @mysql_free_result($query_id);
267 return false;
271 * Escape string used in sql query
273 function sql_escape($msg)
275 if (!$this->db_connect_id)
277 return @mysql_real_escape_string($msg);
280 return @mysql_real_escape_string($msg, $this->db_connect_id);
284 * Expose a DBMS specific function
286 function sql_function($type, $col)
288 switch ($type)
290 case 'length_varchar':
291 case 'length_text':
292 return 'LENGTH(' . $col . ')';
293 break;
297 function sql_handle_data($type, $table, $data, $where = '')
299 if ($type === 'UPDATE')
301 $this->sql_query('INSERT INTO ' . $table . ' ' .
302 $this->sql_build_array('INSERT', $data));
304 else
306 $this->sql_query('UPDATE ' . $table . '
307 SET ' . $db->sql_build_array('UPDATE', $data) .
308 $where);
313 * Build LIKE expression
314 * @access private
316 function _sql_like_expression($expression)
318 return $expression;
322 * Build db-specific query data
323 * @access private
325 function _sql_custom_build($stage, $data)
327 switch ($stage)
329 case 'FROM':
330 $data = '(' . $data . ')';
331 break;
334 return $data;
338 * return sql error array
339 * @access private
341 function _sql_error()
343 if (!$this->db_connect_id)
345 return array(
346 'message' => @mysql_error(),
347 'code' => @mysql_errno()
351 return array(
352 'message' => @mysql_error($this->db_connect_id),
353 'code' => @mysql_errno($this->db_connect_id)
358 * Close sql connection
359 * @access private
361 function _sql_close()
363 return @mysql_close($this->db_connect_id);
367 * Build db-specific report
368 * @access private
370 function _sql_report($mode, $query = '')
372 static $test_prof;
373 static $test_extend;
375 // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
376 if ($test_prof === null)
378 $test_prof = $test_extend = false;
379 if (version_compare($this->sql_server_info(true), '5.0.37', '>=') && version_compare($this->sql_server_info(true), '5.1', '<'))
381 $test_prof = true;
384 if (version_compare($ver, '4.1.1', '>='))
386 $test_extend = true;
390 switch ($mode)
392 case 'start':
394 $explain_query = $query;
395 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
397 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
399 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
401 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
404 if (preg_match('/^SELECT/', $explain_query))
406 $html_table = false;
408 // begin profiling
409 if ($test_prof)
411 @mysql_query('SET profiling = 1;', $this->db_connect_id);
414 if ($result = @mysql_query('EXPLAIN ' . (($test_extend) ? 'EXTENDED ' : '') . "$explain_query", $this->db_connect_id))
416 while ($row = @mysql_fetch_assoc($result))
418 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
421 @mysql_free_result($result);
423 if ($html_table)
425 $this->html_hold .= '</table>';
428 if ($test_extend)
430 $html_table = false;
432 if ($result = @mysql_query('SHOW WARNINGS', $this->db_connect_id))
434 $this->html_hold .= '<br />';
435 while ($row = @mysql_fetch_assoc($result))
437 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
440 @mysql_free_result($result);
442 if ($html_table)
444 $this->html_hold .= '</table>';
449 if ($test_prof)
451 $html_table = false;
453 // get the last profile
454 if ($result = @mysql_query('SHOW PROFILE ALL;', $this->db_connect_id))
456 $this->html_hold .= '<br />';
457 while ($row = @mysql_fetch_assoc($result))
459 // make <unknown> HTML safe
460 if (!empty($row['Source_function']))
462 $row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
465 // remove unsupported features
466 foreach ($row as $key => $val)
468 if ($val === null)
470 unset($row[$key]);
473 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
476 @mysql_free_result($result);
478 if ($html_table)
480 $this->html_hold .= '</table>';
483 @mysql_query('SET profiling = 0;', $this->db_connect_id);
487 break;
489 case 'fromcache':
490 $endtime = explode(' ', microtime());
491 $endtime = $endtime[0] + $endtime[1];
493 $result = @mysql_query($query, $this->db_connect_id);
494 while ($void = @mysql_fetch_assoc($result))
496 // Take the time spent on parsing rows into account
498 @mysql_free_result($result);
500 $splittime = explode(' ', microtime());
501 $splittime = $splittime[0] + $splittime[1];
503 $this->sql_report('record_fromcache', $query, $endtime, $splittime);
505 break;