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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
<?php namespace Illuminate\Database\Query\Grammars;
use Illuminate\Database\Query\Builder; use Illuminate\Database\Grammar as BaseGrammar;
class Grammar extends BaseGrammar {
/** * The components that make up a select clause. * * @var array */ protected $selectComponents = array( 'aggregate', 'columns', 'from', 'joins', 'wheres', 'groups', 'havings', 'orders', 'limit', 'offset', 'unions', 'lock', );
/** * Compile a select query into SQL. * * @param \Illuminate\Database\Query\Builder * @return string */ public function compileSelect(Builder $query) { if (is_null($query->columns)) $query->columns = array('*');
return trim($this->concatenate($this->compileComponents($query))); }
/** * Compile the components necessary for a select clause. * * @param \Illuminate\Database\Query\Builder * @return array */ protected function compileComponents(Builder $query) { $sql = array();
foreach ($this->selectComponents as $component) { // To compile the query, we'll spin through each component of the query and // see if that component exists. If it does we'll just call the compiler // function for the component which is responsible for making the SQL. if ( ! is_null($query->$component)) { $method = 'compile'.ucfirst($component);
$sql[$component] = $this->$method($query, $query->$component); } }
return $sql; }
/** * Compile an aggregated select clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $aggregate * @return string */ protected function compileAggregate(Builder $query, $aggregate) { $column = $this->columnize($aggregate['columns']);
// If the query has a "distinct" constraint and we're not asking for all columns // we need to prepend "distinct" onto the column name so that the query takes // it into account when it performs the aggregating operations on the data. if ($query->distinct && $column !== '*') { $column = 'distinct '.$column; }
return 'select '.$aggregate['function'].'('.$column.') as aggregate'; }
/** * Compile the "select *" portion of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $columns * @return string */ protected function compileColumns(Builder $query, $columns) { // If the query is actually performing an aggregating select, we will let that // compiler handle the building of the select clauses, as it will need some // more syntax that is best handled by that function to keep things neat. if ( ! is_null($query->aggregate)) return;
$select = $query->distinct ? 'select distinct ' : 'select ';
return $select.$this->columnize($columns); }
/** * Compile the "from" portion of the query. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @return string */ protected function compileFrom(Builder $query, $table) { return 'from '.$this->wrapTable($table); }
/** * Compile the "join" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $joins * @return string */ protected function compileJoins(Builder $query, $joins) { $sql = array();
foreach ($joins as $join) { $table = $this->wrapTable($join->table);
// First we need to build all of the "on" clauses for the join. There may be many // of these clauses so we will need to iterate through each one and build them // separately, then we'll join them up into a single string when we're done. $clauses = array();
foreach ($join->clauses as $clause) { $clauses[] = $this->compileJoinConstraint($clause); }
// Once we have constructed the clauses, we'll need to take the boolean connector // off of the first clause as it obviously will not be required on that clause // because it leads the rest of the clauses, thus not requiring any boolean. $clauses[0] = $this->removeLeadingBoolean($clauses[0]);
$clauses = implode(' ', $clauses);
$type = $join->type;
// Once we have everything ready to go, we will just concatenate all the parts to // build the final join statement SQL for the query and we can then return the // final clause back to the callers as a single, stringified join statement. $sql[] = "$type join $table on $clauses"; }
return implode(' ', $sql); }
/** * Create a join clause constraint segment. * * @param array $clause * @return string */ protected function compileJoinConstraint(array $clause) { $first = $this->wrap($clause['first']);
$second = $clause['where'] ? '?' : $this->wrap($clause['second']);
return "{$clause['boolean']} $first {$clause['operator']} $second"; }
/** * Compile the "where" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileWheres(Builder $query) { $sql = array();
if (is_null($query->wheres)) return '';
// Each type of where clauses has its own compiler function which is responsible // for actually creating the where clauses SQL. This helps keep the code nice // and maintainable since each clause has a very small method that it uses. foreach ($query->wheres as $where) { $method = "where{$where['type']}";
$sql[] = $where['boolean'].' '.$this->$method($query, $where); }
// If we actually have some where clauses, we will strip off the first boolean // operator, which is added by the query builders for convenience so we can // avoid checking for the first clauses in each of the compilers methods. if (count($sql) > 0) { $sql = implode(' ', $sql);
return 'where '.preg_replace('/and |or /', '', $sql, 1); }
return ''; }
/** * Compile a nested where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNested(Builder $query, $where) { $nested = $where['query'];
return '('.substr($this->compileWheres($nested), 6).')'; }
/** * Compile a where condition with a sub-select. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereSub(Builder $query, $where) { $select = $this->compileSelect($where['query']);
return $this->wrap($where['column']).' '.$where['operator']." ($select)"; }
/** * Compile a basic where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereBasic(Builder $query, $where) { $value = $this->parameter($where['value']);
return $this->wrap($where['column']).' '.$where['operator'].' '.$value; }
/** * Compile a "between" where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereBetween(Builder $query, $where) { $between = $where['not'] ? 'not between' : 'between';
return $this->wrap($where['column']).' '.$between.' ? and ?'; }
/** * Compile a where exists clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereExists(Builder $query, $where) { return 'exists ('.$this->compileSelect($where['query']).')'; }
/** * Compile a where exists clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNotExists(Builder $query, $where) { return 'not exists ('.$this->compileSelect($where['query']).')'; }
/** * Compile a "where in" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereIn(Builder $query, $where) { $values = $this->parameterize($where['values']);
return $this->wrap($where['column']).' in ('.$values.')'; }
/** * Compile a "where not in" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNotIn(Builder $query, $where) { $values = $this->parameterize($where['values']);
return $this->wrap($where['column']).' not in ('.$values.')'; }
/** * Compile a where in sub-select clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereInSub(Builder $query, $where) { $select = $this->compileSelect($where['query']);
return $this->wrap($where['column']).' in ('.$select.')'; }
/** * Compile a where not in sub-select clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNotInSub(Builder $query, $where) { $select = $this->compileSelect($where['query']);
return $this->wrap($where['column']).' not in ('.$select.')'; }
/** * Compile a "where null" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNull(Builder $query, $where) { return $this->wrap($where['column']).' is null'; }
/** * Compile a "where not null" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNotNull(Builder $query, $where) { return $this->wrap($where['column']).' is not null'; }
/** * Compile a "where day" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereDay(Builder $query, $where) { return $this->dateBasedWhere('day', $query, $where); }
/** * Compile a "where month" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereMonth(Builder $query, $where) { return $this->dateBasedWhere('month', $query, $where); }
/** * Compile a "where year" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereYear(Builder $query, $where) { return $this->dateBasedWhere('year', $query, $where); }
/** * Compile a date based where clause. * * @param string $type * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function dateBasedWhere($type, Builder $query, $where) { $value = $this->parameter($where['value']);
return $type.'('.$this->wrap($where['column']).') '.$where['operator'].' '.$value; }
/** * Compile a raw where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereRaw(Builder $query, $where) { return $where['sql']; }
/** * Compile the "group by" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $groups * @return string */ protected function compileGroups(Builder $query, $groups) { return 'group by '.$this->columnize($groups); }
/** * Compile the "having" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $havings * @return string */ protected function compileHavings(Builder $query, $havings) { $sql = implode(' ', array_map(array($this, 'compileHaving'), $havings));
return 'having '.preg_replace('/and /', '', $sql, 1); }
/** * Compile a single having clause. * * @param array $having * @return string */ protected function compileHaving(array $having) { // If the having clause is "raw", we can just return the clause straight away // without doing any more processing on it. Otherwise, we will compile the // clause into SQL based on the components that make it up from builder. if ($having['type'] === 'raw') { return $having['boolean'].' '.$having['sql']; }
return $this->compileBasicHaving($having); }
/** * Compile a basic having clause. * * @param array $having * @return string */ protected function compileBasicHaving($having) { $column = $this->wrap($having['column']);
$parameter = $this->parameter($having['value']);
return 'and '.$column.' '.$having['operator'].' '.$parameter; }
/** * Compile the "order by" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $orders * @return string */ protected function compileOrders(Builder $query, $orders) { $me = $this;
return 'order by '.implode(', ', array_map(function($order) use ($me) { if (isset($order['sql'])) return $order['sql'];
return $me->wrap($order['column']).' '.$order['direction']; } , $orders)); }
/** * Compile the "limit" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param int $limit * @return string */ protected function compileLimit(Builder $query, $limit) { return 'limit '.(int) $limit; }
/** * Compile the "offset" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param int $offset * @return string */ protected function compileOffset(Builder $query, $offset) { return 'offset '.(int) $offset; }
/** * Compile the "union" queries attached to the main query. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileUnions(Builder $query) { $sql = '';
foreach ($query->unions as $union) { $sql .= $this->compileUnion($union); }
return ltrim($sql); }
/** * Compile a single union statement. * * @param array $union * @return string */ protected function compileUnion(array $union) { $joiner = $union['all'] ? ' union all ' : ' union ';
return $joiner.$union['query']->toSql(); }
/** * Compile an insert statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileInsert(Builder $query, array $values) { // Essentially we will force every insert to be treated as a batch insert which // simply makes creating the SQL easier for us since we can utilize the same // basic routine regardless of an amount of records given to us to insert. $table = $this->wrapTable($query->from);
if ( ! is_array(reset($values))) { $values = array($values); }
$columns = $this->columnize(array_keys(reset($values)));
// We need to build a list of parameter place-holders of values that are bound // to the query. Each insert should have the exact same amount of parameter // bindings so we can just go off the first list of values in this array. $parameters = $this->parameterize(reset($values));
$value = array_fill(0, count($values), "($parameters)");
$parameters = implode(', ', $value);
return "insert into $table ($columns) values $parameters"; }
/** * Compile an insert and get ID statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param string $sequence * @return string */ public function compileInsertGetId(Builder $query, $values, $sequence) { return $this->compileInsert($query, $values); }
/** * Compile an update statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileUpdate(Builder $query, $values) { $table = $this->wrapTable($query->from);
// Each one of the columns in the update statements needs to be wrapped in the // keyword identifiers, also a place-holder needs to be created for each of // the values in the list of bindings so we can make the sets statements. $columns = array();
foreach ($values as $key => $value) { $columns[] = $this->wrap($key).' = '.$this->parameter($value); }
$columns = implode(', ', $columns);
// If the query has any "join" clauses, we will setup the joins on the builder // and compile them so we can attach them to this update, as update queries // can get join statements to attach to other tables when they're needed. if (isset($query->joins)) { $joins = ' '.$this->compileJoins($query, $query->joins); } else { $joins = ''; }
// Of course, update queries may also be constrained by where clauses so we'll // need to compile the where clauses and attach it to the query so only the // intended records are updated by the SQL statements we generate to run. $where = $this->compileWheres($query);
return trim("update {$table}{$joins} set $columns $where"); }
/** * Compile a delete statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileDelete(Builder $query) { $table = $this->wrapTable($query->from);
$where = is_array($query->wheres) ? $this->compileWheres($query) : '';
return trim("delete from $table ".$where); }
/** * Compile a truncate table statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return array */ public function compileTruncate(Builder $query) { return array('truncate '.$this->wrapTable($query->from) => array()); }
/** * Compile the lock into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param bool|string $value * @return string */ protected function compileLock(Builder $query, $value) { return is_string($value) ? $value : ''; }
/** * Concatenate an array of segments, removing empties. * * @param array $segments * @return string */ protected function concatenate($segments) { return implode(' ', array_filter($segments, function($value) { return (string) $value !== ''; })); }
/** * Remove the leading boolean from a statement. * * @param string $value * @return string */ protected function removeLeadingBoolean($value) { return preg_replace('/and |or /', '', $value, 1); }
}
|