1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plasma Driver MySQL component |
4
|
|
|
* Copyright 2018-2019 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
|
|
|
* @var \Plasma\ColumnDefinitionInterface[] |
36
|
|
|
*/ |
37
|
|
|
protected $paramsDef; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $cursor; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* @param \Plasma\DriverInterface $driver |
47
|
|
|
* @param mixed $id |
48
|
|
|
* @param string $query |
49
|
|
|
* @param array $params |
50
|
|
|
* @param \Plasma\ColumnDefinitionInterface[] $paramsDef |
51
|
|
|
* @param int $cursor |
52
|
|
|
*/ |
53
|
2 |
|
function __construct(\Plasma\DriverInterface $driver, $id, string $query, array $params, array $paramsDef, int $cursor = 0) { |
54
|
2 |
|
parent::__construct($driver, $query); |
55
|
|
|
|
56
|
2 |
|
$this->id = $id; |
57
|
2 |
|
$this->params = $params; |
58
|
2 |
|
$this->paramsDef = $paramsDef; |
59
|
2 |
|
$this->cursor = $cursor; |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the encoded message for writing to the database connection. |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
2 |
|
function getEncodedMessage(): string { |
67
|
2 |
|
$packet = \chr(static::COMMAND_ID); |
68
|
2 |
|
$packet .= \Plasma\BinaryBuffer::writeInt4($this->id); |
69
|
|
|
|
70
|
2 |
|
$packet .= \chr($this->cursor); |
71
|
2 |
|
$packet .= \Plasma\BinaryBuffer::writeInt4(1); // Iteration count is always 1 |
72
|
|
|
|
73
|
2 |
|
$bitmapOffset = \strlen($packet); |
74
|
2 |
|
$packet .= \str_repeat("\x00", ((\count($this->params) + 7) >> 3)); |
75
|
|
|
|
76
|
2 |
|
$bound = 0; |
77
|
|
|
|
78
|
2 |
|
$types = ''; |
79
|
2 |
|
$values = ''; |
80
|
|
|
|
81
|
2 |
|
foreach($this->params as $id => $param) { |
82
|
2 |
|
if($param === null) { |
83
|
|
|
$offset = $bitmapOffset + ($id >> 3); |
84
|
|
|
$packet[$offset] = $packet[$offset] | \chr((1 << ($id % 8))); |
85
|
|
|
} else { |
86
|
2 |
|
$bound = 1; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
try { |
90
|
2 |
|
$encode = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql') |
91
|
|
|
->encodeType($param, $this->paramsDef[$id]); |
92
|
|
|
|
93
|
|
|
$unsigned = $encode->isUnsigned(); |
94
|
|
|
$type = $encode->getDatabaseType(); |
95
|
|
|
$value = $encode->getValue(); |
96
|
2 |
|
} catch (\Plasma\Exception $e) { |
97
|
2 |
|
[ $unsigned, $type, $value ] = \Plasma\Drivers\MySQL\BinaryProtocolValues::encode($param); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$types .= \chr($type).($unsigned ? "\x80" : "\x00"); |
101
|
|
|
$values .= $value; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$packet .= \chr($bound); |
105
|
|
|
|
106
|
|
|
if($bound) { |
107
|
|
|
$packet .= $types; |
108
|
|
|
$packet .= $values; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $packet; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Whether the sequence ID should be resetted. |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
1 |
|
function resetSequence(): bool { |
119
|
1 |
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Sends the next received value into the command. |
124
|
|
|
* @param mixed $value |
125
|
|
|
* @return void |
126
|
|
|
* @throws \Plasma\Exception |
127
|
|
|
*/ |
128
|
|
|
function onNext($value): void { |
129
|
|
|
if($this->cursor > 0 && $this->fieldsCount > 0 && $this->fieldsCount <= \count($this->fields)) { |
130
|
|
|
if(!($value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage)) { |
131
|
|
|
throw new \Plasma\Exception('Requested a cursor, but received row instead'); |
132
|
|
|
} elseif(($value->statusFlags & \Plasma\Drivers\MySQL\StatusFlags::SERVER_STATUS_CURSOR_EXISTS) === 0) { |
133
|
|
|
throw new \Plasma\Exception('Requested a cursor, but did not receive one'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
parent::onNext($value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Parses the binary resultset row and returns the row. |
142
|
|
|
* @param \Plasma\BinaryBuffer $buffer |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
protected function parseResultsetRow(\Plasma\BinaryBuffer $buffer): array { |
146
|
|
|
$buffer->read(1); // remove packet header |
147
|
|
|
|
148
|
|
|
$nullRow = array(); |
149
|
|
|
$i = 0; |
150
|
|
|
|
151
|
|
|
/** @var \Plasma\ColumnDefinitionInterface $column */ |
152
|
|
|
foreach($this->fields as $column) { // Handle NULL-bitmap |
153
|
|
|
if((\ord($buffer[(($i + 2) >> 3)]) & (1 << (($i + 2) % 8))) !== 0) { |
154
|
|
|
$nullRow[$column->getName()] = null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$i++; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$buffer->read(((\count($this->fields) + 9) >> 3)); // Remove NULL-bitmap |
161
|
|
|
$row = array(); |
162
|
|
|
|
163
|
|
|
/** @var \Plasma\ColumnDefinitionInterface $column */ |
164
|
|
|
foreach($this->fields as $column) { |
165
|
|
|
if(\array_key_exists($column->getName(), $nullRow)) { |
166
|
|
|
$row[$column->getName()] = null; |
167
|
|
|
continue; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
try { |
171
|
|
|
$value = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql') |
172
|
|
|
->decodeType($column->getType(), $buffer) |
173
|
|
|
->getValue(); |
174
|
|
|
} catch (\Plasma\Exception $e) { |
175
|
|
|
$value = \Plasma\Drivers\MySQL\BinaryProtocolValues::decode($column, $buffer); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$row[$column->getName()] = $value; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $row; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|