PlasmaPHP /
driver-mysql
| 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 | 3 | function __construct() { |
|||||
| 38 | 3 | $this->deferred = new \React\Promise\Deferred(); |
|||||
| 39 | |||||||
| 40 | $this->once('error', function (\Throwable $error) { |
||||||
| 41 | $this->deferred->reject($error); |
||||||
| 42 | 3 | }); |
|||||
| 43 | |||||||
| 44 | $this->once('end', function () { |
||||||
| 45 | 2 | $this->deferred->resolve($this->resolveValue); |
|||||
| 46 | 3 | }); |
|||||
| 47 | 3 | } |
|||||
| 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 | 3 | function getPromise(): \React\Promise\PromiseInterface { |
|||||
| 60 | 3 | return $this->deferred->promise(); |
|||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | /** |
||||||
| 64 | * Sets the parser state, if necessary. If not, return `-1`. |
||||||
| 65 | * @return int |
||||||
| 66 | */ |
||||||
| 67 | 3 | function setParserState(): int { |
|||||
| 68 | 3 | 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'); |
|||||
| 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 | 3 | function hasFinished(): bool { |
|||||
| 102 | 3 | return $this->finished; |
|||||
| 103 | } |
||||||
| 104 | |||||||
| 105 | /** |
||||||
| 106 | * Whether this command sets the connection as busy. |
||||||
| 107 | * @return bool |
||||||
| 108 | */ |
||||||
| 109 | 3 | function waitForCompletion(): bool { |
|||||
| 110 | 3 | 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 | $buffer = $value->getBuffer(); |
|||||
| 120 | 1 | $parser = $value->getParser(); |
|||||
| 121 | |||||||
| 122 | 1 | if($this->resolveValue !== null) { |
|||||
| 123 | $row = $this->parseResultsetRow($buffer); |
||||||
| 124 | $this->emit('data', array($row)); |
||||||
| 125 | } else { |
||||||
| 126 | 1 | $field = $this->handleQueryOnNextCallerColumns($buffer, $parser); |
|||||
| 127 | 1 | if($field) { |
|||||
| 128 | 1 | $this->fields[$field->getName()] = $field; |
|||||
| 129 | } |
||||||
| 130 | } |
||||||
| 131 | |||||||
| 132 | // By ref doesn't seem to work, this is a workaround for now |
||||||
| 133 | 1 | $value->setBuffer($buffer); |
|||||
| 134 | 1 | } |
|||||
| 135 | |||||||
| 136 | /** |
||||||
| 137 | * Handles the column definitions of query commands on next caller. |
||||||
| 138 | * @param string $buffer |
||||||
| 139 | * @param \Plasma\Drivers\MySQL\ProtocolParser $parser |
||||||
| 140 | * @return \Plasma\ColumnDefinitionInterface|null |
||||||
| 141 | */ |
||||||
| 142 | 2 | function handleQueryOnNextCallerColumns(string &$buffer, \Plasma\Drivers\MySQL\ProtocolParser $parser): ?\Plasma\ColumnDefinitionInterface { |
|||||
| 143 | 2 | if($this->fieldsCount === null) { |
|||||
| 144 | 1 | $fieldCount = \Plasma\Drivers\MySQL\Messages\MessageUtility::readIntLength($buffer); |
|||||
| 145 | |||||||
| 146 | 1 | if($fieldCount === 0xFB) { |
|||||
| 147 | // Handle it on future tick, so we can cleanly finish the buffer of this call |
||||||
| 148 | $this->driver->getLoop()->futureTick(function () use (&$parser) { |
||||||
| 149 | $localMsg = new \Plasma\Drivers\MySQL\Messages\LocalInFileRequestMessage(); |
||||||
| 150 | $parser->handleMessage($localMsg); |
||||||
| 151 | }); |
||||||
| 152 | |||||||
| 153 | return null; |
||||||
| 154 | } |
||||||
| 155 | |||||||
| 156 | 1 | $this->fieldsCount = $fieldCount; |
|||||
| 157 | 1 | return null; |
|||||
| 158 | } |
||||||
| 159 | |||||||
| 160 | 2 | return static::parseColumnDefinition($buffer); |
|||||
| 161 | } |
||||||
| 162 | |||||||
| 163 | /** |
||||||
| 164 | * Parses the column definition. |
||||||
| 165 | * @param string $buffer |
||||||
| 166 | * @return \Plasma\ColumnDefinitionInterface |
||||||
| 167 | */ |
||||||
| 168 | 2 | static function parseColumnDefinition(string &$buffer): \Plasma\ColumnDefinitionInterface { |
|||||
| 169 | 2 | \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); // catalog - always "def" |
|||||
| 170 | 2 | $database = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
|||||
| 171 | 2 | $table = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
|||||
| 172 | 2 | \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); // orgTable |
|||||
| 173 | 2 | $name = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
|||||
| 174 | 2 | \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); // orgName |
|||||
| 175 | 2 | $buffer = \substr($buffer, 1); // 0x0C |
|||||
| 176 | |||||||
| 177 | 2 | $charset = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer); |
|||||
| 178 | 2 | $length = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt4($buffer); |
|||||
| 179 | 2 | $type = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer); |
|||||
| 180 | 2 | $flags = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer); |
|||||
| 181 | 2 | $decimals = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer); |
|||||
| 182 | |||||||
| 183 | 2 | $buffer = \substr($buffer, 2); // fillers |
|||||
| 184 | |||||||
| 185 | /*if($this instanceof COM_FIELD_LIST) { |
||||||
| 186 | \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||||||
| 187 | }*/ |
||||||
| 188 | |||||||
| 189 | 2 | $charset = \Plasma\Drivers\MySQL\CharacterSetFlags::CHARSET_MAP[$charset] ?? 'Unknown charset "'.$charset.'"'; |
|||||
| 190 | 2 | $type = \Plasma\Drivers\MySQL\FieldFlags::TYPE_MAP[$type] ?? 'Unknown type "'.$type.'"'; |
|||||
| 191 | 2 | $nullable = (($flags & \Plasma\Drivers\MySQL\FieldFlags::NOT_NULL_FLAG) === 0); |
|||||
| 192 | |||||||
| 193 | 2 | return (new \Plasma\ColumnDefinition($database, $table, $name, $type, $charset, $length, $nullable, $flags, $decimals)); |
|||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
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
Loading history...
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
Loading history...
|
|||||||
| 194 | } |
||||||
| 195 | |||||||
| 196 | /** |
||||||
| 197 | * Parses the text resultset row and returns the row. |
||||||
| 198 | * @param \Plasma\ColumnDefinitionInterface $column |
||||||
| 199 | * @param string $buffer |
||||||
| 200 | * @return array |
||||||
| 201 | */ |
||||||
| 202 | protected function parseResultsetRow(string &$buffer): array { |
||||||
| 203 | $row = array(); |
||||||
| 204 | |||||||
| 205 | /** @var \Plasma\ColumnDefinitionInterface $column */ |
||||||
| 206 | foreach($this->fields as $column) { |
||||||
| 207 | $rawValue = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||||||
| 208 | |||||||
| 209 | try { |
||||||
| 210 | $value = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql') |
||||||
| 211 | ->decodeType($column->getType(), $rawValue) |
||||||
| 212 | ->getValue(); |
||||||
| 213 | } catch (\Plasma\Exception $e) { |
||||||
| 214 | $value = $this->stdDecodeValue($column, $rawValue); |
||||||
|
0 ignored issues
–
show
It seems like
$rawValue can also be of type null; however, parameter $param of Plasma\Drivers\MySQL\Com...mmand::stdDecodeValue() 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
Loading history...
|
|||||||
| 215 | } |
||||||
| 216 | |||||||
| 217 | $row[$column->getName()] = $value; |
||||||
| 218 | } |
||||||
| 219 | |||||||
| 220 | return $row; |
||||||
| 221 | } |
||||||
| 222 | |||||||
| 223 | /** |
||||||
| 224 | * Standard decode value, if type extensions failed. |
||||||
| 225 | * @param \Plasma\ColumnDefinitionInterface $column |
||||||
| 226 | * @param string $param |
||||||
| 227 | * @return mixed |
||||||
| 228 | * @throws \Plasma\Exception |
||||||
| 229 | */ |
||||||
| 230 | protected function stdDecodeValue(\Plasma\ColumnDefinitionInterface $column, string $param) { |
||||||
| 231 | $flags = $column->getFlags(); |
||||||
| 232 | |||||||
| 233 | if($param !== null && ($flags & \Plasma\Drivers\MySQL\FieldFlags::ZEROFILL_FLAG) === 0) { |
||||||
| 234 | switch($column->getType()) { |
||||||
| 235 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG: |
||||||
| 236 | if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) === 0 || \PHP_INT_SIZE > 4) { |
||||||
| 237 | $param = (int) $param; |
||||||
| 238 | } |
||||||
| 239 | break; |
||||||
| 240 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG: |
||||||
| 241 | if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) === 0 && \PHP_INT_SIZE > 4) { |
||||||
| 242 | $param = (int) $param; |
||||||
| 243 | } |
||||||
| 244 | break; |
||||||
| 245 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY: |
||||||
| 246 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT: |
||||||
| 247 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_INT24: |
||||||
| 248 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TIMESTAMP: |
||||||
| 249 | $param = (int) $param; |
||||||
| 250 | break; |
||||||
| 251 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_FLOAT: |
||||||
| 252 | case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE: |
||||||
| 253 | $param = (float) $param; |
||||||
| 254 | break; |
||||||
| 255 | // Other types are taken as string |
||||||
| 256 | } |
||||||
| 257 | } |
||||||
| 258 | |||||||
| 259 | return $param; |
||||||
| 260 | } |
||||||
| 261 | } |
||||||
| 262 |