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\Commands; |
||||||||
11 | |||||||||
12 | /** |
||||||||
13 | * Abstract command which has a promise. |
||||||||
14 | * @internal |
||||||||
15 | */ |
||||||||
16 | abstract class PromiseCommand implements CommandInterface { |
||||||||
17 | use \Evenement\EventEmitterTrait; |
||||||||
18 | |||||||||
19 | /** |
||||||||
20 | * @var \React\Promise\Deferred |
||||||||
21 | */ |
||||||||
22 | protected $deferred; |
||||||||
23 | |||||||||
24 | /** |
||||||||
25 | * @var mixed |
||||||||
26 | */ |
||||||||
27 | protected $resolveValue; |
||||||||
28 | |||||||||
29 | /** |
||||||||
30 | * @var bool |
||||||||
31 | */ |
||||||||
32 | protected $finished = false; |
||||||||
33 | |||||||||
34 | /** |
||||||||
35 | * Constructor. |
||||||||
36 | */ |
||||||||
37 | 2 | function __construct() { |
|||||||
38 | 2 | $this->deferred = new \React\Promise\Deferred(); |
|||||||
39 | |||||||||
40 | $this->once('error', function (\Throwable $error) { |
||||||||
41 | $this->deferred->reject($error); |
||||||||
42 | 2 | }); |
|||||||
43 | |||||||||
44 | $this->once('end', function () { |
||||||||
45 | 2 | $this->deferred->resolve($this->resolveValue); |
|||||||
46 | 2 | }); |
|||||||
47 | 2 | } |
|||||||
48 | |||||||||
49 | /** |
||||||||
50 | * Get the encoded message for writing to the database connection. |
||||||||
51 | * @return string |
||||||||
52 | */ |
||||||||
53 | abstract function getEncodedMessage(): string; |
||||||||
54 | |||||||||
55 | /** |
||||||||
56 | * Get the promise. |
||||||||
57 | * @var \React\Promise\PromiseInterface |
||||||||
58 | */ |
||||||||
59 | 2 | function getPromise(): \React\Promise\PromiseInterface { |
|||||||
60 | 2 | return $this->deferred->promise(); |
|||||||
61 | } |
||||||||
62 | |||||||||
63 | /** |
||||||||
64 | * Sets the parser state, if necessary. If not, return `-1`. |
||||||||
65 | * @return int |
||||||||
66 | */ |
||||||||
67 | 2 | function setParserState(): int { |
|||||||
68 | 2 | return -1; |
|||||||
69 | } |
||||||||
70 | |||||||||
71 | /** |
||||||||
72 | * Sets the command as completed. This state gets reported back to the user. |
||||||||
73 | * @return void |
||||||||
74 | */ |
||||||||
75 | 2 | function onComplete(): void { |
|||||||
76 | 2 | $this->finished = true; |
|||||||
77 | 2 | $this->emit('end', array()); |
|||||||
78 | 2 | } |
|||||||
79 | |||||||||
80 | /** |
||||||||
81 | * Sets the command as errored. This state gets reported back to the user. |
||||||||
82 | * @param \Throwable $throwable |
||||||||
83 | * @return void |
||||||||
84 | */ |
||||||||
85 | function onError(\Throwable $throwable): void { |
||||||||
86 | $this->finished = true; |
||||||||
87 | $this->emit('error', array($throwable)); |
||||||||
88 | } |
||||||||
89 | |||||||||
90 | /** |
||||||||
91 | * Sends the next received value into the command. |
||||||||
92 | * @param mixed $value |
||||||||
93 | * @return void |
||||||||
94 | */ |
||||||||
95 | abstract function onNext($value): void; |
||||||||
96 | |||||||||
97 | /** |
||||||||
98 | * Whether the command has finished. |
||||||||
99 | * @return bool |
||||||||
100 | */ |
||||||||
101 | 2 | function hasFinished(): bool { |
|||||||
102 | 2 | return $this->finished; |
|||||||
103 | } |
||||||||
104 | |||||||||
105 | /** |
||||||||
106 | * Whether this command sets the connection as busy. |
||||||||
107 | * @return bool |
||||||||
108 | */ |
||||||||
109 | 2 | function waitForCompletion(): bool { |
|||||||
110 | 2 | return true; |
|||||||
111 | } |
||||||||
112 | |||||||||
113 | /** |
||||||||
114 | * Handles query commands on next caller. |
||||||||
115 | * @param \Plasma\Drivers\MySQL\ProtocolOnNextCaller $value |
||||||||
116 | * @return void |
||||||||
117 | */ |
||||||||
118 | 1 | function handleQueryOnNextCaller(\Plasma\Drivers\MySQL\ProtocolOnNextCaller $value): void { |
|||||||
119 | 1 | $parser = $value->getParser(); |
|||||||
120 | 1 | $buffer = $value->getBuffer(); |
|||||||
121 | |||||||||
122 | 1 | if($this->resolveValue !== null) { |
|||||||
123 | $this->emit('data', array($this->parseResultsetRow($buffer))); |
||||||||
124 | } else { |
||||||||
125 | 1 | $field = $this->handleQueryOnNextCallerColumns($value); |
|||||||
126 | 1 | if($field) { |
|||||||
127 | $this->fields[$field->getName()] = $field; |
||||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||||||
128 | |||||||||
129 | if(\count($this->fields) >= $this->fieldsCount) { |
||||||||
130 | $this->createResolve(); |
||||||||
131 | } |
||||||||
132 | } |
||||||||
133 | } |
||||||||
134 | 1 | } |
|||||||
135 | |||||||||
136 | /** |
||||||||
137 | * Handles the column definitions of query commands on next caller. |
||||||||
138 | * @param \Plasma\Drivers\MySQL\ProtocolOnNextCaller $value |
||||||||
139 | * @return \Plasma\ColumnDefinitionInterface|null |
||||||||
140 | */ |
||||||||
141 | 1 | function handleQueryOnNextCallerColumns(\Plasma\Drivers\MySQL\ProtocolOnNextCaller $value): ?\Plasma\ColumnDefinitionInterface { |
|||||||
142 | 1 | $parser = $value->getParser(); |
|||||||
143 | 1 | $buffer = $value->getBuffer(); |
|||||||
144 | |||||||||
145 | 1 | if($this->fieldsCount === null) { |
|||||||
146 | 1 | $fieldCount = \Plasma\Drivers\MySQL\Messages\MessageUtility::readIntLength($buffer); |
|||||||
147 | |||||||||
148 | 1 | if($fieldCount === 0xFB) { |
|||||||
149 | // Handle it on future tick, so we can cleanly finish the buffer of this call |
||||||||
150 | $this->driver->getLoop()->futureTick(function () use (&$parser) { |
||||||||
151 | $localMsg = new \Plasma\Drivers\MySQL\Messages\LocalInFileRequestMessage(); |
||||||||
152 | $parser->handleMessage($localMsg); |
||||||||
153 | }); |
||||||||
154 | |||||||||
155 | return null; |
||||||||
156 | } |
||||||||
157 | |||||||||
158 | 1 | $this->fieldsCount = $fieldCount; |
|||||||
159 | 1 | return null; |
|||||||
160 | } |
||||||||
161 | |||||||||
162 | return static::parseColumnDefinition($buffer, $parser); |
||||||||
163 | } |
||||||||
164 | |||||||||
165 | /** |
||||||||
166 | * Parses the column definition. |
||||||||
167 | * @param string $buffer |
||||||||
168 | * @param \Plasma\Drivers\MySQL\ProtocolParser $parser |
||||||||
169 | * @return \Plasma\ColumnDefinitionInterface |
||||||||
170 | */ |
||||||||
171 | static function parseColumnDefinition(string &$buffer, \Plasma\Drivers\MySQL\ProtocolParser $parser): \Plasma\ColumnDefinitionInterface { |
||||||||
172 | $catalog = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); // catalog |
||||||||
173 | $database = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||||||||
174 | $table = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||||||||
175 | $orgTable = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||||||||
176 | $name = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||||||||
177 | $orgName = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||||||||
178 | $buffer = \substr($buffer, 1); // 0x0C |
||||||||
179 | |||||||||
180 | $charset = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer); |
||||||||
181 | $length = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt4($buffer); |
||||||||
182 | $type = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer); |
||||||||
183 | $flags = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer); |
||||||||
184 | $decimals = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer); |
||||||||
185 | |||||||||
186 | $buffer = \substr($buffer, 2); // fillers |
||||||||
187 | |||||||||
188 | /*if($this instanceof COM_FIELD_LIST) { |
||||||||
189 | \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||||||||
190 | }*/ |
||||||||
191 | |||||||||
192 | $database = ($database ?: $catalog); |
||||||||
193 | $table = ($table ?: $orgTable); |
||||||||
194 | $name = ($name ?: $orgName); |
||||||||
195 | |||||||||
196 | $charset = \Plasma\Drivers\MySQL\CharacterSetFlags::CHARSET_MAP[$charset] ?? 'Unknown charset "'.$charset.'"'; |
||||||||
197 | $type = $type; |
||||||||
198 | $nullable = ($flags & \Plasma\Drivers\MySQL\FieldFlags::NOT_NULL_FLAG) === 0; |
||||||||
199 | |||||||||
200 | return (new \Plasma\ColumnDefinition($database, $table, $name, $type, $charset, $length, $nullable, $flags, $decimals)); |
||||||||
0 ignored issues
–
show
It seems like
$table can also be of type null ; however, parameter $table of Plasma\ColumnDefinition::__construct() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() It seems like
$name can also be of type null ; however, parameter $name of Plasma\ColumnDefinition::__construct() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() It seems like
$database can also be of type null ; however, parameter $database of Plasma\ColumnDefinition::__construct() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
201 | } |
||||||||
202 | |||||||||
203 | /** |
||||||||
204 | * Parses the text resultset row and returns the row. |
||||||||
205 | * @param \Plasma\ColumnDefinitionInterface $column |
||||||||
206 | * @param string $buffer |
||||||||
207 | * @return array |
||||||||
208 | */ |
||||||||
209 | protected function parseResultsetRow(string &$buffer): array { |
||||||||
210 | $row = array(); |
||||||||
211 | |||||||||
212 | /** @var \Plasma\ColumnDefinitionInterface $column */ |
||||||||
213 | foreach($this->fields as $column) { |
||||||||
214 | $rawValue = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||||||||
215 | |||||||||
216 | try { |
||||||||
217 | $value = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql') |
||||||||
218 | ->decodeType($column->getType(), $rawValue) |
||||||||
219 | ->getValue(); |
||||||||
220 | } catch (\Plasma\Exception $e) { |
||||||||
221 | $value = $this->stdDecodeValue($rawValue); |
||||||||
222 | } |
||||||||
223 | |||||||||
224 | $row[$column->getName()] = $value; |
||||||||
225 | } |
||||||||
226 | |||||||||
227 | return $row; |
||||||||
228 | } |
||||||||
229 | |||||||||
230 | /** |
||||||||
231 | * Standard decode value, if type extensions failed. |
||||||||
232 | * @param \Plasma\ColumnDefinitionInterface $column |
||||||||
233 | * @param string $param |
||||||||
234 | * @return mixed |
||||||||
235 | * @throws \Plasma\Exception |
||||||||
236 | */ |
||||||||
237 | protected function stdDecodeValue(\Plasma\ColumnDefinitionInterface $column, string $param) { |
||||||||
238 | $flags = $column->getFlags(); |
||||||||
239 | |||||||||
240 | if($param !== null && ($flags & \Plasma\Drivers\MySQL\FieldFlags::ZEROFILL_FLAG) === 0) { |
||||||||
241 | switch($column->getType()) { |
||||||||
242 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG: |
||||||||
243 | if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) === 0 || \PHP_INT_SIZE > 4) { |
||||||||
244 | $param = (int) $param; |
||||||||
245 | } |
||||||||
246 | break; |
||||||||
247 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG: |
||||||||
248 | if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) === 0 && \PHP_INT_SIZE > 4) { |
||||||||
249 | $param = (int) $param; |
||||||||
250 | } |
||||||||
251 | break; |
||||||||
252 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY: |
||||||||
253 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT: |
||||||||
254 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_INT24: |
||||||||
255 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TIMESTAMP: |
||||||||
256 | $param = (int) $param; |
||||||||
257 | break; |
||||||||
258 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_FLOAT: |
||||||||
259 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE: |
||||||||
260 | $param = (float) $param; |
||||||||
261 | break; |
||||||||
262 | // Other types are taken as string |
||||||||
263 | } |
||||||||
264 | } |
||||||||
265 | |||||||||
266 | return $param; |
||||||||
267 | } |
||||||||
268 | } |
||||||||
269 |