forked from emonkak/php-database
/
ListenableConnection.php
154 lines (126 loc) · 3.19 KB
/
ListenableConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Emonkak\Database;
class ListenableConnection implements PDOInterface
{
/**
* @var PDOInterface
*/
private $delegate;
/**
* @var PDOListenerInterface[]
*/
private $listeners = [];
public function __construct(PDOInterface $delegate)
{
$this->delegate = $delegate;
}
public function addListaner(PDOListenerInterface $listener): void
{
$this->listeners[] = $listener;
}
/**
* {@inheritdoc}
*/
public function beginTransaction()
{
foreach ($this->listeners as $listener) {
$listener->onBeginTransaction($this->delegate);
}
return $this->delegate->beginTransaction();
}
/**
* {@inheritdoc}
*/
public function commit()
{
foreach ($this->listeners as $listener) {
$listener->onCommit($this->delegate);
}
return $this->delegate->commit();
}
/**
* {@inheritdoc}
*/
public function errorCode()
{
return $this->delegate->errorCode();
}
/**
* {@inheritdoc}
*/
public function errorInfo()
{
return $this->delegate->errorInfo();
}
/**
* {@inheritdoc}
*/
public function exec(string $statement)
{
$start = microtime(true);
$result = $this->delegate->exec($statement);
$elapsedTime = microtime(true) - $start;
foreach ($this->listeners as $listener) {
$listener->onQuery($this->delegate, $statement, [], $elapsedTime);
}
return $result;
}
/**
* {@inheritdoc}
*/
public function inTransaction()
{
return $this->delegate->inTransaction();
}
/**
* {@inheritdoc}
*/
public function lastInsertId(?string $name = null)
{
return $this->delegate->lastInsertId();
}
/**
* {@inheritdoc}
*/
public function prepare(string $statement, array $options = [])
{
$stmt = $this->delegate->prepare($statement, $options);
if ($stmt !== false) {
$stmt = new ListenableStatement($this->delegate, $this->listeners, $stmt, $statement);
}
return $stmt;
}
/**
* {@inheritdoc}
*/
public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs)
{
$start = microtime(true);
$stmt = $this->delegate->query($query, $fetchMode, ...$fetchModeArgs);
if ($stmt !== false) {
$elapsedTime = microtime(true) - $start;
foreach ($this->listeners as $listener) {
$listener->onQuery($this->delegate, $query, [], $elapsedTime);
}
$stmt = new ListenableStatement($this->delegate, $this->listeners, $stmt, $query);
}
return $stmt;
}
/**
* {@inheritdoc}
*/
public function quote(string $string, int $type = \PDO::PARAM_STR)
{
return $this->delegate->quote($string, $type);
}
/**
* {@inheritdoc}
*/
public function rollback()
{
foreach ($this->listeners as $listener) {
$listener->onRollback($this->delegate);
}
return $this->delegate->rollback();
}
}