|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Morris Jobke <[email protected]> |
|
6
|
|
|
* @author Robin Appelman <[email protected]> |
|
7
|
|
|
* @author Thomas Müller <[email protected]> |
|
8
|
|
|
* @author Piotr Mrowczynski <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @license AGPL-3.0 |
|
11
|
|
|
* |
|
12
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
14
|
|
|
* as published by the Free Software Foundation. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU Affero General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace OC\Diagnostics; |
|
27
|
|
|
|
|
28
|
|
|
use OC\Cache\CappedMemoryCache; |
|
29
|
|
|
use OCP\Diagnostics\IQueryLogger; |
|
30
|
|
|
|
|
31
|
|
|
class QueryLogger implements IQueryLogger { |
|
32
|
|
|
/** |
|
33
|
|
|
* @var \OC\Diagnostics\Query |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $activeQuery; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var \OC\Diagnostics\Query[] |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $queries; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* QueryLogger constructor. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct() { |
|
46
|
|
|
$this->queries = new CappedMemoryCache(1024); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool - Module needs to be activated by some app |
|
52
|
|
|
*/ |
|
53
|
|
|
private $activated = false; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function startQuery($sql, array $params = null, array $types = null) { |
|
59
|
|
|
if ($this->activated) { |
|
60
|
|
|
$this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack()); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function getStack() { |
|
65
|
|
|
$stack = debug_backtrace(); |
|
66
|
|
|
array_shift($stack); |
|
67
|
|
|
array_shift($stack); |
|
68
|
|
|
array_shift($stack); |
|
69
|
|
|
return $stack; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @inheritdoc |
|
74
|
|
|
*/ |
|
75
|
|
|
public function stopQuery() { |
|
76
|
|
|
if ($this->activated && $this->activeQuery) { |
|
77
|
|
|
$this->activeQuery->end(microtime(true)); |
|
78
|
|
|
$this->queries[] = $this->activeQuery; |
|
79
|
|
|
$this->activeQuery = null; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritdoc |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getQueries() { |
|
87
|
|
|
return $this->queries->getData(); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritdoc |
|
92
|
|
|
*/ |
|
93
|
|
|
public function activate() { |
|
94
|
|
|
$this->activated = true; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..