source: pro-violet-viettel/sourcecode/api.violet.vn/www/plugins/sfPropel13Plugin/lib/vendor/propel/util/PropelPDO.php

Last change on this file was 289, checked in by dungnv, 11 years ago
File size: 6.2 KB
Line 
1<?php
2/*
3 *  $Id: PropelPDO.php 1067 2008-08-06 07:37:21Z ron $
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 the basic fixes to PDO that are required 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 * @author     Christian Abegg <abegg.ch@gmail.com>
39 * @since      2006-09-22
40 * @package    propel.util
41 */
42class PropelPDO extends PDO {
43
44        /**
45         * Attribute to use to set whether to cache prepared statements.
46         */
47        const PROPEL_ATTR_CACHE_PREPARES = -1;
48
49        /**
50         * The current transaction depth.
51         * @var        int
52         */
53        protected $nestedTransactionCount = 0;
54
55        /**
56         * Cache of prepared statements (PDOStatement) keyed by md5 of SQL.
57         *
58         * @var        array [md5(sql) => PDOStatement]
59         */
60        protected $preparedStatements = array();
61
62        /**
63         * Whether to cache prepared statements.
64         *
65         * @var        boolean
66         */
67        protected $cachePreparedStatements = false;
68
69        /**
70         * Gets the current transaction depth.
71         * @return     int
72         */
73        public function getNestedTransactionCount()
74        {
75                return $this->nestedTransactionCount;
76        }
77
78        /**
79         * Set the current transaction depth.
80         * @param      int $v The new depth.
81         */
82        protected function setNestedTransactionCount($v)
83        {
84                $this->nestedTransactionCount = $v;
85        }
86
87        /**
88         * Decrements the current transaction depth by one.
89         */
90        protected function decrementNestedTransactionCount()
91        {
92                $this->nestedTransactionCount--;
93        }
94
95        /**
96         * Increments the current transaction depth by one.
97         */
98        protected function incrementNestedTransactionCount()
99        {
100                $this->nestedTransactionCount++;
101        }
102
103        /**
104         * Is this PDO connection currently in-transaction?
105         * This is equivalent to asking whether the current nested transaction count
106         * is greater than 0.
107         * @return     boolean
108         */
109        public function isInTransaction()
110        {
111                return ($this->getNestedTransactionCount() > 0);
112        }
113
114        /**
115         * Overrides PDO::beginTransaction() to prevent errors due to already-in-progress transaction.
116         */
117        public function beginTransaction()
118        {
119                $return = true;
120                $opcount = $this->getNestedTransactionCount();
121                if ( $opcount === 0 ) {
122                        $return = parent::beginTransaction();
123                }
124                $this->incrementNestedTransactionCount();
125                return $return;
126        }
127
128        /**
129         * Overrides PDO::commit() to only commit the transaction if we are in the outermost
130         * transaction nesting level.
131         */
132        public function commit()
133        {
134                $return = true;
135                $opcount = $this->getNestedTransactionCount();
136                if ($opcount > 0) {
137                        if ($opcount === 1) {
138                                $return = parent::commit();
139                        }
140                        $this->decrementNestedTransactionCount();
141                }
142                return $return;
143        }
144
145        /**
146         * Overrides PDO::rollBack() to only rollback the transaction if we are in the outermost
147         * transaction nesting level.
148         */
149        public function rollBack()
150        {
151                $return = true;
152                $opcount = $this->getNestedTransactionCount();
153                if ($opcount > 0) {
154                        if ($opcount === 1) {
155                                $return = parent::rollBack();
156                        }
157                        $this->decrementNestedTransactionCount();
158                }
159                return $return;
160        }
161
162        /**
163         * Sets a connection attribute.
164         *
165         * This is overridden here to provide support for setting Propel-specific attributes
166         * too.
167         *
168         * @param      int $attribute The attribute to set (e.g. PropelPDO::PROPEL_ATTR_CACHE_PREPARES).
169         * @param      mixed $value The attribute value.
170         */
171        public function setAttribute($attribute, $value)
172        {
173                switch($attribute) {
174                        case self::PROPEL_ATTR_CACHE_PREPARES:
175                                $this->cachePreparedStatements = $value;
176                                break;
177                        default:
178                                parent::setAttribute($attribute, $value);
179                }
180        }
181
182        /**
183         * Gets a connection attribute.
184         *
185         * This is overridden here to provide support for setting Propel-specific attributes
186         * too.
187         *
188         * @param      int $attribute The attribute to get (e.g. PropelPDO::PROPEL_ATTR_CACHE_PREPARES).
189         */
190        public function getAttribute($attribute)
191        {
192                switch($attribute) {
193                        case self::PROPEL_ATTR_CACHE_PREPARES:
194                                return $this->cachePreparedStatements;
195                                break;
196                        default:
197                                return parent::getAttribute($attribute);
198                }
199        }
200
201        /**
202         * Overrides PDO::prepare() to add query caching support if the
203         * PropelPDO::PROPEL_ATTR_CACHE_PREPARES was set to true.
204         * .
205         * @param      string $sql
206         * @param      array
207         * @return     PDOStatement
208         */
209        public function prepare($sql, $driver_options = array())
210        {
211                if ($this->cachePreparedStatements) {
212                        $key = $sql;
213                        if (!isset($this->preparedStatements[$key])) {
214                                $stmt = parent::prepare($sql, $driver_options);
215                                $this->preparedStatements[$key] = $stmt;
216                                return $stmt;
217                        } else {
218                                return $this->preparedStatements[$key];
219                        }
220                } else {
221                        return parent::prepare($sql, $driver_options);
222                }
223        }
224
225        /**
226         * Clears any stored prepared statements for this connection.
227         */
228        public function clearStatementCache()
229        {
230                $this->preparedStatements = array();
231        }
232
233}
Note: See TracBrowser for help on using the repository browser.