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 | * 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\Drivers\MySQL\Messages\MessageUtility::writeInt4($this->id); |
||
| 55 | |||
| 56 | $packet .= "\0"; // Cursor type flag |
||
| 57 | $packet .= \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt4(1); // Iteration count is always 1 |
||
| 58 | |||
| 59 | $paramCount = \count($this->params); |
||
| 60 | |||
| 61 | $bitmapOffset = \strlen($packet); |
||
| 62 | $packet .= \str_repeat("\0", (($paramCount + 7) >> 3)); |
||
| 63 | |||
| 64 | $bound = 0; |
||
| 65 | |||
| 66 | $types = ''; |
||
| 67 | $values = ''; |
||
| 68 | |||
| 69 | foreach($this->params as $id => $param) { |
||
| 70 | if($param === null) { |
||
| 71 | $offset = $bitmapOffset + ($id >> 3); |
||
| 72 | $packet[$offset] = $packet[$offset] | \chr((1 << ($id % 8))); |
||
| 73 | } else { |
||
| 74 | $bound = 1; |
||
| 75 | } |
||
| 76 | |||
| 77 | $unsigned = false; |
||
| 78 | |||
| 79 | try { |
||
| 80 | $manager = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql'); |
||
| 81 | $encode = $manager->encodeType($param); |
||
| 82 | |||
| 83 | $unsigned = $encode->isUnsigned(); |
||
| 84 | $type = $encode->getSQLType(); |
||
| 85 | $value = $encode->getValue(); |
||
| 86 | } catch (\Plasma\Exception $e) { |
||
| 87 | [ $unsigned, $type, $value ] = $this->stdEncodeValue($param); |
||
| 88 | } |
||
| 89 | |||
| 90 | $types .= \chr($type); |
||
| 91 | $types .= ($unsigned ? "\x80" : "\0"); |
||
| 92 | |||
| 93 | $values .= $value; |
||
| 94 | } |
||
| 95 | |||
| 96 | $packet .= \chr($bound); |
||
| 97 | |||
| 98 | if($bound) { |
||
| 99 | $packet .= $types; |
||
| 100 | $packet .= $values; |
||
| 101 | } |
||
| 102 | |||
| 103 | return $packet; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Parses the binary resultset row and returns the row. |
||
| 108 | * @param \Plasma\ColumnDefinitionInterface $column |
||
| 109 | * @param string $buffer |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | protected function parseResultsetRow(string &$buffer): array { |
||
| 113 | var_dump(unpack('C*', $buffer)); |
||
|
0 ignored issues
–
show
Security
Debugging Code
introduced
by
Loading history...
|
|||
| 114 | //$buffer = \substr($buffer, 1); // Skip first byte (header) |
||
| 115 | |||
| 116 | $nullRow = array(); |
||
| 117 | $i = 0; |
||
| 118 | |||
| 119 | /** @var \Plasma\ColumnDefinitionInterface $column */ |
||
| 120 | foreach($this->fields as $column) { // Handle NULL-bitmap |
||
| 121 | if((\ord($buffer[(($i + 2) >> 3)]) & (1 << (($i + 2) % 8))) !== 0) { |
||
| 122 | $nullRow[$column->getName()] = null; |
||
| 123 | } |
||
| 124 | |||
| 125 | $i++; |
||
| 126 | } |
||
| 127 | |||
| 128 | $buffer = \substr($buffer, ((\count($this->fields) + 9) >> 3)); // Remove NULL-bitmap |
||
| 129 | $row = array(); |
||
| 130 | |||
| 131 | /** @var \Plasma\ColumnDefinitionInterface $column */ |
||
| 132 | foreach($this->fields as $column) { |
||
| 133 | if(\array_key_exists($columm->getName(), $nullRow)) { |
||
| 134 | $row[$columm->getName()] = $nullRow[$columm->getName()]; |
||
| 135 | continue; |
||
| 136 | } |
||
| 137 | |||
| 138 | $rawValue = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
||
| 139 | |||
| 140 | try { |
||
| 141 | $value = $column->parseValue($rawValue); |
||
| 142 | } catch (\Plasma\Exception $e) { |
||
| 143 | $value = $this->stdDecodeValue($rawValue); |
||
| 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 | // TODO: Check if short, long or long long |
||
| 172 | if($param >= 0 && $param < (1 << 15)) { |
||
| 173 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT; |
||
| 174 | $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt2($param); |
||
| 175 | } elseif(\PHP_INT_SIZE === 4) { |
||
| 176 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG; |
||
| 177 | $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt4($param); |
||
| 178 | } else { |
||
| 179 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG; |
||
| 180 | $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt8($param); |
||
| 181 | } |
||
| 182 | break; |
||
| 183 | case 'double': |
||
| 184 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE; |
||
| 185 | $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeFloat($param); |
||
| 186 | break; |
||
| 187 | case 'string': |
||
| 188 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG_BLOB; |
||
| 189 | |||
| 190 | $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt4(\strlen($param)); |
||
| 191 | $value .= $param; |
||
| 192 | break; |
||
| 193 | case 'NULL': |
||
| 194 | $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_NULL; |
||
| 195 | $value = ''; |
||
| 196 | break; |
||
| 197 | default: |
||
| 198 | throw new \Plasma\Exception('Unexpected type for binding parameter: '.\gettype($param)); |
||
| 199 | break; |
||
| 200 | } |
||
| 201 | |||
| 202 | return array($unsigned, $type, $value); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Whether the sequence ID should be resetted. |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | function resetSequence(): bool { |
||
| 210 | return false; |
||
| 211 | } |
||
| 212 | } |
||
| 213 |