| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL\Driver\Mysqli;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\Driver\Exception;
|
|
|
6 |
use Doctrine\DBAL\Driver\Exception\UnknownParameterType;
|
|
|
7 |
use Doctrine\DBAL\Driver\Mysqli\Exception\FailedReadingStreamOffset;
|
|
|
8 |
use Doctrine\DBAL\Driver\Mysqli\Exception\NonStreamResourceUsedAsLargeObject;
|
|
|
9 |
use Doctrine\DBAL\Driver\Mysqli\Exception\StatementError;
|
|
|
10 |
use Doctrine\DBAL\Driver\Result as ResultInterface;
|
|
|
11 |
use Doctrine\DBAL\Driver\Statement as StatementInterface;
|
|
|
12 |
use Doctrine\DBAL\ParameterType;
|
|
|
13 |
use Doctrine\Deprecations\Deprecation;
|
|
|
14 |
use mysqli_sql_exception;
|
|
|
15 |
use mysqli_stmt;
|
|
|
16 |
|
|
|
17 |
use function array_fill;
|
|
|
18 |
use function assert;
|
|
|
19 |
use function count;
|
|
|
20 |
use function feof;
|
|
|
21 |
use function fread;
|
|
|
22 |
use function func_num_args;
|
|
|
23 |
use function get_resource_type;
|
|
|
24 |
use function is_int;
|
|
|
25 |
use function is_resource;
|
|
|
26 |
use function str_repeat;
|
|
|
27 |
|
|
|
28 |
final class Statement implements StatementInterface
|
|
|
29 |
{
|
|
|
30 |
/** @var string[] */
|
|
|
31 |
private static array $paramTypeMap = [
|
|
|
32 |
ParameterType::ASCII => 's',
|
|
|
33 |
ParameterType::STRING => 's',
|
|
|
34 |
ParameterType::BINARY => 's',
|
|
|
35 |
ParameterType::BOOLEAN => 'i',
|
|
|
36 |
ParameterType::NULL => 's',
|
|
|
37 |
ParameterType::INTEGER => 'i',
|
|
|
38 |
ParameterType::LARGE_OBJECT => 'b',
|
|
|
39 |
];
|
|
|
40 |
|
|
|
41 |
private mysqli_stmt $stmt;
|
|
|
42 |
|
|
|
43 |
/** @var mixed[] */
|
|
|
44 |
private array $boundValues;
|
|
|
45 |
|
|
|
46 |
private string $types;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Contains ref values for bindValue().
|
|
|
50 |
*
|
|
|
51 |
* @var mixed[]
|
|
|
52 |
*/
|
|
|
53 |
private array $values = [];
|
|
|
54 |
|
|
|
55 |
/** @internal The statement can be only instantiated by its driver connection. */
|
|
|
56 |
public function __construct(mysqli_stmt $stmt)
|
|
|
57 |
{
|
|
|
58 |
$this->stmt = $stmt;
|
|
|
59 |
|
|
|
60 |
$paramCount = $this->stmt->param_count;
|
|
|
61 |
$this->types = str_repeat('s', $paramCount);
|
|
|
62 |
$this->boundValues = array_fill(1, $paramCount, null);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* {@inheritdoc}
|
|
|
67 |
*
|
|
|
68 |
* @deprecated Use {@see bindValue()} instead.
|
|
|
69 |
*/
|
|
|
70 |
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
|
|
|
71 |
{
|
|
|
72 |
Deprecation::trigger(
|
|
|
73 |
'doctrine/dbal',
|
|
|
74 |
'https://github.com/doctrine/dbal/pull/5563',
|
|
|
75 |
'%s is deprecated. Use bindValue() instead.',
|
|
|
76 |
__METHOD__,
|
|
|
77 |
);
|
|
|
78 |
|
|
|
79 |
assert(is_int($param));
|
|
|
80 |
|
|
|
81 |
if (func_num_args() < 3) {
|
|
|
82 |
Deprecation::trigger(
|
|
|
83 |
'doctrine/dbal',
|
|
|
84 |
'https://github.com/doctrine/dbal/pull/5558',
|
|
|
85 |
'Not passing $type to Statement::bindParam() is deprecated.'
|
|
|
86 |
. ' Pass the type corresponding to the parameter being bound.',
|
|
|
87 |
);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
if (! isset(self::$paramTypeMap[$type])) {
|
|
|
91 |
throw UnknownParameterType::new($type);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$this->boundValues[$param] =& $variable;
|
|
|
95 |
$this->types[$param - 1] = self::$paramTypeMap[$type];
|
|
|
96 |
|
|
|
97 |
return true;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* {@inheritdoc}
|
|
|
102 |
*/
|
|
|
103 |
public function bindValue($param, $value, $type = ParameterType::STRING): bool
|
|
|
104 |
{
|
|
|
105 |
assert(is_int($param));
|
|
|
106 |
|
|
|
107 |
if (func_num_args() < 3) {
|
|
|
108 |
Deprecation::trigger(
|
|
|
109 |
'doctrine/dbal',
|
|
|
110 |
'https://github.com/doctrine/dbal/pull/5558',
|
|
|
111 |
'Not passing $type to Statement::bindValue() is deprecated.'
|
|
|
112 |
. ' Pass the type corresponding to the parameter being bound.',
|
|
|
113 |
);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if (! isset(self::$paramTypeMap[$type])) {
|
|
|
117 |
throw UnknownParameterType::new($type);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
$this->values[$param] = $value;
|
|
|
121 |
$this->boundValues[$param] =& $this->values[$param];
|
|
|
122 |
$this->types[$param - 1] = self::$paramTypeMap[$type];
|
|
|
123 |
|
|
|
124 |
return true;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* {@inheritdoc}
|
|
|
129 |
*/
|
|
|
130 |
public function execute($params = null): ResultInterface
|
|
|
131 |
{
|
|
|
132 |
if ($params !== null) {
|
|
|
133 |
Deprecation::trigger(
|
|
|
134 |
'doctrine/dbal',
|
|
|
135 |
'https://github.com/doctrine/dbal/pull/5556',
|
|
|
136 |
'Passing $params to Statement::execute() is deprecated. Bind parameters using'
|
|
|
137 |
. ' Statement::bindParam() or Statement::bindValue() instead.',
|
|
|
138 |
);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
if ($params !== null && count($params) > 0) {
|
|
|
142 |
if (! $this->bindUntypedValues($params)) {
|
|
|
143 |
throw StatementError::new($this->stmt);
|
|
|
144 |
}
|
|
|
145 |
} elseif (count($this->boundValues) > 0) {
|
|
|
146 |
$this->bindTypedParameters();
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
try {
|
|
|
150 |
$result = $this->stmt->execute();
|
|
|
151 |
} catch (mysqli_sql_exception $e) {
|
|
|
152 |
throw StatementError::upcast($e);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
if (! $result) {
|
|
|
156 |
throw StatementError::new($this->stmt);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
return new Result($this->stmt);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Binds parameters with known types previously bound to the statement
|
|
|
164 |
*
|
|
|
165 |
* @throws Exception
|
|
|
166 |
*/
|
|
|
167 |
private function bindTypedParameters(): void
|
|
|
168 |
{
|
|
|
169 |
$streams = $values = [];
|
|
|
170 |
$types = $this->types;
|
|
|
171 |
|
|
|
172 |
foreach ($this->boundValues as $parameter => $value) {
|
|
|
173 |
assert(is_int($parameter));
|
|
|
174 |
|
|
|
175 |
if (! isset($types[$parameter - 1])) {
|
|
|
176 |
$types[$parameter - 1] = self::$paramTypeMap[ParameterType::STRING];
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
if ($types[$parameter - 1] === self::$paramTypeMap[ParameterType::LARGE_OBJECT]) {
|
|
|
180 |
if (is_resource($value)) {
|
|
|
181 |
if (get_resource_type($value) !== 'stream') {
|
|
|
182 |
throw NonStreamResourceUsedAsLargeObject::new($parameter);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
$streams[$parameter] = $value;
|
|
|
186 |
$values[$parameter] = null;
|
|
|
187 |
continue;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
$types[$parameter - 1] = self::$paramTypeMap[ParameterType::STRING];
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
$values[$parameter] = $value;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
if (! $this->stmt->bind_param($types, ...$values)) {
|
|
|
197 |
throw StatementError::new($this->stmt);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
$this->sendLongData($streams);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Handle $this->_longData after regular query parameters have been bound
|
|
|
205 |
*
|
|
|
206 |
* @param array<int, resource> $streams
|
|
|
207 |
*
|
|
|
208 |
* @throws Exception
|
|
|
209 |
*/
|
|
|
210 |
private function sendLongData(array $streams): void
|
|
|
211 |
{
|
|
|
212 |
foreach ($streams as $paramNr => $stream) {
|
|
|
213 |
while (! feof($stream)) {
|
|
|
214 |
$chunk = fread($stream, 8192);
|
|
|
215 |
|
|
|
216 |
if ($chunk === false) {
|
|
|
217 |
throw FailedReadingStreamOffset::new($paramNr);
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
if (! $this->stmt->send_long_data($paramNr - 1, $chunk)) {
|
|
|
221 |
throw StatementError::new($this->stmt);
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Binds a array of values to bound parameters.
|
|
|
229 |
*
|
|
|
230 |
* @param mixed[] $values
|
|
|
231 |
*/
|
|
|
232 |
private function bindUntypedValues(array $values): bool
|
|
|
233 |
{
|
|
|
234 |
return $this->stmt->bind_param(str_repeat('s', count($values)), ...$values);
|
|
|
235 |
}
|
|
|
236 |
}
|