ConvertPositionalToNamedPlaceholders.php 5.23 KB
Newer Older
1 2 3 4 5 6
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver\OCI8;

7
use Doctrine\DBAL\Driver\Exception;
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
use Doctrine\DBAL\Driver\OCI8\Exception\NonTerminatedStringLiteral;

use function count;
use function implode;
use function preg_match;
use function preg_quote;
use function substr;

use const PREG_OFFSET_CAPTURE;

/**
 * Converts positional (?) into named placeholders (:param<num>).
 *
 * Oracle does not support positional parameters, hence this method converts all
 * positional parameters into artificially named parameters. Note that this conversion
 * is not perfect. All question marks (?) in the original statement are treated as
 * placeholders and converted to a named parameter.
 *
 * @internal This class is not covered by the backward compatibility promise
 */
final class ConvertPositionalToNamedPlaceholders
{
    /**
     * @param string $statement The SQL statement to convert.
     *
     * @return mixed[] [0] => the statement value (string), [1] => the paramMap value (array).
     *
35
     * @throws Exception
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
     */
    public function __invoke(string $statement): array
    {
        $fragmentOffset          = $tokenOffset = 0;
        $fragments               = $paramMap = [];
        $currentLiteralDelimiter = null;

        do {
            if ($currentLiteralDelimiter === null) {
                $result = $this->findPlaceholderOrOpeningQuote(
                    $statement,
                    $tokenOffset,
                    $fragmentOffset,
                    $fragments,
                    $currentLiteralDelimiter,
                    $paramMap
                );
            } else {
                $result = $this->findClosingQuote($statement, $tokenOffset, $currentLiteralDelimiter);
            }
        } while ($result);

        if ($currentLiteralDelimiter !== null) {
            throw NonTerminatedStringLiteral::new($tokenOffset - 1);
        }

        $fragments[] = substr($statement, $fragmentOffset);
        $statement   = implode('', $fragments);

        return [$statement, $paramMap];
    }

    /**
     * Finds next placeholder or opening quote.
     *
     * @param string      $statement               The SQL statement to parse
     * @param int         $tokenOffset             The offset to start searching from
     * @param int         $fragmentOffset          The offset to build the next fragment from
     * @param string[]    $fragments               Fragments of the original statement not containing placeholders
     * @param string|null $currentLiteralDelimiter The delimiter of the current string literal
     *                                             or NULL if not currently in a literal
     * @param string[]    $paramMap                Mapping of the original parameter positions to their named replacements
     *
     * @return bool Whether the token was found
     */
    private function findPlaceholderOrOpeningQuote(
        string $statement,
        int &$tokenOffset,
        int &$fragmentOffset,
        array &$fragments,
        ?string &$currentLiteralDelimiter,
        array &$paramMap
    ): bool {
        $token = $this->findToken($statement, $tokenOffset, '/[?\'"]/');

        if ($token === null) {
            return false;
        }

        if ($token === '?') {
            $position            = count($paramMap) + 1;
            $param               = ':param' . $position;
            $fragments[]         = substr($statement, $fragmentOffset, $tokenOffset - $fragmentOffset);
            $fragments[]         = $param;
            $paramMap[$position] = $param;
            $tokenOffset        += 1;
            $fragmentOffset      = $tokenOffset;

            return true;
        }

        $currentLiteralDelimiter = $token;
        ++$tokenOffset;

        return true;
    }

    /**
     * Finds closing quote
     *
     * @param string $statement               The SQL statement to parse
     * @param int    $tokenOffset             The offset to start searching from
     * @param string $currentLiteralDelimiter The delimiter of the current string literal
     *
     * @return bool Whether the token was found
     */
    private function findClosingQuote(
        string $statement,
        int &$tokenOffset,
        string &$currentLiteralDelimiter
    ): bool {
        $token = $this->findToken(
            $statement,
            $tokenOffset,
            '/' . preg_quote($currentLiteralDelimiter, '/') . '/'
        );

        if ($token === null) {
            return false;
        }

        $currentLiteralDelimiter = null;
        ++$tokenOffset;

        return true;
    }

    /**
     * Finds the token described by regex starting from the given offset. Updates the offset with the position
     * where the token was found.
     *
     * @param string $statement The SQL statement to parse
     * @param int    $offset    The offset to start searching from
     * @param string $regex     The regex containing token pattern
     *
     * @return string|null Token or NULL if not found
     */
    private function findToken(string $statement, int &$offset, string $regex): ?string
    {
        if (preg_match($regex, $statement, $matches, PREG_OFFSET_CAPTURE, $offset) === 1) {
            $offset = $matches[0][1];

            return $matches[0][0];
        }

        return null;
    }
}