|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -declare(strict_types=1); |
4 |
| - |
5 | 3 | namespace Doctrine\DBAL\Driver\OCI8;
|
6 | 4 |
|
7 |
| -use Doctrine\DBAL\Driver\Exception; |
8 |
| -use Doctrine\DBAL\Driver\OCI8\Exception\NonTerminatedStringLiteral; |
| 5 | +use Doctrine\DBAL\SQL\Parser\Visitor; |
9 | 6 |
|
10 | 7 | use function count;
|
11 | 8 | use function implode;
|
12 |
| -use function preg_match; |
13 |
| -use function preg_quote; |
14 |
| -use function substr; |
15 |
| - |
16 |
| -use const PREG_OFFSET_CAPTURE; |
17 | 9 |
|
18 | 10 | /**
|
19 | 11 | * Converts positional (?) into named placeholders (:param<num>).
|
20 | 12 | *
|
21 | 13 | * Oracle does not support positional parameters, hence this method converts all
|
22 |
| - * positional parameters into artificially named parameters. Note that this conversion |
23 |
| - * is not perfect. All question marks (?) in the original statement are treated as |
24 |
| - * placeholders and converted to a named parameter. |
| 14 | + * positional parameters into artificially named parameters. |
25 | 15 | *
|
26 | 16 | * @internal This class is not covered by the backward compatibility promise
|
27 | 17 | */
|
28 |
| -final class ConvertPositionalToNamedPlaceholders |
| 18 | +final class ConvertPositionalToNamedPlaceholders implements Visitor |
29 | 19 | {
|
30 |
| - /** |
31 |
| - * @param string $statement The SQL statement to convert. |
32 |
| - * |
33 |
| - * @return mixed[] [0] => the statement value (string), [1] => the paramMap value (array). |
34 |
| - * |
35 |
| - * @throws Exception |
36 |
| - */ |
37 |
| - public function __invoke(string $statement): array |
38 |
| - { |
39 |
| - $fragmentOffset = $tokenOffset = 0; |
40 |
| - $fragments = $paramMap = []; |
41 |
| - $currentLiteralDelimiter = null; |
42 |
| - |
43 |
| - do { |
44 |
| - if ($currentLiteralDelimiter === null) { |
45 |
| - $result = $this->findPlaceholderOrOpeningQuote( |
46 |
| - $statement, |
47 |
| - $tokenOffset, |
48 |
| - $fragmentOffset, |
49 |
| - $fragments, |
50 |
| - $currentLiteralDelimiter, |
51 |
| - $paramMap |
52 |
| - ); |
53 |
| - } else { |
54 |
| - $result = $this->findClosingQuote($statement, $tokenOffset, $currentLiteralDelimiter); |
55 |
| - } |
56 |
| - } while ($result); |
| 20 | + /** @var list<string> */ |
| 21 | + private $buffer = []; |
57 | 22 |
|
58 |
| - if ($currentLiteralDelimiter !== null) { |
59 |
| - throw NonTerminatedStringLiteral::new($tokenOffset - 1); |
60 |
| - } |
| 23 | + /** @var array<int,string> */ |
| 24 | + private $parameterMap = []; |
61 | 25 |
|
62 |
| - $fragments[] = substr($statement, $fragmentOffset); |
63 |
| - $statement = implode('', $fragments); |
64 |
| - |
65 |
| - return [$statement, $paramMap]; |
| 26 | + public function acceptOther(string $sql): void |
| 27 | + { |
| 28 | + $this->buffer[] = $sql; |
66 | 29 | }
|
67 | 30 |
|
68 |
| - /** |
69 |
| - * Finds next placeholder or opening quote. |
70 |
| - * |
71 |
| - * @param string $statement The SQL statement to parse |
72 |
| - * @param int $tokenOffset The offset to start searching from |
73 |
| - * @param int $fragmentOffset The offset to build the next fragment from |
74 |
| - * @param string[] $fragments Fragments of the original statement not containing placeholders |
75 |
| - * @param string|null $currentLiteralDelimiter The delimiter of the current string literal |
76 |
| - * or NULL if not currently in a literal |
77 |
| - * @param string[] $paramMap Mapping of the original parameter positions |
78 |
| - * to their named replacements |
79 |
| - * |
80 |
| - * @return bool Whether the token was found |
81 |
| - */ |
82 |
| - private function findPlaceholderOrOpeningQuote( |
83 |
| - string $statement, |
84 |
| - int &$tokenOffset, |
85 |
| - int &$fragmentOffset, |
86 |
| - array &$fragments, |
87 |
| - ?string &$currentLiteralDelimiter, |
88 |
| - array &$paramMap |
89 |
| - ): bool { |
90 |
| - $token = $this->findToken($statement, $tokenOffset, '/[?\'"]/'); |
91 |
| - |
92 |
| - if ($token === null) { |
93 |
| - return false; |
94 |
| - } |
95 |
| - |
96 |
| - if ($token === '?') { |
97 |
| - $position = count($paramMap) + 1; |
98 |
| - $param = ':param' . $position; |
99 |
| - $fragments[] = substr($statement, $fragmentOffset, $tokenOffset - $fragmentOffset); |
100 |
| - $fragments[] = $param; |
101 |
| - $paramMap[$position] = $param; |
102 |
| - $tokenOffset += 1; |
103 |
| - $fragmentOffset = $tokenOffset; |
104 |
| - |
105 |
| - return true; |
106 |
| - } |
| 31 | + public function acceptPositionalParameter(string $sql): void |
| 32 | + { |
| 33 | + $position = count($this->parameterMap) + 1; |
| 34 | + $param = ':param' . $position; |
107 | 35 |
|
108 |
| - $currentLiteralDelimiter = $token; |
109 |
| - ++$tokenOffset; |
| 36 | + $this->parameterMap[$position] = $param; |
110 | 37 |
|
111 |
| - return true; |
| 38 | + $this->buffer[] = $param; |
112 | 39 | }
|
113 | 40 |
|
114 |
| - /** |
115 |
| - * Finds closing quote |
116 |
| - * |
117 |
| - * @param string $statement The SQL statement to parse |
118 |
| - * @param int $tokenOffset The offset to start searching from |
119 |
| - * @param string $currentLiteralDelimiter The delimiter of the current string literal |
120 |
| - * |
121 |
| - * @return bool Whether the token was found |
122 |
| - */ |
123 |
| - private function findClosingQuote( |
124 |
| - string $statement, |
125 |
| - int &$tokenOffset, |
126 |
| - string &$currentLiteralDelimiter |
127 |
| - ): bool { |
128 |
| - $token = $this->findToken( |
129 |
| - $statement, |
130 |
| - $tokenOffset, |
131 |
| - '/' . preg_quote($currentLiteralDelimiter, '/') . '/' |
132 |
| - ); |
133 |
| - |
134 |
| - if ($token === null) { |
135 |
| - return false; |
136 |
| - } |
137 |
| - |
138 |
| - $currentLiteralDelimiter = null; |
139 |
| - ++$tokenOffset; |
| 41 | + public function acceptNamedParameter(string $sql): void |
| 42 | + { |
| 43 | + $this->buffer[] = $sql; |
| 44 | + } |
140 | 45 |
|
141 |
| - return true; |
| 46 | + public function getSQL(): string |
| 47 | + { |
| 48 | + return implode('', $this->buffer); |
142 | 49 | }
|
143 | 50 |
|
144 | 51 | /**
|
145 |
| - * Finds the token described by regex starting from the given offset. Updates the offset with the position |
146 |
| - * where the token was found. |
147 |
| - * |
148 |
| - * @param string $statement The SQL statement to parse |
149 |
| - * @param int $offset The offset to start searching from |
150 |
| - * @param string $regex The regex containing token pattern |
151 |
| - * |
152 |
| - * @return string|null Token or NULL if not found |
| 52 | + * @return array<int,string> |
153 | 53 | */
|
154 |
| - private function findToken(string $statement, int &$offset, string $regex): ?string |
| 54 | + public function getParameterMap(): array |
155 | 55 | {
|
156 |
| - if (preg_match($regex, $statement, $matches, PREG_OFFSET_CAPTURE, $offset) === 1) { |
157 |
| - $offset = $matches[0][1]; |
158 |
| - |
159 |
| - return $matches[0][0]; |
160 |
| - } |
161 |
| - |
162 |
| - return null; |
| 56 | + return $this->parameterMap; |
163 | 57 | }
|
164 | 58 | }
|
0 commit comments