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
{
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)
{
$error['message'] .= '<br />' . $row['message'];
$row = @mssql_fetch_assoc($result_id);
$error['code'] = $row['code'];
@mssql_free_result($result_id);
}
@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)
{
$row = @mssql_fetch_assoc($result_id);
if (!empty($row['message']))
{
$error['message'] .= '<br />' . $row['message'];
}
@mssql_free_result($result_id);
}
}
else
{
$error = array(
'message' => $this->connect_error,
'code' => '',
);
}
return $error;

View file

@ -31,6 +31,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
@ -67,7 +68,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('');
}
@ -349,10 +367,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;
}
/**

View file

@ -198,16 +198,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
@ -521,31 +523,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)
{
$error_message .= "SQLSTATE: ".$error[ 'SQLSTATE']."\n";
$error_message .= "code: ".$error[ 'code']."\n";
$code = $error['code'];
$error_message .= "message: ".$error[ 'message']."\n";
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;
}
$this->last_error_result = $error_message;
$error = $this->last_error_result;
else
{
$error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
}
$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;
}
/**

View file

@ -29,6 +29,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
@ -43,7 +44,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 != '')
{
@ -426,18 +444,29 @@ class dbal_mysql extends dbal
*/
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(),
'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;
}
/**

View file

@ -26,12 +26,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;
@ -423,18 +430,29 @@ class dbal_mysqli extends dbal
*/
function _sql_error()
{
if (!$this->db_connect_id)
if ($this->db_connect_id)
{
return array(
$error = array(
'message' => @mysqli_error($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()
'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;
}
/**

View file

@ -24,6 +24,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
@ -47,7 +48,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('');
}
@ -646,17 +673,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;

View file

@ -24,6 +24,8 @@ include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
*/
class dbal_sqlite extends dbal
{
var $connect_error = '';
/**
* Connect to server
*/
@ -35,7 +37,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)
{
@ -280,10 +299,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;
}
/**

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" ?>
<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>
</dataset>