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