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 | * Statement Execute command. |
||
14 | * @internal |
||
15 | */ |
||
16 | class StatementExecuteCommand extends QueryCommand { |
||
17 | /** |
||
18 | * The identifier for this command. |
||
19 | * @var int |
||
20 | * @source |
||
21 | */ |
||
22 | const COMMAND_ID = 0x17; |
||
23 | |||
24 | /** |
||
25 | * @var mixed |
||
26 | */ |
||
27 | protected $id; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $params; |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * @param \Plasma\DriverInterface $driver |
||
37 | * @param mixed $id |
||
38 | * @param string $query |
||
39 | * @param array $params |
||
40 | */ |
||
41 | function __construct(\Plasma\DriverInterface $driver, $id, string $query, array $params) { |
||
42 | parent::__construct($driver, $query); |
||
43 | |||
44 | $this->id = $id; |
||
45 | $this->params = $params; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get the encoded message for writing to the database connection. |
||
50 | * @return string |
||
51 | */ |
||
52 | function getEncodedMessage(): string { |
||
53 | $packet = \chr(static::COMMAND_ID); |
||
54 | $packet .= \Plasma\BinaryBuffer::writeInt4($this->id); |
||
55 | |||
56 | $packet .= "\x00"; // Cursor type flag |
||
57 | $packet .= \Plasma\BinaryBuffer::writeInt4(1); // Iteration count is always 1 |
||
58 | |||
59 | $bitmapOffset = \strlen($packet); |
||
60 | $packet .= \str_repeat("\x00", ((\count($this->params) + 7) >> 3)); |
||
61 | |||
62 | $bound = 0; |
||
63 | |||
64 | $types = ''; |
||
65 | $values = ''; |
||
66 | |||
67 | foreach($this->params as $id => $param) { |
||
68 | if($param === null) { |
||
69 | $offset = $bitmapOffset + ($id >> 3); |
||
70 | $packet[$offset] = $packet[$offset] | \chr((1 << ($id % 8))); |
||
71 | } else { |
||
72 | $bound = 1; |
||
73 | } |
||
74 | |||
75 | try { |
||
76 | $manager = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql'); |
||
77 | $encode = $manager->encodeType($param); |
||
78 | |||
79 | $unsigned = $encode->isUnsigned(); |
||
80 | $type = $encode->getSQLType(); |
||
81 | $value = $encode->getValue(); |
||
82 | } catch (\Plasma\Exception $e) { |
||
83 | [ $unsigned, $type, $value ] = $this->stdEncodeValue($param); |
||
84 | } |
||
85 | |||
86 | $types .= \chr($type).($unsigned ? "\x80" : "\x00"); |
||
87 | $values .= $value; |
||
88 | } |
||
89 | |||
90 | $packet .= \chr($bound); |
||
91 | |||
92 | if($bound) { |
||
93 | $packet .= $types; |
||
94 | $packet .= $values; |
||
95 | } |
||
96 | |||
97 | return $packet; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Whether the sequence ID should be resetted. |
||
102 | * @return bool |
||
103 | */ |
||
104 | function resetSequence(): bool { |
||
105 | return true; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Parses the binary resultset row and returns the row. |
||
110 | * @param \Plasma\ColumnDefinitionInterface $column |
||
111 | * @param \Plasma\BinaryBuffer $buffer |
||
112 | * @return array |
||
113 | */ |
||
114 | protected function parseResultsetRow(\Plasma\BinaryBuffer $buffer): array { |
||
115 | $buffer->read(1); // remove packet header |
||
116 | |||
117 | $nullRow = array(); |
||
118 | $i = 0; |
||
119 | |||
120 | /** @var \Plasma\ColumnDefinitionInterface $column */ |
||
121 | foreach($this->fields as $column) { // Handle NULL-bitmap |
||
122 | if((\ord($buffer[(($i + 2) >> 3)]) & (1 << (($i + 2) % 8))) !== 0) { |
||
123 | $nullRow[$column->getName()] = null; |
||
124 | } |
||
125 | |||
126 | $i++; |
||
127 | } |
||
128 | |||
129 | $buffer->read(((\count($this->fields) + 9) >> 3)); // Remove NULL-bitmap |
||
130 | $row = array(); |
||
131 | |||
132 | /** @var \Plasma\ColumnDefinitionInterface $column */ |
||
133 | foreach($this->fields as $column) { |
||
134 | if(\array_key_exists($column->getName(), $nullRow)) { |
||
135 | $row[$column->getName()] = null; |
||
136 | continue; |
||
137 | } |
||
138 | |||
139 | $value = $this->stdDecodeBinaryValue($column, $buffer); |
||
140 | $parsedValue = $column->parseValue($value); |
||
141 | |||
142 | if($value !== $parsedValue) { |
||
143 | $value = $parsedValue; |
||
144 | } |
||
145 | |||
146 | $row[$column->getName()] = $value; |
||
147 | } |
||
148 | |||
149 | return $row; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Standard encode value, if type extensions failed. |
||
154 | * @param mixed $param |
||
155 | * @return array |
||
156 | * @throws \Plasma\Exception |
||
157 | */ |
||
158 | protected function stdEncodeValue($param): array { |
||
159 | $unsigned = false; |
||
160 | |||
161 | switch(\gettype($param)) { |
||
162 | case 'boolean': |
||
163 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY; |
||
164 | $value = ($param ? "\x01" : "\0"); |
||
165 | break; |
||
166 | case 'integer': |
||
167 | if($param >= 0) { |
||
168 | $unsigned = true; |
||
169 | } |
||
170 | |||
171 | if($param >= 0 && $param < (1 << 15)) { |
||
172 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT; |
||
173 | $value = \Plasma\BinaryBuffer::writeInt2($param); |
||
174 | } elseif(\PHP_INT_SIZE === 4) { |
||
175 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG; |
||
176 | $value = \Plasma\BinaryBuffer::writeInt4($param); |
||
177 | } else { |
||
178 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG; |
||
179 | $value = \Plasma\BinaryBuffer::writeInt8($param); |
||
180 | } |
||
181 | break; |
||
182 | case 'double': |
||
183 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE; |
||
184 | $value = \Plasma\BinaryBuffer::writeDouble($param); |
||
185 | break; |
||
186 | case 'string': |
||
187 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG_BLOB; |
||
188 | $value = \Plasma\BinaryBuffer::writeStringLength($param); |
||
189 | break; |
||
190 | case 'NULL': |
||
191 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_NULL; |
||
192 | $value = ''; |
||
193 | break; |
||
194 | default: |
||
195 | throw new \Plasma\Exception('Unexpected type for binding parameter: '.\gettype($param)); |
||
196 | break; |
||
197 | } |
||
198 | |||
199 | return array($unsigned, $type, $value); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Standard decode value, if type extensions failed. |
||
204 | * @param \Plasma\ColumnDefinitionInterface $column |
||
205 | * @param \Plasma\BinaryBuffer $buffer |
||
206 | * @return mixed |
||
207 | * @throws \Plasma\Exception |
||
208 | */ |
||
209 | protected function stdDecodeBinaryValue(\Plasma\ColumnDefinitionInterface $column, \Plasma\BinaryBuffer $buffer) { |
||
210 | $flags = $column->getFlags(); |
||
211 | |||
212 | switch(true) { |
||
213 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_STRING) !== 0): |
||
214 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_VARCHAR) !== 0): |
||
215 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_VAR_STRING) !== 0): |
||
216 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_ENUM) !== 0): |
||
217 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SET) !== 0): |
||
218 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG_BLOB) !== 0): |
||
219 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_MEDIUM_BLOB) !== 0): |
||
220 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_BLOB) !== 0): |
||
221 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY_BLOB) !== 0): |
||
222 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_GEOMETRY) !== 0): |
||
223 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_BIT) !== 0): |
||
224 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DECIMAL) !== 0): |
||
225 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_NEWDECIMAL) !== 0): |
||
226 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_JSON) !== 0): |
||
227 | case ($column->getType() === 'VARSTRING'): // WTF is going on ?! flags = 0, type = VARSTRING |
||
228 | $value = $buffer->readStringLength(); |
||
229 | break; |
||
1 ignored issue
–
show
Coding Style
introduced
by
![]() |
|||
230 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY) !== 0): |
||
231 | $value = $buffer->readInt1(); |
||
232 | $value = $this->zeroFillInts($column, $value); |
||
233 | break; |
||
234 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT) !== 0): |
||
235 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_YEAR) !== 0): |
||
236 | $value = $buffer->readInt2(); |
||
237 | $value = $this->zeroFillInts($column, $value); |
||
238 | break; |
||
239 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_INT24) !== 0): |
||
240 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG) !== 0): |
||
241 | $value = $buffer->readInt4(); |
||
242 | |||
243 | if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) !== 0 && \PHP_INT_SIZE <= 4) { |
||
244 | $value = \bcadd($value, '18446744073709551616'); |
||
245 | } |
||
246 | |||
247 | $value = $this->zeroFillInts($column, $value); |
||
248 | break; |
||
249 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG) !== 0): |
||
250 | $value = $buffer->readInt8(); |
||
251 | |||
252 | if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) !== 0) { |
||
253 | $value = \bcadd($value, '18446744073709551616'); |
||
254 | } elseif(\PHP_INT_SIZE > 4) { |
||
255 | $value = (int) $value; |
||
256 | } |
||
257 | |||
258 | $value = $this->zeroFillInts($column, $value); |
||
259 | break; |
||
260 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_FLOAT) !== 0): |
||
261 | $value = $buffer->readFloat(); |
||
262 | break; |
||
263 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE) !== 0): |
||
264 | $value = $buffer->readDouble(); |
||
265 | break; |
||
266 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DATE) !== 0): |
||
267 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_NEWDATE) !== 0): |
||
268 | $length = $buffer->readIntLength(); |
||
269 | if($length > 0) { |
||
270 | $year = $buffer->readInt2(); |
||
271 | $month = $buffer->readInt1(); |
||
272 | $day = $buffer->readInt1(); |
||
273 | |||
274 | $value = \sprintf('%d-%d-%d', $year, $month, $day); |
||
275 | } else { |
||
276 | $value = '0000-00-00'; |
||
277 | } |
||
278 | break; |
||
1 ignored issue
–
show
|
|||
279 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DATETIME) !== 0): |
||
280 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TIMESTAMP) !== 0): |
||
281 | $length = $buffer->readIntLength(); |
||
282 | if($length > 0) { |
||
283 | $year = $buffer->readInt2(); |
||
284 | $month = $buffer->readInt1(); |
||
285 | $day = $buffer->readInt1(); |
||
286 | |||
287 | if($length > 4) { |
||
288 | $hour = $buffer->readInt1(); |
||
289 | $min = $buffer->readInt1(); |
||
290 | $sec = $buffer->readInt1(); |
||
291 | } else { |
||
292 | $hour = 0; |
||
293 | $min = 0; |
||
294 | $sec = 0; |
||
295 | } |
||
296 | |||
297 | if($length > 7) { |
||
298 | $micro = $buffer->readInt4(); |
||
299 | } else { |
||
300 | $micro = 0; |
||
301 | } |
||
302 | |||
303 | $timestamp = (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TIMESTAMP) !== 0); |
||
304 | |||
305 | $micro = \str_pad($micro, 6, '0', \STR_PAD_LEFT); |
||
306 | $micro = ($timestamp ? $micro : \number_format($micro, 0, '', ' ')); |
||
307 | |||
308 | $value = \sprintf('%d-%d-%d %d:%d:%d'.($micro > 0 ? '.%s' : ''), $year, $month, $day, $hour, $min, $sec, $micro); |
||
309 | |||
310 | if($timestamp) { |
||
311 | $value = \DateTime::createFromFormat('Y-m-d H:i:s'.($micro > 0 ? '.u' : ''), $value)->getTimestamp(); |
||
312 | } |
||
313 | } else { |
||
314 | $value = '0000-00-00 00:00:00.000 000'; |
||
315 | } |
||
316 | break; |
||
317 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TIME) !== 0): |
||
318 | $length = $buffer->readIntLength(); |
||
319 | if($length > 0) { |
||
320 | $sign = $buffer->readInt1(); |
||
321 | $days = $buffer->readInt4(); |
||
322 | |||
323 | if($sign === 1) { |
||
324 | $days *= -1; |
||
325 | } |
||
326 | |||
327 | $hour = $buffer->readInt1(); |
||
328 | $min = $buffer->readInt1(); |
||
329 | $sec = $buffer->readInt1(); |
||
330 | |||
331 | if($length > 8) { |
||
332 | $micro = $buffer->readInt4(); |
||
333 | } else { |
||
334 | $micro = 0; |
||
335 | } |
||
336 | |||
337 | $micro = \str_pad($micro, 6, '0', \STR_PAD_LEFT); |
||
338 | $micro = \number_format($micro, 0, '', ' '); |
||
339 | |||
340 | $value = \sprintf('%dd %d:%d:%d'.($micro > 0 ? '.%s' : ''), $days, $hour, $min, $sec, $micro); |
||
341 | } else { |
||
342 | $value = '0d 00:00:00'; |
||
343 | } |
||
344 | break; |
||
345 | case (($flags & \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_NULL) !== 0): |
||
346 | $value = null; |
||
347 | break; |
||
1 ignored issue
–
show
|
|||
348 | default: |
||
349 | throw new \InvalidArgumentException('Unknown column type (flags: '.$flags.', type: '.$column->getType().')'); |
||
350 | break; |
||
351 | } |
||
352 | |||
353 | return $value; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @param \Plasma\ColumnDefinitionInterface $column |
||
358 | * @param string|int $value |
||
359 | * @return string|int |
||
360 | */ |
||
361 | protected function zeroFillInts(\Plasma\ColumnDefinitionInterface $column, $value) { |
||
362 | $flags = $column->getFlags(); |
||
363 | |||
364 | if(($flags & \Plasma\Drivers\MySQL\FieldFlags::ZEROFILL_FLAG) !== 0) { |
||
365 | $value = \str_pad(((string) $value), $column->getLength(), '0', \STR_PAD_LEFT); |
||
366 | } |
||
367 | |||
368 | return $value; |
||
369 | } |
||
370 | } |
||
371 |