Merge branch 'develop-olympus' into develop

* develop-olympus:
  [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.

Conflicts:
	tests/fixtures/empty.xml
This commit is contained in:
Andreas Fischer 2012-12-08 03:08:21 +01:00
commit b7b8fefdd0
9 changed files with 301 additions and 77 deletions

View file

@ -24,11 +24,19 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
*/ */
class dbal_mssql extends dbal class dbal_mssql extends dbal
{ {
var $connect_error = '';
/** /**
* Connect to server * Connect to server
*/ */
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false) 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->persistency = $persistency;
$this->user = $sqluser; $this->user = $sqluser;
$this->dbname = $database; $this->dbname = $database;
@ -354,10 +362,12 @@ class dbal_mssql extends dbal
* @access private * @access private
*/ */
function _sql_error() function _sql_error()
{
if (function_exists('mssql_get_last_message'))
{ {
$error = array( $error = array(
'message' => @mssql_get_last_message(), 'message' => @mssql_get_last_message(),
'code' => '' 'code' => '',
); );
// Get error code number // Get error code number
@ -384,6 +394,14 @@ class dbal_mssql extends dbal
} }
@mssql_free_result($result_id); @mssql_free_result($result_id);
} }
}
else
{
$error = array(
'message' => $this->connect_error,
'code' => '',
);
}
return $error; return $error;
} }

View file

@ -31,6 +31,7 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
class dbal_mssql_odbc extends dbal class dbal_mssql_odbc extends dbal
{ {
var $last_query_text = ''; var $last_query_text = '';
var $connect_error = '';
/** /**
* Connect to server * Connect to server
@ -67,7 +68,24 @@ class dbal_mssql_odbc extends dbal
@ini_set('odbc.defaultlrl', $max_size); @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(''); return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
} }
@ -349,11 +367,23 @@ class dbal_mssql_odbc extends dbal
*/ */
function _sql_error() function _sql_error()
{ {
return array( if (function_exists('odbc_errormsg'))
{
$error = array(
'message' => @odbc_errormsg(), 'message' => @odbc_errormsg(),
'code' => @odbc_error() 'code' => @odbc_error(),
); );
} }
else
{
$error = array(
'message' => $this->connect_error,
'code' => '',
);
}
return $error;
}
/** /**
* Close sql connection * Close sql connection

View file

@ -198,16 +198,18 @@ class dbal_mssqlnative extends dbal
var $m_insert_id = NULL; var $m_insert_id = NULL;
var $last_query_text = ''; var $last_query_text = '';
var $query_options = array(); var $query_options = array();
var $connect_error = '';
/** /**
* Connect to server * Connect to server
*/ */
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false) 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')) 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 //set up connection variables
@ -520,6 +522,8 @@ class dbal_mssqlnative extends dbal
* @access private * @access private
*/ */
function _sql_error() function _sql_error()
{
if (function_exists('sqlsrv_errors'))
{ {
$errors = @sqlsrv_errors(SQLSRV_ERR_ERRORS); $errors = @sqlsrv_errors(SQLSRV_ERR_ERRORS);
$error_message = ''; $error_message = '';
@ -542,11 +546,21 @@ class dbal_mssqlnative extends dbal
$error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array(); $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
} }
return array( $error = array(
'message' => $error, 'message' => $error,
'code' => $code, 'code' => $code,
); );
} }
else
{
$error = array(
'message' => $this->connect_error,
'code' => '',
);
}
return $error;
}
/** /**
* Build db-specific query data * Build db-specific query data

View file

@ -29,6 +29,7 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
class dbal_mysql extends dbal class dbal_mysql extends dbal
{ {
var $multi_insert = true; var $multi_insert = true;
var $connect_error = '';
/** /**
* Connect to server * Connect to server
@ -43,7 +44,24 @@ class dbal_mysql extends dbal
$this->sql_layer = 'mysql4'; $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 != '') if ($this->db_connect_id && $this->dbname != '')
{ {
@ -426,18 +444,29 @@ class dbal_mysql extends dbal
*/ */
function _sql_error() function _sql_error()
{ {
if (!$this->db_connect_id) if ($this->db_connect_id)
{ {
return array( $error = array(
'message' => @mysql_error($this->db_connect_id),
'code' => @mysql_errno($this->db_connect_id),
);
}
else if (function_exists('mysql_error'))
{
$error = array(
'message' => @mysql_error(), 'message' => @mysql_error(),
'code' => @mysql_errno() 'code' => @mysql_errno(),
);
}
else
{
$error = array(
'message' => $this->connect_error,
'code' => '',
); );
} }
return array( return $error;
'message' => @mysql_error($this->db_connect_id),
'code' => @mysql_errno($this->db_connect_id)
);
} }
/** /**

View file

@ -26,12 +26,19 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
class dbal_mysqli extends dbal class dbal_mysqli extends dbal
{ {
var $multi_insert = true; var $multi_insert = true;
var $connect_error = '';
/** /**
* Connect to server * Connect to server
*/ */
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false , $new_link = false) 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 // Mysqli extension supports persistent connection since PHP 5.3.0
$this->persistency = (version_compare(PHP_VERSION, '5.3.0', '>=')) ? $persistency : false; $this->persistency = (version_compare(PHP_VERSION, '5.3.0', '>=')) ? $persistency : false;
$this->user = $sqluser; $this->user = $sqluser;
@ -423,19 +430,30 @@ class dbal_mysqli extends dbal
*/ */
function _sql_error() function _sql_error()
{ {
if (!$this->db_connect_id) if ($this->db_connect_id)
{ {
return array( $error = array(
'message' => @mysqli_connect_error(),
'code' => @mysqli_connect_errno()
);
}
return array(
'message' => @mysqli_error($this->db_connect_id), 'message' => @mysqli_error($this->db_connect_id),
'code' => @mysqli_errno($this->db_connect_id) 'code' => @mysqli_errno($this->db_connect_id)
); );
} }
else if (function_exists('mysqli_connect_error'))
{
$error = array(
'message' => @mysqli_connect_error(),
'code' => @mysqli_connect_errno(),
);
}
else
{
$error = array(
'message' => $this->connect_error,
'code' => '',
);
}
return $error;
}
/** /**
* Close sql connection * Close sql connection

View file

@ -24,6 +24,7 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
class dbal_oracle extends dbal class dbal_oracle extends dbal
{ {
var $last_query_text = ''; var $last_query_text = '';
var $connect_error = '';
/** /**
* Connect to server * Connect to server
@ -47,7 +48,33 @@ class dbal_oracle extends dbal
$connect = $sqlserver . (($port) ? ':' . $port : '') . '/' . $database; $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(''); return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
} }
@ -645,6 +672,8 @@ class dbal_oracle extends dbal
* @access private * @access private
*/ */
function _sql_error() function _sql_error()
{
if (function_exists('ocierror'))
{ {
$error = @ocierror(); $error = @ocierror();
$error = (!$error) ? @ocierror($this->query_result) : $error; $error = (!$error) ? @ocierror($this->query_result) : $error;
@ -658,6 +687,14 @@ class dbal_oracle extends dbal
{ {
$error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array(); $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
} }
}
else
{
$error = array(
'message' => $this->connect_error,
'code' => '',
);
}
return $error; return $error;
} }

View file

@ -24,6 +24,8 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
*/ */
class dbal_sqlite extends dbal class dbal_sqlite extends dbal
{ {
var $connect_error = '';
/** /**
* Connect to server * Connect to server
*/ */
@ -35,7 +37,24 @@ class dbal_sqlite extends dbal
$this->dbname = $database; $this->dbname = $database;
$error = ''; $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) if ($this->db_connect_id)
{ {
@ -280,11 +299,23 @@ class dbal_sqlite extends dbal
*/ */
function _sql_error() function _sql_error()
{ {
return array( if (function_exists('sqlite_error_string'))
{
$error = array(
'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)), 'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
'code' => @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;
}
/** /**
* Build db-specific query data * Build db-specific query data

View file

@ -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());
}
}
}

View file

@ -1,5 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<dataset> <dataset>
<table name="phpbb_posts"> <table name="phpbb_sessions">
<column>session_id</column>
<column>session_user_id</column>
<column>session_ip</column>
<column>session_browser</column>
</table> </table>
</dataset> </dataset>