Completed
Pull Request — master (#3600)
by Sergei
27:59
created

OCI8Statement::fetchAll()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 49
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.0056

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 49
ccs 25
cts 26
cp 0.9615
rs 7.6666
c 0
b 0
f 0
cc 10
nc 11
nop 3
crap 10.0056

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\DBAL\Driver\OCI8;
4
5
use Doctrine\DBAL\Driver\Statement;
6
use Doctrine\DBAL\Driver\StatementIterator;
7
use Doctrine\DBAL\FetchMode;
8
use Doctrine\DBAL\ParameterType;
9
use InvalidArgumentException;
10
use IteratorAggregate;
11
use PDO;
12
use const OCI_ASSOC;
13
use const OCI_B_BIN;
14
use const OCI_B_BLOB;
15
use const OCI_BOTH;
16
use const OCI_D_LOB;
17
use const OCI_FETCHSTATEMENT_BY_COLUMN;
18
use const OCI_FETCHSTATEMENT_BY_ROW;
19
use const OCI_NUM;
20
use const OCI_RETURN_LOBS;
21
use const OCI_RETURN_NULLS;
22
use const OCI_TEMP_BLOB;
23
use const PREG_OFFSET_CAPTURE;
24
use const SQLT_CHR;
25
use function array_key_exists;
26
use function assert;
27
use function count;
28
use function implode;
29
use function is_int;
30
use function is_resource;
31
use function oci_bind_by_name;
32
use function oci_cancel;
33
use function oci_error;
34
use function oci_execute;
35
use function oci_fetch_all;
36
use function oci_fetch_array;
37
use function oci_fetch_object;
38
use function oci_new_descriptor;
39
use function oci_num_fields;
40
use function oci_num_rows;
41
use function oci_parse;
42
use function preg_match;
43
use function preg_quote;
44
use function sprintf;
45
use function substr;
46
47
/**
48
 * The OCI8 implementation of the Statement interface.
49
 */
50
class OCI8Statement implements IteratorAggregate, Statement
51
{
52
    /** @var resource */
53
    protected $_dbh;
54
55
    /** @var resource */
56
    protected $_sth;
57
58
    /** @var OCI8Connection */
59
    protected $_conn;
60
61
    /**
62
     * @deprecated
63
     *
64
     * @var string
65
     */
66
    protected static $_PARAM = ':param';
67
68
    /** @var int[] */
69
    protected static $fetchModeMap = [
70
        FetchMode::MIXED       => OCI_BOTH,
71
        FetchMode::ASSOCIATIVE => OCI_ASSOC,
72
        FetchMode::NUMERIC     => OCI_NUM,
73
        FetchMode::COLUMN      => OCI_NUM,
74
    ];
75
76
    /** @var int */
77
    protected $_defaultFetchMode = FetchMode::MIXED;
78
79
    /** @var string[] */
80
    protected $_paramMap = [];
81
82
    /**
83
     * Holds references to bound parameter values.
84
     *
85
     * This is a new requirement for PHP7's oci8 extension that prevents bound values from being garbage collected.
86
     *
87
     * @var mixed[]
88
     */
89
    private $boundValues = [];
90
91
    /**
92
     * Indicates whether the statement is in the state when fetching results is possible
93
     *
94
     * @var bool
95
     */
96
    private $result = false;
97
98
    /**
99
     * Creates a new OCI8Statement that uses the given connection handle and SQL statement.
100
     *
101
     * @param resource $dbh   The connection handle.
102
     * @param string   $query The SQL query.
103
     */
104 298
    public function __construct($dbh, $query, OCI8Connection $conn)
105
    {
106 298
        [$query, $paramMap] = self::convertPositionalToNamedPlaceholders($query);
107
108 298
        $stmt = oci_parse($dbh, $query);
109 298
        assert(is_resource($stmt));
110
111 298
        $this->_sth      = $stmt;
112 298
        $this->_dbh      = $dbh;
113 298
        $this->_paramMap = $paramMap;
114 298
        $this->_conn     = $conn;
115 298
    }
116
117
    /**
118
     * Converts positional (?) into named placeholders (:param<num>).
119
     *
120
     * Oracle does not support positional parameters, hence this method converts all
121
     * positional parameters into artificially named parameters. Note that this conversion
122
     * is not perfect. All question marks (?) in the original statement are treated as
123
     * placeholders and converted to a named parameter.
124
     *
125
     * The algorithm uses a state machine with two possible states: InLiteral and NotInLiteral.
126
     * Question marks inside literal strings are therefore handled correctly by this method.
127
     * This comes at a cost, the whole sql statement has to be looped over.
128
     *
129
     * @param string $statement The SQL statement to convert.
130
     *
131
     * @return mixed[] [0] => the statement value (string), [1] => the paramMap value (array).
132
     *
133
     * @throws OCI8Exception
134
     *
135
     * @todo extract into utility class in Doctrine\DBAL\Util namespace
136
     * @todo review and test for lost spaces. we experienced missing spaces with oci8 in some sql statements.
137
     */
138 474
    public static function convertPositionalToNamedPlaceholders($statement)
139
    {
140 474
        $fragmentOffset          = $tokenOffset = 0;
141 474
        $fragments               = $paramMap = [];
142 474
        $currentLiteralDelimiter = null;
143
144
        do {
145 474
            if (! $currentLiteralDelimiter) {
146 474
                $result = self::findPlaceholderOrOpeningQuote(
147 474
                    $statement,
148 324
                    $tokenOffset,
149 324
                    $fragmentOffset,
150 324
                    $fragments,
151 324
                    $currentLiteralDelimiter,
152 324
                    $paramMap
153
                );
154
            } else {
155 271
                $result = self::findClosingQuote($statement, $tokenOffset, $currentLiteralDelimiter);
0 ignored issues
show
Bug introduced by
$currentLiteralDelimiter of type void is incompatible with the type string expected by parameter $currentLiteralDelimiter of Doctrine\DBAL\Driver\OCI...ent::findClosingQuote(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

155
                $result = self::findClosingQuote($statement, $tokenOffset, /** @scrutinizer ignore-type */ $currentLiteralDelimiter);
Loading history...
156
            }
157 474
        } while ($result);
158
159 474
        if ($currentLiteralDelimiter) {
160 6
            throw new OCI8Exception(sprintf(
161 6
                'The statement contains non-terminated string literal starting at offset %d',
162 6
                $tokenOffset - 1
163
            ));
164
        }
165
166 468
        $fragments[] = substr($statement, $fragmentOffset);
167 468
        $statement   = implode('', $fragments);
168
169 468
        return [$statement, $paramMap];
170
    }
171
172
    /**
173
     * Finds next placeholder or opening quote.
174
     *
175
     * @param string             $statement               The SQL statement to parse
176
     * @param string             $tokenOffset             The offset to start searching from
177
     * @param int                $fragmentOffset          The offset to build the next fragment from
178
     * @param string[]           $fragments               Fragments of the original statement not containing placeholders
179
     * @param string|null        $currentLiteralDelimiter The delimiter of the current string literal
180
     *                                                    or NULL if not currently in a literal
181
     * @param array<int, string> $paramMap                Mapping of the original parameter positions to their named replacements
182
     *
183
     * @return bool Whether the token was found
184
     */
185 474
    private static function findPlaceholderOrOpeningQuote(
186
        $statement,
187
        &$tokenOffset,
188
        &$fragmentOffset,
189
        &$fragments,
190
        &$currentLiteralDelimiter,
191
        &$paramMap
192
    ) {
193 474
        $token = self::findToken($statement, $tokenOffset, '/[?\'"]/');
0 ignored issues
show
Bug introduced by
$tokenOffset of type string is incompatible with the type integer expected by parameter $offset of Doctrine\DBAL\Driver\OCI...8Statement::findToken(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

193
        $token = self::findToken($statement, /** @scrutinizer ignore-type */ $tokenOffset, '/[?\'"]/');
Loading history...
194
195 474
        if (! $token) {
196 468
            return false;
197
        }
198
199 420
        if ($token === '?') {
200 294
            $position            = count($paramMap) + 1;
201 294
            $param               = ':param' . $position;
202 294
            $fragments[]         = substr($statement, $fragmentOffset, $tokenOffset - $fragmentOffset);
203 294
            $fragments[]         = $param;
204 294
            $paramMap[$position] = $param;
205 294
            $tokenOffset        += 1;
206 294
            $fragmentOffset      = $tokenOffset;
207
208 294
            return true;
209
        }
210
211 271
        $currentLiteralDelimiter = $token;
212 271
        ++$tokenOffset;
213
214 271
        return true;
215
    }
216
217
    /**
218
     * Finds closing quote
219
     *
220
     * @param string $statement               The SQL statement to parse
221
     * @param string $tokenOffset             The offset to start searching from
222
     * @param string $currentLiteralDelimiter The delimiter of the current string literal
223
     *
224
     * @return bool Whether the token was found
225
     */
226 271
    private static function findClosingQuote(
227
        $statement,
228
        &$tokenOffset,
229
        &$currentLiteralDelimiter
230
    ) {
231 271
        $token = self::findToken(
232 171
            $statement,
233 171
            $tokenOffset,
0 ignored issues
show
Bug introduced by
$tokenOffset of type string is incompatible with the type integer expected by parameter $offset of Doctrine\DBAL\Driver\OCI...8Statement::findToken(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

233
            /** @scrutinizer ignore-type */ $tokenOffset,
Loading history...
234 271
            '/' . preg_quote($currentLiteralDelimiter, '/') . '/'
235
        );
236
237 271
        if (! $token) {
238 6
            return false;
239
        }
240
241 267
        $currentLiteralDelimiter = false;
242 267
        ++$tokenOffset;
243
244 267
        return true;
245
    }
246
247
    /**
248
     * Finds the token described by regex starting from the given offset. Updates the offset with the position
249
     * where the token was found.
250
     *
251
     * @param string $statement The SQL statement to parse
252
     * @param int    $offset    The offset to start searching from
253
     * @param string $regex     The regex containing token pattern
254
     *
255
     * @return string|null Token or NULL if not found
256
     */
257 474
    private static function findToken($statement, &$offset, $regex)
258
    {
259 474
        if (preg_match($regex, $statement, $matches, PREG_OFFSET_CAPTURE, $offset)) {
260 420
            $offset = $matches[0][1];
261
262 420
            return $matches[0][0];
263
        }
264
265 474
        return null;
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271 119
    public function bindValue($param, $value, $type = ParameterType::STRING)
272
    {
273 119
        return $this->bindParam($param, $value, $type, null);
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279 124
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
280
    {
281 124
        $column = $this->_paramMap[$column];
282
283 124
        if ($type === ParameterType::LARGE_OBJECT) {
284 4
            $lob = oci_new_descriptor($this->_dbh, OCI_D_LOB);
285
286 4
            $class = 'OCI-Lob';
287 4
            assert($lob instanceof $class);
288
289 4
            $lob->writeTemporary($variable, OCI_TEMP_BLOB);
290
291 4
            $variable =& $lob;
292
        }
293
294 124
        $this->boundValues[$column] =& $variable;
295
296 124
        return oci_bind_by_name(
297 124
            $this->_sth,
298 124
            $column,
299 124
            $variable,
300 124
            $length ?? -1,
301 124
            $this->convertParameterType($type)
302
        );
303
    }
304
305
    /**
306
     * Converts DBAL parameter type to oci8 parameter type
307
     */
308 124
    private function convertParameterType(int $type) : int
309
    {
310 124
        switch ($type) {
311
            case ParameterType::BINARY:
312 1
                return OCI_B_BIN;
313
314
            case ParameterType::LARGE_OBJECT:
315 4
                return OCI_B_BLOB;
316
317
            default:
318 122
                return SQLT_CHR;
319
        }
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     */
325 20
    public function closeCursor()
326
    {
327
        // not having the result means there's nothing to close
328 20
        if (! $this->result) {
329 4
            return true;
330
        }
331
332 16
        oci_cancel($this->_sth);
333
334 16
        $this->result = false;
335
336 16
        return true;
337
    }
338
339
    /**
340
     * {@inheritdoc}
341
     */
342 4
    public function columnCount()
343
    {
344 4
        return oci_num_fields($this->_sth) ?: 0;
345
    }
346
347
    /**
348
     * {@inheritdoc}
349
     */
350
    public function errorCode()
351
    {
352
        $error = oci_error($this->_sth);
353
        if ($error !== false) {
354
            $error = $error['code'];
355
        }
356
357
        return $error;
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363 146
    public function errorInfo()
364
    {
365 146
        $error = oci_error($this->_sth);
366
367 146
        if ($error === false) {
368
            return [];
369
        }
370
371 146
        return $error;
372
    }
373
374
    /**
375
     * {@inheritdoc}
376
     */
377 301
    public function execute($params = null)
378
    {
379 301
        if ($params) {
380 94
            $hasZeroIndex = array_key_exists(0, $params);
381
382 94
            foreach ($params as $key => $val) {
383 94
                if ($hasZeroIndex && is_int($key)) {
384 94
                    $this->bindValue($key + 1, $val);
385
                } else {
386 94
                    $this->bindValue($key, $val);
387
                }
388
            }
389
        }
390
391 297
        $ret = @oci_execute($this->_sth, $this->_conn->getExecuteMode());
392 297
        if (! $ret) {
393 150
            throw OCI8Exception::fromErrorInfo($this->errorInfo());
394
        }
395
396 292
        $this->result = true;
397
398 292
        return $ret;
399
    }
400
401
    /**
402
     * {@inheritdoc}
403
     */
404 265
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
405
    {
406 265
        $this->_defaultFetchMode = $fetchMode;
407
408 265
        return true;
409
    }
410
411
    /**
412
     * {@inheritdoc}
413
     */
414 5
    public function getIterator()
415
    {
416 5
        return new StatementIterator($this);
417
    }
418
419
    /**
420
     * {@inheritdoc}
421
     */
422 80
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
423
    {
424
        // do not try fetching from the statement if it's not expected to contain result
425
        // in order to prevent exceptional situation
426 80
        if (! $this->result) {
427 3
            return false;
428
        }
429
430 77
        $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
431
432 77
        if ($fetchMode === FetchMode::COLUMN) {
433 1
            return $this->fetchColumn();
434
        }
435
436 76
        if ($fetchMode === FetchMode::STANDARD_OBJECT) {
437 1
            return oci_fetch_object($this->_sth);
438
        }
439
440 75
        if (! isset(self::$fetchModeMap[$fetchMode])) {
441
            throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode);
442
        }
443
444 75
        return oci_fetch_array(
445 75
            $this->_sth,
446 75
            self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | OCI_RETURN_LOBS
447
        );
448
    }
449
450
    /**
451
     * {@inheritdoc}
452
     */
453 116
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
454
    {
455 116
        $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
456
457 116
        $result = [];
458
459 116
        if ($fetchMode === FetchMode::STANDARD_OBJECT) {
460 1
            while ($row = $this->fetch($fetchMode)) {
461 1
                $result[] = $row;
462
            }
463
464 1
            return $result;
465
        }
466
467 115
        if (! isset(self::$fetchModeMap[$fetchMode])) {
468
            throw new InvalidArgumentException('Invalid fetch style: ' . $fetchMode);
469
        }
470
471 115
        if (self::$fetchModeMap[$fetchMode] === OCI_BOTH) {
472 1
            while ($row = $this->fetch($fetchMode)) {
473 1
                $result[] = $row;
474
            }
475
        } else {
476 114
            $fetchStructure = OCI_FETCHSTATEMENT_BY_ROW;
477
478 114
            if ($fetchMode === FetchMode::COLUMN) {
479 6
                $fetchStructure = OCI_FETCHSTATEMENT_BY_COLUMN;
480
            }
481
482
            // do not try fetching from the statement if it's not expected to contain result
483
            // in order to prevent exceptional situation
484 114
            if (! $this->result) {
485 3
                return [];
486
            }
487
488 111
            oci_fetch_all(
489 111
                $this->_sth,
490 111
                $result,
491 111
                0,
492 111
                -1,
493 111
                self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | $fetchStructure | OCI_RETURN_LOBS
494
            );
495
496 111
            if ($fetchMode === FetchMode::COLUMN) {
497 6
                $result = $result[0];
498
            }
499
        }
500
501 112
        return $result;
502
    }
503
504
    /**
505
     * {@inheritdoc}
506
     */
507 69
    public function fetchColumn($columnIndex = 0)
508
    {
509
        // do not try fetching from the statement if it's not expected to contain result
510
        // in order to prevent exceptional situation
511 69
        if (! $this->result) {
512 3
            return false;
513
        }
514
515 66
        $row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
516
517 66
        if ($row === false) {
518 3
            return false;
519
        }
520
521 63
        return $row[$columnIndex] ?? null;
522
    }
523
524
    /**
525
     * {@inheritdoc}
526
     */
527 174
    public function rowCount()
528
    {
529 174
        return oci_num_rows($this->_sth) ?: 0;
530
    }
531
}
532