Skip to content
Permalink
Browse files

Extracting PDO driver functions into a trait, starting Sqlite driver

  • Loading branch information...
lorenzo committed Jul 11, 2012
1 parent ac1821a commit 3097c1a2750b3b02cc4d0efbe53101fc493f738e
@@ -38,6 +38,15 @@ abstract class Driver {
**/
public abstract function disconnect();
/**
* Returns correct connection resource or object that is internally used
* If first argument is passed, it will set internal conenction object or
* result to the value passed
*
* @return mixed connection object used internally
**/
public abstract function connection($connection = null);
/**
* Returns whether php is able to use this driver for connecting to database
*
@@ -2,11 +2,12 @@
namespace Cake\Model\Datasource\Database\Driver;
use Cake\Model\Datasource\Database\Statement;
use PDO;
class Mysql extends \Cake\Model\Datasource\Database\Driver {
use PDODriver { connect as protected _connect; }
/**
* Base configuration settings for MySQL driver
*
@@ -20,7 +21,8 @@ class Mysql extends \Cake\Model\Datasource\Database\Driver {
'database' => 'cake',
'port' => '3306',
'flags' => array(),
'encoding' => 'utf8'
'encoding' => 'utf8',
'dsn' => null
];
/**
@@ -31,35 +33,21 @@ class Mysql extends \Cake\Model\Datasource\Database\Driver {
**/
public function connect(array $config) {
$config += $this->_baseConfig;
$flags = [
$config['flags'] += [
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
] + $config['flags'];
if (empty($config['unix_socket'])) {
$dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']};charset={$config['encoding']}";
} else {
$dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
];
if (empty($config['dsn'])) {
if (empty($config['unix_socket'])) {
$config['dsn'] = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']};charset={$config['encoding']}";
} else {
$config['dsn'] = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
}
}
$this->_connection = new PDO(
$dsn,
$config['login'],
$config['password'],
$flags
);
return true;
}
/**
* Disconnects from database server
*
* @return void
**/
public function disconnect() {
$this->_connection = null;
return $this->_connect($config);
}
/**
@@ -72,61 +60,5 @@ public function enabled() {
return in_array('mysql', PDO::getAvailableDrivers());
}
/**
* Prepares a sql statement to be executed
*
* @param string $sql
* @return Cake\Model\Datasource\Database\Statement
**/
public function prepare($sql) {
$statement = $this->_connection->prepare($sql);
return new Statement($statement, $this);
}
/**
* Starts a transaction
*
* @return boolean true on success, false otherwise
**/
public function beginTransaction() {
return $this->_connection->beginTransaction();
}
/**
* Commits a transaction
*
* @return boolean true on success, false otherwise
**/
public function commitTransaction() {
return $this->_connection->commit();
}
/**
* Rollsback a transaction
*
* @return boolean true on success, false otherwise
**/
public function rollbackTransaction() {
return $this->_connection->rollback();
}
/**
* Returns a value in a safe representation to be used in a query string
*
* @return string
**/
public function quote($value, $type) {
return $this->_connection->quote($value, $type);
}
/**
* Returns last id generated for a table or sequence in database
*
* @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();
}
}
@@ -0,0 +1,107 @@
<?php
namespace Cake\Model\Datasource\Database\Driver;
use Cake\Model\Datasource\Database\Statement;
use PDO;
trait PDODriver {
/**
* Establishes a connection to the databse server
*
* @param array $config configuration to be used for creating connection
* @return boolean true on success
**/
public function connect(array $config) {
$connection = new PDO(
$config['dsn'],
$config['login'],
$config['password'],
$config['flags']
);
$this->connection($connection);
return true;
}
/**
* Returns correct connection resource or object that is internally used
* If first argument is passed, it will set internal conenction object or
* result to the value passed
*
* @return mixed connection object used internally
**/
public function connection($connection = null) {
if ($connection !== null) {
$this->_connection = $connection;
}
return $this->_connection;
}
/**
* Disconnects from database server
*
* @return void
**/
public function disconnect() {
$this->_connection = null;
}
/**
* Prepares a sql statement to be executed
*
* @param string $sql
* @return Cake\Model\Datasource\Database\Statement
**/
public function prepare($sql) {
$statement = $this->connection()->prepare($sql);
return new Statement($statement, $this);
}
/**
* Starts a transaction
*
* @return boolean true on success, false otherwise
**/
public function beginTransaction() {
return $this->connection()->beginTransaction();
}
/**
* Commits a transaction
*
* @return boolean true on success, false otherwise
**/
public function commitTransaction() {
return $this->connection()->commit();
}
/**
* Rollsback a transaction
*
* @return boolean true on success, false otherwise
**/
public function rollbackTransaction() {
return $this->connection()->rollback();
}
/**
* Returns a value in a safe representation to be used in a query string
*
* @return string
**/
public function quote($value, $type) {
return $this->connection()->quote($value, $type);
}
/**
* Returns last id generated for a table or sequence in database
*
* @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();
}
}
@@ -0,0 +1,55 @@
<?php
namespace Cake\Model\Datasource\Database\Driver;
use Cake\Model\Datasource\Database\Statement;
use PDO;
class Sqlite extends \Cake\Model\Datasource\Database\Driver {
use PDODriver { connect as protected _connect; }
/**
* Base configuration settings for Sqlite driver
*
* @var array
*/
protected $_baseConfig = [
'persistent' => false,
'database' => ':memory:',
'encoding' => 'utf8',
'flags' => array(),
'dsn' => null
];
/**
* Establishes a connection to the databse server
*
* @param array $config configuration to be used for creating connection
* @return boolean true on success
**/
public function connect(array $config) {
$config = $this->_baseConfig + array('login' => null, 'password' => null);
$config['flags'] += [
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
if (empty($config['dsn'])) {
$config['dsn'] = "sqlite:{$config['database']}";
}
return $this->_connect($config);
}
/**
* 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 function enabled() {
return in_array('sqlite', PDO::getAvailableDrivers());
}
}
@@ -77,6 +77,7 @@ public function testDisabledDriver() {
public function testWrongCredentials() {
$connection = new Connection(['database' => 'foobar'] + Configure::read('Connections.test'));
$connection->connect();
$this->skipIf($connection->driver() instanceof \Cake\Model\Datasource\Database\Driver\Sqlite);
}
/**

0 comments on commit 3097c1a

Please sign in to comment.
You can’t perform that action at this time.