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 | 10 | function __construct(\Plasma\Drivers\MySQL\ProtocolParser $parser) { |
|
70 | 10 | $this->parser = $parser; |
|
71 | 10 | } |
|
72 | |||
73 | /** |
||
74 | * Get the identifier for the packet. |
||
75 | * @return string |
||
76 | */ |
||
77 | static function getID(): string { |
||
78 | 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 | 10 | function parseMessage(\Plasma\BinaryBuffer $buffer): bool { |
|
89 | try { |
||
90 | 10 | $handshake = $this->parser->getHandshakeMessage(); |
|
91 | 10 | if(!$handshake) { |
|
92 | throw new \Plasma\Drivers\MySQL\Messages\ParseException('No handshake message when receiving ok response packet'); |
||
93 | } |
||
94 | |||
95 | 10 | $this->affectedRows = $buffer->readIntLength(); |
|
96 | 10 | $this->lastInsertedID = $buffer->readIntLength(); |
|
97 | |||
98 | 10 | $this->statusFlags = $buffer->readInt2(); |
|
99 | 10 | $this->warningsCount = $buffer->readInt2(); |
|
100 | |||
101 | 10 | if(($handshake->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_SESSION_TRACK) !== 0) { |
|
102 | 10 | $this->sessionInfo = $buffer->readStringLength(); |
|
103 | |||
104 | if(($statusFlags & \Plasma\Drivers\MySQL\StatusFlags::SERVER_SESSION_STATE_CHANGED) !== 0) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
105 | $this->sessionStateChanges = $buffer->readStringLength(); |
||
106 | } |
||
107 | |||
108 | $this->info = null; |
||
109 | } else { |
||
110 | $this->info = $buffer->getContents(); |
||
111 | $buffer->clear(); |
||
112 | } |
||
113 | |||
114 | return true; |
||
115 | 10 | } catch (\InvalidArgumentException $e) { |
|
116 | 10 | return false; |
|
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Get the parser which created this message. |
||
122 | * @return \Plasma\Drivers\MySQL\ProtocolParser |
||
123 | */ |
||
124 | function getParser(): \Plasma\Drivers\MySQL\ProtocolParser { |
||
125 | return $this->parser; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Sets the parser state, if necessary. If not, return `-1`. |
||
130 | * @return int |
||
131 | */ |
||
132 | 10 | function setParserState(): int { |
|
133 | 10 | return \Plasma\Drivers\MySQL\ProtocolParser::STATE_OK; |
|
134 | } |
||
135 | } |
||
136 |