put sql_handle_data() into dbal and let DBMS who support this overwrite it. David...
[phpbb.git] / phpBB / includes / db / mysql.php
blobac0384d523641a68a5f96b55310ec741ac2eea6f
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 /**
20 * MySQL Database Abstraction Layer
21 * Compatible with:
22 * MySQL 4.1+
23 * MySQL 5.0+
24 * @package dbal
26 class phpbb_dbal_mysql extends phpbb_dbal
28 /**
29 * @var string Database type. No distinction between versions or used extensions.
31 public $dbms_type = 'mysql';
33 /**
34 * @var array Database type map, column layout information
36 public $dbms_type_map = array(
37 'INT:' => 'int(%d)',
38 'BINT' => 'bigint(20)',
39 'UINT' => 'mediumint(8) UNSIGNED',
40 'UINT:' => 'int(%d) UNSIGNED',
41 'TINT:' => 'tinyint(%d)',
42 'USINT' => 'smallint(4) UNSIGNED',
43 'BOOL' => 'tinyint(1) UNSIGNED',
44 'VCHAR' => 'varchar(255)',
45 'VCHAR:' => 'varchar(%d)',
46 'CHAR:' => 'char(%d)',
47 'XSTEXT' => 'text',
48 'XSTEXT_UNI'=> 'varchar(100)',
49 'STEXT' => 'text',
50 'STEXT_UNI' => 'varchar(255)',
51 'TEXT' => 'text',
52 'TEXT_UNI' => 'text',
53 'MTEXT' => 'mediumtext',
54 'MTEXT_UNI' => 'mediumtext',
55 'TIMESTAMP' => 'int(11) UNSIGNED',
56 'DECIMAL' => 'decimal(5,2)',
57 'DECIMAL:' => 'decimal(%d,2)',
58 'PDECIMAL' => 'decimal(6,3)',
59 'PDECIMAL:' => 'decimal(%d,3)',
60 'VCHAR_UNI' => 'varchar(255)',
61 'VCHAR_UNI:'=> 'varchar(%d)',
62 'VARBINARY' => 'varbinary(255)',
65 /**
66 * Connect to server. See {@link phpbb_dbal::sql_connect() sql_connect()} for details.
68 public function sql_connect($server, $user, $password, $database, $port = false, $persistency = false , $new_link = false)
70 $this->persistency = $persistency;
71 $this->user = $user;
72 $this->server = $server . (($port) ? ':' . $port : '');
73 $this->dbname = $database;
74 $this->port = $port;
76 $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $password, $new_link) : @mysql_connect($this->server, $this->user, $password, $new_link);
78 if (!$this->db_connect_id || !$this->dbname)
80 return $this->sql_error('');
83 if (!@mysql_select_db($this->dbname, $this->db_connect_id))
85 return $this->sql_error('');
88 @mysql_query("SET NAMES 'utf8'", $this->db_connect_id);
90 // enforce strict mode on databases that support it
91 if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
93 $result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id);
94 $row = @mysql_fetch_assoc($result);
95 @mysql_free_result($result);
97 $modes = array_map('trim', explode(',', $row['sql_mode']));
99 // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
100 if (!in_array('TRADITIONAL', $modes))
102 if (!in_array('STRICT_ALL_TABLES', $modes))
104 $modes[] = 'STRICT_ALL_TABLES';
107 if (!in_array('STRICT_TRANS_TABLES', $modes))
109 $modes[] = 'STRICT_TRANS_TABLES';
113 $mode = implode(',', $modes);
114 @mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id);
117 return $this->db_connect_id;
121 * Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details.
123 public function sql_server_info($raw = false)
125 if (!phpbb::registered('acm') || ($this->sql_server_version = phpbb::$acm->get('#mysql_version')) === false)
127 $result = @mysql_query('SELECT VERSION() AS version', $this->db_connect_id);
128 $row = @mysql_fetch_assoc($result);
129 @mysql_free_result($result);
131 $this->sql_server_version = trim($row['version']);
133 if (phpbb::registered('acm'))
135 phpbb::$acm->put('#mysql_version', $this->sql_server_version);
139 return ($raw) ? $this->sql_server_version : 'MySQL ' . $this->sql_server_version;
143 * DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
145 protected function _sql_query($query)
147 return @mysql_query($query, $this->db_connect_id);
151 * Build LIMIT query and run it. See {@link phpbb_dbal::_sql_query_limit() _sql_query_limit()} for details.
153 protected function _sql_query_limit($query, $total, $offset, $cache_ttl)
155 // if $total is set to 0 we do not want to limit the number of rows
156 if ($total == 0)
158 // MySQL 4.1+ no longer supports -1 in limit queries
159 $total = '18446744073709551615';
162 $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
163 return $this->sql_query($query, $cache_ttl);
167 * Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details.
169 protected function _sql_close()
171 return @mysql_close($this->db_connect_id);
175 * SQL Transaction. See {@link phpbb_dbal::_sql_transaction() _sql_transaction()} for details.
177 protected function _sql_transaction($status)
179 switch ($status)
181 case 'begin':
182 return @mysql_query('BEGIN', $this->db_connect_id);
183 break;
185 case 'commit':
186 return @mysql_query('COMMIT', $this->db_connect_id);
187 break;
189 case 'rollback':
190 return @mysql_query('ROLLBACK', $this->db_connect_id);
191 break;
194 return true;
198 * Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details.
200 public function sql_affectedrows()
202 return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false;
206 * Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details.
208 public function sql_nextid()
210 return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false;
214 * Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
216 protected function _sql_fetchrow($query_id)
218 return @mysql_fetch_assoc($query_id);
222 * Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
224 protected function _sql_freeresult($query_id)
226 return @mysql_free_result($query_id);
230 * Correctly adjust LIKE expression for special characters. See {@link phpbb_dbal::_sql_like_expression() _sql_like_expression()} for details.
232 protected function _sql_like_expression($expression)
234 return $expression;
238 * Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
240 public function sql_escape($msg)
242 if (!$this->db_connect_id)
244 return @mysql_real_escape_string($msg);
247 return @mysql_real_escape_string($msg, $this->db_connect_id);
251 * Expose a DBMS specific function. See {@link phpbb_dbal::sql_function() sql_function()} for details.
253 public function sql_function($type, $col)
255 switch ($type)
257 case 'length_varchar':
258 case 'length_text':
259 return 'LENGTH(' . $col . ')';
260 break;
265 * Handle data by using prepared statements. See {@link phpbb_dbal::sql_handle_data() sql_handle_data()} for details.
266 public function sql_handle_data($type, $table, $data, $where = '')
272 * Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
274 protected function _sql_custom_build($stage, $data)
276 switch ($stage)
278 case 'FROM':
279 $data = '(' . $data . ')';
280 break;
283 return $data;
287 * return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
289 protected function _sql_error()
291 if (!$this->db_connect_id)
293 return array(
294 'message' => @mysql_error(),
295 'code' => @mysql_errno()
299 return array(
300 'message' => @mysql_error($this->db_connect_id),
301 'code' => @mysql_errno($this->db_connect_id)
306 * Run DB-specific code to build SQL Report to explain queries, show statistics and runtime information. See {@link phpbb_dbal::_sql_report() _sql_report()} for details.
308 protected function _sql_report($mode, $query = '')
310 static $test_prof;
311 static $test_extend;
313 // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
314 if ($test_prof === null)
316 $test_prof = $test_extend = false;
317 if (version_compare($this->sql_server_info(true), '5.0.37', '>=') && version_compare($this->sql_server_info(true), '5.1', '<'))
319 $test_prof = true;
322 if (version_compare($this->sql_server_info(true), '4.1.1', '>='))
324 $test_extend = true;
328 switch ($mode)
330 case 'start':
332 $explain_query = $query;
333 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
335 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
337 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
339 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
342 if (preg_match('/^SELECT/', $explain_query))
344 $html_table = false;
346 // begin profiling
347 if ($test_prof)
349 @mysql_query('SET profiling = 1;', $this->db_connect_id);
352 if ($result = @mysql_query('EXPLAIN ' . (($test_extend) ? 'EXTENDED ' : '') . "$explain_query", $this->db_connect_id))
354 while ($row = @mysql_fetch_assoc($result))
356 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
359 @mysql_free_result($result);
361 if ($html_table)
363 $this->html_hold .= '</table>';
366 if ($test_extend)
368 $html_table = false;
370 if ($result = @mysql_query('SHOW WARNINGS', $this->db_connect_id))
372 $this->html_hold .= '<br />';
373 while ($row = @mysql_fetch_assoc($result))
375 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
378 @mysql_free_result($result);
380 if ($html_table)
382 $this->html_hold .= '</table>';
386 if ($test_prof)
388 $html_table = false;
390 // get the last profile
391 if ($result = @mysql_query('SHOW PROFILE ALL;', $this->db_connect_id))
393 $this->html_hold .= '<br />';
394 while ($row = @mysql_fetch_assoc($result))
396 // make <unknown> HTML safe
397 if (!empty($row['Source_function']))
399 $row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
402 // remove unsupported features
403 foreach ($row as $key => $val)
405 if ($val === null)
407 unset($row[$key]);
410 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
413 @mysql_free_result($result);
415 if ($html_table)
417 $this->html_hold .= '</table>';
420 @mysql_query('SET profiling = 0;', $this->db_connect_id);
424 break;
426 case 'fromcache':
427 $endtime = explode(' ', microtime());
428 $endtime = $endtime[0] + $endtime[1];
430 $result = @mysql_query($query, $this->db_connect_id);
431 while ($void = @mysql_fetch_assoc($result))
433 // Take the time spent on parsing rows into account
435 @mysql_free_result($result);
437 $splittime = explode(' ', microtime());
438 $splittime = $splittime[0] + $splittime[1];
440 $this->sql_report('record_fromcache', $query, $endtime, $splittime);
442 break;