propel-util
[ class tree: propel-util ] [ index: propel-util ] [ all elements ]

Source for file PropelPDO.php

Documentation is available at PropelPDO.php

  1. <?php
  2. /*
  3.  *  $Id: PropelPDO.php 806 2007-11-15 00:44:47Z david $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information please see
  19.  * <http://propel.phpdb.org>.
  20.  */
  21.  
  22.  
  23. /**
  24.  * PDO connection subclass that provides some enhanced functionality needed by Propel.
  25.  *
  26.  * This class was designed to work around the limitation in PDO where attempting to begin
  27.  * a transaction when one has already been begun will trigger a PDOException.  Propel
  28.  * relies on the ability to create nested transactions, even if the underlying layer
  29.  * simply ignores these (because it doesn't support nested transactions).
  30.  *
  31.  * The changes that this class makes to the underlying API include the addition of the
  32.  * getNestedTransactionDepth() and isInTransaction() and the fact that beginTransaction()
  33.  * will no longer throw a PDOException (or trigger an error) if a transaction is already
  34.  * in-progress.
  35.  *
  36.  * @author     Cameron Brunner <cameron.brunner@gmail.com>
  37.  * @author     Hans Lellelid <hans@xmpl.org>
  38.  * @since      2006-09-22
  39.  * @package    propel.util
  40.  */
  41. class PropelPDO extends PDO {
  42.  
  43.     /**
  44.      * The current transaction depth.
  45.      * @var        int 
  46.      */
  47.     protected $nestedTransactionCount = 0;
  48.  
  49.     /**
  50.      * Gets the current transaction depth.
  51.      * @return     int 
  52.      */
  53.     public function getNestedTransactionCount()
  54.     {
  55.         return $this->nestedTransactionCount;
  56.     }
  57.  
  58.     /**
  59.      * Set the current transaction depth.
  60.      * @param      int $v The new depth.
  61.      */
  62.     protected function setNestedTransactionCount($v)
  63.     {
  64.         $this->nestedTransactionCount = $v;
  65.     }
  66.  
  67.     /**
  68.      * Decrements the current transaction depth by one.
  69.      */
  70.     protected function decrementNestedTransactionCount()
  71.     {
  72.         $this->nestedTransactionCount--;
  73.     }
  74.  
  75.     /**
  76.      * Increments the current transaction depth by one.
  77.      */
  78.     protected function incrementNestedTransactionCount()
  79.     {
  80.         $this->nestedTransactionCount++;
  81.     }
  82.  
  83.     /**
  84.      * Is this PDO connection currently in-transaction?
  85.      * This is equivalent to asking whether the current nested transaction count
  86.      * is greater than 0.
  87.      * @return     boolean 
  88.      */
  89.     public function isInTransaction()
  90.     {
  91.         return ($this->getNestedTransactionCount(0);
  92.     }
  93.  
  94.     /**
  95.      * Overrides PDO::beginTransaction() to prevent errors due to already-in-progress transaction.
  96.      */
  97.     public function beginTransaction()
  98.     {
  99.         $return true;
  100.         $opcount $this->getNestedTransactionCount();
  101.         if $opcount === {
  102.             $return parent::beginTransaction();
  103.         }
  104.         $this->incrementNestedTransactionCount();
  105.         return $return;
  106.     }
  107.  
  108.     /**
  109.      * Overrides PDO::commit() to only commit the transaction if we are in the outermost
  110.      * transaction nesting level.
  111.      */
  112.     public function commit()
  113.     {
  114.         $return true;
  115.         $opcount $this->getNestedTransactionCount();
  116.         if ($opcount 0{
  117.             if ($opcount === 1{
  118.                 $return parent::commit();
  119.             }
  120.             $this->decrementNestedTransactionCount();
  121.         }
  122.         return $return;
  123.     }
  124.  
  125.     /**
  126.      * Overrides PDO::rollback() to only rollback the transaction if we are in the outermost
  127.      * transaction nesting level.
  128.      */
  129.     public function rollback()
  130.     {
  131.         $return true;
  132.         $opcount $this->getNestedTransactionCount();
  133.         if ($opcount 0{
  134.             if ($opcount === 1{
  135.                 $return parent::rollback();
  136.             }
  137.             $this->decrementNestedTransactionCount();
  138.         }
  139.         return $return;
  140.     }
  141.  
  142.     /**
  143.      * Overrides PDO::prepare() to add logging.
  144.      */
  145.     public function prepare($sql$driver_options array())
  146.     {
  147.         Propel::log($sqlPropel::LOG_DEBUG);
  148.         return parent::prepare($sql$driver_options);
  149.     }
  150. }

Documentation generated on Thu, 22 Nov 2007 03:33:54 +0000 by phpDocumentor 1.4.0