Transaction.php 17.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?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>.
 */
21
Doctrine::autoload('Doctrine_Connection_Module');
22
/**
zYne's avatar
zYne committed
23 24
 * Doctrine_Transaction
 * Handles transaction savepoint and isolation abstraction
25 26
 *
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
zYne's avatar
zYne committed
27
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28 29 30 31 32 33 34
 * @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$
 */
lsmith's avatar
lsmith committed
35 36
class Doctrine_Transaction extends Doctrine_Connection_Module
{
37
    /**
38
     * Doctrine_Transaction is in sleep state when it has no active transactions
39
     */
40
    const STATE_SLEEP       = 0;
41 42 43
    /**
     * Doctrine_Transaction is in active state when it has one active transaction
     */
44
    const STATE_ACTIVE      = 1;
45 46 47
    /**
     * Doctrine_Transaction is in busy state when it has multiple active transactions
     */
48
    const STATE_BUSY        = 2;
49
    /**
zYne's avatar
zYne committed
50
     * @var integer $transactionLevel      the nesting level of transactions, used by transaction methods
51 52
     */
    protected $transactionLevel  = 0;
53 54 55 56 57 58 59 60 61
    /**
     * @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();
zYne's avatar
zYne committed
62 63 64
    /**
     * @var array $savepoints               an array containing all savepoints
     */
zYne's avatar
zYne committed
65
    protected $savePoints       = array();
66 67 68 69
    /**
     * @var array $_collections             an array of Doctrine_Collection objects that were affected during the Transaction
     */
    protected $_collections     = array();
zYne's avatar
zYne committed
70

zYne's avatar
zYne committed
71
    /**
72
     * addCollection
zYne's avatar
zYne committed
73 74 75 76 77
     * adds a collection in the internal array of collections
     *
     * at the end of each commit this array is looped over and
     * of every collection Doctrine then takes a snapshot in order
     * to keep the collections up to date with the database
78
     *
zYne's avatar
zYne committed
79
     * @param Doctrine_Collection $coll     a collection to be added
zYne's avatar
zYne committed
80
     * @return Doctrine_Transaction         this object
81 82 83 84
     */
    public function addCollection(Doctrine_Collection $coll)
    {
        $this->_collections[] = $coll;
zYne's avatar
zYne committed
85 86 87

        return $this;
    }
88 89 90 91 92 93 94
    /**
     * getState
     * returns the state of this connection
     *
     * @see Doctrine_Connection_Transaction::STATE_* constants
     * @return integer          the connection state
     */
lsmith's avatar
lsmith committed
95 96
    public function getState()
    {
lsmith's avatar
lsmith committed
97
        switch ($this->transactionLevel) {
98 99 100 101 102 103 104 105
            case 0:
                return Doctrine_Transaction::STATE_SLEEP;
                break;
            case 1:
                return Doctrine_Transaction::STATE_ACTIVE;
                break;
            default:
                return Doctrine_Transaction::STATE_BUSY;
106 107
        }
    }
zYne's avatar
zYne committed
108

109
    /**
zYne's avatar
zYne committed
110
     * addDelete
111
     * adds record into pending delete list
zYne's avatar
zYne committed
112 113 114
     *
     * @param Doctrine_Record $record       a record to be added
     * @return void
115
     */
lsmith's avatar
lsmith committed
116 117
    public function addDelete(Doctrine_Record $record)
    {
118 119
        $name = $record->getTable()->getComponentName();
        $this->delete[$name][] = $record;
lsmith's avatar
lsmith committed
120
    }
zYne's avatar
zYne committed
121

122 123 124 125 126
    /**
     * addInvalid
     * adds record into invalid records list
     *
     * @param Doctrine_Record $record
lsmith's avatar
lsmith committed
127
     * @return boolean        false if record already existed in invalid records list,
128 129
     *                        otherwise true
     */
lsmith's avatar
lsmith committed
130 131
    public function addInvalid(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
132
        if (in_array($record, $this->invalid)) {
133
            return false;
lsmith's avatar
lsmith committed
134
        }
135 136 137 138 139 140 141 142 143
        $this->invalid[] = $record;
        return true;
    }

    /**
     * returns the pending delete list
     *
     * @return array
     */
lsmith's avatar
lsmith committed
144 145
    public function getDeletes()
    {
146 147
        return $this->delete;
    }
zYne's avatar
zYne committed
148

149 150 151 152 153 154
    /**
     * bulkDelete
     * deletes all records from the pending delete list
     *
     * @return void
     */
lsmith's avatar
lsmith committed
155 156
    public function bulkDelete()
    {
157

lsmith's avatar
lsmith committed
158
        foreach ($this->delete as $name => $deletes) {
159 160
            $record = false;
            $ids    = array();
zYne's avatar
zYne committed
161

zYne's avatar
zYne committed
162
    	    if (is_array($deletes[count($deletes)-1]->getTable()->getIdentifier())) {
163 164 165
                if (count($deletes) > 0) {
                    $query = 'DELETE FROM '
                           . $this->conn->quoteIdentifier($deletes[0]->getTable()->getTableName())
zYne's avatar
zYne committed
166
                           . ' WHERE ';
167 168 169 170
    
                    $params = array();
                    $cond = array();
                    foreach ($deletes as $k => $record) {
171
                        $ids = $record->identifier();
172 173 174 175 176 177
                        $tmp = array();
                        foreach (array_keys($ids) as $id){
                            $tmp[] = $id . ' = ? ';
                        }
                        $params = array_merge($params, array_values($ids));
                        $cond[] = '(' . implode(' AND ', $tmp) . ')';
zYne's avatar
zYne committed
178
                    }
179
                    $query .= implode(' OR ', $cond);
zYne's avatar
zYne committed
180

181
                    $this->conn->execute($query, $params);
zYne's avatar
zYne committed
182 183 184 185 186 187
                }
    	    } else {
    		    foreach ($deletes as $k => $record) {
                    $ids[] = $record->getIncremented();
    		    }
    		    if ($record instanceof Doctrine_Record) {
188
        			$params = substr(str_repeat('?, ', count($ids)), 0, -2);
zYne's avatar
zYne committed
189 190
    
        			$query = 'DELETE FROM '
zYne's avatar
zYne committed
191
        				   . $this->conn->quoteIdentifier($record->getTable()->getTableName())
zYne's avatar
zYne committed
192 193 194 195 196 197 198
        				   . ' WHERE '
        				   . $record->getTable()->getIdentifier()
        				   . ' IN(' . $params . ')';
        
        			$this->conn->execute($query, $ids);
    		    }
    	    }
199 200 201 202

        }
        $this->delete = array();
    }
203 204 205 206 207 208
    /**
     * getTransactionLevel
     * get the current transaction nesting level
     *
     * @return integer
     */
lsmith's avatar
lsmith committed
209 210
    public function getTransactionLevel()
    {
211 212
        return $this->transactionLevel;
    }
zYne's avatar
zYne committed
213 214 215 216 217 218 219 220 221 222 223 224
    /**
     * getTransactionLevel
     * set the current transaction nesting level
     *
     * @return Doctrine_Transaction     this object
     */
    public function setTransactionLevel($level)
    {
        $this->transactionLevel = $level;

        return $this;
    }
225 226 227 228
    /**
     * beginTransaction
     * Start a transaction or set a savepoint.
     *
lsmith's avatar
lsmith committed
229
     * if trying to set a savepoint and there is no active transaction
zYne's avatar
zYne committed
230 231
     * a new transaction is being started
     *
232 233
     * Listeners: onPreTransactionBegin, onTransactionBegin
     *
234
     * @param string $savepoint                 name of a savepoint to set
235
     * @throws Doctrine_Transaction_Exception   if the transaction fails at database level     
236 237
     * @return integer                          current transaction nesting level
     */
lsmith's avatar
lsmith committed
238 239
    public function beginTransaction($savepoint = null)
    {
240
        $this->conn->connect();
zYne's avatar
zYne committed
241 242
        
        $listener = $this->conn->getAttribute(Doctrine::ATTR_LISTENER);
243

zYne's avatar
zYne committed
244
        if ( ! is_null($savepoint)) {
zYne's avatar
zYne committed
245
            $this->savePoints[] = $savepoint;
246

zYne's avatar
zYne committed
247 248 249 250 251 252 253 254 255
            $event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_CREATE);

            $listener->preSavepointCreate($event);

            if ( ! $event->skipOperation) {
                $this->createSavePoint($savepoint);
            }

            $listener->postSavepointCreate($event);
256
        } else {
lsmith's avatar
lsmith committed
257
            if ($this->transactionLevel == 0) {
258
                $event = new Doctrine_Event($this, Doctrine_Event::TX_BEGIN);
259

zYne's avatar
zYne committed
260
                $listener->preTransactionBegin($event);
261

zYne's avatar
zYne committed
262 263 264 265 266 267
                if ( ! $event->skipOperation) {
                    try {
                        $this->conn->getDbh()->beginTransaction();
                    } catch(Exception $e) {
                        throw new Doctrine_Transaction_Exception($e->getMessage());
                    }
268
                }
zYne's avatar
zYne committed
269
                $listener->postTransactionBegin($event);
270
            }
271 272 273 274 275 276 277
        }

        $level = ++$this->transactionLevel;

        return $level;
    }
    /**
zYne's avatar
zYne committed
278
     * commit
279 280
     * Commit the database changes done during a transaction that is in
     * progress or release a savepoint. This function may only be called when
lsmith's avatar
lsmith committed
281
     * auto-committing is disabled, otherwise it will fail.
282
     *
283
     * Listeners: preTransactionCommit, postTransactionCommit
284 285
     *
     * @param string $savepoint                 name of a savepoint to release
286
     * @throws Doctrine_Transaction_Exception   if the transaction fails at database level
287
     * @throws Doctrine_Validator_Exception     if the transaction fails due to record validations
zYne's avatar
zYne committed
288
     * @return boolean                          false if commit couldn't be performed, true otherwise
289
     */
lsmith's avatar
lsmith committed
290 291
    public function commit($savepoint = null)
    {
292 293 294
    	$this->conn->connect();

        if ($this->transactionLevel == 0) {
zYne's avatar
zYne committed
295
            return false;
296
        }
zYne's avatar
zYne committed
297

zYne's avatar
zYne committed
298 299
        $listener = $this->conn->getAttribute(Doctrine::ATTR_LISTENER);

300
        if ( ! is_null($savepoint)) {
zYne's avatar
zYne committed
301
            $this->transactionLevel -= $this->removeSavePoints($savepoint);
zYne's avatar
zYne committed
302

zYne's avatar
zYne committed
303 304 305 306 307 308 309 310 311
            $event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_COMMIT);

            $listener->preSavepointCommit($event);

            if ( ! $event->skipOperation) {
                $this->releaseSavePoint($savepoint);
            }

            $listener->postSavepointCommit($event);
lsmith's avatar
lsmith committed
312
        } else {
313

lsmith's avatar
lsmith committed
314
            if ($this->transactionLevel == 1) {
315
                $event = new Doctrine_Event($this, Doctrine_Event::TX_COMMIT);
316
                
zYne's avatar
zYne committed
317
                $listener->preTransactionCommit($event);
318

319 320 321
                if ( ! $event->skipOperation) {
                    try {
                        $this->bulkDelete();
322

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
                    } catch(Exception $e) {
                        $this->rollback();
    
                        throw new Doctrine_Transaction_Exception($e->getMessage());
                    }
                    if ( ! empty($this->invalid)) {
                        $this->rollback();
    
                        $tmp = $this->invalid;
                        $this->invalid = array();
    
                        throw new Doctrine_Validator_Exception($tmp);
                    }
    
                    // take snapshots of all collections used within this transaction
zYne's avatar
zYne committed
338
                    foreach ($this->_collections as $coll) {
339 340 341 342 343 344
                        $coll->takeSnapshot();
                    }
                    $this->_collections = array();
                    $this->conn->getDbh()->commit();
    
                    //$this->conn->unitOfWork->reset();
345
                }
lsmith's avatar
lsmith committed
346

zYne's avatar
zYne committed
347
                $listener->postTransactionCommit($event);
348
            }
zYne's avatar
zYne committed
349 350
            
            $this->transactionLevel--;
351
        }
lsmith's avatar
lsmith committed
352

zYne's avatar
zYne committed
353
        return true;
354
    }
zYne's avatar
zYne committed
355

356 357 358 359 360 361 362
    /**
     * 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.
     *
363
     * this method can be listened with onPreTransactionRollback and onTransactionRollback
364 365
     * eventlistener methods
     *
366 367
     * @param string $savepoint                 name of a savepoint to rollback to   
     * @throws Doctrine_Transaction_Exception   if the rollback operation fails at database level
zYne's avatar
zYne committed
368
     * @return boolean                          false if rollback couldn't be performed, true otherwise
369
     */
lsmith's avatar
lsmith committed
370 371
    public function rollback($savepoint = null)
    {
372 373 374
        $this->conn->connect();

        if ($this->transactionLevel == 0) {
zYne's avatar
zYne committed
375
            return false;
376
        }
377

zYne's avatar
zYne committed
378 379
        $listener = $this->conn->getAttribute(Doctrine::ATTR_LISTENER);

lsmith's avatar
lsmith committed
380
        if ( ! is_null($savepoint)) {
zYne's avatar
zYne committed
381
            $this->transactionLevel -= $this->removeSavePoints($savepoint);
zYne's avatar
zYne committed
382

zYne's avatar
zYne committed
383
            $event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_ROLLBACK);
384

zYne's avatar
zYne committed
385
            $listener->preSavepointRollback($event);
386
            
zYne's avatar
zYne committed
387 388 389 390 391
            if ( ! $event->skipOperation) {
                $this->rollbackSavePoint($savepoint);
            }

            $listener->postSavepointRollback($event);
392
        } else {
393
            $event = new Doctrine_Event($this, Doctrine_Event::TX_ROLLBACK);
394
    
zYne's avatar
zYne committed
395
            $listener->preTransactionRollback($event);
396 397 398
            
            if ( ! $event->skipOperation) {
                $this->deteles = array();
399

400 401 402 403 404 405
                $this->transactionLevel = 0;
                try {
                    $this->conn->getDbh()->rollback();
                } catch (Exception $e) {
                    throw new Doctrine_Transaction_Exception($e->getMessage());
                }
406
            }
407

zYne's avatar
zYne committed
408
            $listener->postTransactionRollback($event);
409
        }
lsmith's avatar
lsmith committed
410

zYne's avatar
zYne committed
411
        return true;
412
    }
zYne's avatar
zYne committed
413

414 415 416 417 418 419 420
    /**
     * releaseSavePoint
     * creates a new savepoint
     *
     * @param string $savepoint     name of a savepoint to create
     * @return void
     */
lsmith's avatar
lsmith committed
421 422
    protected function createSavePoint($savepoint)
    {
423 424
        throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
    }
zYne's avatar
zYne committed
425

426 427 428 429 430 431 432
    /**
     * releaseSavePoint
     * releases given savepoint
     *
     * @param string $savepoint     name of a savepoint to release
     * @return void
     */
lsmith's avatar
lsmith committed
433 434
    protected function releaseSavePoint($savepoint)
    {
435 436
        throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
    }
zYne's avatar
zYne committed
437

438 439 440 441 442 443 444
    /**
     * rollbackSavePoint
     * releases given savepoint
     *
     * @param string $savepoint     name of a savepoint to rollback to
     * @return void
     */
lsmith's avatar
lsmith committed
445 446
    protected function rollbackSavePoint($savepoint)
    {
447 448
        throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
    }
zYne's avatar
zYne committed
449

zYne's avatar
zYne committed
450 451 452 453 454 455
    /**
     * 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
zYne's avatar
zYne committed
456
     * @return integer              removed savepoints
zYne's avatar
zYne committed
457
     */
lsmith's avatar
lsmith committed
458 459
    private function removeSavePoints($savepoint)
    {
zYne's avatar
zYne committed
460
    	$this->savePoints = array_values($this->savePoints);
zYne's avatar
zYne committed
461

zYne's avatar
zYne committed
462 463
        $found = false;
        $i = 0;
zYne's avatar
zYne committed
464

zYne's avatar
zYne committed
465 466 467 468 469 470 471 472 473 474
        foreach ($this->savePoints as $key => $sp) {
            if ( ! $found) {
                if ($sp === $savepoint) {
                    $found = true;
                }
            }
            if ($found) {
                $i++;
                unset($this->savePoints[$key]);
            }
zYne's avatar
zYne committed
475
        }
zYne's avatar
zYne committed
476 477

        return $i;
zYne's avatar
zYne committed
478
    }
479

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    /**
     * 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)
     *
498
     * @throws Doctrine_Transaction_Exception           if the feature is not supported by the driver
499 500 501
     * @throws PDOException                             if something fails at the PDO level
     * @return void
     */
lsmith's avatar
lsmith committed
502 503
    public function setIsolation($isolation)
    {
504
        throw new Doctrine_Transaction_Exception('Transaction isolation levels not supported by this driver.');
505 506 507 508 509 510 511
    }

    /**
     * getTransactionIsolation
     *
     * fetches the current session transaction isolation level
     *
lsmith's avatar
lsmith committed
512
     * note: some drivers may support setting the transaction isolation level
513
     * but not fetching it
514 515
     *
     * @throws Doctrine_Transaction_Exception           if the feature is not supported by the driver
516 517 518
     * @throws PDOException                             if something fails at the PDO level
     * @return string                                   returns the current session transaction isolation level
     */
lsmith's avatar
lsmith committed
519 520
    public function getIsolation()
    {
521
        throw new Doctrine_Transaction_Exception('Fetching transaction isolation level not supported by this driver.');
522
    }
523
}