1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\DBAL\Driver\SQLAnywhere; |
||
6 | |||
7 | use Doctrine\DBAL\Driver\Statement; |
||
8 | use Doctrine\DBAL\Driver\StatementIterator; |
||
9 | use Doctrine\DBAL\Exception\GetVariableType; |
||
10 | use Doctrine\DBAL\Exception\InvalidColumnIndex; |
||
11 | use Doctrine\DBAL\FetchMode; |
||
12 | use Doctrine\DBAL\ParameterType; |
||
13 | use IteratorAggregate; |
||
14 | use ReflectionClass; |
||
15 | use ReflectionObject; |
||
16 | use stdClass; |
||
17 | use function array_key_exists; |
||
18 | use function assert; |
||
19 | use function count; |
||
20 | use function func_get_args; |
||
21 | use function is_array; |
||
22 | use function is_int; |
||
23 | use function is_object; |
||
24 | use function is_resource; |
||
25 | use function is_string; |
||
26 | use function sasql_fetch_array; |
||
27 | use function sasql_fetch_assoc; |
||
28 | use function sasql_fetch_object; |
||
29 | use function sasql_fetch_row; |
||
30 | use function sasql_prepare; |
||
31 | use function sasql_stmt_affected_rows; |
||
32 | use function sasql_stmt_bind_param_ex; |
||
33 | use function sasql_stmt_execute; |
||
34 | use function sasql_stmt_field_count; |
||
35 | use function sasql_stmt_reset; |
||
36 | use function sasql_stmt_result_metadata; |
||
37 | use function sprintf; |
||
38 | use const SASQL_BOTH; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
39 | |||
40 | /** |
||
41 | * SAP SQL Anywhere implementation of the Statement interface. |
||
42 | */ |
||
43 | final class SQLAnywhereStatement implements IteratorAggregate, Statement |
||
44 | { |
||
45 | /** @var resource The connection resource. */ |
||
46 | private $conn; |
||
47 | |||
48 | /** @var string Name of the default class to instantiate when fetching class instances. */ |
||
49 | private $defaultFetchClass = '\stdClass'; |
||
50 | |||
51 | /** @var mixed[] Constructor arguments for the default class to instantiate when fetching class instances. */ |
||
52 | private $defaultFetchClassCtorArgs = []; |
||
53 | |||
54 | /** @var int Default fetch mode to use. */ |
||
55 | private $defaultFetchMode = FetchMode::MIXED; |
||
56 | |||
57 | /** @var resource The result set resource to fetch. */ |
||
58 | private $result; |
||
59 | |||
60 | /** @var resource The prepared SQL statement to execute. */ |
||
61 | private $stmt; |
||
62 | |||
63 | /** @var mixed[] The references to bound parameter values. */ |
||
64 | private $boundValues = []; |
||
65 | |||
66 | /** |
||
67 | * Prepares given statement for given connection. |
||
68 | * |
||
69 | * @param resource $conn The connection resource to use. |
||
70 | * @param string $sql The SQL statement to prepare. |
||
71 | * |
||
72 | * @throws SQLAnywhereException |
||
73 | */ |
||
74 | public function __construct($conn, string $sql) |
||
75 | { |
||
76 | if (! is_resource($conn)) { |
||
77 | throw new SQLAnywhereException(sprintf( |
||
78 | 'Invalid SQL Anywhere connection resource, %s given.', |
||
79 | (new GetVariableType())->__invoke($conn) |
||
80 | )); |
||
81 | } |
||
82 | |||
83 | $this->conn = $conn; |
||
84 | $this->stmt = sasql_prepare($conn, $sql); |
||
85 | |||
86 | if (! is_resource($this->stmt)) { |
||
87 | throw SQLAnywhereException::fromSQLAnywhereError($conn); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | * |
||
94 | * @throws SQLAnywhereException |
||
95 | */ |
||
96 | public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null) : void |
||
97 | { |
||
98 | assert(is_int($param)); |
||
99 | |||
100 | switch ($type) { |
||
101 | case ParameterType::INTEGER: |
||
102 | case ParameterType::BOOLEAN: |
||
103 | $type = 'i'; |
||
104 | break; |
||
105 | |||
106 | case ParameterType::LARGE_OBJECT: |
||
107 | $type = 'b'; |
||
108 | break; |
||
109 | |||
110 | case ParameterType::NULL: |
||
111 | case ParameterType::STRING: |
||
112 | case ParameterType::BINARY: |
||
113 | $type = 's'; |
||
114 | break; |
||
115 | |||
116 | default: |
||
117 | throw new SQLAnywhereException(sprintf('Unknown type %d.', $type)); |
||
118 | } |
||
119 | |||
120 | $this->boundValues[$param] =& $variable; |
||
121 | |||
122 | if (! sasql_stmt_bind_param_ex($this->stmt, $param - 1, $variable, $type, $variable === null)) { |
||
123 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function bindValue($param, $value, int $type = ParameterType::STRING) : void |
||
131 | { |
||
132 | $this->bindParam($param, $value, $type); |
||
133 | } |
||
134 | |||
135 | public function closeCursor() : void |
||
136 | { |
||
137 | sasql_stmt_reset($this->stmt); |
||
138 | } |
||
139 | |||
140 | public function columnCount() : int |
||
141 | { |
||
142 | return sasql_stmt_field_count($this->stmt); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | * |
||
148 | * @throws SQLAnywhereException |
||
149 | */ |
||
150 | public function execute(?array $params = null) : void |
||
151 | { |
||
152 | if (is_array($params)) { |
||
153 | $hasZeroIndex = array_key_exists(0, $params); |
||
154 | |||
155 | foreach ($params as $key => $val) { |
||
156 | if ($hasZeroIndex && is_int($key)) { |
||
157 | $this->bindValue($key + 1, $val); |
||
158 | } else { |
||
159 | $this->bindValue($key, $val); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | if (! sasql_stmt_execute($this->stmt)) { |
||
165 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||
166 | } |
||
167 | |||
168 | $this->result = sasql_stmt_result_metadata($this->stmt); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | * |
||
174 | * @throws SQLAnywhereException |
||
175 | */ |
||
176 | public function fetch(?int $fetchMode = null, ...$args) |
||
177 | { |
||
178 | if (! is_resource($this->result)) { |
||
179 | return false; |
||
180 | } |
||
181 | |||
182 | $fetchMode = $fetchMode ?: $this->defaultFetchMode; |
||
183 | |||
184 | switch ($fetchMode) { |
||
185 | case FetchMode::COLUMN: |
||
186 | return $this->fetchColumn(); |
||
187 | |||
188 | case FetchMode::ASSOCIATIVE: |
||
189 | return sasql_fetch_assoc($this->result); |
||
190 | |||
191 | case FetchMode::MIXED: |
||
192 | return sasql_fetch_array($this->result, SASQL_BOTH); |
||
193 | |||
194 | case FetchMode::CUSTOM_OBJECT: |
||
195 | $className = $this->defaultFetchClass; |
||
196 | $ctorArgs = $this->defaultFetchClassCtorArgs; |
||
197 | |||
198 | if (count($args) > 0) { |
||
199 | $className = $args[0]; |
||
200 | $ctorArgs = $args[1] ?? []; |
||
201 | } |
||
202 | |||
203 | $result = sasql_fetch_object($this->result); |
||
204 | |||
205 | if ($result instanceof stdClass) { |
||
206 | $result = $this->castObject($result, $className, $ctorArgs); |
||
207 | } |
||
208 | |||
209 | return $result; |
||
210 | |||
211 | case FetchMode::NUMERIC: |
||
212 | return sasql_fetch_row($this->result); |
||
213 | |||
214 | case FetchMode::STANDARD_OBJECT: |
||
215 | return sasql_fetch_object($this->result); |
||
216 | |||
217 | default: |
||
218 | throw new SQLAnywhereException(sprintf('Fetch mode is not supported %d.', $fetchMode)); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function fetchAll(?int $fetchMode = null, ...$args) : array |
||
226 | { |
||
227 | $rows = []; |
||
228 | |||
229 | switch ($fetchMode) { |
||
230 | case FetchMode::CUSTOM_OBJECT: |
||
231 | while (($row = $this->fetch(...func_get_args())) !== false) { |
||
232 | $rows[] = $row; |
||
233 | } |
||
234 | |||
235 | break; |
||
236 | |||
237 | case FetchMode::COLUMN: |
||
238 | while (($row = $this->fetchColumn()) !== false) { |
||
239 | $rows[] = $row; |
||
240 | } |
||
241 | |||
242 | break; |
||
243 | |||
244 | default: |
||
245 | while (($row = $this->fetch($fetchMode)) !== false) { |
||
246 | $rows[] = $row; |
||
247 | } |
||
248 | } |
||
249 | |||
250 | return $rows; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function fetchColumn(int $columnIndex = 0) |
||
257 | { |
||
258 | $row = $this->fetch(FetchMode::NUMERIC); |
||
259 | |||
260 | if ($row === false) { |
||
261 | return false; |
||
262 | } |
||
263 | |||
264 | if (! array_key_exists($columnIndex, $row)) { |
||
265 | throw InvalidColumnIndex::new($columnIndex, count($row)); |
||
266 | } |
||
267 | |||
268 | return $row[$columnIndex]; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | public function getIterator() |
||
275 | { |
||
276 | return new StatementIterator($this); |
||
277 | } |
||
278 | |||
279 | public function rowCount() : int |
||
280 | { |
||
281 | return sasql_stmt_affected_rows($this->stmt); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function setFetchMode(int $fetchMode, ...$args) : void |
||
288 | { |
||
289 | $this->defaultFetchMode = $fetchMode; |
||
290 | |||
291 | if (isset($args[0])) { |
||
292 | $this->defaultFetchClass = $args[0]; |
||
293 | } |
||
294 | |||
295 | if (! isset($args[1])) { |
||
296 | return; |
||
297 | } |
||
298 | |||
299 | $this->defaultFetchClassCtorArgs = (array) $args[1]; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Casts a stdClass object to the given class name mapping its' properties. |
||
304 | * |
||
305 | * @param stdClass $sourceObject Object to cast from. |
||
306 | * @param string|object $destinationClass Name of the class or class instance to cast to. |
||
307 | * @param mixed[] $ctorArgs Arguments to use for constructing the destination class instance. |
||
308 | * |
||
309 | * @throws SQLAnywhereException |
||
310 | */ |
||
311 | private function castObject(stdClass $sourceObject, $destinationClass, array $ctorArgs = []) : object |
||
312 | { |
||
313 | if (! is_string($destinationClass)) { |
||
314 | if (! is_object($destinationClass)) { |
||
315 | throw new SQLAnywhereException(sprintf( |
||
316 | 'Destination class has to be of type string or object, "%s" given.', |
||
317 | (new GetVariableType())->__invoke($destinationClass) |
||
318 | )); |
||
319 | } |
||
320 | } else { |
||
321 | $destinationClass = new ReflectionClass($destinationClass); |
||
322 | $destinationClass = $destinationClass->newInstanceArgs($ctorArgs); |
||
323 | } |
||
324 | |||
325 | $sourceReflection = new ReflectionObject($sourceObject); |
||
326 | $destinationClassReflection = new ReflectionObject($destinationClass); |
||
327 | |||
328 | foreach ($sourceReflection->getProperties() as $sourceProperty) { |
||
329 | $sourceProperty->setAccessible(true); |
||
330 | |||
331 | $name = $sourceProperty->getName(); |
||
332 | $value = $sourceProperty->getValue($sourceObject); |
||
333 | |||
334 | if ($destinationClassReflection->hasProperty($name)) { |
||
335 | $destinationProperty = $destinationClassReflection->getProperty($name); |
||
336 | |||
337 | $destinationProperty->setAccessible(true); |
||
338 | $destinationProperty->setValue($destinationClass, $value); |
||
339 | } else { |
||
340 | $destinationClass->$name = $value; |
||
341 | } |
||
342 | } |
||
343 | |||
344 | return $destinationClass; |
||
345 | } |
||
346 | } |
||
347 |