1 | <?php |
||
14 | class DatabaseQuery |
||
15 | { |
||
16 | /** |
||
17 | * The MySQL query |
||
18 | * @var string |
||
19 | */ |
||
20 | private $query; |
||
21 | |||
22 | /** |
||
23 | * The first keyword of the query (e.g. SELECT) |
||
24 | * @var string |
||
25 | */ |
||
26 | private $queryType; |
||
27 | |||
28 | /** |
||
29 | * The parameters of the query |
||
30 | * @var array|null |
||
31 | */ |
||
32 | private $params; |
||
33 | |||
34 | /** |
||
35 | * The name of the profiler event |
||
36 | * @var string |
||
37 | */ |
||
38 | private $eventName; |
||
39 | |||
40 | /** |
||
41 | * The starting UNIX timestamp of the query |
||
42 | * @var float |
||
43 | */ |
||
44 | private $startTime; |
||
45 | |||
46 | /** |
||
47 | * The UNIX timestamp of the time when the query was completed |
||
48 | * @var float |
||
49 | */ |
||
50 | private $finishTime; |
||
51 | |||
52 | /** |
||
53 | * The duration of the query in microseconds |
||
54 | * @var float |
||
55 | */ |
||
56 | private $duration; |
||
57 | |||
58 | /** |
||
59 | * The results of the query |
||
60 | * @var mixed |
||
61 | */ |
||
62 | private $results; |
||
63 | |||
64 | /** |
||
65 | * @var int |
||
66 | */ |
||
67 | const MICROSECONDS_IN_SECOND = 1000000; |
||
68 | |||
69 | /** |
||
70 | * Debug a database query |
||
71 | * |
||
72 | * @param string $query The MySQL query |
||
73 | * @param array|null $params The query parameters |
||
74 | */ |
||
75 | 76 | public function __construct(&$query, &$params) |
|
76 | { |
||
77 | 76 | $this->query = $query; |
|
78 | 76 | $this->params = ($params !== false) ? array_values($params) : null; |
|
79 | |||
80 | 76 | $this->queryType = strtok($query, ' '); |
|
81 | 76 | $this->eventName = 'database.query.' . $this->queryType; |
|
82 | |||
83 | 76 | Debug::startStopwatch($this->eventName); |
|
84 | |||
85 | 76 | if (\Service::isDebug()) { |
|
86 | $this->startTime = microtime(true); |
||
87 | } |
||
88 | 76 | } |
|
89 | |||
90 | /** |
||
91 | * Mark a query as finished |
||
92 | * |
||
93 | * @param mixed $return The returned values of the query |
||
94 | * @return void |
||
95 | */ |
||
96 | 76 | public function finish(&$return) |
|
117 | |||
118 | /** |
||
119 | * Get the value the MySQL query |
||
120 | * |
||
121 | * @param bool $highlight Whether HTML should be used to highlight to the query |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getQuery($highlight = false) |
||
132 | |||
133 | /** |
||
134 | * Get the first keyword of the query (e.g. SELECT) |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function getQueryType() |
||
142 | |||
143 | /** |
||
144 | * Get the parameters of the query |
||
145 | * |
||
146 | * Alias for DatabaseQuery::getParams() |
||
147 | * |
||
148 | * @return array|null |
||
149 | */ |
||
150 | public function getParams() |
||
151 | { |
||
152 | return $this->getParameters(); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Get the parameters of the query |
||
157 | * |
||
158 | * @return array|null |
||
159 | */ |
||
160 | public function getParameters() |
||
161 | { |
||
162 | return $this->params; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Get the duration of the query in microseconds |
||
167 | * |
||
168 | * @return float |
||
169 | */ |
||
170 | public function getDuration() |
||
174 | |||
175 | /** |
||
176 | * Get the query results |
||
177 | * |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function getResults() |
||
184 | |||
185 | /** |
||
186 | * Get the query string parts |
||
187 | * |
||
188 | * @param bool $highlight Whether HTML should be used to highlight to the query |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getQueryParts($highlight = false) |
||
195 | |||
196 | /** |
||
197 | * Get the resolved query strings (where all `?`s are replaced with the |
||
198 | * actual parameter values) |
||
199 | * |
||
200 | * @param bool $highlight Whether HTML should be used to highlight to the query |
||
201 | * @return string |
||
202 | */ |
||
203 | public function getResolvedQuery($highlight = false) |
||
228 | } |
||
229 |