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
|
|
|
* Constructor. |
41
|
|
|
* @param \Plasma\DriverInterface $driver |
42
|
|
|
* @param mixed $id |
43
|
|
|
* @param string $query |
44
|
|
|
* @param array $params |
45
|
|
|
* @param \Plasma\ColumnDefinitionInterface[] $paramsDef |
46
|
|
|
*/ |
47
|
1 |
|
function __construct(\Plasma\DriverInterface $driver, $id, string $query, array $params, array $paramsDef) { |
48
|
1 |
|
parent::__construct($driver, $query); |
49
|
|
|
|
50
|
1 |
|
$this->id = $id; |
51
|
1 |
|
$this->params = $params; |
52
|
1 |
|
$this->paramsDef = $paramsDef; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the encoded message for writing to the database connection. |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
function getEncodedMessage(): string { |
60
|
|
|
$packet = \chr(static::COMMAND_ID); |
61
|
|
|
$packet .= \Plasma\BinaryBuffer::writeInt4($this->id); |
62
|
|
|
|
63
|
|
|
$packet .= "\x00"; // Cursor type flag |
64
|
|
|
$packet .= \Plasma\BinaryBuffer::writeInt4(1); // Iteration count is always 1 |
65
|
|
|
|
66
|
|
|
$bitmapOffset = \strlen($packet); |
67
|
|
|
$packet .= \str_repeat("\x00", ((\count($this->params) + 7) >> 3)); |
68
|
|
|
|
69
|
|
|
$bound = 0; |
70
|
|
|
|
71
|
|
|
$types = ''; |
72
|
|
|
$values = ''; |
73
|
|
|
|
74
|
|
|
foreach($this->params as $id => $param) { |
75
|
|
|
if($param === null) { |
76
|
|
|
$offset = $bitmapOffset + ($id >> 3); |
77
|
|
|
$packet[$offset] = $packet[$offset] | \chr((1 << ($id % 8))); |
78
|
|
|
} else { |
79
|
|
|
$bound = 1; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
$encode = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql') |
84
|
|
|
->encodeType($param, $this->paramsDef[$id]); |
85
|
|
|
|
86
|
|
|
$unsigned = $encode->isUnsigned(); |
87
|
|
|
$type = $encode->getSQLType(); |
88
|
|
|
$value = $encode->getValue(); |
89
|
|
|
} catch (\Plasma\Exception $e) { |
90
|
|
|
[ $unsigned, $type, $value ] = \Plasma\Drivers\MySQL\BinaryProtocolValues::encode($param); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$types .= \chr($type).($unsigned ? "\x80" : "\x00"); |
94
|
|
|
$values .= $value; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$packet .= \chr($bound); |
98
|
|
|
|
99
|
|
|
if($bound) { |
100
|
|
|
$packet .= $types; |
101
|
|
|
$packet .= $values; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $packet; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Whether the sequence ID should be resetted. |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
function resetSequence(): bool { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Parses the binary resultset row and returns the row. |
117
|
|
|
* @param \Plasma\BinaryBuffer $buffer |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
protected function parseResultsetRow(\Plasma\BinaryBuffer $buffer): array { |
121
|
|
|
$buffer->read(1); // remove packet header |
122
|
|
|
|
123
|
|
|
$nullRow = array(); |
124
|
|
|
$i = 0; |
125
|
|
|
|
126
|
|
|
/** @var \Plasma\ColumnDefinitionInterface $column */ |
127
|
|
|
foreach($this->fields as $column) { // Handle NULL-bitmap |
128
|
|
|
if((\ord($buffer[(($i + 2) >> 3)]) & (1 << (($i + 2) % 8))) !== 0) { |
129
|
|
|
$nullRow[$column->getName()] = null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$i++; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$buffer->read(((\count($this->fields) + 9) >> 3)); // Remove NULL-bitmap |
136
|
|
|
$row = array(); |
137
|
|
|
|
138
|
|
|
/** @var \Plasma\ColumnDefinitionInterface $column */ |
139
|
|
|
foreach($this->fields as $column) { |
140
|
|
|
if(\array_key_exists($column->getName(), $nullRow)) { |
141
|
|
|
$row[$column->getName()] = null; |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
try { |
146
|
|
|
$value = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql') |
147
|
|
|
->decodeType($column->getType(), $buffer) |
148
|
|
|
->getValue(); |
149
|
|
|
} catch (\Plasma\Exception $e) { |
150
|
|
|
$value = \Plasma\Drivers\MySQL\BinaryProtocolValues::decode($column, $buffer); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$row[$column->getName()] = $value; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $row; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|