Adjust some files to support new methods...
[phpbb.git] / phpBB / includes / db / mysqli.php
blobfba7b2c22d87131e28c4b5882a80ce83cecb7867
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 * MySQLi Database Abstraction Layer
21 * Compatible with:
22 * MySQL 4.1+
23 * MySQL 5.0+
24 * @package dbal
26 class phpbb_dbal_mysqli extends phpbb_dbal
28 /**
29 * @var string Database type. No distinction between versions or used extensions.
31 public $dbms_type = 'mysql';
33 /**
34 * Connect to server. See {@link phpbb_dbal::sql_connect() sql_connect()} for details.
36 public function sql_connect($server, $user, $password, $database, $port = false, $persistency = false , $new_link = false)
38 $this->persistency = $persistency;
39 $this->user = $user;
40 $this->server = $server;
41 $this->dbname = $database;
42 $this->port = (!$port) ? NULL : $port;
44 // Persistant connections not supported by the mysqli extension?
45 $this->db_connect_id = @mysqli_connect($this->server, $this->user, $password, $this->dbname, $this->port);
47 if ($this->db_connect_id && $this->dbname != '')
49 @mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
51 // enforce strict mode on databases that support it
52 if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
54 $result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
55 $row = @mysqli_fetch_assoc($result);
56 @mysqli_free_result($result);
58 $modes = array_map('trim', explode(',', $row['sql_mode']));
60 // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
61 if (!in_array('TRADITIONAL', $modes))
63 if (!in_array('STRICT_ALL_TABLES', $modes))
65 $modes[] = 'STRICT_ALL_TABLES';
68 if (!in_array('STRICT_TRANS_TABLES', $modes))
70 $modes[] = 'STRICT_TRANS_TABLES';
74 $mode = implode(',', $modes);
75 @mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
77 return $this->db_connect_id;
80 return $this->sql_error('');
83 /**
84 * Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details.
86 public function sql_server_info($raw = false)
88 if (!phpbb::registered('acm') || ($this->sql_server_version = phpbb::$acm->get('#mysqli_version')) === false)
90 $result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version');
91 $row = @mysqli_fetch_assoc($result);
92 @mysqli_free_result($result);
94 $this->sql_server_version = trim($row['version']);
96 if (phpbb::registered('acm'))
98 phpbb::$acm->put('#mysqli_version', $this->sql_server_version);
102 return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
106 * DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
108 protected function _sql_query($query)
110 return @mysqli_query($this->db_connect_id, $query);
114 * Build LIMIT query and run it. See {@link phpbb_dbal::_sql_query_limit() _sql_query_limit()} for details.
116 protected function _sql_query_limit($query, $total, $offset, $cache_ttl)
118 // if $total is set to 0 we do not want to limit the number of rows
119 if ($total == 0)
121 // MySQL 4.1+ no longer supports -1 in limit queries
122 $total = '18446744073709551615';
125 $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
126 return $this->sql_query($query, $cache_ttl);
130 * Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details.
132 protected function _sql_close()
134 return @mysqli_close($this->db_connect_id);
138 * SQL Transaction. See {@link phpbb_dbal::_sql_transaction() _sql_transaction()} for details.
140 protected function _sql_transaction($status)
142 switch ($status)
144 case 'begin':
145 return @mysqli_autocommit($this->db_connect_id, false);
146 break;
148 case 'commit':
149 $result = @mysqli_commit($this->db_connect_id);
150 @mysqli_autocommit($this->db_connect_id, true);
151 return $result;
152 break;
154 case 'rollback':
155 $result = @mysqli_rollback($this->db_connect_id);
156 @mysqli_autocommit($this->db_connect_id, true);
157 return $result;
158 break;
161 return true;
165 * Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details.
167 public function sql_affectedrows()
169 return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
173 * Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details.
175 public function sql_nextid()
177 return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
181 * Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
183 protected function _sql_fetchrow($query_id)
185 return @mysqli_fetch_assoc($query_id);
189 * Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
191 protected function _sql_freeresult($query_id)
193 return @mysqli_free_result($query_id);
197 * Correctly adjust LIKE expression for special characters. See {@link phpbb_dbal::_sql_like_expression() _sql_like_expression()} for details.
199 protected function _sql_like_expression($expression)
201 return $expression;
205 * Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
207 public function sql_escape($msg)
209 return @mysqli_real_escape_string($this->db_connect_id, $msg);
213 * Expose a DBMS specific function. See {@link phpbb_dbal::sql_function() sql_function()} for details.
215 public function sql_function($type, $col)
217 switch ($type)
219 case 'length_varchar':
220 case 'length_text':
221 return 'LENGTH(' . $col . ')';
222 break;
227 * Handle data by using prepared statements. See {@link phpbb_dbal::sql_handle_data() sql_handle_data()} for details.
228 * @todo implement correctly by using types. ;)
230 public function sql_handle_data($type, $table, $data, $where = '')
232 if ($type === 'INSERT')
234 $stmt = mysqli_prepare($this->db_connect_id, "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES (" . substr(str_repeat('?, ', sizeof($data)) ,0, -1) . ')');
236 else
238 $query = "UPDATE $table SET ";
240 $set = array();
241 foreach (array_keys($data) as $key)
243 $set[] = "$key = ?";
245 $query .= implode(', ', $set);
247 if ($where !== '')
249 $query .= ' WHERE ' . $where;
252 $stmt = mysqli_prepare($this->db_connect_id, $query);
255 // get the stmt onto the top of the function arguments
256 array_unshift($data, $stmt);
258 call_user_func_array('mysqli_stmt_bind_param', $data);
259 mysqli_stmt_execute($stmt);
261 mysqli_stmt_close($stmt);
265 * Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
267 protected function _sql_custom_build($stage, $data)
269 switch ($stage)
271 case 'FROM':
272 $data = '(' . $data . ')';
273 break;
276 return $data;
280 * return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
282 protected function _sql_error()
284 if (!$this->db_connect_id)
286 return array(
287 'message' => @mysqli_connect_error(),
288 'code' => @mysqli_connect_errno()
292 return array(
293 'message' => @mysqli_error($this->db_connect_id),
294 'code' => @mysqli_errno($this->db_connect_id)
300 * 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.
302 protected function _sql_report($mode, $query = '')
304 static $test_prof;
306 // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
307 if ($test_prof === null)
309 $test_prof = false;
310 if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false)
312 $ver = mysqli_get_server_version($this->db_connect_id);
313 if ($ver >= 50037 && $ver < 50100)
315 $test_prof = true;
320 switch ($mode)
322 case 'start':
324 $explain_query = $query;
325 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
327 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
329 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
331 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
334 if (preg_match('/^SELECT/', $explain_query))
336 $html_table = false;
338 // begin profiling
339 if ($test_prof)
341 @mysqli_query($this->db_connect_id, 'SET profiling = 1;');
344 if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
346 while ($row = @mysqli_fetch_assoc($result))
348 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
351 @mysqli_free_result($result);
353 if ($html_table)
355 $this->html_hold .= '</table>';
358 if ($test_prof)
360 $html_table = false;
362 // get the last profile
363 if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
365 $this->html_hold .= '<br />';
366 while ($row = @mysqli_fetch_assoc($result))
368 // make <unknown> HTML safe
369 if (!empty($row['Source_function']))
371 $row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
374 // remove unsupported features
375 foreach ($row as $key => $val)
377 if ($val === null)
379 unset($row[$key]);
382 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
385 @mysqli_free_result($result);
387 if ($html_table)
389 $this->html_hold .= '</table>';
392 @mysqli_query($this->db_connect_id, 'SET profiling = 0;');
396 break;
398 case 'fromcache':
399 $endtime = explode(' ', microtime());
400 $endtime = $endtime[0] + $endtime[1];
402 $result = @mysqli_query($this->db_connect_id, $query);
403 while ($void = @mysqli_fetch_assoc($result))
405 // Take the time spent on parsing rows into account
407 @mysqli_free_result($result);
409 $splittime = explode(' ', microtime());
410 $splittime = $splittime[0] + $splittime[1];
412 $this->sql_report('record_fromcache', $query, $endtime, $splittime);
414 break;