aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/db/driver/mysql.php
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/phpbb/db/driver/mysql.php')
-rw-r--r--phpBB/phpbb/db/driver/mysql.php125
1 files changed, 81 insertions, 44 deletions
diff --git a/phpBB/phpbb/db/driver/mysql.php b/phpBB/phpbb/db/driver/mysql.php
index e311f0dd74..a94e88b331 100644
--- a/phpBB/phpbb/db/driver/mysql.php
+++ b/phpBB/phpbb/db/driver/mysql.php
@@ -1,9 +1,13 @@
<?php
/**
*
-* @package dbal
-* @copyright (c) 2005 phpBB Group
-* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
+* This file is part of the phpBB Forum Software package.
+*
+* @copyright (c) phpBB Limited <https://www.phpbb.com>
+* @license GNU General Public License, version 2 (GPL-2.0)
+*
+* For full copyright and license information, please see
+* the docs/CREDITS.txt file.
*
*/
@@ -16,7 +20,6 @@ namespace phpbb\db\driver;
* MySQL 4.0+
* MySQL 4.1+
* MySQL 5.0+
-* @package dbal
*/
class mysql extends \phpbb\db\driver\mysql_base
{
@@ -24,8 +27,7 @@ class mysql extends \phpbb\db\driver\mysql_base
var $connect_error = '';
/**
- * Connect to server
- * @access public
+ * {@inheritDoc}
*/
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
{
@@ -68,9 +70,16 @@ class mysql extends \phpbb\db\driver\mysql_base
if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
{
$result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id);
- $row = @mysql_fetch_assoc($result);
- @mysql_free_result($result);
- $modes = array_map('trim', explode(',', $row['sql_mode']));
+ if ($result)
+ {
+ $row = mysql_fetch_assoc($result);
+ mysql_free_result($result);
+ $modes = array_map('trim', explode(',', $row['sql_mode']));
+ }
+ else
+ {
+ $modes = array();
+ }
// TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
if (!in_array('TRADITIONAL', $modes))
@@ -103,10 +112,7 @@ class mysql extends \phpbb\db\driver\mysql_base
}
/**
- * Version information about used database
- * @param bool $raw if true, only return the fetched sql_server_version
- * @param bool $use_cache If true, it is safe to retrieve the value from the cache
- * @return string sql server version
+ * {@inheritDoc}
*/
function sql_server_info($raw = false, $use_cache = true)
{
@@ -115,14 +121,17 @@ class mysql extends \phpbb\db\driver\mysql_base
if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysql_version')) === false)
{
$result = @mysql_query('SELECT VERSION() AS version', $this->db_connect_id);
- $row = @mysql_fetch_assoc($result);
- @mysql_free_result($result);
+ if ($result)
+ {
+ $row = mysql_fetch_assoc($result);
+ mysql_free_result($result);
- $this->sql_server_version = $row['version'];
+ $this->sql_server_version = $row['version'];
- if (!empty($cache) && $use_cache)
- {
- $cache->put('mysql_version', $this->sql_server_version);
+ if (!empty($cache) && $use_cache)
+ {
+ $cache->put('mysql_version', $this->sql_server_version);
+ }
}
}
@@ -154,13 +163,7 @@ class mysql extends \phpbb\db\driver\mysql_base
}
/**
- * Base query method
- *
- * @param string $query Contains the SQL query which shall be executed
- * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
- * @return mixed When casted to bool the returned value returns true on success and false on failure
- *
- * @access public
+ * {@inheritDoc}
*/
function sql_query($query = '', $cache_ttl = 0)
{
@@ -173,6 +176,10 @@ class mysql extends \phpbb\db\driver\mysql_base
{
$this->sql_report('start', $query);
}
+ else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
+ {
+ $this->curtime = microtime(true);
+ }
$this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
$this->sql_add_num_queries($this->query_result);
@@ -188,13 +195,22 @@ class mysql extends \phpbb\db\driver\mysql_base
{
$this->sql_report('stop', $query);
}
+ else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
+ {
+ $this->sql_time += microtime(true) - $this->curtime;
+ }
+
+ if (!$this->query_result)
+ {
+ return false;
+ }
if ($cache && $cache_ttl)
{
$this->open_queries[(int) $this->query_result] = $this->query_result;
$this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
}
- else if (strpos($query, 'SELECT') === 0 && $this->query_result)
+ else if (strpos($query, 'SELECT') === 0)
{
$this->open_queries[(int) $this->query_result] = $this->query_result;
}
@@ -213,15 +229,34 @@ class mysql extends \phpbb\db\driver\mysql_base
}
/**
- * Return number of affected rows
+ * {@inheritDoc}
*/
function sql_affectedrows()
{
- return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false;
+ if ($this->db_connect_id)
+ {
+ // We always want the number of matched rows
+ // instead of changed rows, when running an update.
+ // So when mysql_info() returns the number of matched rows
+ // we return that one instead of mysql_affected_rows()
+ $mysql_info = @mysql_info($this->db_connect_id);
+ if ($mysql_info !== false)
+ {
+ $match = array();
+ preg_match('#^Rows matched: (\d)+ Changed: (\d)+ Warnings: (\d)+$#', $mysql_info, $match);
+ if (isset($match[1]))
+ {
+ return $match[1];
+ }
+ }
+
+ return @mysql_affected_rows($this->db_connect_id);
+ }
+ return false;
}
/**
- * Fetch current row
+ * {@inheritDoc}
*/
function sql_fetchrow($query_id = false)
{
@@ -237,12 +272,11 @@ class mysql extends \phpbb\db\driver\mysql_base
return $cache->sql_fetchrow($query_id);
}
- return ($query_id !== false) ? @mysql_fetch_assoc($query_id) : false;
+ return ($query_id) ? mysql_fetch_assoc($query_id) : false;
}
/**
- * Seek to given row number
- * rownum is zero-based
+ * {@inheritDoc}
*/
function sql_rowseek($rownum, &$query_id)
{
@@ -262,7 +296,7 @@ class mysql extends \phpbb\db\driver\mysql_base
}
/**
- * Get last inserted id after insert statement
+ * {@inheritDoc}
*/
function sql_nextid()
{
@@ -270,7 +304,7 @@ class mysql extends \phpbb\db\driver\mysql_base
}
/**
- * Free sql result
+ * {@inheritDoc}
*/
function sql_freeresult($query_id = false)
{
@@ -289,14 +323,14 @@ class mysql extends \phpbb\db\driver\mysql_base
if (isset($this->open_queries[(int) $query_id]))
{
unset($this->open_queries[(int) $query_id]);
- return @mysql_free_result($query_id);
+ return mysql_free_result($query_id);
}
return false;
}
/**
- * Escape string used in sql query
+ * {@inheritDoc}
*/
function sql_escape($msg)
{
@@ -392,12 +426,12 @@ class mysql extends \phpbb\db\driver\mysql_base
if ($result = @mysql_query("EXPLAIN $explain_query", $this->db_connect_id))
{
- while ($row = @mysql_fetch_assoc($result))
+ while ($row = mysql_fetch_assoc($result))
{
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
}
+ mysql_free_result($result);
}
- @mysql_free_result($result);
if ($html_table)
{
@@ -412,7 +446,7 @@ class mysql extends \phpbb\db\driver\mysql_base
if ($result = @mysql_query('SHOW PROFILE ALL;', $this->db_connect_id))
{
$this->html_hold .= '<br />';
- while ($row = @mysql_fetch_assoc($result))
+ while ($row = mysql_fetch_assoc($result))
{
// make <unknown> HTML safe
if (!empty($row['Source_function']))
@@ -430,8 +464,8 @@ class mysql extends \phpbb\db\driver\mysql_base
}
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
}
+ mysql_free_result($result);
}
- @mysql_free_result($result);
if ($html_table)
{
@@ -449,11 +483,14 @@ class mysql extends \phpbb\db\driver\mysql_base
$endtime = $endtime[0] + $endtime[1];
$result = @mysql_query($query, $this->db_connect_id);
- while ($void = @mysql_fetch_assoc($result))
+ if ($result)
{
- // Take the time spent on parsing rows into account
+ while ($void = mysql_fetch_assoc($result))
+ {
+ // Take the time spent on parsing rows into account
+ }
+ mysql_free_result($result);
}
- @mysql_free_result($result);
$splittime = explode(' ', microtime());
$splittime = $splittime[0] + $splittime[1];