1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/lib/Doctrine/Transaction.php

400 lines
14 KiB
PHP
Raw Normal View History

<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
Doctrine::autoload('Doctrine_Connection_Module');
/**
2007-05-16 02:37:04 +04:00
* Doctrine_Transaction
* Handles transaction savepoint and isolation abstraction
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
2006-12-03 01:44:53 +03:00
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @package Doctrine
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
2006-12-29 17:40:47 +03:00
class Doctrine_Transaction extends Doctrine_Connection_Module
{
/**
* Doctrine_Transaction is in sleep state when it has no active transactions
*/
const STATE_SLEEP = 0;
/**
* Doctrine_Transaction is in active state when it has one active transaction
*/
const STATE_ACTIVE = 1;
/**
* Doctrine_Transaction is in busy state when it has multiple active transactions
*/
const STATE_BUSY = 2;
/**
2006-12-01 02:51:44 +03:00
* @var integer $transactionLevel the nesting level of transactions, used by transaction methods
*/
protected $transactionLevel = 0;
/**
* @var array $invalid an array containing all invalid records within this transaction
*/
protected $invalid = array();
/**
* @var array $delete two dimensional pending delete list, the records in
* this list will be deleted when transaction is committed
*/
protected $delete = array();
2006-12-01 02:51:44 +03:00
/**
* @var array $savepoints an array containing all savepoints
*/
2007-04-18 13:48:23 +04:00
protected $savePoints = array();
/**
* getState
* returns the state of this connection
*
* @see Doctrine_Connection_Transaction::STATE_* constants
* @return integer the connection state
*/
2006-12-29 17:40:47 +03:00
public function getState()
{
2006-12-29 17:01:31 +03:00
switch ($this->transactionLevel) {
case 0:
return Doctrine_Transaction::STATE_SLEEP;
break;
case 1:
return Doctrine_Transaction::STATE_ACTIVE;
break;
default:
return Doctrine_Transaction::STATE_BUSY;
}
}
/**
* adds record into pending delete list
* @param Doctrine_Record $record
*/
2006-12-29 17:40:47 +03:00
public function addDelete(Doctrine_Record $record)
{
$name = $record->getTable()->getComponentName();
$this->delete[$name][] = $record;
2006-12-29 17:01:31 +03:00
}
/**
* addInvalid
* adds record into invalid records list
*
* @param Doctrine_Record $record
2006-12-29 17:01:31 +03:00
* @return boolean false if record already existed in invalid records list,
* otherwise true
*/
2006-12-29 17:40:47 +03:00
public function addInvalid(Doctrine_Record $record)
{
2006-12-29 17:01:31 +03:00
if (in_array($record, $this->invalid)) {
return false;
2006-12-29 17:01:31 +03:00
}
$this->invalid[] = $record;
return true;
}
/**
* returns the pending delete list
*
* @return array
*/
2006-12-29 17:40:47 +03:00
public function getDeletes()
{
return $this->delete;
}
/**
* bulkDelete
* deletes all records from the pending delete list
*
* @return void
*/
2006-12-29 17:40:47 +03:00
public function bulkDelete()
{
2006-12-29 17:01:31 +03:00
foreach ($this->delete as $name => $deletes) {
$record = false;
$ids = array();
2007-04-11 22:56:44 +04:00
2007-04-11 22:44:23 +04:00
if (is_array($deletes[count($deletes)-1]->getTable()->getIdentifier())) {
2007-04-18 13:48:23 +04:00
foreach ($deletes as $k => $record) {
2007-04-11 22:56:44 +04:00
$cond = array();
2007-04-11 22:44:23 +04:00
$ids = $record->obtainIdentifier();
2007-04-18 13:48:23 +04:00
$query = 'DELETE FROM '
. $this->conn->quoteIdentifier($record->getTable()->getTableName())
. ' WHERE ';
2006-12-29 17:01:31 +03:00
2007-04-11 22:56:44 +04:00
foreach (array_keys($ids) as $id){
$cond[] = $id . ' = ? ';
2007-04-11 22:44:23 +04:00
}
2007-04-11 22:56:44 +04:00
$query = $query . implode(' AND ', $cond);
2007-04-11 22:44:23 +04:00
$this->conn->execute($query, array_values($ids));
}
} else {
foreach ($deletes as $k => $record) {
$ids[] = $record->getIncremented();
$record->assignIdentifier(false);
}
if ($record instanceof Doctrine_Record) {
2007-04-18 13:48:23 +04:00
$params = substr(str_repeat('?, ', count($ids)),0,-2);
2007-04-11 22:44:23 +04:00
$query = 'DELETE FROM '
2007-04-18 13:48:23 +04:00
. $this->conn->quoteIdentifier($record->getTable()->getTableName())
2007-04-11 22:44:23 +04:00
. ' WHERE '
. $record->getTable()->getIdentifier()
. ' IN(' . $params . ')';
$this->conn->execute($query, $ids);
}
}
}
$this->delete = array();
}
/**
* getTransactionLevel
* get the current transaction nesting level
*
* @return integer
*/
2006-12-29 17:40:47 +03:00
public function getTransactionLevel()
{
return $this->transactionLevel;
}
/**
* beginTransaction
* Start a transaction or set a savepoint.
*
2006-12-29 17:01:31 +03:00
* if trying to set a savepoint and there is no active transaction
2006-12-01 02:51:44 +03:00
* a new transaction is being started
*
* Listeners: onPreTransactionBegin, onTransactionBegin
*
* @param string $savepoint name of a savepoint to set
* @return integer current transaction nesting level
*/
2006-12-29 17:40:47 +03:00
public function beginTransaction($savepoint = null)
{
2006-12-29 17:01:31 +03:00
if ( ! is_null($savepoint)) {
2006-12-01 02:51:44 +03:00
$this->beginTransaction();
2006-12-01 02:51:44 +03:00
$this->savePoints[] = $savepoint;
$this->createSavePoint($savepoint);
} else {
2006-12-29 17:01:31 +03:00
if ($this->transactionLevel == 0) {
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionBegin($this->conn);
2006-12-01 02:51:44 +03:00
$this->conn->getDbh()->beginTransaction();
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionBegin($this->conn);
}
}
$level = ++$this->transactionLevel;
return $level;
}
/**
2006-11-13 23:31:38 +03:00
* commit
* Commit the database changes done during a transaction that is in
* progress or release a savepoint. This function may only be called when
2006-12-29 17:01:31 +03:00
* auto-committing is disabled, otherwise it will fail.
*
* Listeners: onPreTransactionCommit, onTransactionCommit
*
* @param string $savepoint name of a savepoint to release
* @throws Doctrine_Transaction_Exception if the transaction fails at PDO level
* @throws Doctrine_Validator_Exception if the transaction fails due to record validations
2006-12-01 02:51:44 +03:00
* @return boolean false if commit couldn't be performed, true otherwise
*/
2006-12-29 17:40:47 +03:00
public function commit($savepoint = null)
{
2006-12-29 17:01:31 +03:00
if ($this->transactionLevel == 0)
2006-12-01 02:51:44 +03:00
return false;
if ( ! is_null($savepoint)) {
2006-12-01 02:51:44 +03:00
$this->transactionLevel = $this->removeSavePoints($savepoint);
2006-11-13 23:31:38 +03:00
$this->releaseSavePoint($savepoint);
2006-12-29 17:01:31 +03:00
} else {
if ($this->transactionLevel == 1) {
2006-11-13 23:31:38 +03:00
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionCommit($this->conn);
2006-11-13 23:31:38 +03:00
try {
$this->bulkDelete();
2006-11-13 23:31:38 +03:00
} catch(Exception $e) {
$this->rollback();
2007-02-06 22:05:03 +03:00
throw new Doctrine_Transaction_Exception($e->__toString());
2006-11-13 23:31:38 +03:00
}
2006-12-29 17:01:31 +03:00
if ( ! empty($this->invalid)) {
2006-11-13 23:31:38 +03:00
$this->rollback();
2006-12-29 17:01:31 +03:00
$tmp = $this->invalid;
$this->invalid = array();
2006-11-13 23:31:38 +03:00
throw new Doctrine_Validator_Exception($tmp);
}
2006-11-13 23:31:38 +03:00
$this->conn->getDbh()->commit();
2006-12-29 17:01:31 +03:00
//$this->conn->unitOfWork->reset();
2006-12-29 17:01:31 +03:00
2006-11-13 23:31:38 +03:00
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionCommit($this->conn);
}
}
2006-12-29 17:01:31 +03:00
$this->transactionLevel--;
2006-12-01 02:51:44 +03:00
return true;
}
/**
* rollback
* Cancel any database changes done during a transaction or since a specific
* savepoint that is in progress. This function may only be called when
* auto-committing is disabled, otherwise it will fail. Therefore, a new
* transaction is implicitly started after canceling the pending changes.
*
* this method listens to onPreTransactionRollback and onTransactionRollback
* eventlistener methods
*
* @param string $savepoint name of a savepoint to rollback to
2006-12-01 02:51:44 +03:00
* @return boolean false if rollback couldn't be performed, true otherwise
*/
2006-12-29 17:40:47 +03:00
public function rollback($savepoint = null)
{
2006-12-29 17:01:31 +03:00
if ($this->transactionLevel == 0)
2006-12-01 02:51:44 +03:00
return false;
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionRollback($this->conn);
2006-12-29 17:01:31 +03:00
if ( ! is_null($savepoint)) {
2006-12-01 02:51:44 +03:00
$this->transactionLevel = $this->removeSavePoints($savepoint);
$this->rollbackSavePoint($savepoint);
} else {
//$this->conn->unitOfWork->reset();
$this->deteles = array();
$this->transactionLevel = 0;
$this->conn->getDbh()->rollback();
}
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionRollback($this->conn);
2006-12-29 17:01:31 +03:00
2006-12-01 02:51:44 +03:00
return true;
}
/**
* releaseSavePoint
* creates a new savepoint
*
* @param string $savepoint name of a savepoint to create
* @return void
*/
2006-12-29 17:40:47 +03:00
protected function createSavePoint($savepoint)
{
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
}
/**
* releaseSavePoint
* releases given savepoint
*
* @param string $savepoint name of a savepoint to release
* @return void
*/
2006-12-29 17:40:47 +03:00
protected function releaseSavePoint($savepoint)
{
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
}
/**
* rollbackSavePoint
* releases given savepoint
*
* @param string $savepoint name of a savepoint to rollback to
* @return void
*/
2006-12-29 17:40:47 +03:00
protected function rollbackSavePoint($savepoint)
{
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
}
2006-12-01 02:51:44 +03:00
/**
* removeSavePoints
* removes a savepoint from the internal savePoints array of this transaction object
* and all its children savepoints
*
* @param sring $savepoint name of the savepoint to remove
* @return integer the current transaction level
*/
2006-12-29 17:40:47 +03:00
private function removeSavePoints($savepoint)
{
2006-12-01 02:51:44 +03:00
$i = array_search($savepoint, $this->savePoints);
$c = count($this->savePoints);
2006-12-29 17:01:31 +03:00
for ($x = $i; $x < count($this->savePoints); $x++) {
2006-12-01 02:51:44 +03:00
unset($this->savePoints[$x]);
}
return ($c - $i);
}
/**
* setIsolation
*
* Set the transacton isolation level.
* (implemented by the connection drivers)
*
* example:
*
* <code>
* $tx->setIsolation('READ UNCOMMITTED');
* </code>
*
* @param string standard isolation level
* READ UNCOMMITTED (allows dirty reads)
* READ COMMITTED (prevents dirty reads)
* REPEATABLE READ (prevents nonrepeatable reads)
* SERIALIZABLE (prevents phantom reads)
*
* @throws Doctrine_Transaction_Exception if the feature is not supported by the driver
* @throws PDOException if something fails at the PDO level
* @return void
*/
2006-12-29 17:40:47 +03:00
public function setIsolation($isolation)
{
throw new Doctrine_Transaction_Exception('Transaction isolation levels not supported by this driver.');
}
/**
* getTransactionIsolation
*
* fetches the current session transaction isolation level
*
2006-12-29 17:01:31 +03:00
* note: some drivers may support setting the transaction isolation level
* but not fetching it
*
* @throws Doctrine_Transaction_Exception if the feature is not supported by the driver
* @throws PDOException if something fails at the PDO level
* @return string returns the current session transaction isolation level
*/
2006-12-29 17:40:47 +03:00
public function getIsolation()
{
throw new Doctrine_Transaction_Exception('Fetching transaction isolation level not supported by this driver.');
}
}