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