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($payload); |
|
|
|
|
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
|
|
|
$buffer = \substr($buffer, 1); // Skip first byte (header) |
114
|
|
|
|
115
|
|
|
$nullRow = array(); |
116
|
|
|
$i = 0; |
117
|
|
|
|
118
|
|
|
/** @var \Plasma\ColumnDefinitionInterface $column */ |
119
|
|
|
foreach($this->fields as $column) { // Handle NULL-bitmap |
120
|
|
|
if((\ord($buffer[(($i + 2) >> 3)]) & (1 << (($i + 2) % 8))) !== 0) { |
121
|
|
|
$nullRow[$column->getName()] = null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$i++; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$buffer = \substr($buffer, ((\count($column) + 9) >> 3)); // Remove NULL-bitmap |
|
|
|
|
128
|
|
|
$row = array(); |
129
|
|
|
|
130
|
|
|
/** @var \Plasma\ColumnDefinitionInterface $column */ |
131
|
|
|
foreach($this->fields as $column) { |
132
|
|
|
if(\array_key_exists($columm->getName(), $nullRow)) { |
|
|
|
|
133
|
|
|
$row[$columm->getName()] = $nullRow[$columm->getName()]; |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$rawValue = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
$value = $column->parseValue($rawValue); |
141
|
|
|
} catch (\Plasma\Exception $e) { |
142
|
|
|
$value = $this->stdDecodeValue($rawValue); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$row[$column->getName()] = $value; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $row; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Standard encode value, if type extensions failed. |
153
|
|
|
* @param mixed $param |
154
|
|
|
* @return array |
155
|
|
|
* @throws \Plasma\Exception |
156
|
|
|
*/ |
157
|
|
|
protected function stdEncodeValue($param): array { |
158
|
|
|
$unsigned = false; |
159
|
|
|
|
160
|
|
|
switch(\gettype($param)) { |
161
|
|
|
case 'boolean': |
|
|
|
|
162
|
|
|
$type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY; |
163
|
|
|
$value = ($param ? "\x01" : "\0"); |
164
|
|
|
break; |
|
|
|
|
165
|
|
|
case 'integer': |
|
|
|
|
166
|
|
|
if($param >= 0) { |
|
|
|
|
167
|
|
|
$unsigned = true; |
168
|
|
|
} |
|
|
|
|
169
|
|
|
|
170
|
|
|
// TODO: Check if short, long or long long |
171
|
|
|
if($param >= 0 && $param < (1 << 15)) { |
|
|
|
|
172
|
|
|
$type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT; |
173
|
|
|
$value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt2($param); |
174
|
|
|
} elseif(\PHP_INT_SIZE === 4) { |
|
|
|
|
175
|
|
|
$type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG; |
176
|
|
|
$value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt4($param); |
177
|
|
|
} else { |
|
|
|
|
178
|
|
|
$type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG; |
179
|
|
|
$value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt8($param); |
180
|
|
|
} |
|
|
|
|
181
|
|
|
break; |
|
|
|
|
182
|
|
|
case 'double': |
|
|
|
|
183
|
|
|
$type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE; |
184
|
|
|
$value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeFloat($param); |
185
|
|
|
break; |
|
|
|
|
186
|
|
|
case 'string': |
|
|
|
|
187
|
|
|
$type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG_BLOB; |
188
|
|
|
|
189
|
|
|
$value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt4(\strlen($param)); |
190
|
|
|
$value .= $param; |
191
|
|
|
break; |
|
|
|
|
192
|
|
|
case 'NULL': |
|
|
|
|
193
|
|
|
$type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_NULL; |
194
|
|
|
$value = ''; |
195
|
|
|
break; |
|
|
|
|
196
|
|
|
default: |
|
|
|
|
197
|
|
|
throw new \Plasma\Exception('Unexpected type for binding parameter: '.\gettype($param)); |
198
|
|
|
break; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return array($unsigned, $type, $value); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Whether the sequence ID should be resetted. |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
function resetSequence(): bool { |
209
|
|
|
return false; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|