-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathAbstractParser.php
318 lines (293 loc) · 7.61 KB
/
AbstractParser.php
1
2
3
4
5
6
7
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
35
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license https://opensource.org/licenses/MIT MIT
*
*/
namespace Aura\Sql\Parser;
use Aura\Sql\Exception\MissingParameter;
/**
*
* Parsing/rebuilding functionality for all drivers.
*
* Note that this does not validate the syntax; it only replaces/rebuilds
* placeholders in the query.
*
* @package Aura.Sql
*
*/
abstract class AbstractParser implements ParserInterface
{
/**
*
* Split the query string on these regexes.
*
* @var array
*
*/
protected array $split = [
// single-quoted string
"'(?:[^'\\\\]|\\\\'?)*'",
// double-quoted string
'"(?:[^"\\\\]|\\\\"?)*"',
];
/**
*
* Skip query parts matching this regex.
*
* @var string
*
*/
protected string $skip = '/^(\'|\"|\:[^a-zA-Z_])/um';
/**
*
* The current numbered-placeholder in the original statement.
*
* @var int
*
*/
protected int $num = 0;
/**
*
* How many times has a named placeholder been used?
*
* @var array
*
*/
protected array $count = [
'__' => null,
];
/**
*
* The initial values to be bound.
*
* @var array
*
*/
protected array $values = [];
/**
*
* Final placeholders and values to bind.
*
* @var array
*
*/
protected array $final_values = [];
/**
*
* Rebuilds a statement with placeholders and bound values.
*
* @param string $statement The statement to rebuild.
*
* @param array $values The values to bind and/or replace into a statement.
*
* @return array An array where element 0 is the rebuilt statement and
* element 1 is the rebuilt array of values.
*
*/
public function rebuild(string $statement, array $values = []): array
{
// match standard PDO execute() behavior of zero-indexed arrays
if (array_key_exists(0, $values)) {
array_unshift($values, null);
}
$this->values = $values;
$statement = $this->rebuildStatement($statement);
return [$statement, $this->final_values];
}
/**
*
* Given a statement, rebuilds it with array values embedded.
*
* @param string $statement The SQL statement.
*
* @return string The rebuilt statement.
*
*/
protected function rebuildStatement(string $statement): string
{
$parts = $this->getParts($statement);
return $this->rebuildParts($parts);
}
/**
*
* Given an array of statement parts, rebuilds each part.
*
* @param array $parts The statement parts.
*
* @return string The rebuilt statement.
*
*/
protected function rebuildParts(array $parts): string
{
$statement = '';
foreach ($parts as $part) {
$statement .= $this->rebuildPart($part);
}
return $statement;
}
/**
*
* Rebuilds a single statement part.
*
* @param string $part The statement part.
*
* @return string The rebuilt statement.
*
*/
protected function rebuildPart(string $part): string
{
if (preg_match($this->skip, $part)) {
return $part;
}
// split into subparts by ":name" and "?"
$subs = preg_split(
"/(?<!:)(:[a-zA-Z_][a-zA-Z0-9_]*)|(\?)/um",
$part,
-1,
PREG_SPLIT_DELIM_CAPTURE
);
// check subparts to expand placeholders for bound arrays
return $this->prepareValuePlaceholders($subs);
}
/**
*
* Prepares the sub-parts of a query with placeholders.
*
* @param array $subs The query subparts.
*
* @return string The prepared subparts.
*
*/
protected function prepareValuePlaceholders(array $subs): string
{
$str = '';
foreach ($subs as $i => $sub) {
$char = substr($sub, 0, 1);
if ($char == '?') {
$str .= $this->prepareNumberedPlaceholder();
} elseif ($char == ':') {
$str .= $this->prepareNamedPlaceholder($sub);
} else {
$str .= $sub;
}
}
return $str;
}
/**
*
* Bind or quote a numbered placeholder in a query subpart.
*
* @return string The prepared query subpart.
*
* @throws MissingParameter
*/
protected function prepareNumberedPlaceholder()
{
$this->num ++;
if (array_key_exists($this->num, $this->values) === false) {
throw new MissingParameter("Parameter {$this->num} is missing from the bound values");
}
$expanded = [];
$values = (array) $this->values[$this->num];
if (is_null($this->values[$this->num])) {
$values[] = null;
}
foreach ($values as $value) {
$count = ++ $this->count['__'];
$name = "__{$count}";
$expanded[] = ":{$name}";
$this->final_values[$name] = $value;
}
return implode(', ', $expanded);
}
/**
*
* Bind or quote a named placeholder in a query subpart.
*
* @param string $sub The query subpart.
*
* @return string The prepared query subpart.
*
*/
protected function prepareNamedPlaceholder(string $sub): string
{
$orig = substr($sub, 1);
if (array_key_exists($orig, $this->values) === false) {
throw new MissingParameter("Parameter '{$orig}' is missing from the bound values");
}
$name = $this->getPlaceholderName($orig);
// is the corresponding data element an array?
$bind_array = is_array($this->values[$orig]);
if ($bind_array) {
// expand to multiple placeholders
return $this->expandNamedPlaceholder($name, $this->values[$orig]);
}
// not an array, retain the placeholder for later
$this->final_values[$name] = $this->values[$orig];
return ":$name";
}
/**
*
* Given an original placeholder name, return a replacement name.
*
* @param string $orig The original placeholder name.
*
* @return string
*
*/
protected function getPlaceholderName(string $orig): string
{
if (! isset($this->count[$orig])) {
$this->count[$orig] = 0;
return $orig;
}
$count = ++ $this->count[$orig];
return "{$orig}__{$count}";
}
/**
*
* Given a named placeholder for an array, expand it for the array values,
* and bind those values to the expanded names.
*
* @param string $prefix The named placeholder.
*
* @param array $values The array values to be bound.
*
* @return string
*
*/
protected function expandNamedPlaceholder(string $prefix, array $values): string
{
$i = 0;
$expanded = [];
foreach ($values as $value) {
$name = "{$prefix}_{$i}";
$expanded[] = ":{$name}";
$this->final_values[$name] = $value;
$i ++;
}
return implode(', ', $expanded);
}
/**
*
* Given a query string, split it into parts.
*
* @param string $statement The query string.
*
* @return array
*
*/
protected function getParts(string $statement): array
{
$split = implode('|', $this->split);
return preg_split(
"/($split)/um",
$statement,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
}
}