|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace suda\application\database; |
|
4
|
|
|
|
|
5
|
|
|
use suda\framework\Debugger; |
|
6
|
|
|
use suda\orm\statement\Statement; |
|
7
|
|
|
use suda\orm\connection\Connection; |
|
8
|
|
|
use suda\orm\statement\QueryAccess as StatementQueryAccess; |
|
9
|
|
|
use suda\orm\connection\observer\Observer; |
|
10
|
|
|
|
|
11
|
|
|
class DebugObserver implements Observer |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* 调试记录 |
|
15
|
|
|
* |
|
16
|
|
|
* @var Debugger |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $debug; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(Debugger $debug) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->debug = $debug; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param StatementQueryAccess $access |
|
27
|
|
|
* @param Connection $connection |
|
28
|
|
|
* @param Statement $statement |
|
29
|
|
|
* @param $timeSpend |
|
30
|
|
|
* @param bool $result |
|
31
|
|
|
*/ |
|
32
|
|
|
public function observe(StatementQueryAccess $access, Connection $connection, Statement $statement, $timeSpend, bool $result) |
|
33
|
|
|
{ |
|
34
|
|
|
$query = $connection->prefix($statement->getString()); |
|
35
|
|
|
$status = $result ? 'OK' : 'Err'; |
|
36
|
|
|
if ($result) { |
|
37
|
|
|
$effect = $statement->getStatement()->rowCount(); |
|
38
|
|
|
$this->debug->info(sprintf( |
|
39
|
|
|
"query [%s] %s %ss", |
|
40
|
|
|
$status, |
|
41
|
|
|
$query, |
|
42
|
|
|
number_format($timeSpend, 5) |
|
43
|
|
|
)); |
|
44
|
|
|
$this->debug->info(sprintf("query effect %s rows", $effect)); |
|
45
|
|
|
} else { |
|
46
|
|
|
$this->debug->error(sprintf( |
|
47
|
|
|
"query [%s] %s %ss", |
|
48
|
|
|
$status, |
|
49
|
|
|
$query, |
|
50
|
|
|
number_format($timeSpend, 5) |
|
51
|
|
|
)); |
|
52
|
|
|
$this->debug->error(sprintf( |
|
53
|
|
|
"query [%s] %s", |
|
54
|
|
|
$status, |
|
55
|
|
|
implode(':', $statement->getStatement()->errorInfo()) |
|
56
|
|
|
)); |
|
57
|
|
|
} |
|
58
|
|
|
$binder = $statement->getBinder(); |
|
59
|
|
|
foreach ($binder as $item) { |
|
60
|
|
|
if ($item->getKey() !== null) { |
|
61
|
|
|
$value = $access->getMiddleware()->input($item->getName(), $item->getValue()); |
|
62
|
|
|
} else { |
|
63
|
|
|
$value = $item->getValue(); |
|
64
|
|
|
} |
|
65
|
|
|
if (is_string($value) && strlen($value) > 256) { |
|
66
|
|
|
$show = '<size:'.strlen($value).'>'.json_encode(substr($value, 0, 255)).'...'; |
|
67
|
|
|
} else { |
|
68
|
|
|
$show = json_encode($value); |
|
69
|
|
|
} |
|
70
|
|
|
$this->debug->debug(sprintf("query value :%s = %s", $item->getName(), $show)); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|