aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Fischer <bantu@phpbb.com>2012-12-08 03:04:05 +0100
committerAndreas Fischer <bantu@phpbb.com>2012-12-08 03:04:05 +0100
commitb5f94a14f1a5c77366f824780f1120a1b5e9184a (patch)
tree22c4c57a1fc7a3d2e0f2946272c3c0725f125446
parenta8deb7f87b359802f9a8a53e44f4f9a9a6d87575 (diff)
parent5120f36a258ffcbffd5c10e9a1056d64ea794a16 (diff)
downloadforums-b5f94a14f1a5c77366f824780f1120a1b5e9184a.tar
forums-b5f94a14f1a5c77366f824780f1120a1b5e9184a.tar.gz
forums-b5f94a14f1a5c77366f824780f1120a1b5e9184a.tar.bz2
forums-b5f94a14f1a5c77366f824780f1120a1b5e9184a.tar.xz
forums-b5f94a14f1a5c77366f824780f1120a1b5e9184a.zip
Merge remote-tracking branch 'p/ticket/10205' into develop-olympus
* p/ticket/10205: [ticket/10205] Reduce nesting in mysql drivers. [ticket/10205] Rewrite _sql_error implementations to have a single return. [ticket/10205] Cosmetic changes. [ticket/10205] Add some columns to the empty fixture file for mssqlnative. [ticket/10205] Delete stray return. [ticket/10205] Test failed connection attempts. [ticket/10205] Check for function existence in mssql connect method. [ticket/10205] Convert mssqlnative driver to the same logic. [ticket/10205] Fix a parse error in oracle driver. [ticket/10205] Fix remaining db drivers. [ticket/10205] Avoid calling mysqli functions when mysqli is missing. [ticket/10205] Account for potentially missing extensions in dbal.
-rw-r--r--phpBB/includes/db/mssql.php66
-rw-r--r--phpBB/includes/db/mssql_odbc.php40
-rw-r--r--phpBB/includes/db/mssqlnative.php52
-rw-r--r--phpBB/includes/db/mysql.php45
-rw-r--r--phpBB/includes/db/mysqli.php32
-rw-r--r--phpBB/includes/db/oracle.php53
-rw-r--r--phpBB/includes/db/sqlite.php41
-rw-r--r--tests/dbal/connect_test.php43
-rw-r--r--tests/fixtures/empty.xml9
9 files changed, 305 insertions, 76 deletions
diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php
index b7178593dc..2dd95c2508 100644
--- a/phpBB/includes/db/mssql.php
+++ b/phpBB/includes/db/mssql.php
@@ -25,11 +25,19 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
*/
class dbal_mssql extends dbal
{
+ var $connect_error = '';
+
/**
* Connect to server
*/
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
{
+ if (!function_exists('mssql_connect'))
+ {
+ $this->connect_error = 'mssql_connect function does not exist, is mssql extension installed?';
+ return $this->sql_error('');
+ }
+
$this->persistency = $persistency;
$this->user = $sqluser;
$this->dbname = $database;
@@ -355,34 +363,44 @@ class dbal_mssql extends dbal
*/
function _sql_error()
{
- $error = array(
- 'message' => @mssql_get_last_message(),
- 'code' => ''
- );
-
- // Get error code number
- $result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
- if ($result_id)
+ if (function_exists('mssql_get_last_message'))
{
- $row = @mssql_fetch_assoc($result_id);
- $error['code'] = $row['code'];
- @mssql_free_result($result_id);
- }
+ $error = array(
+ 'message' => @mssql_get_last_message(),
+ 'code' => '',
+ );
- // Get full error message if possible
- $sql = 'SELECT CAST(description as varchar(255)) as message
- FROM master.dbo.sysmessages
- WHERE error = ' . $error['code'];
- $result_id = @mssql_query($sql);
-
- if ($result_id)
- {
- $row = @mssql_fetch_assoc($result_id);
- if (!empty($row['message']))
+ // Get error code number
+ $result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
+ if ($result_id)
+ {
+ $row = @mssql_fetch_assoc($result_id);
+ $error['code'] = $row['code'];
+ @mssql_free_result($result_id);
+ }
+
+ // Get full error message if possible
+ $sql = 'SELECT CAST(description as varchar(255)) as message
+ FROM master.dbo.sysmessages
+ WHERE error = ' . $error['code'];
+ $result_id = @mssql_query($sql);
+
+ if ($result_id)
{
- $error['message'] .= '<br />' . $row['message'];
+ $row = @mssql_fetch_assoc($result_id);
+ if (!empty($row['message']))
+ {
+ $error['message'] .= '<br />' . $row['message'];
+ }
+ @mssql_free_result($result_id);
}
- @mssql_free_result($result_id);
+ }
+ else
+ {
+ $error = array(
+ 'message' => $this->connect_error,
+ 'code' => '',
+ );
}
return $error;
diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php
index 2ecc42cadf..04501cce8b 100644
--- a/phpBB/includes/db/mssql_odbc.php
+++ b/phpBB/includes/db/mssql_odbc.php
@@ -32,6 +32,7 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
class dbal_mssql_odbc extends dbal
{
var $last_query_text = '';
+ var $connect_error = '';
/**
* Connect to server
@@ -68,7 +69,24 @@ class dbal_mssql_odbc extends dbal
@ini_set('odbc.defaultlrl', $max_size);
}
- $this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword);
+ if ($this->persistency)
+ {
+ if (!function_exists('odbc_pconnect'))
+ {
+ $this->connect_error = 'odbc_pconnect function does not exist, is odbc extension installed?';
+ return $this->sql_error('');
+ }
+ $this->db_connect_id = @odbc_pconnect($this->server, $this->user, $sqlpassword);
+ }
+ else
+ {
+ if (!function_exists('odbc_connect'))
+ {
+ $this->connect_error = 'odbc_connect function does not exist, is odbc extension installed?';
+ return $this->sql_error('');
+ }
+ $this->db_connect_id = @odbc_connect($this->server, $this->user, $sqlpassword);
+ }
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
}
@@ -342,10 +360,22 @@ class dbal_mssql_odbc extends dbal
*/
function _sql_error()
{
- return array(
- 'message' => @odbc_errormsg(),
- 'code' => @odbc_error()
- );
+ if (function_exists('odbc_errormsg'))
+ {
+ $error = array(
+ 'message' => @odbc_errormsg(),
+ 'code' => @odbc_error(),
+ );
+ }
+ else
+ {
+ $error = array(
+ 'message' => $this->connect_error,
+ 'code' => '',
+ );
+ }
+
+ return $error;
}
/**
diff --git a/phpBB/includes/db/mssqlnative.php b/phpBB/includes/db/mssqlnative.php
index 3ad0ff3e11..b91372ac61 100644
--- a/phpBB/includes/db/mssqlnative.php
+++ b/phpBB/includes/db/mssqlnative.php
@@ -199,16 +199,18 @@ class dbal_mssqlnative extends dbal
var $m_insert_id = NULL;
var $last_query_text = '';
var $query_options = array();
+ var $connect_error = '';
/**
* Connect to server
*/
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
{
- # Test for driver support, to avoid suppressed fatal error
+ // Test for driver support, to avoid suppressed fatal error
if (!function_exists('sqlsrv_connect'))
{
- trigger_error('Native MS SQL Server driver for PHP is missing or needs to be updated. Version 1.1 or later is required to install phpBB3. You can download the driver from: http://www.microsoft.com/sqlserver/2005/en/us/PHP-Driver.aspx\n', E_USER_ERROR);
+ $this->connect_error = 'Native MS SQL Server driver for PHP is missing or needs to be updated. Version 1.1 or later is required to install phpBB3. You can download the driver from: http://www.microsoft.com/sqlserver/2005/en/us/PHP-Driver.aspx';
+ return $this->sql_error('');
}
//set up connection variables
@@ -514,31 +516,43 @@ class dbal_mssqlnative extends dbal
*/
function _sql_error()
{
- $errors = @sqlsrv_errors(SQLSRV_ERR_ERRORS);
- $error_message = '';
- $code = 0;
-
- if ($errors != null)
+ if (function_exists('sqlsrv_errors'))
{
- foreach ($errors as $error)
+ $errors = @sqlsrv_errors(SQLSRV_ERR_ERRORS);
+ $error_message = '';
+ $code = 0;
+
+ if ($errors != null)
+ {
+ foreach ($errors as $error)
+ {
+ $error_message .= "SQLSTATE: " . $error[ 'SQLSTATE'] . "\n";
+ $error_message .= "code: " . $error[ 'code'] . "\n";
+ $code = $error['code'];
+ $error_message .= "message: " . $error[ 'message'] . "\n";
+ }
+ $this->last_error_result = $error_message;
+ $error = $this->last_error_result;
+ }
+ else
{
- $error_message .= "SQLSTATE: ".$error[ 'SQLSTATE']."\n";
- $error_message .= "code: ".$error[ 'code']."\n";
- $code = $error['code'];
- $error_message .= "message: ".$error[ 'message']."\n";
+ $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
}
- $this->last_error_result = $error_message;
- $error = $this->last_error_result;
+
+ $error = array(
+ 'message' => $error,
+ 'code' => $code,
+ );
}
else
{
- $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
+ $error = array(
+ 'message' => $this->connect_error,
+ 'code' => '',
+ );
}
- return array(
- 'message' => $error,
- 'code' => $code,
- );
+ return $error;
}
/**
diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php
index 1ccb785150..252cb20bd4 100644
--- a/phpBB/includes/db/mysql.php
+++ b/phpBB/includes/db/mysql.php
@@ -30,6 +30,7 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
class dbal_mysql extends dbal
{
var $multi_insert = true;
+ var $connect_error = '';
/**
* Connect to server
@@ -44,7 +45,24 @@ class dbal_mysql extends dbal
$this->sql_layer = 'mysql4';
- $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $sqlpassword) : @mysql_connect($this->server, $this->user, $sqlpassword, $new_link);
+ if ($this->persistency)
+ {
+ if (!function_exists('mysql_pconnect'))
+ {
+ $this->connect_error = 'mysql_pconnect function does not exist, is mysql extension installed?';
+ return $this->sql_error('');
+ }
+ $this->db_connect_id = @mysql_pconnect($this->server, $this->user, $sqlpassword);
+ }
+ else
+ {
+ if (!function_exists('mysql_connect'))
+ {
+ $this->connect_error = 'mysql_connect function does not exist, is mysql extension installed?';
+ return $this->sql_error('');
+ }
+ $this->db_connect_id = @mysql_connect($this->server, $this->user, $sqlpassword, $new_link);
+ }
if ($this->db_connect_id && $this->dbname != '')
{
@@ -419,18 +437,29 @@ class dbal_mysql extends dbal
*/
function _sql_error()
{
- if (!$this->db_connect_id)
+ if ($this->db_connect_id)
+ {
+ $error = array(
+ 'message' => @mysql_error($this->db_connect_id),
+ 'code' => @mysql_errno($this->db_connect_id),
+ );
+ }
+ else if (function_exists('mysql_error'))
{
- return array(
+ $error = array(
'message' => @mysql_error(),
- 'code' => @mysql_errno()
+ 'code' => @mysql_errno(),
+ );
+ }
+ else
+ {
+ $error = array(
+ 'message' => $this->connect_error,
+ 'code' => '',
);
}
- return array(
- 'message' => @mysql_error($this->db_connect_id),
- 'code' => @mysql_errno($this->db_connect_id)
- );
+ return $error;
}
/**
diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php
index a311b8cda6..69f1d26a40 100644
--- a/phpBB/includes/db/mysqli.php
+++ b/phpBB/includes/db/mysqli.php
@@ -27,12 +27,19 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
class dbal_mysqli extends dbal
{
var $multi_insert = true;
+ var $connect_error = '';
/**
* Connect to server
*/
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false , $new_link = false)
{
+ if (!function_exists('mysqli_connect'))
+ {
+ $this->connect_error = 'mysqli_connect function does not exist, is mysqli extension installed?';
+ return $this->sql_error('');
+ }
+
// Mysqli extension supports persistent connection since PHP 5.3.0
$this->persistency = (version_compare(PHP_VERSION, '5.3.0', '>=')) ? $persistency : false;
$this->user = $sqluser;
@@ -416,18 +423,29 @@ class dbal_mysqli extends dbal
*/
function _sql_error()
{
- if (!$this->db_connect_id)
+ if ($this->db_connect_id)
+ {
+ $error = array(
+ 'message' => @mysqli_error($this->db_connect_id),
+ 'code' => @mysqli_errno($this->db_connect_id)
+ );
+ }
+ else if (function_exists('mysqli_connect_error'))
{
- return array(
+ $error = array(
'message' => @mysqli_connect_error(),
- 'code' => @mysqli_connect_errno()
+ 'code' => @mysqli_connect_errno(),
+ );
+ }
+ else
+ {
+ $error = array(
+ 'message' => $this->connect_error,
+ 'code' => '',
);
}
- return array(
- 'message' => @mysqli_error($this->db_connect_id),
- 'code' => @mysqli_errno($this->db_connect_id)
- );
+ return $error;
}
/**
diff --git a/phpBB/includes/db/oracle.php b/phpBB/includes/db/oracle.php
index 62b36aa8bf..4a7a4ecc8c 100644
--- a/phpBB/includes/db/oracle.php
+++ b/phpBB/includes/db/oracle.php
@@ -25,6 +25,7 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
class dbal_oracle extends dbal
{
var $last_query_text = '';
+ var $connect_error = '';
/**
* Connect to server
@@ -48,7 +49,33 @@ class dbal_oracle extends dbal
$connect = $sqlserver . (($port) ? ':' . $port : '') . '/' . $database;
}
- $this->db_connect_id = ($new_link) ? @ocinlogon($this->user, $sqlpassword, $connect, 'UTF8') : (($this->persistency) ? @ociplogon($this->user, $sqlpassword, $connect, 'UTF8') : @ocilogon($this->user, $sqlpassword, $connect, 'UTF8'));
+ if ($new_link)
+ {
+ if (!function_exists('ocinlogon'))
+ {
+ $this->connect_error = 'ocinlogon function does not exist, is oci extension installed?';
+ return $this->sql_error('');
+ }
+ $this->db_connect_id = @ocinlogon($this->user, $sqlpassword, $connect, 'UTF8');
+ }
+ else if ($this->persistency)
+ {
+ if (!function_exists('ociplogon'))
+ {
+ $this->connect_error = 'ociplogon function does not exist, is oci extension installed?';
+ return $this->sql_error('');
+ }
+ $this->db_connect_id = @ociplogon($this->user, $sqlpassword, $connect, 'UTF8');
+ }
+ else
+ {
+ if (!function_exists('ocilogon'))
+ {
+ $this->connect_error = 'ocilogon function does not exist, is oci extension installed?';
+ return $this->sql_error('');
+ }
+ $this->db_connect_id = @ocilogon($this->user, $sqlpassword, $connect, 'UTF8');
+ }
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
}
@@ -647,17 +674,27 @@ class dbal_oracle extends dbal
*/
function _sql_error()
{
- $error = @ocierror();
- $error = (!$error) ? @ocierror($this->query_result) : $error;
- $error = (!$error) ? @ocierror($this->db_connect_id) : $error;
-
- if ($error)
+ if (function_exists('ocierror'))
{
- $this->last_error_result = $error;
+ $error = @ocierror();
+ $error = (!$error) ? @ocierror($this->query_result) : $error;
+ $error = (!$error) ? @ocierror($this->db_connect_id) : $error;
+
+ if ($error)
+ {
+ $this->last_error_result = $error;
+ }
+ else
+ {
+ $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
+ }
}
else
{
- $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
+ $error = array(
+ 'message' => $this->connect_error,
+ 'code' => '',
+ );
}
return $error;
diff --git a/phpBB/includes/db/sqlite.php b/phpBB/includes/db/sqlite.php
index 8de72fd394..557b057cce 100644
--- a/phpBB/includes/db/sqlite.php
+++ b/phpBB/includes/db/sqlite.php
@@ -25,6 +25,8 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
*/
class dbal_sqlite extends dbal
{
+ var $connect_error = '';
+
/**
* Connect to server
*/
@@ -36,7 +38,24 @@ class dbal_sqlite extends dbal
$this->dbname = $database;
$error = '';
- $this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0666, $error) : @sqlite_open($this->server, 0666, $error);
+ if ($this->persistency)
+ {
+ if (!function_exists('sqlite_popen'))
+ {
+ $this->connect_error = 'sqlite_popen function does not exist, is sqlite extension installed?';
+ return $this->sql_error('');
+ }
+ $this->db_connect_id = @sqlite_popen($this->server, 0666, $error);
+ }
+ else
+ {
+ if (!function_exists('sqlite_open'))
+ {
+ $this->connect_error = 'sqlite_open function does not exist, is sqlite extension installed?';
+ return $this->sql_error('');
+ }
+ $this->db_connect_id = @sqlite_open($this->server, 0666, $error);
+ }
if ($this->db_connect_id)
{
@@ -281,10 +300,22 @@ class dbal_sqlite extends dbal
*/
function _sql_error()
{
- return array(
- 'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
- 'code' => @sqlite_last_error($this->db_connect_id)
- );
+ if (function_exists('sqlite_error_string'))
+ {
+ $error = array(
+ 'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
+ 'code' => @sqlite_last_error($this->db_connect_id),
+ );
+ }
+ else
+ {
+ $error = array(
+ 'message' => $this->connect_error,
+ 'code' => '',
+ );
+ }
+
+ return $error;
}
/**
diff --git a/tests/dbal/connect_test.php b/tests/dbal/connect_test.php
new file mode 100644
index 0000000000..505ce28fa1
--- /dev/null
+++ b/tests/dbal/connect_test.php
@@ -0,0 +1,43 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2012 phpBB Group
+* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
+*
+*/
+
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+
+class phpbb_dbal_connect_test extends phpbb_database_test_case
+{
+ public function getDataSet()
+ {
+ return $this->createXMLDataSet(dirname(__FILE__) . '/../fixtures/empty.xml');
+ }
+
+ public function test_failing_connect()
+ {
+ global $phpbb_root_path, $phpEx;
+
+ $config = $this->get_database_config();
+
+ require_once dirname(__FILE__) . '/../../phpBB/includes/db/' . $config['dbms'] . '.php';
+ $dbal = 'dbal_' . $config['dbms'];
+ $db = new $dbal();
+
+ // Failure to connect results in a trigger_error call in dbal.
+ // phpunit converts triggered errors to exceptions.
+ // In particular there should be no fatals here.
+ try
+ {
+ $db->sql_connect($config['dbhost'], 'phpbbogus', 'phpbbogus', 'phpbbogus', $config['dbport']);
+ $this->assertFalse(true);
+ }
+ catch (Exception $e)
+ {
+ // should have a legitimate message
+ $this->assertNotEmpty($e->getMessage());
+ }
+ }
+}
diff --git a/tests/fixtures/empty.xml b/tests/fixtures/empty.xml
new file mode 100644
index 0000000000..195e30e38d
--- /dev/null
+++ b/tests/fixtures/empty.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<dataset>
+ <table name="phpbb_sessions">
+ <column>session_id</column>
+ <column>session_user_id</column>
+ <column>session_ip</column>
+ <column>session_browser</column>
+ </table>
+</dataset>