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