Skip to content
Permalink
Browse files

Implemented commit and rollback

  • Loading branch information...
lorenzo committed Jul 4, 2012
1 parent 50f8a05 commit 43ebc96bb74348303de50200a5e8bd49115702b2
@@ -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
@@ -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;
}
/**
@@ -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
*
@@ -46,6 +46,14 @@ abstract class Driver {
**/
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
*
@@ -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;
}
}
@@ -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 43ebc96

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