Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracting PDO driver functions into a trait, starting Sqlite driver
- Loading branch information
Showing
5 changed files
with
186 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
lib/Cake/Model/Datasource/Database/Driver/PDODriver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters