-
Notifications
You must be signed in to change notification settings - Fork 2
/
PDO.php
193 lines (169 loc) · 5.68 KB
/
PDO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* @author Lorenz Weber <[email protected]>
* @copyright (c) 2013, Lorenz Weber
* @package loggedPDO
*
* The MIT License (MIT)
*
* @copyright (c) 2013, Lorenz Weber
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace LoggedPDO;
if (stream_resolve_include_path('Log.php'))
require_once 'Log.php';
else
die("Failure including Log.php\nplease install PEAR::Log or check your include_path\n");
require_once __DIR__ . DIRECTORY_SEPARATOR . 'PDOStatement.php';
/**
* Class extending the php PDO Class for logging purpose
*
* @uses Log.php
*
* simple usage:
* <code>
* require_once 'loggedPDO/PDO.php';
* $logger = Log::factory('console', '', 'PDO');
* $pdo = new \LoggedPDO\PDO($connstr, DB_USERNAME, DB_PASSWORD, null, $logger);
* </code>
*
* users of a firebug logger might alternatively like the Log_firebugJSON class
* <code>
* require_once 'loggedPDO/Log_firebugJSON.php';
* $logger = Log::factory('console', '', 'PDO');
* </code>
*
*
* {@inheritdoc}
*/
class PDO extends \PDO {
/**
* PEAR Log object that will be used for logging
* @var \Log
*/
public $log;
private $logFullTime;
private $logCount;
public static $LOG_QUERY = "query";
public static $LOG_TIME = "time";
public static $LOG_TYPE = "method";
public static $LOG_PARAMS = "parameters";
/**
* If true, parameters will be inserted into query for logging.
* If false, query and parameters will be logged separately.
* @var boolean
*/
public $log_replace_params = true;
/**
* {@inheritdoc}
*
* @param \Log $log a PEAR Log object that will be used for logging
* @throws \Exception if there is no PEAR Log object specified
*/
public function __construct($dsn, $username = null, $password = null, $options = null, \Log $log = null) {
if ($log == null) {
throw new \Exception("We need a PEAR Log object, parameter order has just been kept due to consistency, last element is NOT optional.\n"
. "Please call this class as.\n"
. "new \LoggedPDO\PDO(\$dsn, null, null, null, \$log);.\n"
. "if you have no \$username, \$password or \$options to specify.");
}
parent::__construct($dsn, $username, $password, $options);
$this->log = $log;
$this->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array('\LoggedPDO\PDOStatement', array()));
}
/**
* {@inheritdoc}
*/
public function prepare($statement, $driver_options = array()) {
$pdostatement = parent::prepare($statement, $driver_options);
$pdostatement->pdo = $this;
return $pdostatement;
}
/**
* {@inheritdoc}
*/
public function query($statement) {
$start = microtime(true);
$pdostatement = parent::query($statement);
$time = microtime(true) - $start;
$this->log($statement, round($time * 1000, 3));
$pdostatement->pdo = $this;
return $pdostatement;
}
/**
* {@inheritdoc}
*/
public function exec($statement) {
$start = microtime(true);
parent::exec($statement);
$time = microtime(true) - $start;
$this->log($statement, round($time * 1000, 3));
}
/**
* Returns the assigned PEAR logger
* @return \Log
*/
public function getLogger() {
return $this->log;
}
/**
* Log a query.
* @param type $query Logged Query
* @param type $time Time used by query
* @param type $params Params, only if $log_replace_params is true
*/
public function log($query, $time, $params = null) {
$this->logFullTime+=$time;
$this->logCount++;
$trace = debug_backtrace();
$stackdepth = 1;
$called_from = sprintf('%1$s->%2$s in %3$s on line %4$d'
, $trace[$stackdepth]['class']
, $trace[$stackdepth]['function']
, $trace[$stackdepth]['file']
, $trace[$stackdepth]['line']
);
$log = array(
self::$LOG_TIME => $time,
self::$LOG_QUERY => $query,
self::$LOG_TYPE => $called_from);
if ($this->log_replace_params == false) {
$log[self::$LOG_PARAMS] = $params;
}
$this->log->log(
array(sprintf('query took %s ms', $time), $log)
, PEAR_LOG_DEBUG);
}
/**
* Get time spent by all logged querys until now.
* @return float
*/
public function getFullTime() {
return $this->logFullTime;
}
/**
* Get count of querys executed until now.
* @return integer
*/
public function getQueryCount() {
return $this->logCount;
}
}
?>