1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelCte\Query\Grammars; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
trait CompilesExpressions |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Create a new grammar instance. |
12
|
|
|
*/ |
13
|
94 |
|
public function __construct() |
14
|
|
|
{ |
15
|
94 |
|
array_unshift($this->selectComponents, 'expressions'); |
16
|
|
|
|
17
|
94 |
|
$this->selectComponents[] = 'recursionLimit'; |
|
|
|
|
18
|
94 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Compile the common table expressions. |
22
|
|
|
* |
23
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
24
|
|
|
* @param array $expressions |
25
|
|
|
* @return string |
26
|
94 |
|
*/ |
27
|
|
|
public function compileExpressions(Builder $query, array $expressions) |
|
|
|
|
28
|
94 |
|
{ |
29
|
90 |
|
if (!$expressions) { |
|
|
|
|
30
|
|
|
return ''; |
31
|
|
|
} |
32
|
82 |
|
|
33
|
|
|
$recursive = $this->recursiveKeyword($expressions); |
34
|
82 |
|
|
35
|
|
|
$statements = []; |
36
|
82 |
|
|
37
|
82 |
|
foreach ($expressions as $expression) { |
38
|
|
|
$columns = $expression['columns'] ? '('.$this->columnize($expression['columns']).') ' : ''; |
|
|
|
|
39
|
82 |
|
|
40
|
18 |
|
$materialized = !is_null($expression['materialized']) |
41
|
64 |
|
? ($expression['materialized'] ? 'materialized ' : 'not materialized ') |
42
|
|
|
: ''; |
43
|
82 |
|
|
44
|
|
|
$statements[] = $this->wrapTable($expression['name']).' '.$columns.'as '.$materialized.'('.$expression['query'].')'; |
|
|
|
|
45
|
|
|
} |
46
|
82 |
|
|
47
|
|
|
return 'with '.$recursive.implode(', ', $statements); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the "recursive" keyword. |
52
|
|
|
* |
53
|
|
|
* @param array $expressions |
54
|
|
|
* @return string |
55
|
63 |
|
*/ |
56
|
|
|
protected function recursiveKeyword(array $expressions) |
57
|
63 |
|
{ |
58
|
|
|
return collect($expressions)->where('recursive', true)->isNotEmpty() ? 'recursive ' : ''; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Compile the recursion limit. |
63
|
|
|
* |
64
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
65
|
|
|
* @param int|null $recursionLimit |
66
|
11 |
|
* @return string |
67
|
|
|
*/ |
68
|
11 |
|
public function compileRecursionLimit(Builder $query, $recursionLimit) |
|
|
|
|
69
|
3 |
|
{ |
70
|
|
|
if (is_null($recursionLimit)) { |
71
|
|
|
return ''; |
72
|
8 |
|
} |
73
|
|
|
|
74
|
|
|
return 'option (maxrecursion '.(int) $recursionLimit.')'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Compile a select query into SQL. |
79
|
|
|
* |
80
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
7 |
|
public function compileSelect(Builder $query) |
84
|
|
|
{ |
85
|
7 |
|
$sql = parent::compileSelect($query); |
86
|
|
|
|
87
|
7 |
|
if ($query->unionExpressions) { |
88
|
|
|
$sql = $this->compileExpressions($query, $query->unionExpressions) . " $sql"; |
89
|
7 |
|
} |
90
|
|
|
|
91
|
7 |
|
if (!is_null($query->unionRecursionLimit)) { |
92
|
7 |
|
$sql .= ' ' . $this->compileRecursionLimit($query, $query->unionRecursionLimit); |
93
|
7 |
|
} |
94
|
7 |
|
|
95
|
|
|
return $sql; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Compile an insert statement using a subquery into SQL. |
100
|
|
|
* |
101
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
102
|
|
|
* @param array $columns |
103
|
|
|
* @param string $sql |
104
|
4 |
|
* @return string |
105
|
|
|
*/ |
106
|
4 |
|
public function compileInsertUsing(Builder $query, array $columns, string $sql) |
107
|
|
|
{ |
108
|
4 |
|
$expressions = $this->compileExpressions($query, $query->expressions); |
109
|
4 |
|
|
110
|
4 |
|
$recursionLimit = $this->compileRecursionLimit($query, $query->recursionLimit); |
111
|
|
|
|
112
|
|
|
$compiled = parent::compileInsertUsing($query, $columns, $sql); |
113
|
|
|
|
114
|
|
|
return (string) Str::of($compiled) |
115
|
|
|
->prepend($expressions, ' ') |
116
|
|
|
->append(' ', $recursionLimit) |
117
|
|
|
->trim(); |
118
|
|
|
} |
119
|
|
|
|
120
|
4 |
|
/** |
121
|
|
|
* Compile an update statement into SQL. |
122
|
4 |
|
* |
123
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
124
|
4 |
|
* @param array $values |
125
|
|
|
* @return string |
126
|
4 |
|
*/ |
127
|
|
|
public function compileUpdate(Builder $query, array $values) |
128
|
|
|
{ |
129
|
|
|
$compiled = parent::compileUpdate($query, $values); |
130
|
|
|
|
131
|
|
|
return (string) Str::of($compiled) |
132
|
|
|
->prepend($this->compileExpressions($query, $query->expressions), ' ') |
133
|
|
|
->trim(); |
134
|
|
|
} |
135
|
4 |
|
|
136
|
|
|
/** |
137
|
4 |
|
* Prepare the bindings for an update statement. |
138
|
|
|
* |
139
|
4 |
|
* @param array $bindings |
140
|
4 |
|
* @param array $values |
141
|
4 |
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function prepareBindingsForUpdate(array $bindings, array $values) |
144
|
|
|
{ |
145
|
|
|
$values = array_merge($bindings['expressions'], $values); |
146
|
|
|
|
147
|
|
|
unset($bindings['expressions']); |
148
|
|
|
|
149
|
|
|
return parent::prepareBindingsForUpdate($bindings, $values); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Compile a delete statement into SQL. |
154
|
|
|
* |
155
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function compileDelete(Builder $query) |
159
|
|
|
{ |
160
|
|
|
$compiled = parent::compileDelete($query); |
161
|
|
|
|
162
|
|
|
return (string) Str::of($compiled) |
163
|
|
|
->prepend($this->compileExpressions($query, $query->expressions), ' ') |
164
|
|
|
->trim(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|