1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\application\database; |
4
|
|
|
|
5
|
|
|
use suda\framework\Debugger; |
6
|
|
|
use suda\database\statement\Statement; |
7
|
|
|
use suda\database\connection\Connection; |
8
|
|
|
use suda\database\statement\QueryAccess as StatementQueryAccess; |
9
|
|
|
use suda\database\connection\observer\Observer; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class DebugObserver |
13
|
|
|
* @package suda\application\database |
14
|
|
|
*/ |
15
|
|
|
class DebugObserver implements Observer |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
private $count; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 调试记录 |
24
|
|
|
* |
25
|
|
|
* @var Debugger |
26
|
|
|
*/ |
27
|
|
|
protected $debug; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* DebugObserver constructor. |
31
|
|
|
* @param Debugger $debug |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Debugger $debug) |
34
|
|
|
{ |
35
|
|
|
$this->debug = $debug; |
36
|
|
|
$this->count = 0; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param StatementQueryAccess $access |
41
|
|
|
* @param Connection $connection |
42
|
|
|
* @param Statement $statement |
43
|
|
|
* @param float $timeSpend |
44
|
|
|
* @param bool $result |
45
|
|
|
*/ |
46
|
|
|
public function observe(StatementQueryAccess $access, Connection $connection, Statement $statement, float $timeSpend, bool $result) |
47
|
|
|
{ |
48
|
|
|
$this->count++; |
49
|
|
|
$query = $connection->prefix($statement->getString()); |
50
|
|
|
$this->debug->recordTiming('query', $timeSpend, $this->count . ' queries'); |
51
|
|
|
$status = $result ? 'OK' : 'Err'; |
52
|
|
|
if ($result) { |
53
|
|
|
$effect = $statement->getStatement()->rowCount(); |
54
|
|
|
$this->debug->info(sprintf( |
55
|
|
|
"query [%s] %s %ss", |
56
|
|
|
$status, |
57
|
|
|
$query, |
58
|
|
|
number_format($timeSpend, 5) |
59
|
|
|
)); |
60
|
|
|
$this->debug->info(sprintf("query effect %s rows", $effect)); |
61
|
|
|
} else { |
62
|
|
|
$this->debug->error(sprintf( |
63
|
|
|
"query [%s] %s %ss", |
64
|
|
|
$status, |
65
|
|
|
$query, |
66
|
|
|
number_format($timeSpend, 5) |
67
|
|
|
)); |
68
|
|
|
$this->debug->error(sprintf( |
69
|
|
|
"query [%s] %s", |
70
|
|
|
$status, |
71
|
|
|
implode(':', $statement->getStatement()->errorInfo()) |
72
|
|
|
)); |
73
|
|
|
} |
74
|
|
|
$binder = $statement->getBinder(); |
75
|
|
|
foreach ($binder as $item) { |
76
|
|
|
if ($item->getKey() !== null) { |
77
|
|
|
$value = $access->getMiddleware()->input($item->getKey(), $item->getValue()); |
78
|
|
|
} else { |
79
|
|
|
$value = $item->getValue(); |
80
|
|
|
} |
81
|
|
|
$this->debug->debug(sprintf("query value :%s = %s", $item->getName(), json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* 链接数据库 |
87
|
|
|
* |
88
|
|
|
* @param float $timeSpend |
89
|
|
|
*/ |
90
|
|
|
public function connectDatabase(float $timeSpend) |
91
|
|
|
{ |
92
|
|
|
$this->debug->info('connection database cost {time}s', ['time' => number_format($timeSpend, 4)]); |
93
|
|
|
$this->debug->recordTiming('db', $timeSpend, 'connection database'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|