-
Notifications
You must be signed in to change notification settings - Fork 12
/
QueryCompiler.php
455 lines (408 loc) · 16.2 KB
/
QueryCompiler.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database;
use Cake\Database\Exception\DatabaseException;
use Closure;
use Countable;
/**
* Responsible for compiling a Query object into its SQL representation
*
* @internal
*/
class QueryCompiler
{
/**
* List of sprintf templates that will be used for compiling the SQL for
* this query. There are some clauses that can be built as just as the
* direct concatenation of the internal parts, those are listed here.
*
* @var array
*/
protected $_templates = [
'delete' => 'DELETE',
'where' => ' WHERE %s',
'group' => ' GROUP BY %s ',
'having' => ' HAVING %s ',
'order' => ' %s',
'limit' => ' LIMIT %s',
'offset' => ' OFFSET %s',
'epilog' => ' %s',
];
/**
* The list of query clauses to traverse for generating a SELECT statement
*
* @var array
*/
protected $_selectParts = [
'with', 'select', 'from', 'join', 'where', 'group', 'having', 'window', 'order',
'limit', 'offset', 'union', 'epilog',
];
/**
* The list of query clauses to traverse for generating an UPDATE statement
*
* @var array
*/
protected $_updateParts = ['with', 'update', 'set', 'where', 'epilog'];
/**
* The list of query clauses to traverse for generating a DELETE statement
*
* @var array
*/
protected $_deleteParts = ['with', 'delete', 'modifier', 'from', 'where', 'epilog'];
/**
* The list of query clauses to traverse for generating an INSERT statement
*
* @var array
*/
protected $_insertParts = ['with', 'insert', 'values', 'epilog'];
/**
* Indicate whether or not this query dialect supports ordered unions.
*
* Overridden in subclasses.
*
* @var bool
*/
protected $_orderedUnion = true;
/**
* Indicate whether aliases in SELECT clause need to be always quoted.
*
* @var bool
*/
protected $_quotedSelectAliases = false;
/**
* Returns the SQL representation of the provided query after generating
* the placeholders for the bound values using the provided generator
*
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholders
* @return string
*/
public function compile(Query $query, ValueBinder $binder): string
{
$sql = '';
$type = $query->type();
$query->traverseParts(
$this->_sqlCompiler($sql, $query, $binder),
$this->{"_{$type}Parts"}
);
// Propagate bound parameters from sub-queries if the
// placeholders can be found in the SQL statement.
if ($query->getValueBinder() !== $binder) {
foreach ($query->getValueBinder()->bindings() as $binding) {
$placeholder = ':' . $binding['placeholder'];
if (preg_match('/' . $placeholder . '(?:\W|$)/', $sql) > 0) {
$binder->bind($placeholder, $binding['value'], $binding['type']);
}
}
}
return $sql;
}
/**
* Returns a callable object that can be used to compile a SQL string representation
* of this query.
*
* @param string $sql initial sql string to append to
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return \Closure
*/
protected function _sqlCompiler(string &$sql, Query $query, ValueBinder $binder): Closure
{
return function ($part, $partName) use (&$sql, $query, $binder) {
if (
$part === null ||
(is_array($part) && empty($part)) ||
($part instanceof Countable && count($part) === 0)
) {
return;
}
if ($part instanceof ExpressionInterface) {
$part = [$part->sql($binder)];
}
if (isset($this->_templates[$partName])) {
$part = $this->_stringifyExpressions((array)$part, $binder);
$sql .= sprintf($this->_templates[$partName], implode(', ', $part));
return;
}
$sql .= $this->{'_build' . $partName . 'Part'}($part, $query, $binder);
};
}
/**
* Helper function used to build the string representation of a `WITH` clause,
* it constructs the CTE definitions list and generates the `RECURSIVE`
* keyword when required.
*
* @param array $parts List of CTEs to be transformed to string
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string
*/
protected function _buildWithPart(array $parts, Query $query, ValueBinder $binder): string
{
$recursive = false;
$expressions = [];
foreach ($parts as $cte) {
$recursive = $recursive || $cte->isRecursive();
$expressions[] = $cte->sql($binder);
}
$recursive = $recursive ? 'RECURSIVE ' : '';
return sprintf('WITH %s%s ', $recursive, implode(', ', $expressions));
}
/**
* Helper function used to build the string representation of a SELECT clause,
* it constructs the field list taking care of aliasing and
* converting expression objects to string. This function also constructs the
* DISTINCT clause for the query.
*
* @param array $parts list of fields to be transformed to string
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string
*/
protected function _buildSelectPart(array $parts, Query $query, ValueBinder $binder): string
{
$select = 'SELECT%s %s%s';
if ($this->_orderedUnion && $query->clause('union')) {
$select = '(SELECT%s %s%s';
}
$distinct = $query->clause('distinct');
$modifiers = $this->_buildModifierPart($query->clause('modifier'), $query, $binder);
$driver = $query->getConnection()->getDriver();
$quoteIdentifiers = $driver->isAutoQuotingEnabled() || $this->_quotedSelectAliases;
$normalized = [];
$parts = $this->_stringifyExpressions($parts, $binder);
foreach ($parts as $k => $p) {
if (!is_numeric($k)) {
$p = $p . ' AS ';
if ($quoteIdentifiers) {
$p .= $driver->quoteIdentifier($k);
} else {
$p .= $k;
}
}
$normalized[] = $p;
}
if ($distinct === true) {
$distinct = 'DISTINCT ';
}
if (is_array($distinct)) {
$distinct = $this->_stringifyExpressions($distinct, $binder);
$distinct = sprintf('DISTINCT ON (%s) ', implode(', ', $distinct));
}
return sprintf($select, $modifiers, $distinct, implode(', ', $normalized));
}
/**
* Helper function used to build the string representation of a FROM clause,
* it constructs the tables list taking care of aliasing and
* converting expression objects to string.
*
* @param array $parts list of tables to be transformed to string
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string
*/
protected function _buildFromPart(array $parts, Query $query, ValueBinder $binder): string
{
$select = ' FROM %s';
$normalized = [];
$parts = $this->_stringifyExpressions($parts, $binder);
foreach ($parts as $k => $p) {
if (!is_numeric($k)) {
$p = $p . ' ' . $k;
}
$normalized[] = $p;
}
return sprintf($select, implode(', ', $normalized));
}
/**
* Helper function used to build the string representation of multiple JOIN clauses,
* it constructs the joins list taking care of aliasing and converting
* expression objects to string in both the table to be joined and the conditions
* to be used.
*
* @param array $parts list of joins to be transformed to string
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string
*/
protected function _buildJoinPart(array $parts, Query $query, ValueBinder $binder): string
{
$joins = '';
foreach ($parts as $join) {
if ($join['table'] instanceof ExpressionInterface) {
$join['table'] = '(' . $join['table']->sql($binder) . ')';
}
$joins .= sprintf(' %s JOIN %s %s', $join['type'], $join['table'], $join['alias']);
$condition = '';
if (isset($join['conditions']) && $join['conditions'] instanceof ExpressionInterface) {
$condition = $join['conditions']->sql($binder);
}
if (strlen($condition)) {
$joins .= " ON {$condition}";
} else {
$joins .= ' ON 1 = 1';
}
}
return $joins;
}
/**
* Helper function to build the string representation of a window clause.
*
* @param array $parts List of windows to be transformed to string
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string
*/
protected function _buildWindowPart(array $parts, Query $query, ValueBinder $binder): string
{
$windows = [];
foreach ($parts as $window) {
$windows[] = $window['name']->sql($binder) . ' AS (' . $window['window']->sql($binder) . ')';
}
return ' WINDOW ' . implode(', ', $windows);
}
/**
* Helper function to generate SQL for SET expressions.
*
* @param array $parts List of keys & values to set.
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string
*/
protected function _buildSetPart(array $parts, Query $query, ValueBinder $binder): string
{
$set = [];
foreach ($parts as $part) {
if ($part instanceof ExpressionInterface) {
$part = $part->sql($binder);
}
if ($part[0] === '(') {
$part = substr($part, 1, -1);
}
$set[] = $part;
}
return ' SET ' . implode('', $set);
}
/**
* Builds the SQL string for all the UNION clauses in this query, when dealing
* with query objects it will also transform them using their configured SQL
* dialect.
*
* @param array $parts list of queries to be operated with UNION
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string
*/
protected function _buildUnionPart(array $parts, Query $query, ValueBinder $binder): string
{
$parts = array_map(function ($p) use ($binder) {
$p['query'] = $p['query']->sql($binder);
$p['query'] = $p['query'][0] === '(' ? trim($p['query'], '()') : $p['query'];
$prefix = $p['all'] ? 'ALL ' : '';
if ($this->_orderedUnion) {
return "{$prefix}({$p['query']})";
}
return $prefix . $p['query'];
}, $parts);
if ($this->_orderedUnion) {
return sprintf(")\nUNION %s", implode("\nUNION ", $parts));
}
return sprintf("\nUNION %s", implode("\nUNION ", $parts));
}
/**
* Builds the SQL fragment for INSERT INTO.
*
* @param array $parts The insert parts.
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string SQL fragment.
*/
protected function _buildInsertPart(array $parts, Query $query, ValueBinder $binder): string
{
if (!isset($parts[0])) {
throw new DatabaseException(
'Could not compile insert query. No table was specified. ' .
'Use `into()` to define a table.'
);
}
$table = $parts[0];
$columns = $this->_stringifyExpressions($parts[1], $binder);
$modifiers = $this->_buildModifierPart($query->clause('modifier'), $query, $binder);
return sprintf('INSERT%s INTO %s (%s)', $modifiers, $table, implode(', ', $columns));
}
/**
* Builds the SQL fragment for INSERT INTO.
*
* @param array $parts The values parts.
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string SQL fragment.
*/
protected function _buildValuesPart(array $parts, Query $query, ValueBinder $binder): string
{
return implode('', $this->_stringifyExpressions($parts, $binder));
}
/**
* Builds the SQL fragment for UPDATE.
*
* @param array $parts The update parts.
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string SQL fragment.
*/
protected function _buildUpdatePart(array $parts, Query $query, ValueBinder $binder): string
{
$table = $this->_stringifyExpressions($parts, $binder);
$modifiers = $this->_buildModifierPart($query->clause('modifier'), $query, $binder);
return sprintf('UPDATE%s %s', $modifiers, implode(',', $table));
}
/**
* Builds the SQL modifier fragment
*
* @param array $parts The query modifier parts
* @param \Cake\Database\Query $query The query that is being compiled
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @return string SQL fragment.
*/
protected function _buildModifierPart(array $parts, Query $query, ValueBinder $binder): string
{
if ($parts === []) {
return '';
}
return ' ' . implode(' ', $this->_stringifyExpressions($parts, $binder, false));
}
/**
* Helper function used to covert ExpressionInterface objects inside an array
* into their string representation.
*
* @param array $expressions list of strings and ExpressionInterface objects
* @param \Cake\Database\ValueBinder $binder Value binder used to generate parameter placeholder
* @param bool $wrap Whether to wrap each expression object with parenthesis
* @return array
*/
protected function _stringifyExpressions(array $expressions, ValueBinder $binder, bool $wrap = true): array
{
$result = [];
foreach ($expressions as $k => $expression) {
if ($expression instanceof ExpressionInterface) {
$value = $expression->sql($binder);
$expression = $wrap ? '(' . $value . ')' : $value;
}
$result[$k] = $expression;
}
return $result;
}
}