|
2 | 2 |
|
3 | 3 | namespace React\MySQL\Io;
|
4 | 4 |
|
5 |
| -use Evenement\EventEmitter; |
6 | 5 | use React\MySQL\Commands\AuthenticateCommand;
|
7 | 6 | use React\MySQL\Commands\QueryCommand;
|
8 | 7 | use React\MySQL\Commands\QuitCommand;
|
|
12 | 11 | /**
|
13 | 12 | * @internal
|
14 | 13 | */
|
15 |
| -class Parser extends EventEmitter |
| 14 | +class Parser |
16 | 15 | {
|
17 | 16 | const PHASE_GOT_INIT = 1;
|
18 | 17 | const PHASE_AUTH_SENT = 2;
|
@@ -72,9 +71,6 @@ class Parser extends EventEmitter
|
72 | 71 |
|
73 | 72 | public $protocalVersion = 0;
|
74 | 73 |
|
75 |
| - protected $errno = 0; |
76 |
| - protected $errmsg = ''; |
77 |
| - |
78 | 74 | private $buffer;
|
79 | 75 |
|
80 | 76 | protected $connectOptions;
|
@@ -142,14 +138,15 @@ public function parse($data)
|
142 | 138 | if ($response === 0xFF) {
|
143 | 139 | // error packet before handshake means we did not exchange capabilities and error does not include SQL state
|
144 | 140 | $this->phase = self::PHASE_AUTH_ERR;
|
145 |
| - $this->errno = $this->buffer->readInt2(); |
146 |
| - $this->errmsg = $this->buffer->read($this->pctSize - $len + $this->buffer->length()); |
147 |
| - $this->debug(sprintf("Error Packet:%d %s\n", $this->errno, $this->errmsg)); |
| 141 | + |
| 142 | + $code = $this->buffer->readInt2(); |
| 143 | + $exception = new Exception($this->buffer->read($this->pctSize - $len + $this->buffer->length()), $code); |
| 144 | + $this->debug(sprintf("Error Packet:%d %s\n", $code, $exception->getMessage())); |
148 | 145 |
|
149 | 146 | // error during init phase also means we're not currently executing any command
|
150 | 147 | // simply reject the first outstanding command in the queue (AuthenticateCommand)
|
151 | 148 | $this->currCommand = $this->executor->dequeue();
|
152 |
| - $this->onError(); |
| 149 | + $this->onError($exception); |
153 | 150 | return;
|
154 | 151 | }
|
155 | 152 |
|
@@ -181,12 +178,12 @@ public function parse($data)
|
181 | 178 |
|
182 | 179 | if ($fieldCount === 0xFF) {
|
183 | 180 | // error packet
|
184 |
| - $this->errno = $this->buffer->readInt2(); |
| 181 | + $code = $this->buffer->readInt2(); |
185 | 182 | $this->buffer->skip(6); // skip SQL state
|
186 |
| - $this->errmsg = $this->buffer->read($this->pctSize - $len + $this->buffer->length()); |
187 |
| - $this->debug(sprintf("Error Packet:%d %s\n", $this->errno, $this->errmsg)); |
| 183 | + $exception = new Exception($this->buffer->read($this->pctSize - $len + $this->buffer->length()), $code); |
| 184 | + $this->debug(sprintf("Error Packet:%d %s\n", $code, $exception->getMessage())); |
188 | 185 |
|
189 |
| - $this->onError(); |
| 186 | + $this->onError($exception); |
190 | 187 | $this->nextRequest();
|
191 | 188 | } elseif ($fieldCount === 0x00 && $this->rsState !== self::RS_STATE_ROW) {
|
192 | 189 | // Empty OK Packet terminates a query without a result set (UPDATE, INSERT etc.)
|
@@ -278,15 +275,16 @@ private function onResultRow($row)
|
278 | 275 | $command->emit('result', array($row));
|
279 | 276 | }
|
280 | 277 |
|
281 |
| - protected function onError() |
| 278 | + private function onError(Exception $error) |
282 | 279 | {
|
283 |
| - $command = $this->currCommand; |
284 |
| - $this->currCommand = null; |
| 280 | + // reject current command with error if we're currently executing any commands |
| 281 | + // ignore unsolicited server error in case we're not executing any commands (connection will be dropped) |
| 282 | + if ($this->currCommand !== null) { |
| 283 | + $command = $this->currCommand; |
| 284 | + $this->currCommand = null; |
285 | 285 |
|
286 |
| - $error = new Exception($this->errmsg, $this->errno); |
287 |
| - $this->errmsg = ''; |
288 |
| - $this->errno = 0; |
289 |
| - $command->emit('error', array($error)); |
| 286 | + $command->emit('error', array($error)); |
| 287 | + } |
290 | 288 | }
|
291 | 289 |
|
292 | 290 | protected function onResultDone()
|
@@ -315,9 +313,8 @@ protected function onSuccess()
|
315 | 313 | $command->emit('success');
|
316 | 314 | }
|
317 | 315 |
|
318 |
| - protected function onClose() |
| 316 | + public function onClose() |
319 | 317 | {
|
320 |
| - $this->emit('close'); |
321 | 318 | if ($this->currCommand !== null) {
|
322 | 319 | $command = $this->currCommand;
|
323 | 320 | $this->currCommand = null;
|
|
0 commit comments