Skip to content

Commit

Permalink
Implemented commit and rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 4, 2012
1 parent 6872f64 commit a6f62c7
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 11 deletions.
75 changes: 67 additions & 8 deletions lib/Cake/Model/Datasource/Database/Connection.php
Expand Up @@ -42,6 +42,13 @@ class Connection {
**/
protected $_transactionLevel = 0;

/**
* Whether a transaction is active in this connection
*
* @var int
**/
protected $_transactionStarted = false;

/**
* Whether this connection can and should use savepoints for nested
* transactions
Expand Down Expand Up @@ -231,32 +238,64 @@ public function delete($table, $conditions = array(), $types = array()) {
**/
public function begin() {
$this->connect();
if (!$this->_transactionLevel) {
if (!$this->_transactionStarted) {
$this->_driver->beginTransaction();
$this->_transactionLevel = 0;
$this->_transactionStarted = true;
return;
}
$this->_transactionLevel++;
if ($this->_transactionLevel > 1 && $this->useSavePoints()) {
$this->createSavePoint($this->_transactionLevel);

if ($this->useSavePoints()) {
$this->createSavePoint(++$this->_transactionLevel);
}
}

/**
* Commits current transaction
*
* @return void
* @return boolean true on success, false otherwise
**/

public function commit() {

if (!$this->_transactionStarted) {
return false;
}
$this->connect();

if ($this->_transactionLevel === 0) {
$this->_transactionStarted = false;
return $this->_driver->commitTransaction();
}
if ($this->useSavePoints()) {
$this->releaseSavePoint($this->_transactionLevel);
}

$this->_transactionLevel--;
return true;
}

/**
* Rollsabck current transaction
* Rollsback current transaction
*
* @return void
**/
public function rollback() {
if (!$this->_transactionStarted) {
return false;
}
$this->connect();

$useSavePoint = $this->useSavePoints();
if ($this->_transactionLevel === 0 || !$useSavePoint) {
$this->_transactionLevel = 0;
$this->_transactionStarted = false;
$this->_driver->rollbackTransaction();
return true;
}

if ($useSavePoint) {
$this->rollbackSavepoint($this->_transactionLevel--);
}
return true;
}

/**
Expand Down Expand Up @@ -296,6 +335,26 @@ public function createSavePoint($name) {
$this->execute($this->_driver->savePointSQL($name));
}

/**
* Releases a save point by its name
*
* @return void
**/
public function releaseSavePoint($name) {
$this->connect();
$this->execute($this->_driver->releaseSavePointSQL($name));
}

/**
* Rollsback a save point by its name
*
* @return void
**/
public function rollbackSavepoint($name) {
$this->connect();
$this->execute($this->_driver->rollbackSavePointSQL($name));
}

/**
* Quotes value to be used safely in database query
*
Expand Down
35 changes: 32 additions & 3 deletions lib/Cake/Model/Datasource/Database/Driver.php
Expand Up @@ -46,6 +46,14 @@ public abstract function prepare($sql);
**/
public abstract function beginTransaction();

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


/**
* Returns whether this driver supports save points for nested transactions
*
Expand All @@ -56,12 +64,33 @@ public function supportsSavePoints() {
}

/**
* Returns whether this driver supports save points for nested transactions
* Returns a SQL snippet for creating a new transaction savepoint
*
* @return boolean true if save points are supported, false otherwise
* @param string save point name
* @return string
**/
public function savePointSQL($name) {
return 'SAVEPOINT LEVEL ' . $name;
return 'SAVEPOINT LEVEL' . $name;
}

/**
* Returns a SQL snippet for releasing a previously created save point
*
* @param string save point name
* @return string
**/
public function releaseSavePointSQL($name) {
return 'RELEASE SAVEPOINT LEVEL' . $name;
}

/**
* Returns a SQL snippet for rollbacking a previously created save point
*
* @param string save point name
* @return string
**/
public function rollbackSavePointSQL($name) {
return 'ROLLBACK TO SAVEPOINT LEVEL' . $name;
}

}
9 changes: 9 additions & 0 deletions lib/Cake/Model/Datasource/Database/Driver/Mysql.php
Expand Up @@ -93,4 +93,13 @@ public function beginTransaction() {
return $this->_connection->beginTransaction();
}

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

}

0 comments on commit a6f62c7

Please sign in to comment.