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 a Ok Response Message. |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class OkResponseMessage implements \Plasma\Drivers\MySQL\Messages\MessageInterface { |
17
|
|
|
/** |
18
|
|
|
* Count of affected rows by the last query. |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
public $affectedRows; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Last inserted ID by the last `INSERT` query. |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
public $lastInsertedID; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The server status flags. |
31
|
|
|
* @var int |
32
|
|
|
* @see \Plasma\Drivers\MySQL\StatusFlags |
33
|
|
|
*/ |
34
|
|
|
public $statusFlags; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Count of warnings. |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
public $warningsCount; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Server session information, if any. |
44
|
|
|
* @var string|null |
45
|
|
|
*/ |
46
|
|
|
public $sessionInfo; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Server session state changes, if any. |
50
|
|
|
* @var string|null |
51
|
|
|
*/ |
52
|
|
|
public $sessionStateChanges; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Human readable status information, if any. |
56
|
|
|
* @var string|null |
57
|
|
|
*/ |
58
|
|
|
public $info; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var \Plasma\Drivers\MySQL\ProtocolParser |
62
|
|
|
*/ |
63
|
|
|
protected $parser; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Constructor. |
67
|
|
|
* @param \Plasma\Drivers\MySQL\ProtocolParser $parser |
68
|
|
|
*/ |
69
|
55 |
|
function __construct(\Plasma\Drivers\MySQL\ProtocolParser $parser) { |
70
|
55 |
|
$this->parser = $parser; |
71
|
55 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the identifier for the packet. |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
56 |
|
static function getID(): string { |
78
|
56 |
|
return "\x00"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Parses the message, once the complete string has been received. |
83
|
|
|
* Returns false if not enough data has been received, or the remaining buffer. |
84
|
|
|
* @param \Plasma\BinaryBuffer $buffer |
85
|
|
|
* @return bool |
86
|
|
|
* @throws \Plasma\Drivers\MySQL\Messages\ParseException |
87
|
|
|
*/ |
88
|
55 |
|
function parseMessage(\Plasma\BinaryBuffer $buffer): bool { |
89
|
|
|
try { |
90
|
55 |
|
$handshake = $this->parser->getHandshakeMessage(); |
91
|
55 |
|
if(!$handshake) { |
92
|
|
|
throw new \Plasma\Drivers\MySQL\Messages\ParseException('No handshake message when receiving ok response packet'); |
93
|
|
|
} |
94
|
|
|
|
95
|
55 |
|
$this->affectedRows = $buffer->readIntLength(); |
96
|
55 |
|
$this->lastInsertedID = $buffer->readIntLength(); |
97
|
|
|
|
98
|
55 |
|
$this->statusFlags = $buffer->readInt2(); |
99
|
55 |
|
$this->warningsCount = $buffer->readInt2(); |
100
|
|
|
|
101
|
55 |
|
if(($handshake->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_SESSION_TRACK) !== 0) { |
102
|
55 |
|
if($buffer->getSize() > 0) { |
103
|
|
|
$this->sessionInfo = $buffer->readStringLength(); |
104
|
|
|
|
105
|
|
|
if(($this->statusFlags & \Plasma\Drivers\MySQL\StatusFlags::SERVER_SESSION_STATE_CHANGED) !== 0) { |
106
|
55 |
|
$this->sessionStateChanges = $buffer->readStringLength(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} else { |
110
|
|
|
$this->info = $buffer->getContents(); |
111
|
|
|
$buffer->clear(); |
112
|
|
|
} |
113
|
|
|
|
114
|
55 |
|
return true; |
115
|
|
|
} catch (\OverflowException $e) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the parser which created this message. |
122
|
|
|
* @return \Plasma\Drivers\MySQL\ProtocolParser |
123
|
|
|
*/ |
124
|
53 |
|
function getParser(): \Plasma\Drivers\MySQL\ProtocolParser { |
125
|
53 |
|
return $this->parser; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Sets the parser state, if necessary. If not, return `-1`. |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
55 |
|
function setParserState(): int { |
133
|
55 |
|
return \Plasma\Drivers\MySQL\ProtocolParser::STATE_OK; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|