Transaction.php 12.9 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 23 24
/**
 *
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
zYne's avatar
zYne committed
25
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
26 27 28 29 30 31 32
 * @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
33 34
class Doctrine_Transaction extends Doctrine_Connection_Module
{
35
    /**
36
     * Doctrine_Transaction is in sleep state when it has no active transactions
37
     */
38
    const STATE_SLEEP       = 0;
39 40 41
    /**
     * Doctrine_Transaction is in active state when it has one active transaction
     */
42
    const STATE_ACTIVE      = 1;
43 44 45
    /**
     * Doctrine_Transaction is in busy state when it has multiple active transactions
     */
46
    const STATE_BUSY        = 2;
47
    /**
zYne's avatar
zYne committed
48
     * @var integer $transactionLevel      the nesting level of transactions, used by transaction methods
49 50
     */
    protected $transactionLevel  = 0;
51 52 53 54 55 56 57 58 59
    /**
     * @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
60 61 62 63
    /**
     * @var array $savepoints               an array containing all savepoints
     */
    public $savePoints       = array();
64 65 66 67 68 69 70
    /**
     * getState
     * returns the state of this connection
     *
     * @see Doctrine_Connection_Transaction::STATE_* constants
     * @return integer          the connection state
     */
lsmith's avatar
lsmith committed
71 72
    public function getState()
    {
lsmith's avatar
lsmith committed
73
        switch ($this->transactionLevel) {
74 75 76 77 78 79 80 81
            case 0:
                return Doctrine_Transaction::STATE_SLEEP;
                break;
            case 1:
                return Doctrine_Transaction::STATE_ACTIVE;
                break;
            default:
                return Doctrine_Transaction::STATE_BUSY;
82 83
        }
    }
84 85 86 87
    /**
     * adds record into pending delete list
     * @param Doctrine_Record $record
     */
lsmith's avatar
lsmith committed
88 89
    public function addDelete(Doctrine_Record $record)
    {
90 91
        $name = $record->getTable()->getComponentName();
        $this->delete[$name][] = $record;
lsmith's avatar
lsmith committed
92
    }
93 94 95 96 97
    /**
     * addInvalid
     * adds record into invalid records list
     *
     * @param Doctrine_Record $record
lsmith's avatar
lsmith committed
98
     * @return boolean        false if record already existed in invalid records list,
99 100
     *                        otherwise true
     */
lsmith's avatar
lsmith committed
101 102
    public function addInvalid(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
103
        if (in_array($record, $this->invalid)) {
104
            return false;
lsmith's avatar
lsmith committed
105
        }
106 107 108 109 110 111 112 113 114
        $this->invalid[] = $record;
        return true;
    }

    /**
     * returns the pending delete list
     *
     * @return array
     */
lsmith's avatar
lsmith committed
115 116
    public function getDeletes()
    {
117 118 119 120 121 122 123 124
        return $this->delete;
    }
    /**
     * bulkDelete
     * deletes all records from the pending delete list
     *
     * @return void
     */
lsmith's avatar
lsmith committed
125 126
    public function bulkDelete()
    {
lsmith's avatar
lsmith committed
127
        foreach ($this->delete as $name => $deletes) {
128 129
            $record = false;
            $ids    = array();
lsmith's avatar
lsmith committed
130
            foreach ($deletes as $k => $record) {
131 132 133
                $ids[] = $record->getIncremented();
                $record->assignIdentifier(false);
            }
lsmith's avatar
lsmith committed
134
            if ($record instanceof Doctrine_Record) {
135
                $params  = substr(str_repeat("?, ",count($ids)),0,-2);
lsmith's avatar
lsmith committed
136

137
                $query   = 'DELETE FROM '
lsmith's avatar
lsmith committed
138 139
                         . $record->getTable()->getTableName()
                         . ' WHERE '
140 141 142 143 144 145 146 147 148
                         . $record->getTable()->getIdentifier()
                         . ' IN(' . $params . ')';

                $this->conn->execute($query, $ids);
            }

        }
        $this->delete = array();
    }
149 150 151 152 153 154
    /**
     * getTransactionLevel
     * get the current transaction nesting level
     *
     * @return integer
     */
lsmith's avatar
lsmith committed
155 156
    public function getTransactionLevel()
    {
157 158 159 160 161 162
        return $this->transactionLevel;
    }
    /**
     * beginTransaction
     * Start a transaction or set a savepoint.
     *
lsmith's avatar
lsmith committed
163
     * if trying to set a savepoint and there is no active transaction
zYne's avatar
zYne committed
164 165
     * a new transaction is being started
     *
166 167
     * Listeners: onPreTransactionBegin, onTransactionBegin
     *
168 169 170
     * @param string $savepoint                 name of a savepoint to set
     * @return integer                          current transaction nesting level
     */
lsmith's avatar
lsmith committed
171 172
    public function beginTransaction($savepoint = null)
    {
lsmith's avatar
lsmith committed
173
        if ( ! is_null($savepoint)) {
zYne's avatar
zYne committed
174
            $this->beginTransaction();
175

zYne's avatar
zYne committed
176
            $this->savePoints[] = $savepoint;
177 178 179

            $this->createSavePoint($savepoint);
        } else {
lsmith's avatar
lsmith committed
180
            if ($this->transactionLevel == 0) {
181 182
                $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionBegin($this->conn);

zYne's avatar
zYne committed
183
                $this->conn->getDbh()->beginTransaction();
184 185 186

                $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionBegin($this->conn);
            }
187 188 189 190 191 192 193
        }

        $level = ++$this->transactionLevel;

        return $level;
    }
    /**
zYne's avatar
zYne committed
194
     * commit
195 196
     * 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
197
     * auto-committing is disabled, otherwise it will fail.
198 199
     *
     * Listeners: onPreTransactionCommit, onTransactionCommit
200 201 202 203
     *
     * @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
zYne's avatar
zYne committed
204
     * @return boolean                          false if commit couldn't be performed, true otherwise
205
     */
lsmith's avatar
lsmith committed
206 207
    public function commit($savepoint = null)
    {
lsmith's avatar
lsmith committed
208
        if ($this->transactionLevel == 0)
zYne's avatar
zYne committed
209 210
            return false;

211
        if ( ! is_null($savepoint)) {
zYne's avatar
zYne committed
212 213
            $this->transactionLevel = $this->removeSavePoints($savepoint);

zYne's avatar
zYne committed
214
            $this->releaseSavePoint($savepoint);
lsmith's avatar
lsmith committed
215 216
        } else {
            if ($this->transactionLevel == 1) {
zYne's avatar
zYne committed
217
                $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionCommit($this->conn);
218

zYne's avatar
zYne committed
219 220
                try {
                    $this->bulkDelete();
221

zYne's avatar
zYne committed
222 223
                } catch(Exception $e) {
                    $this->rollback();
224

zYne's avatar
zYne committed
225 226
                    throw new Doctrine_Connection_Transaction_Exception($e->__toString());
                }
lsmith's avatar
lsmith committed
227
                if ( ! empty($this->invalid)) {
zYne's avatar
zYne committed
228
                    $this->rollback();
lsmith's avatar
lsmith committed
229

230 231
                    $tmp = $this->invalid;
                    $this->invalid = array();
232

zYne's avatar
zYne committed
233 234
                    throw new Doctrine_Validator_Exception($tmp);
                }
235

zYne's avatar
zYne committed
236
                $this->conn->getDbh()->commit();
lsmith's avatar
lsmith committed
237

238
                //$this->conn->unitOfWork->reset();
lsmith's avatar
lsmith committed
239

zYne's avatar
zYne committed
240
                $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionCommit($this->conn);
241 242
            }
        }
lsmith's avatar
lsmith committed
243

244 245
        $this->transactionLevel--;

zYne's avatar
zYne committed
246
        return true;
247 248 249 250 251 252 253 254 255 256 257 258
    }
    /**
     * 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
zYne's avatar
zYne committed
259
     * @return boolean                          false if rollback couldn't be performed, true otherwise
260
     */
lsmith's avatar
lsmith committed
261 262
    public function rollback($savepoint = null)
    {
lsmith's avatar
lsmith committed
263
        if ($this->transactionLevel == 0)
zYne's avatar
zYne committed
264
            return false;
265 266 267

        $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionRollback($this->conn);

lsmith's avatar
lsmith committed
268
        if ( ! is_null($savepoint)) {
zYne's avatar
zYne committed
269 270
            $this->transactionLevel = $this->removeSavePoints($savepoint);

271 272
            $this->rollbackSavePoint($savepoint);
        } else {
273 274
            //$this->conn->unitOfWork->reset();
            $this->deteles = array();
275 276 277 278 279 280

            $this->transactionLevel = 0;

            $this->conn->getDbh()->rollback();
        }
        $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionRollback($this->conn);
lsmith's avatar
lsmith committed
281

zYne's avatar
zYne committed
282
        return true;
283 284 285 286 287 288 289 290
    }
    /**
     * releaseSavePoint
     * creates a new savepoint
     *
     * @param string $savepoint     name of a savepoint to create
     * @return void
     */
lsmith's avatar
lsmith committed
291 292
    protected function createSavePoint($savepoint)
    {
293 294 295 296 297 298 299 300 301
        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
     */
lsmith's avatar
lsmith committed
302 303
    protected function releaseSavePoint($savepoint)
    {
304 305 306 307 308 309 310 311 312
        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
     */
lsmith's avatar
lsmith committed
313 314
    protected function rollbackSavePoint($savepoint)
    {
315 316
        throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
    }
zYne's avatar
zYne committed
317 318 319 320 321 322 323 324
    /**
     * 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
     */
lsmith's avatar
lsmith committed
325 326
    private function removeSavePoints($savepoint)
    {
zYne's avatar
zYne committed
327 328 329 330
        $i = array_search($savepoint, $this->savePoints);

        $c = count($this->savePoints);

lsmith's avatar
lsmith committed
331
        for ($x = $i; $x < count($this->savePoints); $x++) {
zYne's avatar
zYne committed
332 333 334 335
            unset($this->savePoints[$x]);
        }
        return ($c - $i);
    }
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    /**
     * 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)
     *
354
     * @throws Doctrine_Transaction_Exception           if the feature is not supported by the driver
355 356 357
     * @throws PDOException                             if something fails at the PDO level
     * @return void
     */
lsmith's avatar
lsmith committed
358 359
    public function setIsolation($isolation)
    {
360
        throw new Doctrine_Transaction_Exception('Transaction isolation levels not supported by this driver.');
361 362 363 364 365 366 367
    }

    /**
     * getTransactionIsolation
     *
     * fetches the current session transaction isolation level
     *
lsmith's avatar
lsmith committed
368
     * note: some drivers may support setting the transaction isolation level
369
     * but not fetching it
370 371
     *
     * @throws Doctrine_Transaction_Exception           if the feature is not supported by the driver
372 373 374
     * @throws PDOException                             if something fails at the PDO level
     * @return string                                   returns the current session transaction isolation level
     */
lsmith's avatar
lsmith committed
375 376
    public function getIsolation()
    {
377
        throw new Doctrine_Transaction_Exception('Fetching transaction isolation level not supported by this driver.');
378
    }
379
}