diff --git a/lib/Cake/Model/Datasource/Database/Connection.php b/lib/Cake/Model/Datasource/Database/Connection.php index 1e6f994ff42..387520d8bac 100644 --- a/lib/Cake/Model/Datasource/Database/Connection.php +++ b/lib/Cake/Model/Datasource/Database/Connection.php @@ -3,7 +3,8 @@ namespace Cake\Model\Datasource\Database; use PDOException, - Exception; + Cake\Model\Datasource\Database\Exception\MissingDriverException, + Cake\Model\Datasource\Database\Exception\MissingExtensionException; /** * Represents a conection with a database server @@ -27,7 +28,7 @@ class Connection { protected $_driver; /** - * Whether connection was stablished or not + * Whether connection was established or not * * @var boolean **/ @@ -37,10 +38,20 @@ class Connection { * Constructor * * @param array $config configuration for conencting to database + * @throws \Cake\Model\Datasource\Database\Exception\MissingDriverException if driver class can not be found + * @throws \Cake\Model\Datasource\Database\Exception\MissingExtensionException if driver cannot be used * @return void **/ public function __construct($config) { + $this->_config = $config; + if (!class_exists($config['datasource'])) { + throw new MissingDriverException(array('driver' => $config['datasource'])); + } + $this->_driver = new $config['datasource']; + if (!$this->_driver->enabled()) { + throw new MissingExtensionException(array('driver' => get_class($this->_driver))); + } } /** @@ -49,7 +60,10 @@ public function __construct($config) { * @return boolean true on success or false if already connected **/ public function connect() { - + if ($this->_connected) { + return false; + } + return $this->_connected = $this->_driver->connect($this->_config); } /** @@ -58,7 +72,8 @@ public function connect() { * @return void **/ public function disconnect() { - + $this->_driver->disconnect(); + $this->_connected = false; } /** diff --git a/lib/Cake/Model/Datasource/Database/Driver.php b/lib/Cake/Model/Datasource/Database/Driver.php index 0815cbd84e3..6bd3fc66a7d 100644 --- a/lib/Cake/Model/Datasource/Database/Driver.php +++ b/lib/Cake/Model/Datasource/Database/Driver.php @@ -17,6 +17,13 @@ abstract class Driver { **/ public abstract function connect(array $config); +/** + * Disconnects from database server + * + * @return void + **/ + public abstract function disconnect(); + /** * Returns wheter php is able to use this driver for connecting to database * @@ -24,4 +31,12 @@ public abstract function connect(array $config); **/ public abstract function enabled(); +/** + * Prepares a sql statement to be executed + * + * @param string $sql + * @return Cake\Model\Datasource\Database\Statement + **/ + public abstract function prepare($sql); + } diff --git a/lib/Cake/Model/Datasource/Database/Driver/Mysql.php b/lib/Cake/Model/Datasource/Database/Driver/Mysql.php index 00d8be8c03c..2c8f373154a 100644 --- a/lib/Cake/Model/Datasource/Database/Driver/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Driver/Mysql.php @@ -2,9 +2,10 @@ namespace Cake\Model\Datasource\Database\Driver; -use PDO; +use PDO, + Cake\Model\Datasource\Database\Statement; -class Mysql extends Cake\Model\Datasource\Database\Driver { +class Mysql extends \Cake\Model\Datasource\Database\Driver { /** * Base configuration settings for MySQL driver @@ -16,7 +17,7 @@ class Mysql extends Cake\Model\Datasource\Database\Driver { 'host' => 'localhost', 'login' => 'root', 'password' => '', - 'database' => '', + 'database' => 'cake', 'port' => '3306', 'flags' => array(), 'encoding' => 'utf8' @@ -60,6 +61,15 @@ public function connect(array $config) { return true; } +/** + * Disconnects from database server + * + * @return void + **/ + public function disconnect() { + $this->_connection = null; + } + /** * Returns wheter php is able to use this driver for connecting to database * diff --git a/lib/Cake/Model/Datasource/Database/Exception/MissingDriverException.php b/lib/Cake/Model/Datasource/Database/Exception/MissingDriverException.php new file mode 100644 index 00000000000..b2cfb69bdf8 --- /dev/null +++ b/lib/Cake/Model/Datasource/Database/Exception/MissingDriverException.php @@ -0,0 +1,25 @@ + + * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests + * @package Cake.Model.Datasource.Database.Exception + * @since CakePHP(tm) v 3.0 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ +namespace Cake\Model\Datasource\Database\Exception; + + +class MissingDriverException extends \Cake\Error\Exception { + + protected $_messageTemplate = 'Database driver %s could not be found.'; + +} diff --git a/lib/Cake/Model/Datasource/Database/Exception/MissingExtensionException.php b/lib/Cake/Model/Datasource/Database/Exception/MissingExtensionException.php new file mode 100644 index 00000000000..ac799c8cdcc --- /dev/null +++ b/lib/Cake/Model/Datasource/Database/Exception/MissingExtensionException.php @@ -0,0 +1,26 @@ + + * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests + * @package Cake.Model.Datasource.Database.Exception + * @since CakePHP(tm) v 3.0 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ +namespace Cake\Model\Datasource\Database\Exception; + + +class MissingExtensionException extends \Cake\Error\Exception { + + protected $_messageTemplate = 'Database driver %s cannot be used due to a missing PHP extension or unmet dependency'; + +} +