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; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Utility class that parses sql statements with regard to types and parameters. |
24
|
|
|
* |
25
|
|
|
* @link www.doctrine-project.org |
26
|
|
|
* @since 2.0 |
27
|
|
|
* @author Benjamin Eberlei <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class SQLParserUtils |
30
|
|
|
{ |
31
|
|
|
const POSITIONAL_TOKEN = '\?'; |
32
|
|
|
const NAMED_TOKEN = '(?<!:):[a-zA-Z_][a-zA-Z0-9_]*'; |
33
|
|
|
|
34
|
|
|
// Quote characters within string literals can be preceded by a backslash. |
35
|
|
|
const ESCAPED_SINGLE_QUOTED_TEXT = "(?:'(?:\\\\\\\\)+'|'(?:[^'\\\\]|\\\\'?|'')*')"; |
36
|
|
|
const ESCAPED_DOUBLE_QUOTED_TEXT = '(?:"(?:\\\\\\\\)+"|"(?:[^"\\\\]|\\\\"?)*")'; |
37
|
|
|
const ESCAPED_BACKTICK_QUOTED_TEXT = '(?:`(?:\\\\\\\\)+`|`(?:[^`\\\\]|\\\\`?)*`)'; |
38
|
|
|
const ESCAPED_BRACKET_QUOTED_TEXT = '(?<!\bARRAY)\[(?:[^\]])*\]'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets an array of the placeholders in an sql statements as keys and their positions in the query string. |
42
|
|
|
* |
43
|
|
|
* Returns an integer => integer pair (indexed from zero) for a positional statement |
44
|
|
|
* and a string => int[] pair for a named statement. |
45
|
|
|
* |
46
|
|
|
* @param string $statement |
47
|
|
|
* @param boolean $isPositional |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
87 |
|
static public function getPlaceholderPositions($statement, $isPositional = true) |
|
|
|
|
52
|
|
|
{ |
53
|
87 |
|
$match = ($isPositional) ? '?' : ':'; |
54
|
87 |
|
if (strpos($statement, $match) === false) { |
55
|
2 |
|
return array(); |
56
|
|
|
} |
57
|
|
|
|
58
|
85 |
|
$token = ($isPositional) ? self::POSITIONAL_TOKEN : self::NAMED_TOKEN; |
59
|
85 |
|
$paramMap = array(); |
60
|
|
|
|
61
|
85 |
|
foreach (self::getUnquotedStatementFragments($statement) as $fragment) { |
62
|
85 |
|
preg_match_all("/$token/", $fragment[0], $matches, PREG_OFFSET_CAPTURE); |
63
|
85 |
|
foreach ($matches[0] as $placeholder) { |
64
|
76 |
|
if ($isPositional) { |
65
|
25 |
|
$paramMap[] = $placeholder[1] + $fragment[1]; |
66
|
|
|
} else { |
67
|
51 |
|
$pos = $placeholder[1] + $fragment[1]; |
68
|
85 |
|
$paramMap[$pos] = substr($placeholder[0], 1, strlen($placeholder[0])); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
85 |
|
return $paramMap; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* For a positional query this method can rewrite the sql statement with regard to array parameters. |
78
|
|
|
* |
79
|
|
|
* @param string $query The SQL query to execute. |
80
|
|
|
* @param array $params The parameters to bind to the query. |
81
|
|
|
* @param array $types The types the previous parameters are in. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
* |
85
|
|
|
* @throws SQLParserUtilsException |
86
|
|
|
*/ |
87
|
146 |
|
static public function expandListParameters($query, $params, $types) |
|
|
|
|
88
|
|
|
{ |
89
|
146 |
|
$isPositional = is_int(key($params)); |
90
|
146 |
|
$arrayPositions = array(); |
91
|
146 |
|
$bindIndex = -1; |
92
|
|
|
|
93
|
146 |
|
if ($isPositional) { |
94
|
113 |
|
ksort($params); |
95
|
113 |
|
ksort($types); |
96
|
|
|
} |
97
|
|
|
|
98
|
146 |
|
foreach ($types as $name => $type) { |
99
|
62 |
|
++$bindIndex; |
100
|
|
|
|
101
|
62 |
|
if ($type !== Connection::PARAM_INT_ARRAY && $type !== Connection::PARAM_STR_ARRAY) { |
102
|
42 |
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
33 |
|
if ($isPositional) { |
106
|
11 |
|
$name = $bindIndex; |
107
|
|
|
} |
108
|
|
|
|
109
|
33 |
|
$arrayPositions[$name] = false; |
110
|
|
|
} |
111
|
|
|
|
112
|
146 |
|
if (( ! $arrayPositions && $isPositional)) { |
|
|
|
|
113
|
103 |
|
return array($query, $params, $types); |
114
|
|
|
} |
115
|
|
|
|
116
|
45 |
|
$paramPos = self::getPlaceholderPositions($query, $isPositional); |
117
|
|
|
|
118
|
45 |
|
if ($isPositional) { |
119
|
11 |
|
$paramOffset = 0; |
120
|
11 |
|
$queryOffset = 0; |
121
|
11 |
|
$params = array_values($params); |
122
|
11 |
|
$types = array_values($types); |
123
|
|
|
|
124
|
11 |
|
foreach ($paramPos as $needle => $needlePos) { |
125
|
11 |
|
if ( ! isset($arrayPositions[$needle])) { |
126
|
5 |
|
continue; |
127
|
|
|
} |
128
|
|
|
|
129
|
11 |
|
$needle += $paramOffset; |
130
|
11 |
|
$needlePos += $queryOffset; |
131
|
11 |
|
$count = count($params[$needle]); |
132
|
|
|
|
133
|
11 |
|
$params = array_merge( |
134
|
11 |
|
array_slice($params, 0, $needle), |
135
|
11 |
|
$params[$needle], |
136
|
11 |
|
array_slice($params, $needle + 1) |
137
|
|
|
); |
138
|
|
|
|
139
|
11 |
|
$types = array_merge( |
140
|
11 |
|
array_slice($types, 0, $needle), |
141
|
11 |
|
$count ? |
142
|
8 |
|
array_fill(0, $count, $types[$needle] - Connection::ARRAY_PARAM_OFFSET) : // array needles are at PDO::PARAM_* + 100 |
143
|
11 |
|
array(), |
144
|
11 |
|
array_slice($types, $needle + 1) |
145
|
|
|
); |
146
|
|
|
|
147
|
11 |
|
$expandStr = $count ? implode(", ", array_fill(0, $count, "?")) : 'NULL'; |
148
|
11 |
|
$query = substr($query, 0, $needlePos) . $expandStr . substr($query, $needlePos + 1); |
149
|
|
|
|
150
|
11 |
|
$paramOffset += ($count - 1); // Grows larger by number of parameters minus the replaced needle. |
151
|
11 |
|
$queryOffset += (strlen($expandStr) - 1); |
152
|
|
|
} |
153
|
|
|
|
154
|
11 |
|
return array($query, $params, $types); |
155
|
|
|
} |
156
|
|
|
|
157
|
34 |
|
$queryOffset = 0; |
158
|
34 |
|
$typesOrd = array(); |
159
|
34 |
|
$paramsOrd = array(); |
160
|
|
|
|
161
|
34 |
|
foreach ($paramPos as $pos => $paramName) { |
162
|
34 |
|
$paramLen = strlen($paramName) + 1; |
163
|
34 |
|
$value = static::extractParam($paramName, $params, true); |
|
|
|
|
164
|
|
|
|
165
|
28 |
|
if ( ! isset($arrayPositions[$paramName]) && ! isset($arrayPositions[':' . $paramName])) { |
166
|
21 |
|
$pos += $queryOffset; |
167
|
21 |
|
$queryOffset -= ($paramLen - 1); |
168
|
21 |
|
$paramsOrd[] = $value; |
169
|
21 |
|
$typesOrd[] = static::extractParam($paramName, $types, false, \PDO::PARAM_STR); |
|
|
|
|
170
|
21 |
|
$query = substr($query, 0, $pos) . '?' . substr($query, ($pos + $paramLen)); |
171
|
|
|
|
172
|
21 |
|
continue; |
173
|
|
|
} |
174
|
|
|
|
175
|
18 |
|
$count = count($value); |
176
|
18 |
|
$expandStr = $count > 0 ? implode(', ', array_fill(0, $count, '?')) : 'NULL'; |
177
|
|
|
|
178
|
18 |
|
foreach ($value as $val) { |
179
|
16 |
|
$paramsOrd[] = $val; |
180
|
16 |
|
$typesOrd[] = static::extractParam($paramName, $types, false) - Connection::ARRAY_PARAM_OFFSET; |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
18 |
|
$pos += $queryOffset; |
184
|
18 |
|
$queryOffset += (strlen($expandStr) - $paramLen); |
185
|
18 |
|
$query = substr($query, 0, $pos) . $expandStr . substr($query, ($pos + $paramLen)); |
186
|
|
|
} |
187
|
|
|
|
188
|
28 |
|
return array($query, $paramsOrd, $typesOrd); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Slice the SQL statement around pairs of quotes and |
193
|
|
|
* return string fragments of SQL outside of quoted literals. |
194
|
|
|
* Each fragment is captured as a 2-element array: |
195
|
|
|
* |
196
|
|
|
* 0 => matched fragment string, |
197
|
|
|
* 1 => offset of fragment in $statement |
198
|
|
|
* |
199
|
|
|
* @param string $statement |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
85 |
|
static private function getUnquotedStatementFragments($statement) |
|
|
|
|
203
|
|
|
{ |
204
|
85 |
|
$literal = self::ESCAPED_SINGLE_QUOTED_TEXT . '|' . |
205
|
85 |
|
self::ESCAPED_DOUBLE_QUOTED_TEXT . '|' . |
206
|
85 |
|
self::ESCAPED_BACKTICK_QUOTED_TEXT . '|' . |
207
|
85 |
|
self::ESCAPED_BRACKET_QUOTED_TEXT; |
208
|
85 |
|
preg_match_all("/([^'\"`\[]+)(?:$literal)?/s", $statement, $fragments, PREG_OFFSET_CAPTURE); |
209
|
|
|
|
210
|
85 |
|
return $fragments[1]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $paramName The name of the parameter (without a colon in front) |
215
|
|
|
* @param array $paramsOrTypes A hash of parameters or types |
216
|
|
|
* @param bool $isParam |
217
|
|
|
* @param mixed $defaultValue An optional default value. If omitted, an exception is thrown |
218
|
|
|
* |
219
|
|
|
* @throws SQLParserUtilsException |
220
|
|
|
* @return mixed |
221
|
|
|
*/ |
222
|
34 |
|
static private function extractParam($paramName, $paramsOrTypes, $isParam, $defaultValue = null) |
|
|
|
|
223
|
|
|
{ |
224
|
34 |
|
if (array_key_exists($paramName, $paramsOrTypes)) { |
225
|
27 |
|
return $paramsOrTypes[$paramName]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// Hash keys can be prefixed with a colon for compatibility |
229
|
14 |
|
if (array_key_exists(':' . $paramName, $paramsOrTypes)) { |
230
|
6 |
|
return $paramsOrTypes[':' . $paramName]; |
231
|
|
|
} |
232
|
|
|
|
233
|
11 |
|
if (null !== $defaultValue) { |
234
|
5 |
|
return $defaultValue; |
235
|
|
|
} |
236
|
|
|
|
237
|
6 |
|
if ($isParam) { |
238
|
6 |
|
throw SQLParserUtilsException::missingParam($paramName); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
throw SQLParserUtilsException::missingType($paramName); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|