From d1f3349f7b7cd7816c125cf83616db154a43b8b1 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Fri, 22 Apr 2005 17:44:36 +0000 Subject: [PATCH] - dbal changes git-svn-id: file:///svn/phpbb/trunk@5130 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/db/firebird.php | 103 +++- phpBB/includes/db/mssql-odbc.php | 99 +--- phpBB/includes/db/mssql.php | 686 ++++++++++++++----------- phpBB/includes/db/mysql4.php | 126 ++--- phpBB/includes/db/mysqli.php | 571 ++++++++++++++++++++ phpBB/install/install.php | 26 +- phpBB/install/schemas/mssql_schema.sql | 6 - phpBB/language/en/admin.php | 2 +- 8 files changed, 1145 insertions(+), 474 deletions(-) create mode 100644 phpBB/includes/db/mysqli.php diff --git a/phpBB/includes/db/firebird.php b/phpBB/includes/db/firebird.php index b0fb130735..afce881eaf 100644 --- a/phpBB/includes/db/firebird.php +++ b/phpBB/includes/db/firebird.php @@ -233,7 +233,7 @@ class sql_db function sql_affectedrows() { - // hmm, maybe doing something similar as in mssql-odbc.php? + // TODO: hmm, maybe doing something similar as in mssql-odbc.php? return ($this->query_result) ? true : false; } @@ -419,6 +419,107 @@ class sql_db return $result; } + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $this->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '' . $msg_title . ''; + echo '
phpBB LogoSQL Report      

Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '
Time spent on MySQL queries: ' . round($this->sql_time, 5) . 's | Time spent on PHP: ' . round($totaltime - $this->sql_time, 5) . 's
'; + echo $sql_report; + echo '

'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = @ibase_query($this->db_connect_id, $query); + while ($void = @ibase_fetch_object($result, IBASE_TEXT)) + { + // Take the time spent on parsing rows into account + } + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '

Query results obtained from the cache

'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: ' . sprintf('%.5f', ($time_cache)) . 's | Elapsed [db]: ' . sprintf('%.5f', $time_db) . 's

'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + @ibase_freeresult($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '

Query #' . $this->num_queries . '
' . $html_hold . '

'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: " . $this->sql_affectedrows($this->query_result) . ' | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: ' . sprintf('%.5f', $endtime - $curtime) . 's'; + } + else + { + $error = $this->sql_error(); + $sql_report .= 'FAILED - ' . SQL_LAYER . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '

'; + + $this->sql_time += $endtime - $curtime; + break; + } + } + } // class sql_db } // if ... define diff --git a/phpBB/includes/db/mssql-odbc.php b/phpBB/includes/db/mssql-odbc.php index 5645827472..9fb49837d1 100644 --- a/phpBB/includes/db/mssql-odbc.php +++ b/phpBB/includes/db/mssql-odbc.php @@ -387,9 +387,9 @@ class sql_db if ($query_id) { - if ($row < $this->num_rows[$query_id]) + if ($rownum < $this->num_rows[$query_id]) { - $getrow = ($row == -1) ? $this->current_row[$query_id] - 1 : $row; + $getrow = ($rownum == -1) ? $this->current_row[$query_id] - 1 : $rownum; return $this->result_rowset[$query_id][$getrow][$this->field_names[$query_id][$field]]; } @@ -407,7 +407,7 @@ class sql_db if (isset($this->current_row[$query_id])) { - $this->current_row[$query_id] = $offset - 1; + $this->current_row[$query_id] = $rownum; return true; } @@ -419,36 +419,6 @@ class sql_db return ($this->next_id[$this->db_connect_id]) ? $this->next_id[$this->db_connect_id] : false; } - function sql_numfields($query_id = false) - { - if (!$query_id) - { - $query_id = $this->query_result; - } - - return ($query_id) ? sizeof($this->field_names[$query_id]) : false; - } - - function sql_fieldname($offset, $query_id = false) - { - if (!$query_id) - { - $query_id = $this->query_result; - } - - return ($query_id) ? $this->field_names[$query_id][$offset] : false; - } - - function sql_fieldtype($offset, $query_id = false) - { - if (!$query_id) - { - $query_id = $this->query_result; - } - - return ($query_id) ? $this->field_types[$query_id][$offset] : false; - } - function sql_freeresult($query_id = false) { if (!$query_id) @@ -508,7 +478,7 @@ class sql_db return; } - global $db, $cache, $starttime, $phpbb_root_path; + global $cache, $starttime, $phpbb_root_path; static $curtime, $query_hold, $html_hold; static $sql_report = ''; static $cache_num_queries = 0; @@ -525,7 +495,7 @@ class sql_db { $cache->unload(); } - $db->sql_close(); + $this->sql_close(); $mtime = explode(' ', microtime()); $totaltime = $mtime[0] + $mtime[1] - $starttime; @@ -534,7 +504,7 @@ class sql_db echo 'th { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic3.gif\') }' . "\n"; echo 'td.cat { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic1.gif\') }' . "\n"; echo '' . $msg_title . ''; - echo '
phpBB LogoSQL Report      

Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '
Time spent on MSSQL queries: ' . round($this->sql_time, 5) . 's | Time spent on PHP: ' . round($totaltime - $this->sql_time, 5) . 's
'; + echo '
phpBB LogoSQL Report      

Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '
Time spent on MySQL queries: ' . round($this->sql_time, 5) . 's | Time spent on PHP: ' . round($totaltime - $this->sql_time, 5) . 's
'; echo $sql_report; echo '

'; exit; @@ -544,58 +514,6 @@ class sql_db $query_hold = $query; $html_hold = ''; - $explain_query = $query; - if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) - { - $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; - } - elseif (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) - { - $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; - } - - if (preg_match('/^SELECT/', $explain_query)) - { - $html_table = FALSE; - - @odbc_exec($this->db_connect_id, 'SET SHOWPLAN_ALL ON'); - - if ($result = $this->_odbc_execute_query($explain_query)) - { - while ($row = $this->sql_fetchrow($result)) - { - if (!$html_table && sizeof($row)) - { - $html_table = TRUE; - $html_hold .= ''; - - foreach (array_keys($row) as $val) - { - $html_hold .= ''; - } - $html_hold .= ''; - } - $html_hold .= ''; - - $class = 'row1'; - foreach (array_values($row) as $val) - { - $class = ($class == 'row1') ? 'row2' : 'row1'; - $html_hold .= ''; - } - $html_hold .= ''; - } - $this->sql_freeresult($result); - } - - @odbc_exec($this->db_connect_id, 'SET SHOWPLAN_ALL OFF'); - - if ($html_table) - { - $html_hold .= '
' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '
' . (($val) ? $val : ' ') . '
'; - } - } - $curtime = explode(' ', microtime()); $curtime = $curtime[0] + $curtime[1]; break; @@ -605,6 +523,7 @@ class sql_db $endtime = $endtime[0] + $endtime[1]; $result = $this->_odbc_execute_query($query); + $splittime = explode(' ', microtime()); $splittime = $splittime[0] + $splittime[1]; @@ -619,7 +538,7 @@ class sql_db // Pad the start time to not interfere with page timing $starttime += $time_db; - $this->sql_freeresult($result); + @odbc_free_result($result); $cache_num_queries++; break; @@ -640,7 +559,7 @@ class sql_db else { $error = $this->sql_error(); - $sql_report .= 'FAILED - MSSQL Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + $sql_report .= 'FAILED - ' . SQL_LAYER . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); } $sql_report .= '

'; diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index f68a904ce8..c13f2f2162 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -11,10 +11,10 @@ /** * @ignore */ -if (!defined("SQL_LAYER")) +if (!defined('SQL_LAYER')) { -define("SQL_LAYER","mssql"); +define('SQL_LAYER', 'mssql'); /** * @package dbal_mssql @@ -23,25 +23,19 @@ define("SQL_LAYER","mssql"); */ class sql_db { - var $db_connect_id; - var $result; - - var $next_id; - var $in_transaction = 0; - - var $row = array(); - var $rowset = array(); - var $limit_offset; - var $query_limit_success; - + var $query_result; + var $return_on_error = false; + var $transaction = false; + var $sql_time = 0; var $num_queries = 0; + var $open_queries = array(); - function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true) + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) { $this->persistency = $persistency; $this->user = $sqluser; - $this->server = $sqlserver; + $this->server = $sqlserver . (($port) ? ':' . $port : ''); $this->dbname = $database; $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword); @@ -58,6 +52,29 @@ class sql_db return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); } + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + if ($this->transaction) + { + @mssql_query('COMMIT', $this->db_connect_id); + } + + if (sizeof($this->open_queries)) + { + foreach ($this->open_queries as $i_query_id => $query_id) + { + @mssql_free_result($query_id); + } + } + + return @mssql_close($this->db_connect_id); + } + function sql_return_on_error($fail = false) { $this->return_on_error = $fail; @@ -68,342 +85,332 @@ class sql_db return $this->num_queries; } - // - // Other base methods - // - function sql_close() + function sql_transaction($status = 'begin') { - if ($this->db_connect_id) + switch ($status) { - // - // Commit any remaining transactions - // - if ($this->in_transaction) + case 'begin': + $result = @mssql_query('BEGIN TRANSACTION', $this->db_connect_id); + $this->transaction = true; + break; + + case 'commit': + $result = @mssql_query('commit', $this->db_connect_id); + $this->transaction = false; + + if (!$result) + { + @mssql_query('ROLLBACK', $this->db_connect_id); + } + break; + + case 'rollback': + $result = @mssql_query('ROLLBACK', $this->db_connect_id); + $this->transaction = false; + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $cache_ttl = 0) + { + if ($query != '') + { + global $cache; + + // EXPLAIN only in extra debug mode + if (defined('DEBUG_EXTRA')) { - @mssql_query("COMMIT", $this->db_connect_id); + $this->sql_report('start', $query); } - return @mssql_close($this->db_connect_id); + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; + + if (!$this->query_result) + { + $this->num_queries++; + + if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false) + { + $this->sql_error($query); + } + + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('stop', $query); + } + + if ($cache_ttl && method_exists($cache, 'sql_save')) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + $cache->sql_save($query, $this->query_result, $cache_ttl); + // sql_freeresult called within sql_save() + } + else if (strpos($query, 'SELECT') !== false && $this->query_result) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + } + } + else if (defined('DEBUG_EXTRA')) + { + $this->sql_report('fromcache', $query); + } } else { return false; } + + return ($this->query_result) ? $this->query_result : false; } - - // - // Query method - // - function sql_query($query = "", $transaction = FALSE) - { - // - // Remove any pre-existing queries - // - unset($this->result); - unset($this->row); - - if ($query != "") + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) + { + if ($query != '') { - $this->num_queries++; + $this->query_result = false; - if ($transaction == BEGIN_TRANSACTION && !$this->in_transaction) + // if $total is set to 0 we do not want to limit the number of rows + if ($total == 0) { - if (!mssql_query("BEGIN TRANSACTION", $this->db_connect_id)) - { - return false; - } - $this->in_transaction = TRUE; + $total = -1; } - // - // Does query contain any LIMIT code? If so pull out relevant start and num_results - // This isn't terribly easy with MSSQL, whatever you do will potentially impact - // performance compared to an 'in-built' limit - // - // Another issue is the 'lack' of a returned true value when a query is valid but has - // no result set (as with all the other DB interfaces). It seems though that it's - // 'fair' to say that if a query returns a false result (ie. no resource id) then the - // SQL was valid but had no result set. If the query returns nothing but the rowcount - // returns something then there's a problem. This may well be a false assumption though - // ... needs checking under Windows itself. - // - if (preg_match("/^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$/s", $query, $limits)) - { - $query = $limits[1]; + $row_offset = ($total) ? $offset : ''; + $num_rows = ($total) ? $total : $offset; - if (!empty($limits[2])) - { - $row_offset = ($limits[4]) ? $limits[3] : ""; - $num_rows = ($limits[4]) ? $limits[4] : $limits[3]; + $query = 'SELECT TOP ' . ($row_offset + $num_rows) . ' ' . substr($query, 6); - $query = "TOP " . ($row_offset + $num_rows) . $query; - } - - $this->result = mssql_query("SELECT $query", $this->db_connect_id); - - if ($this->result) - { - $this->limit_offset[$this->result] = (!empty($row_offset)) ? $row_offset : 0; - - if ($row_offset > 0) - { - mssql_data_seek($this->result, $row_offset); - } - } - } - else if (eregi("^INSERT ", $query)) - { - if (mssql_query($query, $this->db_connect_id)) - { - $this->result = time() + microtime(); - - $result_id = mssql_query("SELECT @@IDENTITY AS id, @@ROWCOUNT as affected", $this->db_connect_id); - if ($result_id) - { - if ($row = mssql_fetch_array($result_id)) - { - $this->next_id[$this->db_connect_id] = $row['id']; - $this->affected_rows[$this->db_connect_id] = $row['affected']; - } - } - } - } - else - { - if (mssql_query($query, $this->db_connect_id)) - { - $this->result = time() + microtime(); - - $result_id = mssql_query("SELECT @@ROWCOUNT as affected", $this->db_connect_id); - if ($result_id) - { - if ($row = mssql_fetch_array($result_id)) - { - $this->affected_rows[$this->db_connect_id] = $row['affected']; - } - } - } - } - - if (!$this->result) - { - if ($this->in_transaction) - { - mssql_query("ROLLBACK", $this->db_connect_id); - $this->in_transaction = FALSE; - } - - return false; - } - - if ($transaction == END_TRANSACTION && $this->in_transaction) - { - $this->in_transaction = FALSE; - - if (!@mssql_query("COMMIT", $this->db_connect_id)) - { - @mssql_query("ROLLBACK", $this->db_connect_id); - return false; - } - } - - return $this->result; - } - else - { - if ($transaction == END_TRANSACTION && $this->in_transaction) - { - $this->in_transaction = FALSE; - - if (!@mssql_query("COMMIT", $this->db_connect_id)) - { - @mssql_query("ROLLBACK", $this->db_connect_id); - return false; - } - } - - return true; - } + return $this->sql_query($query, $cache_ttl); + } + else + { + return false; + } } - // - // Other query methods - // - function sql_numrows($query_id = 0) + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) { - if (!$query_id) - { - $query_id = $this->result; - } - - if ($query_id) - { - return (!empty($this->limit_offset[$query_id])) ? mssql_num_rows($query_id) - $this->limit_offset[$query_id] : @mssql_num_rows($query_id); - } - else + if (!is_array($assoc_ary)) { return false; } - } - function sql_numfields($query_id = 0) - { - if (!$query_id) + $fields = array(); + $values = array(); + if ($query == 'INSERT') { - $query_id = $this->result; - } - - return ($query_id) ? mssql_num_fields($query_id) : false; - } - - function sql_fieldname($offset, $query_id = 0) - { - if (!$query_id) - { - $query_id = $this->result; - } - - return ($query_id) ? mssql_field_name($query_id, $offset) : false; - } - - function sql_fieldtype($offset, $query_id = 0) - { - if (!$query_id) - { - $query_id = $this->result; - } - - return ($query_id) ? mssql_field_type($query_id, $offset) : false; - } - - function sql_fetchrow($query_id = 0) - { - if (!$query_id) - { - $query_id = $this->result; - } - - if ($query_id) - { - empty($row); - - $row = mssql_fetch_array($query_id); - - foreach ($row as $key => $value) + foreach ($assoc_ary as $key => $var) { - $row[$key] = stripslashes($value); - } + $fields[] = $key; - return $row; - } - else - { - return false; - } - } - - function sql_fetchrowset($query_id = 0) - { - if (!$query_id) - { - $query_id = $this->result; - } - - if ($query_id) - { - $i = 0; - $rowset = array(); - - while ($row = mssql_fetch_array($query_id)) - { - foreach ($row as $key => $value) + if (is_null($var)) { - $rowset[$i][$key] = stripslashes($value); + $values[] = 'NULL'; } - $i++; - } - - return $rowset; - } - else - { - return false; - } - } - - function sql_fetchfield($field, $row = -1, $query_id) - { - if (!$query_id) - { - $query_id = $this->result; - } - - if ($query_id) - { - if ($row != -1) - { - if ($this->limit_offset[$query_id] > 0) + elseif (is_string($var)) { - $result = (!empty($this->limit_offset[$query_id])) ? mssql_result($this->result, ($this->limit_offset[$query_id] + $row), $field) : false; + $values[] = "'" . $this->sql_escape($var) . "'"; } else { - $result = mssql_result($this->result, $row, $field); + $values[] = (is_bool($var)) ? intval($var) : $var; } } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE' || $query == 'SELECT') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); + } + + return $query; + } + + // Other query methods + // + // NOTE :: Want to remove _ALL_ reliance on sql_numrows from core code ... + // don't want this here by a middle Milestone + function sql_numrows($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + +// return (isset($this->limit_offset[$query_id])) ? @mssql_num_rows($query_id) - $this->limit_offset[$query_id] : @mssql_num_rows($query_id); + return ($query_id) ? @mssql_num_rows($query_id) : false; + } + + function sql_affectedrows() + { + return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false; + } + + function sql_fetchrow($query_id = false) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($cache->sql_rowset[$query_id])) + { + return $cache->sql_fetchrow($query_id); + } + + $row = @mssql_fetch_array($query_id, MSSQL_ASSOC); + + if ($row) + { + foreach ($row as $key => $value) + { + $row[$key] = ($value === ' ') ? trim($value) : $value; + } + } + + return $row; + } + + function sql_fetchrowset($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + unset($this->rowset[$query_id]); + unset($this->row[$query_id]); + + $result = array(); + while ($this->rowset[$query_id] = $this->sql_fetchrow($query_id)) + { + $result[] = $this->rowset[$query_id]; + } + return $result; + } + + return false; + } + + function sql_fetchfield($field, $rownum = -1, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($rownum > -1) + { +// (!empty($this->limit_offset[$query_id])) ? @mssql_data_seek($query_id, ($this->limit_offset[$query_id] + $rownum)) : @mssql_data_seek($query_id, $rownum); + @mssql_data_seek($query_id, $rownum); + $row = @mssql_fetch_array($query_id, MSSQL_ASSOC); + $result = isset($row[$field]) ? $row[$field] : false; + } else { - if (empty($this->row[$query_id])) + if (empty($this->row[$query_id]) && empty($this->rowset[$query_id])) { - $this->row[$query_id] = mssql_fetch_array($query_id); - $result = stripslashes($this->row[$query_id][$field]); + if ($this->sql_fetchrow($query_id)) + { + $result = $this->row[$query_id][$field]; + } + } + else + { + if ($this->rowset[$query_id]) + { + $result = $this->rowset[$query_id][$field]; + } + elseif ($this->row[$query_id]) + { + $result = $this->row[$query_id][$field]; + } } } return $result; } - else - { - return false; - } + + return false; } - function sql_rowseek($rownum, $query_id = 0) + function sql_rowseek($rownum, $query_id = false) { if (!$query_id) { - $query_id = $this->result; + $query_id = $this->query_result; } - if ($query_id) + if (isset($this->current_row[$query_id])) { - return (!empty($this->limit_offset[$query_id])) ? mssql_data_seek($query_id, ($this->limit_offset[$query_id] + $rownum)) : mssql_data_seek($query_id, $rownum); - } - else - { - return false; +// (!empty($this->limit_offset[$query_id])) ? @mssql_data_seek($query_id, ($this->limit_offset[$query_id] + $rownum)) : @mssql_data_seek($query_id, $rownum); + @mssql_data_seek($query_id, $rownum); + return true; } + + return false; } function sql_nextid() { - return ($this->next_id[$this->db_connect_id]) ? $this->next_id[$this->db_connect_id] : false; + $result_id = @mssql_query('SELECT @@IDENTITY', $this->db_connect_id); + if ($result_id) + { + if (@mssql_fetch_array($result_id, MSSQL_ASSOC)) + { + return @mssql_result($result_id, 1); + } + } + + return false; } - function sql_affectedrows() - { - return ($this->affected_rows[$this->db_connect_id]) ? $this->affected_rows[$this->db_connect_id] : false; - } - - function sql_freeresult($query_id = 0) + function sql_freeresult($query_id = false) { if (!$query_id) { - $query_id = $this->result; + $query_id = $this->query_result; } - return ($query_id) ? mssql_free_result($query_id) : false; + if (isset($this->open_queries[$query_id])) + { + unset($this->open_queries[$query_id]); + unset($this->result_rowset[$query_id]); + + return @mssql_free_result($query_id); + } + + return false; } function sql_escape($msg) @@ -415,8 +422,8 @@ class sql_db { if (!$this->return_on_error) { - $this_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; - $this_page .= '&' . ((!empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : $_ENV['QUERY_STRING']); + $this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : '')); $message = 'SQL ERROR [ ' . SQL_LAYER . ' ]

' . @mssql_get_last_message() . '

CALLING PAGE

' . htmlspecialchars($this_page) . (($sql != '') ? '

SQL

' . $sql : '') . '
'; @@ -429,13 +436,114 @@ class sql_db } $result = array( - 'message' => @mssql_get_last_message(), + 'message' => @mssql_get_last_message($this->db_connect_id), 'code' => '' ); return $result; } + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $this->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '' . $msg_title . ''; + echo '
phpBB LogoSQL Report      

Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '
Time spent on MySQL queries: ' . round($this->sql_time, 5) . 's | Time spent on PHP: ' . round($totaltime - $this->sql_time, 5) . 's
'; + echo $sql_report; + echo '

'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = @mssql_query($query, $this->db_connect_id); + while ($void = @mssql_fetch_array($result, MSSQL_ASSOC)) + { + // Take the time spent on parsing rows into account + } + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '

Query results obtained from the cache

'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: ' . sprintf('%.5f', ($time_cache)) . 's | Elapsed [db]: ' . sprintf('%.5f', $time_db) . 's

'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + @mssql_free_result($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '

Query #' . $this->num_queries . '
' . $html_hold . '

'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: " . $this->sql_affectedrows($this->query_result) . ' | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: ' . sprintf('%.5f', $endtime - $curtime) . 's'; + } + else + { + $error = $this->sql_error(); + $sql_report .= 'FAILED - ' . SQL_LAYER . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '

'; + + $this->sql_time += $endtime - $curtime; + break; + } + } + } // class sql_db } // if ... define diff --git a/phpBB/includes/db/mysql4.php b/phpBB/includes/db/mysql4.php index 041e148c11..bf323ec54e 100644 --- a/phpBB/includes/db/mysql4.php +++ b/phpBB/includes/db/mysql4.php @@ -19,9 +19,7 @@ define('SQL_LAYER', 'mysql4'); /** * @package dbal_mysql4 * MySQL4 Database Abstraction Layer -* Minimum Requirement is MySQL 4.1+ -* -* @todo Rename mysql4.php to mysqli.php to reflect the used module +* Minimum Requirement is 4.0+/4.1+ */ class sql_db { @@ -33,8 +31,6 @@ class sql_db var $num_queries = 0; var $open_queries = array(); - var $indexed = 0; - function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) { $this->persistency = $persistency; @@ -42,11 +38,11 @@ class sql_db $this->server = $sqlserver . (($port) ? ':' . $port : ''); $this->dbname = $database; - $this->db_connect_id = ($this->persistency) ? @mysqli_pconnect($this->server, $this->user, $sqlpassword) : @mysqli_connect($this->server, $this->user, $sqlpassword); + $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $sqlpassword) : @mysql_connect($this->server, $this->user, $sqlpassword); if ($this->db_connect_id && $this->dbname != '') { - if (@mysqli_select_db($this->db_connect_id, $this->dbname)) + if (@mysql_select_db($this->dbname)) { return $this->db_connect_id; } @@ -65,12 +61,15 @@ class sql_db return false; } - if ($this->transaction) + if (sizeof($this->open_queries)) { - @mysqli_commit($this->db_connect_id); + foreach ($this->open_queries as $i_query_id => $query_id) + { + @mysql_free_result($query_id); + } } - return @mysqli_close($this->db_connect_id); + return @mysql_close($this->db_connect_id); } function sql_return_on_error($fail = false) @@ -88,25 +87,22 @@ class sql_db switch ($status) { case 'begin': - $result = @mysqli_autocommit($this->db_connect_id, false); + $result = @mysql_query('BEGIN', $this->db_connect_id); $this->transaction = true; break; case 'commit': - $result = @mysqli_commit($this->db_connect_id); - @mysqli_autocommit($this->db_connect_id, true); + $result = @mysql_query('COMMIT', $this->db_connect_id); $this->transaction = false; - + if (!$result) { - @mysqli_rollback($this->db_connect_id); - @mysqli_autocommit($this->db_connect_id, true); + @mysql_query('ROLLBACK', $this->db_connect_id); } break; case 'rollback': - $result = @mysqli_rollback($this->db_connect_id); - @mysqli_autocommit($this->db_connect_id, true); + $result = @mysql_query('ROLLBACK', $this->db_connect_id); $this->transaction = false; break; @@ -131,21 +127,16 @@ class sql_db } $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; - + if (!$this->query_result) { $this->num_queries++; - if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false) + if (($this->query_result = @mysql_query($query, $this->db_connect_id)) === false) { $this->sql_error($query); } - if (is_object($this->query_result)) - { - $this->query_result->cur_index = $this->indexed++; - } - if (defined('DEBUG_EXTRA')) { $this->sql_report('stop', $query); @@ -153,7 +144,13 @@ class sql_db if ($cache_ttl && method_exists($cache, 'sql_save')) { + $this->open_queries[(int) $this->query_result] = $this->query_result; $cache->sql_save($query, $this->query_result, $cache_ttl); + // mysql_free_result called within sql_save() + } + else if (strpos($query, 'SELECT') !== false && $this->query_result) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; } } else if (defined('DEBUG_EXTRA')) @@ -258,12 +255,12 @@ class sql_db $query_id = $this->query_result; } - return ($query_id) ? @mysqli_num_rows($query_id) : false; + return ($query_id) ? @mysql_num_rows($query_id) : false; } function sql_affectedrows() { - return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false; + return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false; } function sql_fetchrow($query_id = false) @@ -275,12 +272,12 @@ class sql_db $query_id = $this->query_result; } - if (!is_object($query_id) && isset($cache->sql_rowset[$query_id])) + if (isset($cache->sql_rowset[$query_id])) { return $cache->sql_fetchrow($query_id); } - return ($query_id) ? @mysqli_fetch_assoc($query_id) : false; + return ($query_id) ? @mysql_fetch_assoc($query_id) : false; } function sql_fetchrowset($query_id = false) @@ -292,19 +289,17 @@ class sql_db if ($query_id) { - $cur_index = (is_object($query_id)) ? $query_id->cur_index : $query_id; + unset($this->rowset[$query_id]); + unset($this->row[$query_id]); - unset($this->rowset[$cur_index]); - unset($this->row[$cur_index]); - $result = array(); - while ($this->rowset[$cur_index] = $this->sql_fetchrow($query_id)) + while ($this->rowset[$query_id] = $this->sql_fetchrow($query_id)) { - $result[] = $this->rowset[$cur_index]; + $result[] = $this->rowset[$query_id]; } return $result; } - + return false; } @@ -319,30 +314,26 @@ class sql_db { if ($rownum > -1) { - @mysqli_data_seek($query_id, $rownum); - $row = @mysqli_fetch_assoc($query_id); - $result = isset($row[$field]) ? $row[$field] : false; + $result = @mysql_result($query_id, $rownum, $field); } else { - $cur_index = (is_object($query_id)) ? $query_id->cur_index : $query_id; - - if (empty($this->row[$cur_index]) && empty($this->rowset[$cur_index])) + if (empty($this->row[$query_id]) && empty($this->rowset[$query_id])) { - if ($this->row[$cur_index] = $this->sql_fetchrow($query_id)) + if ($this->sql_fetchrow($query_id)) { - $result = $this->row[$cur_index][$field]; + $result = $this->row[$query_id][$field]; } } else { - if ($this->rowset[$cur_index]) + if ($this->rowset[$query_id]) { - $result = $this->rowset[$cur_index][$field]; + $result = $this->rowset[$query_id][$field]; } - elseif ($this->row[$cur_index]) + elseif ($this->row[$query_id]) { - $result = $this->row[$cur_index][$field]; + $result = $this->row[$query_id][$field]; } } } @@ -358,12 +349,12 @@ class sql_db $query_id = $this->query_result; } - return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false; + return ($query_id) ? @mysql_data_seek($query_id, $rownum) : false; } function sql_nextid() { - return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false; + return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false; } function sql_freeresult($query_id = false) @@ -373,25 +364,18 @@ class sql_db $query_id = $this->query_result; } - $cur_index = (is_object($query_id)) ? $query_id->cur_index : $query_id; - - unset($this->rowset[$cur_index]); - unset($this->row[$cur_index]); - - if (is_object($query_id)) + if (isset($this->open_queries[(int) $query_id])) { - $this->indexed--; - return @mysqli_free_result($query_id); - } - else - { - return false; + unset($this->open_queries[(int) $query_id]); + return @mysql_free_result($query_id); } + + return false; } function sql_escape($msg) { - return @mysqli_real_escape_string($this->db_connect_id, $msg); + return mysql_escape_string($msg); } function sql_error($sql = '') @@ -401,7 +385,7 @@ class sql_db $this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; $this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : '')); - $message = 'SQL ERROR [ ' . SQL_LAYER . ' ]

' . @mysqli_error($this->db_connect_id) . '

CALLING PAGE

' . htmlspecialchars($this_page) . (($sql != '') ? '

SQL

' . $sql : '') . '
'; + $message = 'SQL ERROR [ ' . SQL_LAYER . ' ]

' . @mysql_error() . '

CALLING PAGE

' . htmlspecialchars($this_page) . (($sql != '') ? '

SQL

' . $sql : '') . '
'; if ($this->transaction) { @@ -412,8 +396,8 @@ class sql_db } $result = array( - 'message' => @mysqli_error($this->db_connect_id), - 'code' => @mysqli_errno($this->db_connect_id) + 'message' => @mysql_error(), + 'code' => @mysql_errno() ); return $result; @@ -476,9 +460,9 @@ class sql_db { $html_table = FALSE; - if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query")) + if ($result = mysql_query("EXPLAIN $explain_query", $this->db_connect_id)) { - while ($row = @mysqli_fetch_assoc($result)) + while ($row = mysql_fetch_assoc($result)) { if (!$html_table && sizeof($row)) { @@ -517,8 +501,8 @@ class sql_db $endtime = explode(' ', microtime()); $endtime = $endtime[0] + $endtime[1]; - $result = @mysqli_query($this->db_connect_id, $query); - while ($void = @mysqli_fetch_assoc($result)) + $result = mysql_query($query, $this->db_connect_id); + while ($void = mysql_fetch_assoc($result)) { // Take the time spent on parsing rows into account } @@ -536,7 +520,7 @@ class sql_db // Pad the start time to not interfere with page timing $starttime += $time_db; - @mysqli_free_result($result); + mysql_free_result($result); $cache_num_queries++; break; diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php new file mode 100644 index 0000000000..c4222ae4e5 --- /dev/null +++ b/phpBB/includes/db/mysqli.php @@ -0,0 +1,571 @@ +persistency = $persistency; + $this->user = $sqluser; + $this->server = $sqlserver . (($port) ? ':' . $port : ''); + $this->dbname = $database; + + $this->db_connect_id = ($this->persistency) ? @mysqli_pconnect($this->server, $this->user, $sqlpassword) : @mysqli_connect($this->server, $this->user, $sqlpassword); + + if ($this->db_connect_id && $this->dbname != '') + { + if (@mysqli_select_db($this->db_connect_id, $this->dbname)) + { + return $this->db_connect_id; + } + } + + return $this->sql_error(''); + } + + // + // Other base methods + // + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + if ($this->transaction) + { + @mysqli_commit($this->db_connect_id); + } + + return @mysqli_close($this->db_connect_id); + } + + function sql_return_on_error($fail = false) + { + $this->return_on_error = $fail; + } + + function sql_num_queries() + { + return $this->num_queries; + } + + function sql_transaction($status = 'begin') + { + switch ($status) + { + case 'begin': + $result = @mysqli_autocommit($this->db_connect_id, false); + $this->transaction = true; + break; + + case 'commit': + $result = @mysqli_commit($this->db_connect_id); + @mysqli_autocommit($this->db_connect_id, true); + $this->transaction = false; + + if (!$result) + { + @mysqli_rollback($this->db_connect_id); + @mysqli_autocommit($this->db_connect_id, true); + } + break; + + case 'rollback': + $result = @mysqli_rollback($this->db_connect_id); + @mysqli_autocommit($this->db_connect_id, true); + $this->transaction = false; + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $cache_ttl = 0) + { + if ($query != '') + { + global $cache; + + // EXPLAIN only in extra debug mode + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('start', $query); + } + + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; + + if (!$this->query_result) + { + $this->num_queries++; + + if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false) + { + $this->sql_error($query); + } + + if (is_object($this->query_result)) + { + $this->query_result->cur_index = $this->indexed++; + } + + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('stop', $query); + } + + if ($cache_ttl && method_exists($cache, 'sql_save')) + { + $cache->sql_save($query, $this->query_result, $cache_ttl); + } + } + else if (defined('DEBUG_EXTRA')) + { + $this->sql_report('fromcache', $query); + } + } + else + { + return false; + } + + return ($this->query_result) ? $this->query_result : false; + } + + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) + { + if ($query != '') + { + $this->query_result = false; + + // if $total is set to 0 we do not want to limit the number of rows + if ($total == 0) + { + $total = -1; + } + + $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total); + + return $this->sql_query($query, $cache_ttl); + } + else + { + return false; + } + } + + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) + { + if (!is_array($assoc_ary)) + { + return false; + } + + $fields = array(); + $values = array(); + if ($query == 'INSERT') + { + foreach ($assoc_ary as $key => $var) + { + $fields[] = $key; + + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE' || $query == 'SELECT') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); + } + + return $query; + } + + // Other query methods + // + // NOTE :: Want to remove _ALL_ reliance on sql_numrows from core code ... + // don't want this here by a middle Milestone + function sql_numrows($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @mysqli_num_rows($query_id) : false; + } + + function sql_affectedrows() + { + return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false; + } + + function sql_fetchrow($query_id = false) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (!is_object($query_id) && isset($cache->sql_rowset[$query_id])) + { + return $cache->sql_fetchrow($query_id); + } + + return ($query_id) ? @mysqli_fetch_assoc($query_id) : false; + } + + function sql_fetchrowset($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + $cur_index = (is_object($query_id)) ? $query_id->cur_index : $query_id; + + unset($this->rowset[$cur_index]); + unset($this->row[$cur_index]); + + $result = array(); + while ($this->rowset[$cur_index] = $this->sql_fetchrow($query_id)) + { + $result[] = $this->rowset[$cur_index]; + } + return $result; + } + + return false; + } + + function sql_fetchfield($field, $rownum = -1, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($rownum > -1) + { + @mysqli_data_seek($query_id, $rownum); + $row = @mysqli_fetch_assoc($query_id); + $result = isset($row[$field]) ? $row[$field] : false; + } + else + { + $cur_index = (is_object($query_id)) ? $query_id->cur_index : $query_id; + + if (empty($this->row[$cur_index]) && empty($this->rowset[$cur_index])) + { + if ($this->row[$cur_index] = $this->sql_fetchrow($query_id)) + { + $result = $this->row[$cur_index][$field]; + } + } + else + { + if ($this->rowset[$cur_index]) + { + $result = $this->rowset[$cur_index][$field]; + } + elseif ($this->row[$cur_index]) + { + $result = $this->row[$cur_index][$field]; + } + } + } + return $result; + } + return false; + } + + function sql_rowseek($rownum, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false; + } + + function sql_nextid() + { + return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false; + } + + function sql_freeresult($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + $cur_index = (is_object($query_id)) ? $query_id->cur_index : $query_id; + + unset($this->rowset[$cur_index]); + unset($this->row[$cur_index]); + + if (is_object($query_id)) + { + $this->indexed--; + return @mysqli_free_result($query_id); + } + else + { + return false; + } + } + + function sql_escape($msg) + { + return @mysqli_real_escape_string($this->db_connect_id, $msg); + } + + function sql_error($sql = '') + { + if (!$this->return_on_error) + { + $this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : '')); + + $message = 'SQL ERROR [ ' . SQL_LAYER . ' ]

' . @mysqli_error($this->db_connect_id) . '

CALLING PAGE

' . htmlspecialchars($this_page) . (($sql != '') ? '

SQL

' . $sql : '') . '
'; + + if ($this->transaction) + { + $this->sql_transaction('rollback'); + } + + trigger_error($message, E_USER_ERROR); + } + + $result = array( + 'message' => @mysqli_error($this->db_connect_id), + 'code' => @mysqli_errno($this->db_connect_id) + ); + + return $result; + } + + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $db, $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $db->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '' . $msg_title . ''; + echo '
phpBB LogoSQL Report      

Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '
Time spent on MySQL queries: ' . round($this->sql_time, 5) . 's | Time spent on PHP: ' . round($totaltime - $this->sql_time, 5) . 's
'; + echo $sql_report; + echo '

'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $explain_query = $query; + if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) + { + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; + } + elseif (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) + { + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; + } + + if (preg_match('/^SELECT/', $explain_query)) + { + $html_table = FALSE; + + if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query")) + { + while ($row = @mysqli_fetch_assoc($result)) + { + if (!$html_table && sizeof($row)) + { + $html_table = TRUE; + $html_hold .= ''; + + foreach (array_keys($row) as $val) + { + $html_hold .= ''; + } + $html_hold .= ''; + } + $html_hold .= ''; + + $class = 'row1'; + foreach (array_values($row) as $val) + { + $class = ($class == 'row1') ? 'row2' : 'row1'; + $html_hold .= ''; + } + $html_hold .= ''; + } + } + + if ($html_table) + { + $html_hold .= '
' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '
' . (($val) ? $val : ' ') . '
'; + } + } + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = @mysqli_query($this->db_connect_id, $query); + while ($void = @mysqli_fetch_assoc($result)) + { + // Take the time spent on parsing rows into account + } + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '

Query results obtained from the cache

'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: ' . sprintf('%.5f', ($time_cache)) . 's | Elapsed [db]: ' . sprintf('%.5f', $time_db) . 's

'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + @mysqli_free_result($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '

Query #' . $this->num_queries . '
' . $html_hold . '

'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: " . $this->sql_affectedrows($this->query_result) . ' | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: ' . sprintf('%.5f', $endtime - $curtime) . 's'; + } + else + { + $error = $this->sql_error(); + $sql_report .= 'FAILED - MySQL Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '

'; + + $this->sql_time += $endtime - $curtime; + break; + } + } +} // class sql_db + +} // if ... define + +?> \ No newline at end of file diff --git a/phpBB/install/install.php b/phpBB/install/install.php index b9bb36276e..2980c9eef1 100644 --- a/phpBB/install/install.php +++ b/phpBB/install/install.php @@ -92,13 +92,20 @@ $available_dbms = array( 'DELIM' => ';', 'COMMENTS' => 'remove_remarks' ), - 'mysql4' => array( - 'LABEL' => 'MySQL 4.1.x', + 'mysqli' => array( + 'LABEL' => 'MySQL 4.1.x (MySQLi)', 'SCHEMA' => 'mysql', 'MODULE' => 'mysqli', 'DELIM' => ';', 'COMMENTS' => 'remove_remarks' ), + 'mysql4' => array( + 'LABEL' => 'MySQL 4', + 'SCHEMA' => 'mysql', + 'MODULE' => 'mysql', + 'DELIM' => ';', + 'COMMENTS' => 'remove_remarks' + ), 'mssql' => array( 'LABEL' => 'MS SQL Server 7/2000', 'SCHEMA' => 'mssql', @@ -106,13 +113,6 @@ $available_dbms = array( 'DELIM' => 'GO', 'COMMENTS' => 'remove_comments' ), - 'msaccess' => array( - 'LABEL' => 'MS Access [ ODBC ]', - 'SCHEMA' => '', - 'MODULE' => 'odbc', - 'DELIM' => '', - 'COMMENTS' => '' - ), 'mssql-odbc'=> array( 'LABEL' => 'MS SQL Server [ ODBC ]', 'SCHEMA' => 'mssql', @@ -1402,6 +1402,7 @@ function connect_check_db($error_connect, &$error, &$dbms, &$table_prefix, &$dbh { case 'mysql': case 'mysql4': + case 'mysqli': $sql = "SHOW TABLES"; $field = "Tables_in_{$dbname}"; break; @@ -1414,13 +1415,6 @@ function connect_check_db($error_connect, &$error, &$dbms, &$table_prefix, &$dbh $field = "name"; break; - case 'msaccess': - $sql = 'SELECT * FROM MSysObjects - WHERE Type = 1 - AND Name Not Like "MSys*"'; - $field = "Name"; - break; - case 'postgres': $sql = "SELECT relname FROM pg_class diff --git a/phpBB/install/schemas/mssql_schema.sql b/phpBB/install/schemas/mssql_schema.sql index 789505768b..a07eda57d7 100644 --- a/phpBB/install/schemas/mssql_schema.sql +++ b/phpBB/install/schemas/mssql_schema.sql @@ -1407,12 +1407,6 @@ ALTER TABLE [phpbb_ranks] WITH NOCHECK ADD CONSTRAINT [DF_ranks__rank_special] DEFAULT (0) FOR [rank_special] GO -ALTER TABLE [phpbb_ratings] WITH NOCHECK ADD - CONSTRAINT [DF_rating_post_id] DEFAULT (0) FOR [post_id], - CONSTRAINT [DF_rating_user_id] DEFAULT (0) FOR [user_id], - CONSTRAINT [DF_rating_rating] DEFAULT (0) FOR [rating] -GO - ALTER TABLE [phpbb_reports] WITH NOCHECK ADD CONSTRAINT [DF_report_reason_id] DEFAULT (0) FOR [reason_id], CONSTRAINT [DF_report_post_id] DEFAULT (0) FOR [post_id], diff --git a/phpBB/language/en/admin.php b/phpBB/language/en/admin.php index 08b032870a..342eaa0d31 100644 --- a/phpBB/language/en/admin.php +++ b/phpBB/language/en/admin.php @@ -1933,9 +1933,9 @@ $lang += array( 'DLL_FIREBIRD' => 'Firebird 1.5+', 'DLL_MYSQL' => 'MySQL 3.23.x/4.x', 'DLL_MYSQL4' => 'MySQL 4.1+', + 'DLL_MYSQLI' => 'MySQL 4.1+ with MySQLi Extension', 'DLL_MSSQL' => 'MSSQL Server 2000', 'DLL_MSSQL-ODBC' => 'MSSQL Server 2000 via ODBC', - 'DLL_MSACCESS' => 'MS Access via ODBC', 'DLL_ORACLE' => 'Oracle', 'DLL_POSTGRES' => 'PostgreSQL 7.x', 'DLL_SQLITE' => 'SQLite',