/
QueryAnalyzer.php
242 lines (223 loc) · 5.9 KB
/
QueryAnalyzer.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace Test\Tool;
use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @package Gedmo.Tool.Logging.DBAL
* @subpackage QueryAnalyzer
* @link http://www.gediminasm.org
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class QueryAnalyzer implements SQLLogger
{
/**
* Used database platform
*
* @var AbstractPlatform
*/
protected $platform;
/**
* Start time of currently executed query
*
* @var integer
*/
private $queryStartTime = null;
/**
* Total execution time of all queries
*
* @var integer
*/
private $totalExecutionTime = 0;
/**
* List of queries executed
*
* @var array
*/
private $queries = array();
/**
* Query execution times indexed
* in same order as queries
*
* @var array
*/
private $queryExecutionTimes = array();
/**
* Initialize log listener with database
* platform, which is needed for parameter
* conversion
*
* @param AbstractPlatform $platform
*/
public function __construct(AbstractPlatform $platform)
{
$this->platform = $platform;
}
/**
* {@inheritdoc}
*/
public function startQuery($sql, array $params = null, array $types = null)
{
$this->queryStartTime = microtime(true);
$this->queries[] = $this->generateSql($sql, $params, $types);
}
/**
* {@inheritdoc}
*/
public function stopQuery()
{
$ms = round(microtime(true) - $this->queryStartTime, 4) * 1000;
$this->queryExecutionTimes[] = $ms;
$this->totalExecutionTime += $ms;
}
/**
* Clean all collected data
*
* @return QueryAnalyzer
*/
public function cleanUp()
{
$this->queries = array();
$this->queryExecutionTimes = array();
$this->totalExecutionTime = 0;
return $this;
}
/**
* Dump the statistics of executed queries
*
* @param boolean $dumpOnlySql
* @return void
*/
public function getOutput($dumpOnlySql = false)
{
$output = '';
if (!$dumpOnlySql) {
$output .= 'Platform: ' . $this->platform->getName() . PHP_EOL;
$output .= 'Executed queries: ' . count($this->queries) . ', total time: ' . $this->totalExecutionTime . ' ms' . PHP_EOL;
}
foreach ($this->queries as $index => $sql) {
if (!$dumpOnlySql) {
$output .= 'Query(' . ($index+1) . ') - ' . $this->queryExecutionTimes[$index] . ' ms' . PHP_EOL;
}
$output .= $sql . ';' . PHP_EOL;
}
$output .= PHP_EOL;
return $output;
}
/**
* Index of the slowest query executed
*
* @return integer
*/
public function getSlowestQueryIndex()
{
$index = 0;
$slowest = 0;
foreach ($this->queryExecutionTimes as $i => $time) {
if ($time > $slowest) {
$slowest = $time;
$index = $i;
}
}
return $index;
}
/**
* Get total execution time of queries
*
* @return integer
*/
public function getTotalExecutionTime()
{
return $this->totalExecutionTime;
}
/**
* Get all queries
*
* @return array
*/
public function getExecutedQueries()
{
return $this->queries;
}
/**
* Get number of executed queries
*
* @return integer
*/
public function getNumExecutedQueries()
{
return count($this->queries);
}
/**
* Get all query execution times
*
* @return array
*/
public function getExecutionTimes()
{
return $this->queryExecutionTimes;
}
/**
* Create the SQL with mapped parameters
*
* @param string $sql
* @param array $params
* @param array $types
* @return sql
*/
private function generateSql($sql, $params, $types)
{
if (!count($params)) {
return $sql;
}
$converted = $this->getConvertedParams($params, $types);
if (is_int(key($params))) {
$index = key($converted);
$sql = preg_replace_callback('@\?@sm', function($match) use (&$index, $converted) {
return implode(' ', $converted[$index++]);
}, $sql);
} else {
foreach ($converted as $key => $value) {
$sql = str_replace(':' . $key, $value, $sql);
}
}
return $sql;
}
/**
* Get the converted parameter list
*
* @param array $params
* @param array $types
* @return array
*/
private function getConvertedParams($params, $types)
{
$result = array();
foreach ($params as $position => $value) {
if (isset($types[$position])) {
$type = $types[$position];
if (is_string($type)) {
$type = Type::getType($type);
}
if ($type instanceof Type) {
$value = $type->convertToDatabaseValue($value, $this->platform);
}
} else {
if (is_object($value) && $value instanceof \DateTime) {
$value = $value->format($this->platform->getDateTimeFormatString());
} elseif (!is_null($value)) {
$type = Type::getType(gettype($value));
$value = $type->convertToDatabaseValue($value, $this->platform);
}
}
if (is_string($value)) {
$value = "'{$value}'";
} elseif (is_null($value)) {
$value = 'NULL';
}
$result[$position] = $value;
}
return $result;
}
}