1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver\OCI8; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\DBALException; |
8
|
|
|
use Doctrine\DBAL\Driver\Statement; |
9
|
|
|
use Doctrine\DBAL\Driver\StatementIterator; |
10
|
|
|
use Doctrine\DBAL\FetchMode; |
11
|
|
|
use Doctrine\DBAL\ParameterType; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use IteratorAggregate; |
14
|
|
|
use const OCI_ASSOC; |
15
|
|
|
use const OCI_B_BIN; |
16
|
|
|
use const OCI_B_BLOB; |
17
|
|
|
use const OCI_BOTH; |
18
|
|
|
use const OCI_D_LOB; |
19
|
|
|
use const OCI_FETCHSTATEMENT_BY_COLUMN; |
20
|
|
|
use const OCI_FETCHSTATEMENT_BY_ROW; |
21
|
|
|
use const OCI_NUM; |
22
|
|
|
use const OCI_RETURN_LOBS; |
23
|
|
|
use const OCI_RETURN_NULLS; |
24
|
|
|
use const OCI_TEMP_BLOB; |
25
|
|
|
use const PREG_OFFSET_CAPTURE; |
26
|
|
|
use const SQLT_CHR; |
27
|
|
|
use function array_key_exists; |
28
|
|
|
use function assert; |
29
|
|
|
use function count; |
30
|
|
|
use function implode; |
31
|
|
|
use function is_int; |
32
|
|
|
use function is_resource; |
33
|
|
|
use function oci_bind_by_name; |
34
|
|
|
use function oci_cancel; |
35
|
|
|
use function oci_error; |
36
|
|
|
use function oci_execute; |
37
|
|
|
use function oci_fetch_all; |
38
|
|
|
use function oci_fetch_array; |
39
|
|
|
use function oci_fetch_object; |
40
|
|
|
use function oci_new_descriptor; |
41
|
|
|
use function oci_num_fields; |
42
|
|
|
use function oci_num_rows; |
43
|
|
|
use function oci_parse; |
44
|
|
|
use function preg_match; |
45
|
|
|
use function preg_quote; |
46
|
|
|
use function sprintf; |
47
|
|
|
use function substr; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The OCI8 implementation of the Statement interface. |
51
|
|
|
*/ |
52
|
|
|
class OCI8Statement implements IteratorAggregate, Statement |
53
|
|
|
{ |
54
|
|
|
/** @var resource */ |
55
|
|
|
protected $_dbh; |
56
|
|
|
|
57
|
|
|
/** @var resource */ |
58
|
|
|
protected $_sth; |
59
|
|
|
|
60
|
|
|
/** @var OCI8Connection */ |
61
|
|
|
protected $_conn; |
62
|
|
|
|
63
|
|
|
/** @var string */ |
64
|
|
|
protected static $_PARAM = ':param'; |
65
|
|
|
|
66
|
|
|
/** @var int[] */ |
67
|
|
|
protected static $fetchModeMap = [ |
68
|
|
|
FetchMode::MIXED => OCI_BOTH, |
69
|
|
|
FetchMode::ASSOCIATIVE => OCI_ASSOC, |
70
|
|
|
FetchMode::NUMERIC => OCI_NUM, |
71
|
|
|
FetchMode::COLUMN => OCI_NUM, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** @var int */ |
75
|
|
|
protected $_defaultFetchMode = FetchMode::MIXED; |
76
|
|
|
|
77
|
|
|
/** @var string[] */ |
78
|
|
|
protected $_paramMap = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Holds references to bound parameter values. |
82
|
|
|
* |
83
|
|
|
* This is a new requirement for PHP7's oci8 extension that prevents bound values from being garbage collected. |
84
|
|
|
* |
85
|
|
|
* @var mixed[] |
86
|
|
|
*/ |
87
|
|
|
private $boundValues = []; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Indicates whether the statement is in the state when fetching results is possible |
91
|
|
|
* |
92
|
|
|
* @var bool |
93
|
|
|
*/ |
94
|
|
|
private $result = false; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Creates a new OCI8Statement that uses the given connection handle and SQL statement. |
98
|
|
|
* |
99
|
|
|
* @param resource $dbh The connection handle. |
100
|
|
|
* @param string $query The SQL query. |
101
|
|
|
*/ |
102
|
|
|
public function __construct($dbh, $query, OCI8Connection $conn) |
103
|
|
|
{ |
104
|
|
|
[$query, $paramMap] = self::convertPositionalToNamedPlaceholders($query); |
105
|
|
|
|
106
|
|
|
$stmt = oci_parse($dbh, $query); |
107
|
|
|
assert(is_resource($stmt)); |
108
|
|
|
|
109
|
|
|
$this->_sth = $stmt; |
110
|
|
|
$this->_dbh = $dbh; |
111
|
|
|
$this->_paramMap = $paramMap; |
112
|
|
|
$this->_conn = $conn; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Converts positional (?) into named placeholders (:param<num>). |
117
|
|
|
* |
118
|
|
|
* Oracle does not support positional parameters, hence this method converts all |
119
|
|
|
* positional parameters into artificially named parameters. Note that this conversion |
120
|
|
|
* is not perfect. All question marks (?) in the original statement are treated as |
121
|
|
|
* placeholders and converted to a named parameter. |
122
|
|
|
* |
123
|
|
|
* The algorithm uses a state machine with two possible states: InLiteral and NotInLiteral. |
124
|
|
|
* Question marks inside literal strings are therefore handled correctly by this method. |
125
|
|
|
* This comes at a cost, the whole sql statement has to be looped over. |
126
|
|
|
* |
127
|
|
|
* @param string $statement The SQL statement to convert. |
128
|
|
|
* |
129
|
|
|
* @return mixed[] [0] => the statement value (string), [1] => the paramMap value (array). |
130
|
|
|
* |
131
|
|
|
* @throws OCI8Exception |
132
|
|
|
* |
133
|
|
|
* @todo extract into utility class in Doctrine\DBAL\Util namespace |
134
|
|
|
* @todo review and test for lost spaces. we experienced missing spaces with oci8 in some sql statements. |
135
|
|
|
*/ |
136
|
168 |
|
public static function convertPositionalToNamedPlaceholders($statement) |
137
|
|
|
{ |
138
|
168 |
|
$fragmentOffset = $tokenOffset = 0; |
139
|
168 |
|
$fragments = $paramMap = []; |
140
|
168 |
|
$currentLiteralDelimiter = null; |
141
|
|
|
|
142
|
|
|
do { |
143
|
168 |
|
if (! $currentLiteralDelimiter) { |
144
|
168 |
|
$result = self::findPlaceholderOrOpeningQuote( |
145
|
161 |
|
$statement, |
146
|
|
|
$tokenOffset, |
147
|
|
|
$fragmentOffset, |
148
|
|
|
$fragments, |
149
|
|
|
$currentLiteralDelimiter, |
150
|
7 |
|
$paramMap |
151
|
|
|
); |
152
|
|
|
} else { |
153
|
120 |
|
$result = self::findClosingQuote($statement, $tokenOffset, $currentLiteralDelimiter); |
|
|
|
|
154
|
|
|
} |
155
|
168 |
|
} while ($result); |
156
|
|
|
|
157
|
168 |
|
if ($currentLiteralDelimiter) { |
158
|
|
|
throw new OCI8Exception(sprintf( |
159
|
|
|
'The statement contains non-terminated string literal starting at offset %d', |
160
|
|
|
$tokenOffset - 1 |
161
|
|
|
)); |
162
|
|
|
} |
163
|
|
|
|
164
|
168 |
|
$fragments[] = substr($statement, $fragmentOffset); |
165
|
168 |
|
$statement = implode('', $fragments); |
166
|
|
|
|
167
|
168 |
|
return [$statement, $paramMap]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Finds next placeholder or opening quote. |
172
|
|
|
* |
173
|
|
|
* @param string $statement The SQL statement to parse |
174
|
|
|
* @param string $tokenOffset The offset to start searching from |
175
|
|
|
* @param int $fragmentOffset The offset to build the next fragment from |
176
|
|
|
* @param string[] $fragments Fragments of the original statement not containing placeholders |
177
|
|
|
* @param string|null $currentLiteralDelimiter The delimiter of the current string literal |
178
|
|
|
* or NULL if not currently in a literal |
179
|
|
|
* @param array<int, string> $paramMap Mapping of the original parameter positions to their named replacements |
180
|
|
|
* |
181
|
|
|
* @return bool Whether the token was found |
182
|
|
|
*/ |
183
|
168 |
|
private static function findPlaceholderOrOpeningQuote( |
184
|
|
|
$statement, |
185
|
|
|
&$tokenOffset, |
186
|
|
|
&$fragmentOffset, |
187
|
|
|
&$fragments, |
188
|
|
|
&$currentLiteralDelimiter, |
189
|
|
|
&$paramMap |
190
|
|
|
) { |
191
|
168 |
|
$token = self::findToken($statement, $tokenOffset, '/[?\'"]/'); |
|
|
|
|
192
|
|
|
|
193
|
168 |
|
if (! $token) { |
194
|
168 |
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
168 |
|
if ($token === '?') { |
198
|
168 |
|
$position = count($paramMap) + 1; |
199
|
168 |
|
$param = ':param' . $position; |
200
|
168 |
|
$fragments[] = substr($statement, $fragmentOffset, $tokenOffset - $fragmentOffset); |
201
|
168 |
|
$fragments[] = $param; |
202
|
168 |
|
$paramMap[$position] = $param; |
203
|
168 |
|
$tokenOffset += 1; |
204
|
168 |
|
$fragmentOffset = $tokenOffset; |
205
|
|
|
|
206
|
168 |
|
return true; |
207
|
|
|
} |
208
|
|
|
|
209
|
120 |
|
$currentLiteralDelimiter = $token; |
210
|
120 |
|
++$tokenOffset; |
211
|
|
|
|
212
|
120 |
|
return true; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Finds closing quote |
217
|
|
|
* |
218
|
|
|
* @param string $statement The SQL statement to parse |
219
|
|
|
* @param string $tokenOffset The offset to start searching from |
220
|
|
|
* @param string $currentLiteralDelimiter The delimiter of the current string literal |
221
|
|
|
* |
222
|
|
|
* @return bool Whether the token was found |
223
|
|
|
*/ |
224
|
120 |
|
private static function findClosingQuote( |
225
|
|
|
$statement, |
226
|
|
|
&$tokenOffset, |
227
|
|
|
&$currentLiteralDelimiter |
228
|
|
|
) { |
229
|
120 |
|
$token = self::findToken( |
230
|
|
|
$statement, |
231
|
|
|
$tokenOffset, |
|
|
|
|
232
|
120 |
|
'/' . preg_quote($currentLiteralDelimiter, '/') . '/' |
233
|
|
|
); |
234
|
|
|
|
235
|
120 |
|
if (! $token) { |
236
|
|
|
return false; |
237
|
|
|
} |
238
|
|
|
|
239
|
120 |
|
$currentLiteralDelimiter = false; |
240
|
120 |
|
++$tokenOffset; |
241
|
|
|
|
242
|
120 |
|
return true; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Finds the token described by regex starting from the given offset. Updates the offset with the position |
247
|
|
|
* where the token was found. |
248
|
|
|
* |
249
|
|
|
* @param string $statement The SQL statement to parse |
250
|
|
|
* @param int $offset The offset to start searching from |
251
|
|
|
* @param string $regex The regex containing token pattern |
252
|
|
|
* |
253
|
|
|
* @return string|null Token or NULL if not found |
254
|
|
|
*/ |
255
|
168 |
|
private static function findToken($statement, &$offset, $regex) |
256
|
|
|
{ |
257
|
168 |
|
if (preg_match($regex, $statement, $matches, PREG_OFFSET_CAPTURE, $offset)) { |
258
|
168 |
|
$offset = $matches[0][1]; |
259
|
|
|
|
260
|
168 |
|
return $matches[0][0]; |
261
|
|
|
} |
262
|
|
|
|
263
|
168 |
|
return null; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function bindValue($param, $value, $type = ParameterType::STRING) : void |
270
|
|
|
{ |
271
|
|
|
$this->bindParam($param, $value, $type, null); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* {@inheritdoc} |
276
|
|
|
*/ |
277
|
|
|
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void |
278
|
|
|
{ |
279
|
|
|
$column = $this->_paramMap[$column]; |
280
|
|
|
|
281
|
|
|
if ($type === ParameterType::LARGE_OBJECT) { |
282
|
|
|
$lob = oci_new_descriptor($this->_dbh, OCI_D_LOB); |
283
|
|
|
|
284
|
|
|
$class = 'OCI-Lob'; |
285
|
|
|
assert($lob instanceof $class); |
286
|
|
|
|
287
|
|
|
$lob->writeTemporary($variable, OCI_TEMP_BLOB); |
288
|
|
|
|
289
|
|
|
$variable =& $lob; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$this->boundValues[$column] =& $variable; |
293
|
|
|
|
294
|
|
|
if (! oci_bind_by_name( |
295
|
|
|
$this->_sth, |
296
|
|
|
$column, |
297
|
|
|
$variable, |
298
|
|
|
$length ?? -1, |
299
|
|
|
$this->convertParameterType($type) |
300
|
|
|
)) { |
301
|
|
|
throw OCI8Exception::fromErrorInfo(oci_error($this->_sth)); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Converts DBAL parameter type to oci8 parameter type |
307
|
|
|
*/ |
308
|
|
|
private function convertParameterType(int $type) : int |
309
|
|
|
{ |
310
|
|
|
switch ($type) { |
311
|
|
|
case ParameterType::BINARY: |
312
|
|
|
return OCI_B_BIN; |
313
|
|
|
|
314
|
|
|
case ParameterType::LARGE_OBJECT: |
315
|
|
|
return OCI_B_BLOB; |
316
|
|
|
|
317
|
|
|
default: |
318
|
|
|
return SQLT_CHR; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* {@inheritdoc} |
324
|
|
|
*/ |
325
|
|
|
public function closeCursor() : void |
326
|
|
|
{ |
327
|
|
|
// not having the result means there's nothing to close |
328
|
|
|
if (! $this->result) { |
329
|
|
|
return; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
oci_cancel($this->_sth); |
333
|
|
|
|
334
|
|
|
$this->result = false; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* {@inheritdoc} |
339
|
|
|
*/ |
340
|
|
|
public function columnCount() |
341
|
|
|
{ |
342
|
|
|
return oci_num_fields($this->_sth) ?: 0; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* {@inheritdoc} |
347
|
|
|
*/ |
348
|
|
|
public function execute($params = null) : void |
349
|
|
|
{ |
350
|
|
|
if ($params) { |
351
|
|
|
$hasZeroIndex = array_key_exists(0, $params); |
352
|
|
|
|
353
|
|
|
foreach ($params as $key => $val) { |
354
|
|
|
if ($hasZeroIndex && is_int($key)) { |
355
|
|
|
$param = $key + 1; |
356
|
|
|
} else { |
357
|
|
|
$param = $key; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
$this->bindValue($param, $val); |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
$ret = @oci_execute($this->_sth, $this->_conn->getExecuteMode()); |
365
|
|
|
if (! $ret) { |
366
|
|
|
throw OCI8Exception::fromErrorInfo(oci_error($this->_sth)); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
$this->result = true; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* {@inheritdoc} |
374
|
|
|
*/ |
375
|
|
|
public function setFetchMode($fetchMode, ...$args) : void |
376
|
|
|
{ |
377
|
|
|
$this->_defaultFetchMode = $fetchMode; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* {@inheritdoc} |
382
|
|
|
*/ |
383
|
|
|
public function getIterator() |
384
|
|
|
{ |
385
|
|
|
return new StatementIterator($this); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* {@inheritdoc} |
390
|
|
|
*/ |
391
|
|
|
public function fetch($fetchMode = null, ...$args) |
392
|
|
|
{ |
393
|
|
|
// do not try fetching from the statement if it's not expected to contain result |
394
|
|
|
// in order to prevent exceptional situation |
395
|
|
|
if (! $this->result) { |
396
|
|
|
return false; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$fetchMode = $fetchMode ?: $this->_defaultFetchMode; |
400
|
|
|
|
401
|
|
|
if ($fetchMode === FetchMode::COLUMN) { |
402
|
|
|
return $this->fetchColumn(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
if ($fetchMode === FetchMode::STANDARD_OBJECT) { |
406
|
|
|
return oci_fetch_object($this->_sth); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
if (! isset(self::$fetchModeMap[$fetchMode])) { |
410
|
|
|
throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return oci_fetch_array( |
414
|
|
|
$this->_sth, |
415
|
|
|
self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | OCI_RETURN_LOBS |
416
|
|
|
); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* {@inheritdoc} |
421
|
|
|
*/ |
422
|
|
|
public function fetchAll($fetchMode = null, ...$args) |
423
|
|
|
{ |
424
|
|
|
$fetchMode = $fetchMode ?: $this->_defaultFetchMode; |
425
|
|
|
|
426
|
|
|
$result = []; |
427
|
|
|
|
428
|
|
|
if ($fetchMode === FetchMode::STANDARD_OBJECT) { |
429
|
|
|
while ($row = $this->fetch($fetchMode)) { |
430
|
|
|
$result[] = $row; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return $result; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
if (! isset(self::$fetchModeMap[$fetchMode])) { |
437
|
|
|
throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
if (self::$fetchModeMap[$fetchMode] === OCI_BOTH) { |
441
|
|
|
while ($row = $this->fetch($fetchMode)) { |
442
|
|
|
$result[] = $row; |
443
|
|
|
} |
444
|
|
|
} else { |
445
|
|
|
$fetchStructure = OCI_FETCHSTATEMENT_BY_ROW; |
446
|
|
|
|
447
|
|
|
if ($fetchMode === FetchMode::COLUMN) { |
448
|
|
|
$fetchStructure = OCI_FETCHSTATEMENT_BY_COLUMN; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
// do not try fetching from the statement if it's not expected to contain result |
452
|
|
|
// in order to prevent exceptional situation |
453
|
|
|
if (! $this->result) { |
454
|
|
|
return []; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
oci_fetch_all( |
458
|
|
|
$this->_sth, |
459
|
|
|
$result, |
460
|
|
|
0, |
461
|
|
|
-1, |
462
|
|
|
self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | $fetchStructure | OCI_RETURN_LOBS |
463
|
|
|
); |
464
|
|
|
|
465
|
|
|
if ($fetchMode === FetchMode::COLUMN) { |
466
|
|
|
$result = $result[0]; |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
return $result; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* {@inheritdoc} |
475
|
|
|
*/ |
476
|
|
|
public function fetchColumn($columnIndex = 0) |
477
|
|
|
{ |
478
|
|
|
// do not try fetching from the statement if it's not expected to contain result |
479
|
|
|
// in order to prevent exceptional situation |
480
|
|
|
if (! $this->result) { |
481
|
|
|
return false; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
$row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS); |
485
|
|
|
|
486
|
|
|
if ($row === false) { |
487
|
|
|
return false; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
if (! array_key_exists($columnIndex, $row)) { |
491
|
|
|
throw DBALException::invalidColumnIndex($columnIndex, count($row)); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
return $row[$columnIndex]; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* {@inheritdoc} |
499
|
|
|
*/ |
500
|
|
|
public function rowCount() : int |
501
|
|
|
{ |
502
|
|
|
return oci_num_rows($this->_sth) ?: 0; |
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
|