1 | <?php |
||
2 | /** |
||
3 | * Plasma Driver MySQL component |
||
4 | * Copyright 2018 PlasmaPHP, All Rights Reserved |
||
5 | * |
||
6 | * Website: https://github.com/PlasmaPHP |
||
7 | * License: https://github.com/PlasmaPHP/driver-mysql/blob/master/LICENSE |
||
8 | */ |
||
9 | |||
10 | namespace Plasma\Drivers\MySQL\Messages; |
||
11 | |||
12 | /** |
||
13 | * Represents an Err Response Message. |
||
14 | * @internal |
||
15 | */ |
||
16 | class ErrResponseMessage implements \Plasma\Drivers\MySQL\Messages\MessageInterface { |
||
17 | /** |
||
18 | * Error code. |
||
19 | * @var int |
||
20 | */ |
||
21 | public $errorCode; |
||
22 | |||
23 | /** |
||
24 | * SQL State marker, if any. |
||
25 | * @var string|null |
||
26 | */ |
||
27 | public $sqlStateMarker; |
||
28 | |||
29 | /** |
||
30 | * SQL State, if any. |
||
31 | * @var string|null |
||
32 | */ |
||
33 | public $sqlState; |
||
34 | |||
35 | /** |
||
36 | * Error message. |
||
37 | * @var string |
||
38 | */ |
||
39 | public $errorMessage; |
||
40 | |||
41 | /** |
||
42 | * @var \Plasma\Drivers\MySQL\ProtocolParser |
||
43 | */ |
||
44 | protected $parser; |
||
45 | |||
46 | /** |
||
47 | * Constructor. |
||
48 | * @param \Plasma\Drivers\MySQL\ProtocolParser $parser |
||
49 | */ |
||
50 | 1 | function __construct(\Plasma\Drivers\MySQL\ProtocolParser $parser) { |
|
51 | 1 | $this->parser = $parser; |
|
52 | 1 | } |
|
53 | |||
54 | /** |
||
55 | * Get the identifier for the packet. |
||
56 | * @return string |
||
57 | */ |
||
58 | 11 | static function getID(): string { |
|
59 | 11 | return "\xFF"; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Parses the message, once the complete string has been received. |
||
64 | * Returns false if not enough data has been received, or the remaining buffer. |
||
65 | * @param \Plasma\BinaryBuffer $buffer |
||
66 | * @return bool |
||
67 | * @throws \Plasma\Drivers\MySQL\Messages\ParseException |
||
68 | */ |
||
69 | 1 | function parseMessage(\Plasma\BinaryBuffer $buffer): bool { |
|
70 | 1 | $this->errorCode = $buffer->readInt2(); |
|
71 | |||
72 | 1 | $handshake = $this->parser->getHandshakeMessage(); |
|
73 | 1 | if(!$handshake || $this->parser->getState() == \Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE) { |
|
74 | $exception = new \Plasma\Drivers\MySQL\Messages\ParseException($buffer, $this->errorCode); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
75 | $exception->setState(\Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE_ERROR); |
||
76 | $exception->setBuffer(''); |
||
77 | |||
78 | throw $exception; |
||
79 | } |
||
80 | |||
81 | 1 | $this->sqlStateMarker = $buffer->readStringLength(1); |
|
82 | 1 | $this->sqlState = $buffer->readStringLength(5); |
|
83 | 1 | $this->errorMessage = $buffer->getContents(); |
|
84 | |||
85 | 1 | $buffer->clear(); |
|
86 | 1 | return true; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get the parser which created this message. |
||
91 | * @return \Plasma\Drivers\MySQL\ProtocolParser |
||
92 | */ |
||
93 | function getParser(): \Plasma\Drivers\MySQL\ProtocolParser { |
||
94 | return $this->parser; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Sets the parser state, if necessary. If not, return `-1`. |
||
99 | * @return int |
||
100 | */ |
||
101 | 1 | function setParserState(): int { |
|
102 | 1 | return \Plasma\Drivers\MySQL\ProtocolParser::STATE_OK; |
|
103 | } |
||
104 | } |
||
105 |