5
* @version $Id: mysql.php,v 1.62 2007/10/05 14:36:32 acydburn Exp $
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
33
var $multi_insert = true;
39
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
41
$this->persistency = $persistency;
42
$this->user = $sqluser;
43
$this->server = $sqlserver . (($port) ? ':' . $port : '');
44
$this->dbname = $database;
46
$this->sql_layer = 'mysql4';
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
// Determine what version we are using and if it natively supports UNICODE
55
$this->mysql_version = mysql_get_server_info($this->db_connect_id);
57
if (version_compare($this->mysql_version, '4.1.3', '>='))
59
@mysql_query("SET NAMES 'utf8'", $this->db_connect_id);
60
// enforce strict mode on databases that support it
61
if (version_compare($this->mysql_version, '5.0.2', '>='))
63
$result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id);
64
$row = @mysql_fetch_assoc($result);
65
@mysql_free_result($result);
66
$modes = array_map('trim', explode(',', $row['sql_mode']));
68
// TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
69
if (!in_array('TRADITIONAL', $modes))
71
if (!in_array('STRICT_ALL_TABLES', $modes))
73
$modes[] = 'STRICT_ALL_TABLES';
76
if (!in_array('STRICT_TRANS_TABLES', $modes))
78
$modes[] = 'STRICT_TRANS_TABLES';
82
$mode = implode(',', $modes);
83
@mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id);
86
else if (version_compare($this->mysql_version, '4.0.0', '<'))
88
$this->sql_layer = 'mysql';
91
return $this->db_connect_id;
95
return $this->sql_error('');
99
* Version information about used database
101
function sql_server_info()
103
return 'MySQL ' . $this->mysql_version;
110
function _sql_transaction($status = 'begin')
115
return @mysql_query('BEGIN', $this->db_connect_id);
119
return @mysql_query('COMMIT', $this->db_connect_id);
123
return @mysql_query('ROLLBACK', $this->db_connect_id);
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
139
function sql_query($query = '', $cache_ttl = 0)
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);
186
return ($this->query_result) ? $this->query_result : false;
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
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;
219
function sql_fetchrow($query_id = false)
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
* Seek to given row number
238
* rownum is zero-based
240
function sql_rowseek($rownum, &$query_id)
244
if ($query_id === false)
246
$query_id = $this->query_result;
249
if (isset($cache->sql_rowset[$query_id]))
251
return $cache->sql_rowseek($rownum, $query_id);
254
return ($query_id !== false) ? @mysql_data_seek($query_id, $rownum) : false;
258
* Get last inserted id after insert statement
260
function sql_nextid()
262
return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false;
268
function sql_freeresult($query_id = false)
272
if ($query_id === false)
274
$query_id = $this->query_result;
277
if (isset($cache->sql_rowset[$query_id]))
279
return $cache->sql_freeresult($query_id);
282
if (isset($this->open_queries[(int) $query_id]))
284
unset($this->open_queries[(int) $query_id]);
285
return @mysql_free_result($query_id);
292
* Escape string used in sql query
294
function sql_escape($msg)
296
if (!$this->db_connect_id)
298
return @mysql_real_escape_string($msg);
301
return @mysql_real_escape_string($msg, $this->db_connect_id);
305
* Build LIKE expression
308
function _sql_like_expression($expression)
314
* Build db-specific query data
317
function _sql_custom_build($stage, $data)
322
$data = '(' . $data . ')';
330
* return sql error array
333
function _sql_error()
335
if (!$this->db_connect_id)
338
'message' => @mysql_error(),
339
'code' => @mysql_errno()
344
'message' => @mysql_error($this->db_connect_id),
345
'code' => @mysql_errno($this->db_connect_id)
350
* Close sql connection
353
function _sql_close()
355
return @mysql_close($this->db_connect_id);
359
* Build db-specific report
362
function _sql_report($mode, $query = '')
366
// current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
367
if ($test_prof === null)
370
if (strpos($this->mysql_version, 'community') !== false)
372
$ver = substr($this->mysql_version, 0, strpos($this->mysql_version, '-'));
373
if (version_compare($ver, '5.0.37', '>=') && version_compare($ver, '5.1', '<'))
384
$explain_query = $query;
385
if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
387
$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
389
else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
391
$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
394
if (preg_match('/^SELECT/', $explain_query))
401
@mysql_query('SET profiling = 1;', $this->db_connect_id);
404
if ($result = @mysql_query("EXPLAIN $explain_query", $this->db_connect_id))
406
while ($row = @mysql_fetch_assoc($result))
408
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
411
@mysql_free_result($result);
415
$this->html_hold .= '</table>';
422
// get the last profile
423
if ($result = @mysql_query('SHOW PROFILE ALL;', $this->db_connect_id))
425
$this->html_hold .= '<br />';
426
while ($row = @mysql_fetch_assoc($result))
428
// make <unknown> HTML safe
429
if (!empty($row['Source_function']))
431
$row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']);
434
// remove unsupported features
435
foreach ($row as $key => $val)
442
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
445
@mysql_free_result($result);
449
$this->html_hold .= '</table>';
452
@mysql_query('SET profiling = 0;', $this->db_connect_id);
459
$endtime = explode(' ', microtime());
460
$endtime = $endtime[0] + $endtime[1];
462
$result = @mysql_query($query, $this->db_connect_id);
463
while ($void = @mysql_fetch_assoc($result))
465
// Take the time spent on parsing rows into account
467
@mysql_free_result($result);
469
$splittime = explode(' ', microtime());
470
$splittime = $splittime[0] + $splittime[1];
472
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
b'\\ No newline at end of file'