-
Notifications
You must be signed in to change notification settings - Fork 66
/
Connection.php
228 lines (196 loc) · 6.46 KB
/
Connection.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace React\MySQL\Io;
use Evenement\EventEmitter;
use React\MySQL\Commands\CommandInterface;
use React\MySQL\Commands\PingCommand;
use React\MySQL\Commands\QueryCommand;
use React\MySQL\Commands\QuitCommand;
use React\MySQL\ConnectionInterface;
use React\MySQL\Exception;
use React\MySQL\QueryResult;
use React\Promise\Deferred;
use React\Promise\Promise;
use React\Socket\ConnectionInterface as SocketConnectionInterface;
/**
* @internal
* @see ConnectionInterface
*/
class Connection extends EventEmitter implements ConnectionInterface
{
const STATE_AUTHENTICATED = 5;
const STATE_CLOSEING = 6;
const STATE_CLOSED = 7;
/**
* @var Executor
*/
private $executor;
/**
* @var integer
*/
private $state = self::STATE_AUTHENTICATED;
/**
* @var SocketConnectionInterface
*/
private $stream;
/**
* Connection constructor.
*
* @param SocketConnectionInterface $stream
* @param Executor $executor
*/
public function __construct(SocketConnectionInterface $stream, Executor $executor)
{
$this->stream = $stream;
$this->executor = $executor;
$stream->on('error', [$this, 'handleConnectionError']);
$stream->on('close', [$this, 'handleConnectionClosed']);
}
/**
* {@inheritdoc}
*/
public function query($sql, array $params = [])
{
$query = new Query($sql);
if ($params) {
$query->bindParamsFromArray($params);
}
$command = new QueryCommand();
$command->setQuery($query);
try {
$this->_doCommand($command);
} catch (\Exception $e) {
return \React\Promise\reject($e);
}
$deferred = new Deferred();
// store all result set rows until result set end
$rows = [];
$command->on('result', function ($row) use (&$rows) {
$rows[] = $row;
});
$command->on('end', function () use ($command, $deferred, &$rows) {
$result = new QueryResult();
$result->resultFields = $command->resultFields;
$result->resultRows = $rows;
$result->warningCount = $command->warningCount;
$rows = [];
$deferred->resolve($result);
});
// resolve / reject status reply (response without result set)
$command->on('error', function ($error) use ($deferred) {
$deferred->reject($error);
});
$command->on('success', function () use ($command, $deferred) {
$result = new QueryResult();
$result->affectedRows = $command->affectedRows;
$result->insertId = $command->insertId;
$result->warningCount = $command->warningCount;
$deferred->resolve($result);
});
return $deferred->promise();
}
public function queryStream($sql, $params = [])
{
$query = new Query($sql);
if ($params) {
$query->bindParamsFromArray($params);
}
$command = new QueryCommand();
$command->setQuery($query);
$this->_doCommand($command);
return new QueryStream($command, $this->stream);
}
public function ping()
{
return new Promise(function ($resolve, $reject) {
$this->_doCommand(new PingCommand())
->on('error', function ($reason) use ($reject) {
$reject($reason);
})
->on('success', function () use ($resolve) {
$resolve();
});
});
}
public function quit()
{
return new Promise(function ($resolve, $reject) {
$this->_doCommand(new QuitCommand())
->on('error', function ($reason) use ($reject) {
$reject($reason);
})
->on('success', function () use ($resolve) {
$this->state = self::STATE_CLOSED;
$this->emit('end', [$this]);
$this->emit('close', [$this]);
$resolve();
});
$this->state = self::STATE_CLOSEING;
});
}
public function close()
{
if ($this->state === self::STATE_CLOSED) {
return;
}
$this->state = self::STATE_CLOSED;
$remoteClosed = $this->stream->isReadable() === false && $this->stream->isWritable() === false;
$this->stream->close();
// reject all pending commands if connection is closed
while (!$this->executor->isIdle()) {
$command = $this->executor->dequeue();
assert($command instanceof CommandInterface);
if ($remoteClosed) {
$command->emit('error', [new \RuntimeException(
'Connection closed by peer (ECONNRESET)',
\defined('SOCKET_ECONNRESET') ? \SOCKET_ECONNRESET : 104
)]);
} else {
$command->emit('error', [new \RuntimeException(
'Connection closing (ECONNABORTED)',
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
)]);
}
}
$this->emit('close');
$this->removeAllListeners();
}
/**
* @param Exception $err Error from socket.
*
* @return void
* @internal
*/
public function handleConnectionError($err)
{
$this->emit('error', [$err, $this]);
}
/**
* @return void
* @internal
*/
public function handleConnectionClosed()
{
if ($this->state < self::STATE_CLOSEING) {
$this->emit('error', [new \RuntimeException(
'Connection closed by peer (ECONNRESET)',
\defined('SOCKET_ECONNRESET') ? \SOCKET_ECONNRESET : 104
)]);
}
$this->close();
}
/**
* @param CommandInterface $command The command which should be executed.
* @return CommandInterface
* @throws Exception Can't send command
*/
protected function _doCommand(CommandInterface $command)
{
if ($this->state !== self::STATE_AUTHENTICATED) {
throw new \RuntimeException(
'Connection ' . ($this->state === self::STATE_CLOSED ? 'closed' : 'closing'). ' (ENOTCONN)',
\defined('SOCKET_ENOTCONN') ? \SOCKET_ENOTCONN : 107
);
}
return $this->executor->enqueue($command);
}
}