6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * MySQLi Database Abstraction Layer
26 class phpbb_dbal_mysqli
extends phpbb_dbal
29 * @var string Database type. No distinction between versions or used extensions.
31 public $dbms_type = 'mysql';
34 * @var array Database type map, column layout information
36 public $dbms_type_map = array(
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)',
48 'XSTEXT_UNI'=> 'varchar(100)',
50 'STEXT_UNI' => 'varchar(255)',
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)',
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;
72 $this->server
= $server;
73 $this->dbname
= $database;
74 $this->port
= (!$port) ?
NULL : $port;
76 // Persistant connections not supported by the mysqli extension?
77 $this->db_connect_id
= @mysqli_connect
($this->server
, $this->user
, $password, $this->dbname
, $this->port
);
79 if (!$this->db_connect_id ||
!$this->dbname
)
81 return $this->sql_error('');
84 @mysqli_query
($this->db_connect_id
, "SET NAMES 'utf8'");
86 // enforce strict mode on databases that support it
87 if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
89 $result = @mysqli_query
($this->db_connect_id
, 'SELECT @@session.sql_mode AS sql_mode');
90 $row = @mysqli_fetch_assoc
($result);
91 @mysqli_free_result
($result);
93 $modes = array_map('trim', explode(',', $row['sql_mode']));
95 // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
96 if (!in_array('TRADITIONAL', $modes))
98 if (!in_array('STRICT_ALL_TABLES', $modes))
100 $modes[] = 'STRICT_ALL_TABLES';
103 if (!in_array('STRICT_TRANS_TABLES', $modes))
105 $modes[] = 'STRICT_TRANS_TABLES';
109 $mode = implode(',', $modes);
110 @mysqli_query
($this->db_connect_id
, "SET SESSION sql_mode='{$mode}'");
113 return $this->db_connect_id
;
117 * Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details.
119 public function sql_server_info($raw = false)
121 if (!phpbb
::registered('acm') ||
($this->sql_server_version
= phpbb
::$acm->get('#mysqli_version')) === false)
123 $result = @mysqli_query
($this->db_connect_id
, 'SELECT VERSION() AS version');
124 $row = @mysqli_fetch_assoc
($result);
125 @mysqli_free_result
($result);
127 $this->sql_server_version
= trim($row['version']);
129 if (phpbb
::registered('acm'))
131 phpbb
::$acm->put('#mysqli_version', $this->sql_server_version
);
135 return ($raw) ?
$this->sql_server_version
: 'MySQL(i) ' . $this->sql_server_version
;
139 * DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
141 protected function _sql_query($query)
143 return @mysqli_query
($this->db_connect_id
, $query);
147 * Build LIMIT query and run it. See {@link phpbb_dbal::_sql_query_limit() _sql_query_limit()} for details.
149 protected function _sql_query_limit($query, $total, $offset, $cache_ttl)
151 // if $total is set to 0 we do not want to limit the number of rows
154 // MySQL 4.1+ no longer supports -1 in limit queries
155 $total = '18446744073709551615';
158 $query .= "\n LIMIT " . ((!empty($offset)) ?
$offset . ', ' . $total : $total);
159 return $this->sql_query($query, $cache_ttl);
163 * Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details.
165 protected function _sql_close()
167 return @mysqli_close
($this->db_connect_id
);
171 * SQL Transaction. See {@link phpbb_dbal::_sql_transaction() _sql_transaction()} for details.
173 protected function _sql_transaction($status)
178 return @mysqli_autocommit
($this->db_connect_id
, false);
182 $result = @mysqli_commit
($this->db_connect_id
);
183 @mysqli_autocommit
($this->db_connect_id
, true);
188 $result = @mysqli_rollback
($this->db_connect_id
);
189 @mysqli_autocommit
($this->db_connect_id
, 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
) ? @mysqli_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
) ? @mysqli_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 @mysqli_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 @mysqli_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)
238 * Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
240 public function sql_escape($msg)
242 return @mysqli_real_escape_string
($this->db_connect_id
, $msg);
246 * Expose a DBMS specific function. See {@link phpbb_dbal::sql_function() sql_function()} for details.
248 public function sql_function($type, $col)
252 case 'length_varchar':
254 return 'LENGTH(' . $col . ')';
260 * Handle data by using prepared statements. See {@link phpbb_dbal::sql_handle_data() sql_handle_data()} for details.
261 public function sql_handle_data($type, $table, $data, $where = '')
263 if ($type === 'INSERT')
265 $stmt = mysqli_prepare($this->db_connect_id, "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES (" . substr(str_repeat('?, ', sizeof($data)) ,0, -1) . ')');
269 $query = "UPDATE $table SET ";
272 foreach (array_keys($data) as $key)
276 $query .= implode(', ', $set);
280 $query .= ' WHERE ' . $where;
283 $stmt = mysqli_prepare($this->db_connect_id, $query);
286 // get the stmt onto the top of the function arguments
287 array_unshift($data, $stmt);
289 call_user_func_array('mysqli_stmt_bind_param', $data);
290 mysqli_stmt_execute($stmt);
292 mysqli_stmt_close($stmt);
297 * Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
299 protected function _sql_custom_build($stage, $data)
304 $data = '(' . $data . ')';
312 * return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
314 protected function _sql_error()
316 if (!$this->db_connect_id
)
319 'message' => @mysqli_connect_error
(),
320 'code' => @mysqli_connect_errno
()
325 'message' => @mysqli_error
($this->db_connect_id
),
326 'code' => @mysqli_errno
($this->db_connect_id
)
331 * 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.
333 protected function _sql_report($mode, $query = '')
338 // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
339 if ($test_prof === null)
341 $test_prof = $test_extend = false;
342 if (version_compare($this->sql_server_info(true), '5.0.37', '>=') && version_compare($this->sql_server_info(true), '5.1', '<'))
347 if (version_compare($this->sql_server_info(true), '4.1.1', '>='))
357 $explain_query = $query;
358 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
360 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
362 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
364 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
367 if (preg_match('/^SELECT/', $explain_query))
374 @mysqli_query
($this->db_connect_id
, 'SET profiling = 1;');
377 if ($result = @mysqli_query
($this->db_connect_id
, 'EXPLAIN ' . (($test_extend) ?
'EXTENDED ' : '') . "$explain_query"))
379 while ($row = @mysqli_fetch_assoc
($result))
381 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
384 @mysqli_free_result
($result);
388 $this->html_hold
.= '</table>';
395 if ($result = @mysqli_query
($this->db_connect_id
, 'SHOW WARNINGS'))
397 $this->html_hold
.= '<br />';
398 while ($row = @mysqli_fetch_assoc
($result))
400 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
403 @mysqli_free_result
($result);
407 $this->html_hold
.= '</table>';
415 // get the last profile
416 if ($result = @mysqli_query
($this->db_connect_id
, 'SHOW PROFILE ALL;'))
418 $this->html_hold
.= '<br />';
419 while ($row = @mysqli_fetch_assoc
($result))
421 // make <unknown> HTML safe
422 if (!empty($row['Source_function']))
424 $row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']);
427 // remove unsupported features
428 foreach ($row as $key => $val)
435 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
438 @mysqli_free_result
($result);
442 $this->html_hold
.= '</table>';
445 @mysqli_query
($this->db_connect_id
, 'SET profiling = 0;');
452 $endtime = explode(' ', microtime());
453 $endtime = $endtime[0] +
$endtime[1];
455 $result = @mysqli_query
($this->db_connect_id
, $query);
456 while ($void = @mysqli_fetch_assoc
($result))
458 // Take the time spent on parsing rows into account
460 @mysqli_free_result
($result);
462 $splittime = explode(' ', microtime());
463 $splittime = $splittime[0] +
$splittime[1];
465 $this->sql_report('record_fromcache', $query, $endtime, $splittime);