Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sqltrackerquery.php
1<?php
2namespace Bitrix\Main\Diag;
3
4class SqlTrackerQuery implements \ArrayAccess
5{
7 protected $sql = "";
9 protected $binds = null;
11 protected $state = "";
13 protected $node = "";
15 protected $startTime = 0.0;
17 protected $finishTime = 0.0;
19 protected $time = 0.0;
21 protected $trace = null;
23 protected $tracker;
24
29 {
30 $this->tracker = $tracker;
31 }
32
41 public function startQuery($sql, array $binds = null)
42 {
43 $this->sql = $sql;
44 $this->binds = $binds;
45 $this->startTime = Helper::getCurrentMicrotime();
46 }
47
55 public function finishQuery($skip = 3)
56 {
57 $this->finishTime = Helper::getCurrentMicrotime();
58 $this->time = $this->finishTime - $this->startTime;
59 $this->trace = $this->filterTrace(Helper::getBackTrace($this->tracker->getDepthBackTrace(), null, $skip));
60
61 $this->tracker->addTime($this->time);
62 $this->tracker->writeFileLog($this->sql, $this->time, "", 4);
63 }
64
72 public function restartQuery()
73 {
74 $this->startTime = Helper::getCurrentMicrotime();
75 }
76
84 public function refinishQuery()
85 {
86 $this->finishTime = Helper::getCurrentMicrotime();
87 $this->addTime($this->finishTime - $this->startTime);
88 }
89
95 public function getSql()
96 {
97 return $this->sql;
98 }
99
108 public function setSql($sql)
109 {
110 $this->sql = (string)$sql;
111 return $this;
112 }
113
119 public function getBinds()
120 {
121 return $this->binds;
122 }
123
132 public function setBinds(array $binds)
133 {
134 $this->binds = $binds;
135 return $this;
136 }
137
143 public function getState()
144 {
145 return $this->state;
146 }
147
156 public function setState($state)
157 {
158 $this->state = (string)$state;
159 return $this;
160 }
161
167 public function getNode()
168 {
169 return $this->node;
170 }
171
180 public function setNode($node)
181 {
182 $this->node = (string)$node;
183 return $this;
184 }
185
191 public function getTime()
192 {
193 return $this->time;
194 }
195
204 public function setTime($time)
205 {
206 $this->tracker->addTime(-$this->time);
207 $this->time = (float)$time;
208 $this->tracker->addTime($this->time);
209 return $this;
210 }
211
219 public function addTime($time)
220 {
221 $time = (float)$time;
222 $this->time += $time;
223 $this->tracker->addTime($time);
224 }
225
231 public function getTrace()
232 {
233 return $this->trace;
234 }
235
244 public function setTrace(array $trace)
245 {
246 $this->trace = $this->filterTrace($trace);
247 return $this;
248 }
249
258 public function offsetExists($offset): bool
259 {
260 switch ((string)$offset)
261 {
262 case "BX_STATE":
263 case "TIME":
264 case "QUERY":
265 case "TRACE":
266 case "NODE_ID":
267 return true;
268 default:
269 return false;
270 }
271 }
272
281 #[\ReturnTypeWillChange]
282 public function offsetGet($offset)
283 {
284 switch ($offset)
285 {
286 case "BX_STATE":
287 return $this->state;
288 case "TIME":
289 return $this->time;
290 case "QUERY":
291 return $this->sql;
292 case "TRACE":
293 return $this->trace;
294 case "NODE_ID":
295 return $this->node;
296 default:
297 return false;
298 }
299 }
300
310 public function offsetSet($offset, $value): void
311 {
312 }
313
322 public function offsetUnset($offset): void
323 {
324 }
325
333 protected function filterTrace($trace)
334 {
335 $filtered = array();
336 foreach ($trace as $i => $tr)
337 {
338 $args = array();
339 if (!empty($tr["args"]) && is_array($tr["args"]))
340 {
341 foreach ($tr["args"] as $k1 => $v1)
342 {
343 if (is_array($v1))
344 {
345 foreach ($v1 as $k2 => $v2)
346 {
347 if (is_scalar($v2))
348 {
349 $args[$k1][$k2] = $v2;
350 }
351 elseif (is_object($v2))
352 {
353 $args[$k1][$k2] = get_class($v2);
354 }
355 else
356 {
357 $args[$k1][$k2] = gettype($v2);
358 }
359 }
360 }
361 else
362 {
363 if (is_scalar($v1))
364 {
365 $args[$k1] = $v1;
366 }
367 elseif (is_object($v1))
368 {
369 $args[$k1] = get_class($v1);
370 }
371 else
372 {
373 $args[$k1] = gettype($v1);
374 }
375 }
376 }
377 }
378
379 $filtered[] = [
380 "file" => $tr["file"] ?? null,
381 "line" => $tr["line"] ?? null,
382 "class" => $tr["class"] ?? null,
383 "type" => $tr["type"] ?? null,
384 "function" => $tr["function"] ?? null,
385 "args" => $args,
386 ];
387 }
388 return $filtered;
389 }
390}
static getCurrentMicrotime()
Definition helper.php:11
static getBackTrace($limit=0, $options=null, $skip=1)
Definition helper.php:26
startQuery($sql, array $binds=null)