Skip to content

Commit

Permalink
Shift connection logic into the drivers.
Browse files Browse the repository at this point in the history
This allows classes that only access the driver (like schema platforms)
to have access to lazy connection features. I think this also makes the
drivers better encapsulate their behavior. Before some of the
connected/connection logic was in the connection class which I think
will slowly become the datasource class, or a wrapper for the various
SQL based datasources at the very least.
  • Loading branch information
markstory committed May 5, 2013
1 parent b528048 commit 15905fd
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 79 deletions.
42 changes: 7 additions & 35 deletions lib/Cake/Database/Connection.php
Expand Up @@ -46,13 +46,6 @@ class Connection {
*/
protected $_driver;

/**
* Whether connection was established or not
*
* @var boolean
*/
protected $_connected = false;

/**
* Contains how many nested transactions have been started
*
Expand Down Expand Up @@ -103,7 +96,7 @@ public function __construct($config) {
throw new MissingDriverException(['driver' => $config['datasource']]);
}

$this->driver($config['datasource']);
$this->driver($config['datasource'], $config);
if (!$this->_driver->enabled()) {
throw new MissingExtensionException(['driver' => get_class($this->_driver)]);
}
Expand All @@ -121,9 +114,6 @@ public function __construct($config) {
* @return void
*/
public function __destruct() {
if ($this->_connected) {
$this->_driver->disconnect();
}
unset($this->_driver);
}

Expand All @@ -143,14 +133,15 @@ public function config() {
* If no params are passed it will return the current driver instance
*
* @param string|Driver $driver
* @param array|null $config Either config for a new driver or null.
* @return Driver
*/
public function driver($driver = null) {
public function driver($driver = null, $config = null) {
if ($driver === null) {
return $this->_driver;
}
if (is_string($driver)) {
$driver = new $driver;
$driver = new $driver($config);
}
return $this->_driver = $driver;
}
Expand All @@ -162,11 +153,9 @@ public function driver($driver = null) {
* @return boolean true on success or false if already connected.
*/
public function connect() {
if ($this->_connected) {
return false;
}
try {
return $this->_connected = $this->_driver->connect($this->_config);
$this->_driver->connect();
return true;
} catch(\Exception $e) {
throw new MissingConnectionException(['reason' => $e->getMessage()]);
}
Expand All @@ -179,7 +168,6 @@ public function connect() {
*/
public function disconnect() {
$this->_driver->disconnect();
$this->_connected = false;
}

/**
Expand All @@ -188,7 +176,7 @@ public function disconnect() {
* @return boolean
*/
public function isConnected() {
return $this->_connected;
return $this->_driver->isConnected();
}

/**
Expand All @@ -198,7 +186,6 @@ public function isConnected() {
* @return \Cake\Database\Statement
*/
public function prepare($sql) {
$this->connect();
$statement = $this->_driver->prepare($sql);

if ($this->_logQueries) {
Expand All @@ -218,7 +205,6 @@ public function prepare($sql) {
* @return \Cake\Database\Statement executed statement
*/
public function execute($query, array $params = [], array $types = []) {
$this->connect();
if ($params) {
$statement = $this->prepare($query);
$statement->bind($params, $types);
Expand All @@ -236,7 +222,6 @@ public function execute($query, array $params = [], array $types = []) {
* @return \Cake\Database\Statement
*/
public function query($sql) {
$this->connect();
$statement = $this->prepare($sql);
$statement->execute();
return $statement;
Expand All @@ -260,8 +245,6 @@ public function newQuery() {
* @return \Cake\Database\Statement
*/
public function insert($table, array $data, array $types = []) {
$this->connect();

$columns = array_keys($data);
return $this->newQuery()->insert($table, $columns, $types)
->values($data)
Expand All @@ -278,7 +261,6 @@ public function insert($table, array $data, array $types = []) {
* @return \Cake\Database\Statement
*/
public function update($table, array $data, array $conditions = [], $types = []) {
$this->connect();
$columns = array_keys($data);

return $this->newQuery()->update($table)
Expand All @@ -296,7 +278,6 @@ public function update($table, array $data, array $conditions = [], $types = [])
* @return \Cake\Database\Statement
*/
public function delete($table, $conditions = [], $types = []) {
$this->connect();
return $this->newQuery()->delete($table)
->where($conditions, $types)
->execute();
Expand All @@ -308,7 +289,6 @@ public function delete($table, $conditions = [], $types = []) {
* @return void
*/
public function begin() {
$this->connect();
if (!$this->_transactionStarted) {
if ($this->_logQueries) {
$this->log('BEGIN');
Expand All @@ -334,7 +314,6 @@ public function commit() {
if (!$this->_transactionStarted) {
return false;
}
$this->connect();

if ($this->_transactionLevel === 0) {
$this->_transactionStarted = false;
Expand All @@ -360,7 +339,6 @@ public function rollback() {
if (!$this->_transactionStarted) {
return false;
}
$this->connect();

$useSavePoint = $this->useSavePoints();
if ($this->_transactionLevel === 0 || !$useSavePoint) {
Expand Down Expand Up @@ -414,7 +392,6 @@ public function useSavePoints($enable = null) {
* @return void
*/
public function createSavePoint($name) {
$this->connect();
$this->execute($this->_driver->savePointSQL($name));
}

Expand All @@ -425,7 +402,6 @@ public function createSavePoint($name) {
* @return void
*/
public function releaseSavePoint($name) {
$this->connect();
$this->execute($this->_driver->releaseSavePointSQL($name));
}

Expand All @@ -436,7 +412,6 @@ public function releaseSavePoint($name) {
* @return void
*/
public function rollbackSavepoint($name) {
$this->connect();
$this->execute($this->_driver->rollbackSavePointSQL($name));
}

Expand All @@ -448,7 +423,6 @@ public function rollbackSavepoint($name) {
* @return mixed quoted value
*/
public function quote($value, $type = null) {
$this->connect();
list($value, $type) = $this->cast($value, $type);
return $this->_driver->quote($value, $type);
}
Expand All @@ -459,7 +433,6 @@ public function quote($value, $type = null) {
* @return boolean
*/
public function supportsQuoting() {
$this->connect();
return $this->_driver->supportsQuoting();
}

Expand All @@ -481,7 +454,6 @@ public function quoteIdentifier($identifier) {
* @return string|integer
*/
public function lastInsertId($table) {
$this->connect();
return $this->_driver->lastInsertId($table);
}

Expand Down
69 changes: 56 additions & 13 deletions lib/Cake/Database/Driver.php
Expand Up @@ -23,66 +23,91 @@
* Represents a database diver containing all specificities for
* a database engine including its SQL dialect
*
**/
*/
abstract class Driver {

/**
* Configuration data.
*
* @var array
*/
protected $_config;

/**
* Base configuration that is merged into the user
* supplied configuration data.
*
* @var array
*/
protected $_baseConfig = [];

/**
* Constructor
*
* @param array $config The configuration for the driver.
* @return void
*/
public function __construct($config = []) {
$config += $this->_baseConfig;
$this->_config = $config;
}

/**
* Establishes a connection to the database server
*
* @param array $config configuration to be used for creating connection
* @return boolean true con success
**/
public abstract function connect(array $config);
*/
public abstract function connect();

/**
* Disconnects from database server
*
* @return void
**/
*/
public abstract function disconnect();

/**
* Returns correct connection resource or object that is internally used
* If first argument is passed,
*
* @return void
**/
*/
public abstract function connection($connection = null);

/**
* Returns whether php is able to use this driver for connecting to database
*
* @return boolean true if it is valid to use this driver
**/
*/
public abstract function enabled();

/**
* Prepares a sql statement to be executed
*
* @param string $sql
* @return Cake\Database\Statement
**/
*/
public abstract function prepare($sql);

/**
* Starts a transaction
*
* @return boolean true on success, false otherwise
**/
*/
public abstract function beginTransaction();

/**
* Commits a transaction
*
* @return boolean true on success, false otherwise
**/
*/
public abstract function commitTransaction();

/**
* Rollsback a transaction
*
* @return boolean true on success, false otherwise
**/
*/
public abstract function rollbackTransaction();

/**
Expand All @@ -105,7 +130,7 @@ public abstract function quote($value, $type);
* Checks if the driver supports quoting
*
* @return boolean
**/
*/
public function supportsQuoting() {
return true;
}
Expand All @@ -115,9 +140,27 @@ public function supportsQuoting() {
*
* @param string $table table name or sequence to get last insert value from
* @return string|integer
**/
*/
public function lastInsertId($table = null) {
return $this->_connection->lastInsertId($table);
}

/**
* Check whether or not the driver is connected.
*
* @return boolean
*/
public function isConnected() {
return $this->_connection !== null;
}

/**
* Destructor
*
* @return void
*/
public function __destruct() {
$this->_connection = null;
}

}
8 changes: 5 additions & 3 deletions lib/Cake/Database/Driver/Mysql.php
Expand Up @@ -46,11 +46,13 @@ class Mysql extends \Cake\Database\Driver {
/**
* Establishes a connection to the database server
*
* @param array $config configuration to be used for creating connection
* @return boolean true on success
*/
public function connect(array $config) {
$config += $this->_baseConfig;
public function connect() {
if ($this->_connection) {
return true;
}
$config = $this->_config;

if ($config['timezone'] === 'UTC') {
$config['timezone'] = '+0:00';
Expand Down

0 comments on commit 15905fd

Please sign in to comment.