| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: DebugPDOStatement.php 1601 2010-03-15 13:06:11Z francois $
|
|
|
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 |
* PDOStatement that provides some enhanced functionality needed by Propel.
|
|
|
24 |
*
|
|
|
25 |
* Simply adds the ability to count the number of queries executed and log the queries/method calls.
|
|
|
26 |
*
|
|
|
27 |
* @author Oliver Schonrock <oliver@realtsp.com>
|
|
|
28 |
* @author Jarno Rantanen <jarno.rantanen@tkk.fi>
|
|
|
29 |
* @since 2007-07-12
|
|
|
30 |
* @package propel.util
|
|
|
31 |
*/
|
|
|
32 |
class DebugPDOStatement extends PDOStatement
|
|
|
33 |
{
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* The PDO connection from which this instance was created.
|
|
|
37 |
*
|
|
|
38 |
* @var DebugPDO
|
|
|
39 |
*/
|
|
|
40 |
protected $pdo;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Hashmap for resolving the PDO::PARAM_* class constants to their human-readable names.
|
|
|
44 |
*
|
|
|
45 |
* This is only used in logging the binding of variables.
|
|
|
46 |
*
|
|
|
47 |
* @see self::bindValue()
|
|
|
48 |
* @var array
|
|
|
49 |
*/
|
|
|
50 |
protected static $typeMap = array(
|
|
|
51 |
PDO::PARAM_BOOL => "PDO::PARAM_BOOL",
|
|
|
52 |
PDO::PARAM_INT => "PDO::PARAM_INT",
|
|
|
53 |
PDO::PARAM_STR => "PDO::PARAM_STR",
|
|
|
54 |
PDO::PARAM_LOB => "PDO::PARAM_LOB",
|
|
|
55 |
PDO::PARAM_NULL => "PDO::PARAM_NULL",
|
|
|
56 |
);
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* @var array The values that have been bound
|
|
|
60 |
*/
|
|
|
61 |
protected $boundValues = array();
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Construct a new statement class with reference to main DebugPDO object from
|
|
|
65 |
* which this instance was created.
|
|
|
66 |
*
|
|
|
67 |
* @param DebugPDO $pdo Reference to the parent PDO instance.
|
|
|
68 |
*/
|
|
|
69 |
protected function __construct(DebugPDO $pdo)
|
|
|
70 |
{
|
|
|
71 |
$this->pdo = $pdo;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public function getExecutedQueryString()
|
|
|
75 |
{
|
|
|
76 |
$sql = $this->queryString;
|
|
|
77 |
|
|
|
78 |
$matches = array();
|
|
|
79 |
if (preg_match_all('/(:p[0-9]+\b)/', $sql, $matches)) {
|
|
|
80 |
$size = count($matches[1]);
|
|
|
81 |
for ($i = $size-1; $i >= 0; $i--) {
|
|
|
82 |
$pos = $matches[1][$i];
|
|
|
83 |
$sql = str_replace($pos, $this->boundValues[$pos], $sql);
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return $sql;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Executes a prepared statement. Returns a boolean value indicating success.
|
|
|
92 |
*
|
|
|
93 |
* Overridden for query counting and logging.
|
|
|
94 |
*
|
|
|
95 |
* @return bool
|
|
|
96 |
*/
|
|
|
97 |
public function execute($input_parameters = null)
|
|
|
98 |
{
|
|
|
99 |
$debug = $this->pdo->getDebugSnapshot();
|
|
|
100 |
$return = parent::execute($input_parameters);
|
|
|
101 |
|
|
|
102 |
$sql = $this->getExecutedQueryString();
|
|
|
103 |
$this->pdo->log($sql, null, __METHOD__, $debug);
|
|
|
104 |
$this->pdo->setLastExecutedQuery($sql);
|
|
|
105 |
$this->pdo->incrementQueryCount();
|
|
|
106 |
|
|
|
107 |
return $return;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Binds a value to a corresponding named or question mark placeholder in the SQL statement
|
|
|
112 |
* that was use to prepare the statement. Returns a boolean value indicating success.
|
|
|
113 |
*
|
|
|
114 |
* @param int $pos Parameter identifier (for determining what to replace in the query).
|
|
|
115 |
* @param mixed $value The value to bind to the parameter.
|
|
|
116 |
* @param int $type Explicit data type for the parameter using the PDO::PARAM_* constants. Defaults to PDO::PARAM_STR.
|
|
|
117 |
* @return boolean
|
|
|
118 |
*/
|
|
|
119 |
public function bindValue($pos, $value, $type = PDO::PARAM_STR)
|
|
|
120 |
{
|
|
|
121 |
$debug = $this->pdo->getDebugSnapshot();
|
|
|
122 |
$typestr = isset(self::$typeMap[$type]) ? self::$typeMap[$type] : '(default)';
|
|
|
123 |
$return = parent::bindValue($pos, $value, $type);
|
|
|
124 |
$valuestr = $type == PDO::PARAM_LOB ? '[LOB value]' : var_export($value, true);
|
|
|
125 |
$msg = "Binding $valuestr at position $pos w/ PDO type $typestr";
|
|
|
126 |
|
|
|
127 |
$this->boundValues[$pos] = $valuestr;
|
|
|
128 |
|
|
|
129 |
$this->pdo->log($msg, null, __METHOD__, $debug);
|
|
|
130 |
|
|
|
131 |
return $return;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
}
|