6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
19 include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
22 * MySQL4 Database Abstraction Layer
30 class dbal_mysql extends dbal
32 var $multi_insert = true;
38 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
40 $this->persistency = $persistency;
41 $this->user = $sqluser;
42 $this->server = $sqlserver . (($port) ? ':' . $port : '');
43 $this->dbname = $database;
45 $this->sql_layer = 'mysql4';
47 $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $sqlpassword) : @mysql_connect($this->server, $this->user, $sqlpassword, $new_link);
49 if ($this->db_connect_id && $this->dbname != '')
51 if (@mysql_select_db($this->dbname, $this->db_connect_id))
53 // Determine what version we are using and if it natively supports UNICODE
54 if (version_compare($this->sql_server_info(true), '4.1.0', '>='))
56 @mysql_query("SET NAMES 'utf8'", $this->db_connect_id);
58 // enforce strict mode on databases that support it
59 if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
61 $result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id);
62 $row = @mysql_fetch_assoc($result);
63 @mysql_free_result($result);
64 $modes = array_map('trim', explode(',', $row['sql_mode']));
66 // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
67 if (!in_array('TRADITIONAL', $modes))
69 if (!in_array('STRICT_ALL_TABLES', $modes))
71 $modes[] = 'STRICT_ALL_TABLES';
74 if (!in_array('STRICT_TRANS_TABLES', $modes))
76 $modes[] = 'STRICT_TRANS_TABLES';
80 $mode = implode(',', $modes);
81 @mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id);
84 else if (version_compare($this->sql_server_info(true), '4.0.0', '<'))
86 $this->sql_layer = 'mysql';
89 return $this->db_connect_id;
93 return $this->sql_error('');
97 * Version information about used database
98 * @param bool $raw if true, only return the fetched sql_server_version
99 * @param bool $use_cache If true, it is safe to retrieve the value from the cache
100 * @return string sql server version
102 function sql_server_info($raw = false, $use_cache = true)
106 if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysql_version')) === false)
108 $result = @mysql_query('SELECT VERSION() AS version', $this->db_connect_id);
109 $row = @mysql_fetch_assoc($result);
110 @mysql_free_result($result);
112 $this->sql_server_version = $row['version'];
114 if (!empty($cache) && $use_cache)
116 $cache->put('mysql_version', $this->sql_server_version);
120 return ($raw) ? $this->sql_server_version : 'MySQL ' . $this->sql_server_version;
127 function _sql_transaction($status = 'begin')
132 return @mysql_query('BEGIN', $this->db_connect_id);
136 return @mysql_query('COMMIT', $this->db_connect_id);
140 return @mysql_query('ROLLBACK', $this->db_connect_id);
150 * @param string $query Contains the SQL query which shall be executed
151 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
152 * @return mixed When casted to bool the returned value returns true on success and false on failure
156 function sql_query($query = '', $cache_ttl = 0)
162 // EXPLAIN only in extra debug mode
163 if (defined('DEBUG_EXTRA'))
165 $this->sql_report('start', $query);
168 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
169 $this->sql_add_num_queries($this->query_result);
171 if ($this->query_result === false)
173 if (($this->query_result = @mysql_query($query, $this->db_connect_id)) === false)
175 $this->sql_error($query);
178 if (defined('DEBUG_EXTRA'))
180 $this->sql_report('stop', $query);
183 if ($cache_ttl && method_exists($cache, 'sql_save'))
185 $this->open_queries[(int) $this->query_result] = $this->query_result;
186 $cache->sql_save($query, $this->query_result, $cache_ttl);
188 else if (strpos($query, 'SELECT') === 0 && $this->query_result)
190 $this->open_queries[(int) $this->query_result] = $this->query_result;
193 else if (defined('DEBUG_EXTRA'))
195 $this->sql_report('fromcache', $query);
203 return $this->query_result;
209 function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
211 $this->query_result = false;
213 // if $total is set to 0 we do not want to limit the number of rows
216 // Having a value of -1 was always a bug
217 $total = '18446744073709551615';
220 $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
222 return $this->sql_query($query, $cache_ttl);
226 * Return number of affected rows
228 function sql_affectedrows()
230 return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false;
236 function sql_fetchrow($query_id = false)
240 if ($query_id === false)
242 $query_id = $this->query_result;
245 if (isset($cache->sql_rowset[$query_id]))
247 return $cache->sql_fetchrow($query_id);
250 return ($query_id !== false) ? @mysql_fetch_assoc($query_id) : false;
254 * Seek to given row number
255 * rownum is zero-based
257 function sql_rowseek($rownum, &$query_id)
261 if ($query_id === false)
263 $query_id = $this->query_result;
266 if (isset($cache->sql_rowset[$query_id]))
268 return $cache->sql_rowseek($rownum, $query_id);
271 return ($query_id !== false) ? @mysql_data_seek($query_id, $rownum) : false;
275 * Get last inserted id after insert statement
277 function sql_nextid()
279 return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false;
285 function sql_freeresult($query_id = false)
289 if ($query_id === false)
291 $query_id = $this->query_result;
294 if (isset($cache->sql_rowset[$query_id]))
296 return $cache->sql_freeresult($query_id);
299 if (isset($this->open_queries[(int) $query_id]))
301 unset($this->open_queries[(int) $query_id]);
302 return @mysql_free_result($query_id);
309 * Escape string used in sql query
311 function sql_escape($msg)
313 if (!$this->db_connect_id)
315 return @mysql_real_escape_string($msg);
318 return @mysql_real_escape_string($msg, $this->db_connect_id);
322 * Build LIKE expression
325 function _sql_like_expression($expression)
331 * Build db-specific query data
334 function _sql_custom_build($stage, $data)
339 $data = '(' . $data . ')';
347 * return sql error array
350 function _sql_error()
352 if (!$this->db_connect_id)
355 'message' => @mysql_error(),
356 'code' => @mysql_errno()
361 'message' => @mysql_error($this->db_connect_id),
362 'code' => @mysql_errno($this->db_connect_id)
367 * Close sql connection
370 function _sql_close()
372 return @mysql_close($this->db_connect_id);
376 * Build db-specific report
379 function _sql_report($mode, $query = '')
383 // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
384 if ($test_prof === null)
387 if (version_compare($this->sql_server_info(true), '5.0.37', '>=') && version_compare($this->sql_server_info(true), '5.1', '<'))
397 $explain_query = $query;
398 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
400 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
402 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
404 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
407 if (preg_match('/^SELECT/', $explain_query))
414 @mysql_query('SET profiling = 1;', $this->db_connect_id);
417 if ($result = @mysql_query("EXPLAIN $explain_query", $this->db_connect_id))
419 while ($row = @mysql_fetch_assoc($result))
421 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
424 @mysql_free_result($result);
428 $this->html_hold .= '</table>';
435 // get the last profile
436 if ($result = @mysql_query('SHOW PROFILE ALL;', $this->db_connect_id))
438 $this->html_hold .= '<br />';
439 while ($row = @mysql_fetch_assoc($result))
441 // make <unknown> HTML safe
442 if (!empty($row['Source_function']))
444 $row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']);
447 // remove unsupported features
448 foreach ($row as $key => $val)
455 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
458 @mysql_free_result($result);
462 $this->html_hold .= '</table>';
465 @mysql_query('SET profiling = 0;', $this->db_connect_id);
472 $endtime = explode(' ', microtime());
473 $endtime = $endtime[0] + $endtime[1];
475 $result = @mysql_query($query, $this->db_connect_id);
476 while ($void = @mysql_fetch_assoc($result))
478 // Take the time spent on parsing rows into account
480 @mysql_free_result($result);
482 $splittime = explode(' ', microtime());
483 $splittime = $splittime[0] + $splittime[1];
485 $this->sql_report('record_fromcache', $query, $endtime, $splittime);