Added exception for invalid drivers
This commit is contained in:
parent
508c9c3a4f
commit
d9a4b5fbe5
@ -13,6 +13,26 @@
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Generic exception for bad drivers
|
||||
*
|
||||
* @package Query
|
||||
* @subpackage Query
|
||||
*/
|
||||
class BadDBDriverException extends UnexpectedValueException {}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Generic exception for bad connection strings
|
||||
*
|
||||
* @package Query
|
||||
* @subpackage Query
|
||||
*/
|
||||
class BadConnectionException extends UnexpectedValueException {}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Convienience class for creating sql queries - also the class that
|
||||
* instantiates the specific db driver
|
||||
@ -186,6 +206,12 @@ class Query_Builder {
|
||||
$dsn = strtolower($dbtype).':'.$dsn;
|
||||
}
|
||||
|
||||
// Make sure the class exists
|
||||
if ( ! class_exists($dbtype))
|
||||
{
|
||||
throw new BadDBDriverException('Database driver does not exist, or is not supported');
|
||||
}
|
||||
|
||||
// Create the dsn for the database to connect to
|
||||
switch($dbtype)
|
||||
{
|
||||
@ -213,6 +239,9 @@ class Query_Builder {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// Create the database connection
|
||||
if ( ! empty($params->user))
|
||||
{
|
||||
@ -222,6 +251,11 @@ class Query_Builder {
|
||||
{
|
||||
$this->db = new $dbtype($dsn);
|
||||
}
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
throw new BadConnectionException('Connection failed, invalid arguments');
|
||||
}
|
||||
|
||||
// Set the connection name property, if applicable
|
||||
if (isset($params->name))
|
||||
|
@ -24,7 +24,7 @@ class CoreTest extends UnitTestCase {
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function __construct()
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
@ -37,7 +37,7 @@ class CoreTest extends UnitTestCase {
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function TestPHPVersion()
|
||||
public function TestPHPVersion()
|
||||
{
|
||||
$this->assertTrue(version_compare(PHP_VERSION, "5.2", "ge"));
|
||||
}
|
||||
@ -50,7 +50,7 @@ class CoreTest extends UnitTestCase {
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function TestHasPDO()
|
||||
public function TestHasPDO()
|
||||
{
|
||||
// PDO class exists
|
||||
$this->assertTrue(class_exists('PDO'));
|
||||
|
@ -425,6 +425,29 @@ abstract class QBTest extends UnitTestCase {
|
||||
$this->assertTrue(is_numeric($this->db->num_rows()));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// ! Other Tests
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Handles invalid drivers
|
||||
*/
|
||||
public function TestBadDriver()
|
||||
{
|
||||
$params = array(
|
||||
'host' => '127.0.0.1',
|
||||
'port' => '3306',
|
||||
'database' => 'test',
|
||||
'user' => 'root',
|
||||
'pass' => NULL,
|
||||
'type' => 'QGYFHGEG'
|
||||
);
|
||||
|
||||
$this->expectException('BadDBDriverException');
|
||||
|
||||
$this->db = new Query_Builder($params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// End of db_qb_test.php
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user