1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Driver\SQLAnywhere; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Driver\Statement; |
23
|
|
|
use Doctrine\DBAL\Driver\StatementIterator; |
24
|
|
|
use Doctrine\DBAL\FetchMode; |
25
|
|
|
use Doctrine\DBAL\ParameterType; |
26
|
|
|
use IteratorAggregate; |
27
|
|
|
use const SASQL_BOTH; |
|
|
|
|
28
|
|
|
use function array_key_exists; |
29
|
|
|
use function call_user_func_array; |
30
|
|
|
use function func_get_args; |
31
|
|
|
use function func_num_args; |
32
|
|
|
use function gettype; |
33
|
|
|
use function is_array; |
34
|
|
|
use function is_numeric; |
35
|
|
|
use function is_object; |
36
|
|
|
use function is_resource; |
37
|
|
|
use function is_string; |
38
|
|
|
use function sasql_fetch_array; |
|
|
|
|
39
|
|
|
use function sasql_fetch_assoc; |
|
|
|
|
40
|
|
|
use function sasql_fetch_object; |
|
|
|
|
41
|
|
|
use function sasql_fetch_row; |
|
|
|
|
42
|
|
|
use function sasql_prepare; |
|
|
|
|
43
|
|
|
use function sasql_stmt_affected_rows; |
|
|
|
|
44
|
|
|
use function sasql_stmt_bind_param_ex; |
|
|
|
|
45
|
|
|
use function sasql_stmt_errno; |
|
|
|
|
46
|
|
|
use function sasql_stmt_error; |
|
|
|
|
47
|
|
|
use function sasql_stmt_execute; |
|
|
|
|
48
|
|
|
use function sasql_stmt_field_count; |
|
|
|
|
49
|
|
|
use function sasql_stmt_reset; |
|
|
|
|
50
|
|
|
use function sasql_stmt_result_metadata; |
|
|
|
|
51
|
|
|
use function sprintf; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* SAP SQL Anywhere implementation of the Statement interface. |
55
|
|
|
* |
56
|
|
|
* @author Steve Müller <[email protected]> |
57
|
|
|
* @link www.doctrine-project.org |
58
|
|
|
* @since 2.5 |
59
|
|
|
*/ |
60
|
|
|
class SQLAnywhereStatement implements IteratorAggregate, Statement |
61
|
|
|
{ |
62
|
|
|
/** |
63
|
|
|
* @var resource The connection resource. |
64
|
|
|
*/ |
65
|
|
|
private $conn; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string Name of the default class to instantiate when fetching class instances. |
69
|
|
|
*/ |
70
|
|
|
private $defaultFetchClass = '\stdClass'; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string Constructor arguments for the default class to instantiate when fetching class instances. |
74
|
|
|
*/ |
75
|
|
|
private $defaultFetchClassCtorArgs = []; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var int Default fetch mode to use. |
79
|
|
|
*/ |
80
|
|
|
private $defaultFetchMode = FetchMode::MIXED; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var resource The result set resource to fetch. |
84
|
|
|
*/ |
85
|
|
|
private $result; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var resource The prepared SQL statement to execute. |
89
|
|
|
*/ |
90
|
|
|
private $stmt; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Constructor. |
94
|
|
|
* |
95
|
|
|
* Prepares given statement for given connection. |
96
|
|
|
* |
97
|
|
|
* @param resource $conn The connection resource to use. |
98
|
|
|
* @param string $sql The SQL statement to prepare. |
99
|
|
|
* |
100
|
|
|
* @throws SQLAnywhereException |
101
|
|
|
*/ |
102
|
|
|
public function __construct($conn, $sql) |
103
|
|
|
{ |
104
|
|
|
if ( ! is_resource($conn)) { |
105
|
|
|
throw new SQLAnywhereException('Invalid SQL Anywhere connection resource: ' . $conn); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->conn = $conn; |
109
|
|
|
$this->stmt = sasql_prepare($conn, $sql); |
|
|
|
|
110
|
|
|
|
111
|
|
|
if ( ! is_resource($this->stmt)) { |
112
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($conn); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
* |
119
|
|
|
* @throws SQLAnywhereException |
120
|
|
|
*/ |
121
|
|
|
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) |
122
|
|
|
{ |
123
|
|
|
switch ($type) { |
124
|
|
|
case ParameterType::INTEGER: |
125
|
|
|
case ParameterType::BOOLEAN: |
126
|
|
|
$type = 'i'; |
127
|
|
|
break; |
128
|
|
|
|
129
|
|
|
case ParameterType::LARGE_OBJECT: |
130
|
|
|
$type = 'b'; |
131
|
|
|
break; |
132
|
|
|
|
133
|
|
|
case ParameterType::NULL: |
134
|
|
|
case ParameterType::STRING: |
135
|
|
|
$type = 's'; |
136
|
|
|
break; |
137
|
|
|
|
138
|
|
|
default: |
139
|
|
|
throw new SQLAnywhereException('Unknown type: ' . $type); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ( ! sasql_stmt_bind_param_ex($this->stmt, $column - 1, $variable, $type, $variable === null)) { |
|
|
|
|
143
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function bindValue($param, $value, $type = ParameterType::STRING) |
153
|
|
|
{ |
154
|
|
|
return $this->bindParam($param, $value, $type); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
* |
160
|
|
|
* @throws SQLAnywhereException |
161
|
|
|
*/ |
162
|
|
|
public function closeCursor() |
163
|
|
|
{ |
164
|
|
|
if (!sasql_stmt_reset($this->stmt)) { |
|
|
|
|
165
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function columnCount() |
175
|
|
|
{ |
176
|
|
|
return sasql_stmt_field_count($this->stmt); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
|
|
public function errorCode() |
183
|
|
|
{ |
184
|
|
|
return sasql_stmt_errno($this->stmt); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
|
|
public function errorInfo() |
191
|
|
|
{ |
192
|
|
|
return sasql_stmt_error($this->stmt); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
* |
198
|
|
|
* @throws SQLAnywhereException |
199
|
|
|
*/ |
200
|
|
|
public function execute($params = null) |
201
|
|
|
{ |
202
|
|
|
if (is_array($params)) { |
203
|
|
|
$hasZeroIndex = array_key_exists(0, $params); |
204
|
|
|
|
205
|
|
|
foreach ($params as $key => $val) { |
206
|
|
|
$key = ($hasZeroIndex && is_numeric($key)) ? $key + 1 : $key; |
207
|
|
|
|
208
|
|
|
$this->bindValue($key, $val); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if ( ! sasql_stmt_execute($this->stmt)) { |
|
|
|
|
213
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$this->result = sasql_stmt_result_metadata($this->stmt); |
|
|
|
|
217
|
|
|
|
218
|
|
|
return true; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritdoc} |
223
|
|
|
* |
224
|
|
|
* @throws SQLAnywhereException |
225
|
|
|
*/ |
226
|
|
|
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
227
|
|
|
{ |
228
|
|
|
if ( ! is_resource($this->result)) { |
229
|
|
|
return false; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$fetchMode = $fetchMode ?: $this->defaultFetchMode; |
233
|
|
|
|
234
|
|
|
switch ($fetchMode) { |
235
|
|
|
case FetchMode::COLUMN: |
236
|
|
|
return $this->fetchColumn(); |
237
|
|
|
|
238
|
|
|
case FetchMode::ASSOCIATIVE: |
239
|
|
|
return sasql_fetch_assoc($this->result); |
|
|
|
|
240
|
|
|
|
241
|
|
|
case FetchMode::MIXED: |
242
|
|
|
return sasql_fetch_array($this->result, SASQL_BOTH); |
|
|
|
|
243
|
|
|
|
244
|
|
|
case FetchMode::CUSTOM_OBJECT: |
245
|
|
|
$className = $this->defaultFetchClass; |
246
|
|
|
$ctorArgs = $this->defaultFetchClassCtorArgs; |
247
|
|
|
|
248
|
|
|
if (func_num_args() >= 2) { |
249
|
|
|
$args = func_get_args(); |
250
|
|
|
$className = $args[1]; |
251
|
|
|
$ctorArgs = $args[2] ?? []; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$result = sasql_fetch_object($this->result); |
|
|
|
|
255
|
|
|
|
256
|
|
|
if ($result instanceof \stdClass) { |
257
|
|
|
$result = $this->castObject($result, $className, $ctorArgs); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $result; |
261
|
|
|
|
262
|
|
|
case FetchMode::NUMERIC: |
|
|
|
|
263
|
|
|
return sasql_fetch_row($this->result); |
|
|
|
|
264
|
|
|
|
265
|
|
|
case FetchMode::STANDARD_OBJECT: |
|
|
|
|
266
|
|
|
return sasql_fetch_object($this->result); |
267
|
|
|
|
268
|
|
|
default: |
269
|
|
|
throw new SQLAnywhereException('Fetch mode is not supported: ' . $fetchMode); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* {@inheritdoc} |
275
|
|
|
*/ |
276
|
|
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
277
|
|
|
{ |
278
|
|
|
$rows = []; |
279
|
|
|
|
280
|
|
|
switch ($fetchMode) { |
281
|
|
|
case FetchMode::CUSTOM_OBJECT: |
282
|
|
|
while ($row = call_user_func_array([$this, 'fetch'], func_get_args())) { |
283
|
|
|
$rows[] = $row; |
284
|
|
|
} |
285
|
|
|
break; |
286
|
|
|
|
287
|
|
|
case FetchMode::COLUMN: |
288
|
|
|
while ($row = $this->fetchColumn()) { |
289
|
|
|
$rows[] = $row; |
290
|
|
|
} |
291
|
|
|
break; |
292
|
|
|
|
293
|
|
|
default: |
294
|
|
|
while ($row = $this->fetch($fetchMode)) { |
295
|
|
|
$rows[] = $row; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $rows; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* {@inheritdoc} |
304
|
|
|
*/ |
305
|
|
|
public function fetchColumn($columnIndex = 0) |
306
|
|
|
{ |
307
|
|
|
$row = $this->fetch(FetchMode::NUMERIC); |
308
|
|
|
|
309
|
|
|
if (false === $row) { |
310
|
|
|
return false; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return $row[$columnIndex] ?? null; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* {@inheritdoc} |
318
|
|
|
*/ |
319
|
2 |
|
public function getIterator() |
320
|
|
|
{ |
321
|
2 |
|
return new StatementIterator($this); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* {@inheritdoc} |
326
|
|
|
*/ |
327
|
|
|
public function rowCount() |
328
|
|
|
{ |
329
|
|
|
return sasql_stmt_affected_rows($this->stmt); |
|
|
|
|
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* {@inheritdoc} |
334
|
|
|
*/ |
335
|
|
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
336
|
|
|
{ |
337
|
|
|
$this->defaultFetchMode = $fetchMode; |
338
|
|
|
$this->defaultFetchClass = $arg2 ? $arg2 : $this->defaultFetchClass; |
339
|
|
|
$this->defaultFetchClassCtorArgs = $arg3 ? (array) $arg3 : $this->defaultFetchClassCtorArgs; |
|
|
|
|
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Casts a stdClass object to the given class name mapping its' properties. |
344
|
|
|
* |
345
|
|
|
* @param \stdClass $sourceObject Object to cast from. |
346
|
|
|
* @param string|object $destinationClass Name of the class or class instance to cast to. |
347
|
|
|
* @param array $ctorArgs Arguments to use for constructing the destination class instance. |
348
|
|
|
* |
349
|
|
|
* @return object |
350
|
|
|
* |
351
|
|
|
* @throws SQLAnywhereException |
352
|
|
|
*/ |
353
|
|
|
private function castObject(\stdClass $sourceObject, $destinationClass, array $ctorArgs = []) |
354
|
|
|
{ |
355
|
|
|
if ( ! is_string($destinationClass)) { |
356
|
|
|
if ( ! is_object($destinationClass)) { |
357
|
|
|
throw new SQLAnywhereException(sprintf( |
358
|
|
|
'Destination class has to be of type string or object, %s given.', gettype($destinationClass) |
359
|
|
|
)); |
360
|
|
|
} |
361
|
|
|
} else { |
362
|
|
|
$destinationClass = new \ReflectionClass($destinationClass); |
363
|
|
|
$destinationClass = $destinationClass->newInstanceArgs($ctorArgs); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
$sourceReflection = new \ReflectionObject($sourceObject); |
367
|
|
|
$destinationClassReflection = new \ReflectionObject($destinationClass); |
368
|
|
|
|
369
|
|
|
foreach ($sourceReflection->getProperties() as $sourceProperty) { |
370
|
|
|
$sourceProperty->setAccessible(true); |
371
|
|
|
|
372
|
|
|
$name = $sourceProperty->getName(); |
373
|
|
|
$value = $sourceProperty->getValue($sourceObject); |
374
|
|
|
|
375
|
|
|
if ($destinationClassReflection->hasProperty($name)) { |
376
|
|
|
$destinationProperty = $destinationClassReflection->getProperty($name); |
377
|
|
|
|
378
|
|
|
$destinationProperty->setAccessible(true); |
379
|
|
|
$destinationProperty->setValue($destinationClass, $value); |
380
|
|
|
} else { |
381
|
|
|
$destinationClass->$name = $value; |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
return $destinationClass; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|