Skip to content

Commit

Permalink
Initial imlpementation for transactions and nested transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 4, 2012
1 parent 681fd7c commit 6872f64
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
63 changes: 61 additions & 2 deletions lib/Cake/Model/Datasource/Database/Connection.php
Expand Up @@ -35,6 +35,21 @@ class Connection {
**/
protected $_connected = false;

/**
* Contains how many nested transactions have been started
*
* @var int
**/
protected $_transactionLevel = 0;

/**
* Whether this connection can and should use savepoints for nested
* transactions
*
* @var boolean
**/
protected $_useSavePoints = false;

/**
* Constructor
*
Expand Down Expand Up @@ -215,7 +230,14 @@ public function delete($table, $conditions = array(), $types = array()) {
* @return void
**/
public function begin() {

$this->connect();
if (!$this->_transactionLevel) {
$this->_driver->beginTransaction();
}
$this->_transactionLevel++;
if ($this->_transactionLevel > 1 && $this->useSavePoints()) {
$this->createSavePoint($this->_transactionLevel);
}
}

/**
Expand All @@ -225,7 +247,7 @@ public function begin() {
**/

public function commit() {

}

/**
Expand All @@ -237,6 +259,43 @@ public function rollback() {

}

/**
* Returns whether this connection is using savepoints for nested transactions
* If a boolean is passed as argument it will enable/disable the usage of savepoints
* only if driver the allows it.
*
* If you are trying to enable this feature, make sure you check the return value of this
* function to verify it was enabled successfuly
*
* ## Example:
*
* `$connection->useSavePoints(true)` Returns true if drivers supports save points, false otherwise
* `$connection->useSavePoints(false)` Disables usage of savepoints and returns false
* `$connection->useSavePoints()` Returns current status
*
* @return boolean true if enabled, false otherwise
**/
public function useSavePoints($enable = null) {
if ($enable === null) {
return $this->_useSavePoints;
}
if ($enable === false) {
return $this->_useSavePoints = false;
}

return $this->_useSavePoints = $this->_driver->supportsSavePoints();
}

/**
* Creates a new save point for nested transactions
*
* @return void
**/
public function createSavePoint($name) {
$this->connect();
$this->execute($this->_driver->savePointSQL($name));
}

/**
* Quotes value to be used safely in database query
*
Expand Down
25 changes: 25 additions & 0 deletions lib/Cake/Model/Datasource/Database/Driver.php
Expand Up @@ -39,4 +39,29 @@ public abstract function enabled();
**/
public abstract function prepare($sql);

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

/**
* Returns whether this driver supports save points for nested transactions
*
* @return boolean true if save points are supported, false otherwise
**/
public function supportsSavePoints() {
true;
}

/**
* Returns whether this driver supports save points for nested transactions
*
* @return boolean true if save points are supported, false otherwise
**/
public function savePointSQL($name) {
return 'SAVEPOINT LEVEL ' . $name;
}

}
18 changes: 10 additions & 8 deletions lib/Cake/Model/Datasource/Database/Driver/Mysql.php
Expand Up @@ -24,14 +24,7 @@ class Mysql extends \Cake\Model\Datasource\Database\Driver {
);

/**
* PDO instance associated to this connection
*
* @var PDO
**/
protected $_conenction;

/**
* Establishes a conenction to the databse server
* Establishes a connection to the databse server
*
* @param array $config configuretion to be used for creating connection
* @return boolean true on success
Expand Down Expand Up @@ -91,4 +84,13 @@ public function prepare($sql) {
return new Statement($statement, $this);
}

/**
* Starts a transaction
*
* @return boolean true on success, false otherwise
**/
public function beginTransaction() {
return $this->_connection->beginTransaction();
}

}

0 comments on commit 6872f64

Please sign in to comment.