|
2 | 2 |
|
3 | 3 | namespace Arris\Database; |
4 | 4 |
|
| 5 | +use ArrayAccess; |
5 | 6 | use PDO; |
6 | 7 | use Psr\Log\LoggerInterface; |
7 | 8 | use Psr\Log\NullLogger; |
8 | 9 |
|
9 | 10 | /** |
10 | 11 | * @method int|false exec(string $statement = '') |
11 | | - * |
12 | | - * PDOStatement|false _prepare($query = '', array $options = []) |
13 | | - * PDOStatement|false _query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, ...$fetch_mode_args) |
14 | | - * |
15 | 12 | * @method bool beginTransaction() |
16 | 13 | * @method bool commit() |
17 | 14 | * @method bool rollback() |
|
24 | 21 | * |
25 | 22 | * @method string errorCode() |
26 | 23 | * @method array errorInfo() |
| 24 | + * |
| 25 | + * PDOStatement|false _prepare($query = '', array $options = []) |
| 26 | + * PDOStatement|false _query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, ...$fetch_mode_args) |
27 | 27 | */ |
28 | 28 | class DBWrapper |
29 | 29 | { |
@@ -144,7 +144,16 @@ public function __call($function, $args) |
144 | 144 | $this->config->total_queries++; |
145 | 145 |
|
146 | 146 | if ($this->last_state['time'] >= $this->config->slow_query_threshold && $this->config->slow_query_threshold > 0) { |
147 | | - $this->logger->debug($function); |
| 147 | + $debug = \debug_backtrace(); |
| 148 | + $debug = $debug[1] ?? $debug[0]; |
| 149 | + $caller = \sprintf("%s%s%s", ($debug['class'] ?? ''), ($debug['type'] ?? ''), ($debug['function'] ?? '')); |
| 150 | + |
| 151 | + $this->logger->info("PDO::{$function}() slow: ", [ |
| 152 | + $this->last_state['time'], |
| 153 | + $caller, |
| 154 | + ((PHP_SAPI === "cli") ? __FILE__ : ($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])), |
| 155 | + $args |
| 156 | + ]); |
148 | 157 | } |
149 | 158 |
|
150 | 159 | return $result; |
@@ -221,24 +230,28 @@ public function getLastState():array |
221 | 230 |
|
222 | 231 | /** |
223 | 232 | * @param int $precision |
224 | | - * @return array{total_queries:int,total_time:string} |
| 233 | + * @return array{total_queries: int, total_time: string} |
225 | 234 | */ |
226 | 235 | public function getStats(int $precision = 6) |
227 | 236 | { |
228 | | - return [ |
229 | | - 'total_queries' => $this->config->total_queries, |
230 | | - 'total_time' => $this->config->formatTime($this->config->total_time, $precision) |
231 | | - ]; |
232 | | - |
233 | | - /*$object = (new class() extends stdClass { |
| 237 | + /*$object = (new class() implements ArrayAccess { |
234 | 238 | public int $total_queries; |
235 | 239 | public string $total_time; |
| 240 | +
|
| 241 | + #[ReturnTypeWillChange] public function offsetExists($offset): bool { return isset($this->$offset); } |
| 242 | + #[ReturnTypeWillChange] public function offsetGet($offset) { return $this->$offset ?? null; } |
| 243 | + #[ReturnTypeWillChange] public function offsetSet($offset, $value):void { $this->$offset = $value; } |
| 244 | + #[ReturnTypeWillChange] public function offsetUnset($offset):void { unset($this->$offset);} |
236 | 245 | }); |
237 | 246 | $object->total_queries = $this->config->total_queries; |
238 | 247 | $object->total_time = $this->config->formatTime($this->config->total_time, $precision); |
239 | 248 |
|
240 | | - return $object; |
241 | | - */ |
| 249 | + return $object;*/ |
| 250 | + |
| 251 | + return [ |
| 252 | + 'total_queries' => $this->config->total_queries, |
| 253 | + 'total_time' => $this->config->formatTime($this->config->total_time, $precision) |
| 254 | + ]; |
242 | 255 | } |
243 | 256 |
|
244 | 257 | private function updateLastState($args) |
|
0 commit comments