diff --git a/classes/query_builder.php b/classes/query_builder.php index 0d28e82..d9a22e4 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -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 @@ -185,6 +205,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,14 +239,22 @@ class Query_Builder { break; } - // Create the database connection - if ( ! empty($params->user)) + + try { - $this->db = new $dbtype($dsn, $params->user, $params->pass); + // Create the database connection + if ( ! empty($params->user)) + { + $this->db = new $dbtype($dsn, $params->user, $params->pass); + } + else + { + $this->db = new $dbtype($dsn); + } } - else + catch(Exception $e) { - $this->db = new $dbtype($dsn); + throw new BadConnectionException('Connection failed, invalid arguments'); } // Set the connection name property, if applicable diff --git a/tests/core/core.php b/tests/core/core.php index 557534c..f645552 100644 --- a/tests/core/core.php +++ b/tests/core/core.php @@ -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')); diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index d4b9de8..55279ce 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -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 \ No newline at end of file diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 702a596..08cb9f1 100644 Binary files a/tests/db_files/FB_TEST_DB.FDB and b/tests/db_files/FB_TEST_DB.FDB differ