1 | <?php |
---|
2 | /* |
---|
3 | * $Id: DebugPDO.php 1052 2008-05-28 09:43:01Z hans $ |
---|
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 | * PDO connection subclass that provides some basic support for query counting and logging. |
---|
24 | * |
---|
25 | * This class is ONLY intended for development use. This class is also a work in-progress |
---|
26 | * and, as such, it should be expected that this class' API may change. We will be looking |
---|
27 | * at ways to refactor this class to provide a more pluggable system for hooking in more query |
---|
28 | * informatics. In the meantime, this class should illustrate how you can |
---|
29 | * |
---|
30 | * @author Cameron Brunner <cameron.brunner@gmail.com> |
---|
31 | * @author Hans Lellelid <hans@xmpl.org> |
---|
32 | * @author Christian Abegg <abegg.ch@gmail.com> |
---|
33 | * @since 2006-09-22 |
---|
34 | * @package propel.util |
---|
35 | */ |
---|
36 | class DebugPDO extends PropelPDO { |
---|
37 | |
---|
38 | /** |
---|
39 | * Count of queries performed |
---|
40 | * @var int |
---|
41 | */ |
---|
42 | protected $queryCount = 0; |
---|
43 | |
---|
44 | /** |
---|
45 | * The statement class to use. |
---|
46 | * |
---|
47 | * @var string |
---|
48 | */ |
---|
49 | protected $statementClass = 'DebugPDOStatement'; |
---|
50 | |
---|
51 | /** |
---|
52 | * Configured BasicLogger (or compatible) logger. |
---|
53 | * |
---|
54 | * @var BasicLogger |
---|
55 | */ |
---|
56 | protected $logger; |
---|
57 | |
---|
58 | /** |
---|
59 | * The log level to use for logging. |
---|
60 | * |
---|
61 | * @var int |
---|
62 | */ |
---|
63 | private $logLevel = Propel::LOG_DEBUG; |
---|
64 | |
---|
65 | /** |
---|
66 | * Construct a new DebugPDO connection. |
---|
67 | * |
---|
68 | * This method is overridden in order to specify a custom PDOStatement class. |
---|
69 | * |
---|
70 | * @param string $dsn Connection DSN |
---|
71 | * @param string $username (optional |
---|
72 | * @param string $password |
---|
73 | * @param array $driver_options |
---|
74 | * @throws PDOException - if there is an error during connection initialization |
---|
75 | */ |
---|
76 | public function __construct($dsn, $username = null, $password = null, $driver_options = array()) |
---|
77 | { |
---|
78 | parent::__construct($dsn, $username, $password, $driver_options); |
---|
79 | $this->configureStatementClass($suppress=true); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Configures the PDOStatement class for this connection. |
---|
84 | * @param boolean $suppressError Whether to suppress an exception if the statement class cannot be set. |
---|
85 | * @throws PropelException if the statement class cannot be set (and $suppressError is false) |
---|
86 | */ |
---|
87 | protected function configureStatementClass($suppressError = false) |
---|
88 | { |
---|
89 | // extending PDOStatement is not supported with persistent connections |
---|
90 | if (!$this->getAttribute(PDO::ATTR_PERSISTENT)) { |
---|
91 | $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array($this->getStatementClass(), array($this))); |
---|
92 | } elseif (!$suppressError) { |
---|
93 | throw new PropelException('Extending PDOStatement is not supported with persistent connections.'); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Sets the custom classname to use for PDOStatement. |
---|
99 | * |
---|
100 | * It is assumed that the specified classname has been loaded (or can be loaded |
---|
101 | * on-demand with autoload). |
---|
102 | * |
---|
103 | * @param string $classname |
---|
104 | */ |
---|
105 | public function setStatementClass($classname) |
---|
106 | { |
---|
107 | $this->statementClass = $classname; |
---|
108 | $this->configureStatementClass(); |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Gets the custom classname to use for PDOStatement. |
---|
113 | * @return string |
---|
114 | */ |
---|
115 | public function getStatementClass() |
---|
116 | { |
---|
117 | return $this->statementClass; |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * Gets the query count |
---|
122 | * @return int |
---|
123 | * @throws PropelException - if persistent connection is used (since unable to override PDOStatement in that case). |
---|
124 | */ |
---|
125 | public function getQueryCount() |
---|
126 | { |
---|
127 | // extending PDOStatement is not supported with persistent connections |
---|
128 | if ($this->getAttribute(PDO::ATTR_PERSISTENT)) { |
---|
129 | throw new PropelException('Extending PDOStatement is not supported with persistent connections. ' . |
---|
130 | 'Count would be inaccurate, because we cannot count the PDOStatment::execute() calls. ' . |
---|
131 | 'Either don\'t use persistent connections or don\'t call PropelPDO::getQueryCount()'); |
---|
132 | } |
---|
133 | return $this->queryCount; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * increments the query count |
---|
138 | * @return int |
---|
139 | */ |
---|
140 | public function incrementQueryCount() |
---|
141 | { |
---|
142 | $this->queryCount++; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Overrides PDO::prepare() to add logging. |
---|
147 | * . |
---|
148 | * @param string $sql |
---|
149 | * @param array |
---|
150 | * @return PDOStatement |
---|
151 | */ |
---|
152 | public function prepare($sql, $driver_options = array()) |
---|
153 | { |
---|
154 | $this->log('prepare: ' . $sql); |
---|
155 | return parent::prepare($sql, $driver_options); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * overridden for query counting |
---|
160 | * @return int |
---|
161 | */ |
---|
162 | public function exec($sql) |
---|
163 | { |
---|
164 | $this->log('exec: ' . $sql); |
---|
165 | $this->incrementQueryCount(); |
---|
166 | return parent::exec($sql); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Overridden for query counting and logging. |
---|
171 | * @return int |
---|
172 | */ |
---|
173 | public function query() |
---|
174 | { |
---|
175 | $args = func_get_args(); |
---|
176 | $this->log('query: ' . $args[0]); |
---|
177 | $this->incrementQueryCount(); |
---|
178 | return call_user_func_array(array($this, 'parent::query'), $args); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Sets the logging level to use for logging SQL statements. |
---|
183 | * |
---|
184 | * @param int $level |
---|
185 | */ |
---|
186 | public function setLogLevel($level) |
---|
187 | { |
---|
188 | $this->logLevel = $level; |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Sets a logger to use. |
---|
193 | * @param BasicLogger $logger A Logger with an API compatible with BasicLogger (or PEAR Log). |
---|
194 | */ |
---|
195 | public function setLogger($logger) |
---|
196 | { |
---|
197 | $this->logger = $logger; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Logs the SQL using the Propel::log() method or registered logger class. |
---|
202 | * |
---|
203 | * @param string $msg Message to log. |
---|
204 | * @param int $level (optional) Log level to use; will use setLogLevel() specified level by default. |
---|
205 | * @see setLogger() |
---|
206 | * @see |
---|
207 | */ |
---|
208 | public function log($msg, $level = null) |
---|
209 | { |
---|
210 | if ($level === null) { |
---|
211 | $level = $this->logLevel; |
---|
212 | } |
---|
213 | if ($this->logger) { |
---|
214 | $this->logger->log($msg, $level); |
---|
215 | } else { |
---|
216 | Propel::log($msg, $level); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | } |
---|