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.' . PHP_EXT
);
22 * MySQL Database Abstraction Layer
28 class dbal_mysql
extends dbal
30 var $multi_insert = true;
32 // Supports multiple table deletion
33 var $multi_table_deletion = true;
35 var $dbms_type = 'mysql';
41 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
43 $this->persistency
= $persistency;
44 $this->user
= $sqluser;
45 $this->server
= $sqlserver . (($port) ?
':' . $port : '');
46 $this->dbname
= $database;
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 @mysql_query
("SET NAMES 'utf8'", $this->db_connect_id
);
56 // enforce strict mode on databases that support it
57 if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
59 if (!in_array('STRICT_ALL_TABLES', $modes))
61 $modes[] = 'STRICT_ALL_TABLES';
64 if (!in_array('STRICT_TRANS_TABLES', $modes))
66 $modes[] = 'STRICT_TRANS_TABLES';
69 $mode = implode(',', $modes);
70 @mysql_query
("SET SESSION sql_mode='{$mode}'", $this->db_connect_id
);
73 return $this->db_connect_id
;
77 return $this->sql_error('');
81 * Version information about used database
82 * @param bool $raw if true, only return the fetched sql_server_version
83 * @return string sql server version
85 function sql_server_info($raw = false)
89 if (empty($cache) ||
($this->sql_server_version
= $cache->get('mysql_version')) === false)
91 $result = @mysql_query
('SELECT VERSION() AS version', $this->db_connect_id
);
92 $row = @mysql_fetch_assoc
($result);
93 @mysql_free_result
($result);
95 $this->sql_server_version
= $row['version'];
99 $cache->put('mysql_version', $this->sql_server_version
);
103 return ($raw) ?
$this->sql_server_version
: 'MySQL ' . $this->sql_server_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
;
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 * Get last inserted id after insert statement
239 function sql_nextid()
241 return ($this->db_connect_id
) ? @mysql_insert_id
($this->db_connect_id
) : false;
247 function sql_freeresult($query_id = false)
251 if ($query_id === false)
253 $query_id = $this->query_result
;
256 if (isset($cache->sql_rowset
[$query_id]))
258 return $cache->sql_freeresult($query_id);
261 if (isset($this->open_queries
[(int) $query_id]))
263 unset($this->open_queries
[(int) $query_id]);
264 return @mysql_free_result
($query_id);
271 * Escape string used in sql query
273 function sql_escape($msg)
275 if (!$this->db_connect_id
)
277 return @mysql_real_escape_string
($msg);
280 return @mysql_real_escape_string
($msg, $this->db_connect_id
);
284 * Expose a DBMS specific function
286 function sql_function($type, $col)
290 case 'length_varchar':
292 return 'LENGTH(' . $col . ')';
297 function sql_handle_data($type, $table, $data, $where = '')
299 if ($type === 'UPDATE')
301 $this->sql_query('INSERT INTO ' . $table . ' ' .
302 $this->sql_build_array('INSERT', $data));
306 $this->sql_query('UPDATE ' . $table . '
307 SET ' . $db->sql_build_array('UPDATE', $data) .
313 * Build LIKE expression
316 function _sql_like_expression($expression)
322 * Build db-specific query data
325 function _sql_custom_build($stage, $data)
330 $data = '(' . $data . ')';
338 * return sql error array
341 function _sql_error()
343 if (!$this->db_connect_id
)
346 'message' => @mysql_error
(),
347 'code' => @mysql_errno
()
352 'message' => @mysql_error
($this->db_connect_id
),
353 'code' => @mysql_errno
($this->db_connect_id
)
358 * Close sql connection
361 function _sql_close()
363 return @mysql_close
($this->db_connect_id
);
367 * Build db-specific report
370 function _sql_report($mode, $query = '')
375 // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
376 if ($test_prof === null)
378 $test_prof = $test_extend = false;
379 if (version_compare($this->sql_server_info(true), '5.0.37', '>=') && version_compare($this->sql_server_info(true), '5.1', '<'))
384 if (version_compare($ver, '4.1.1', '>='))
394 $explain_query = $query;
395 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
397 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
399 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
401 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
404 if (preg_match('/^SELECT/', $explain_query))
411 @mysql_query
('SET profiling = 1;', $this->db_connect_id
);
414 if ($result = @mysql_query
('EXPLAIN ' . (($test_extend) ?
'EXTENDED ' : '') . "$explain_query", $this->db_connect_id
))
416 while ($row = @mysql_fetch_assoc
($result))
418 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
421 @mysql_free_result
($result);
425 $this->html_hold
.= '</table>';
432 if ($result = @mysql_query
('SHOW WARNINGS', $this->db_connect_id
))
434 $this->html_hold
.= '<br />';
435 while ($row = @mysql_fetch_assoc
($result))
437 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
440 @mysql_free_result
($result);
444 $this->html_hold
.= '</table>';
453 // get the last profile
454 if ($result = @mysql_query
('SHOW PROFILE ALL;', $this->db_connect_id
))
456 $this->html_hold
.= '<br />';
457 while ($row = @mysql_fetch_assoc
($result))
459 // make <unknown> HTML safe
460 if (!empty($row['Source_function']))
462 $row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']);
465 // remove unsupported features
466 foreach ($row as $key => $val)
473 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
476 @mysql_free_result
($result);
480 $this->html_hold
.= '</table>';
483 @mysql_query
('SET profiling = 0;', $this->db_connect_id
);
490 $endtime = explode(' ', microtime());
491 $endtime = $endtime[0] +
$endtime[1];
493 $result = @mysql_query
($query, $this->db_connect_id
);
494 while ($void = @mysql_fetch_assoc
($result))
496 // Take the time spent on parsing rows into account
498 @mysql_free_result
($result);
500 $splittime = explode(' ', microtime());
501 $splittime = $splittime[0] +
$splittime[1];
503 $this->sql_report('record_fromcache', $query, $endtime, $splittime);