1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Phuria SQL Builder package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phuria\SQLBuilder\QueryCompiler; |
13
|
|
|
|
14
|
|
|
use Phuria\SQLBuilder\QueryBuilder\AbstractBuilder; |
15
|
|
|
use Phuria\SQLBuilder\QueryBuilder\AbstractInsertBuilder; |
16
|
|
|
use Phuria\SQLBuilder\QueryBuilder\BuilderInterface; |
17
|
|
|
use Phuria\SQLBuilder\QueryBuilder\Clause; |
18
|
|
|
use Phuria\SQLBuilder\QueryBuilder\Component; |
19
|
|
|
use Phuria\SQLBuilder\QueryBuilder\InsertSelectBuilder; |
20
|
|
|
use Phuria\SQLBuilder\QueryBuilder\UpdateBuilder; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class QueryCompiler implements QueryCompilerInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var TableCompiler |
29
|
|
|
*/ |
30
|
|
|
private $tableCompiler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ReferenceCompiler |
34
|
|
|
*/ |
35
|
|
|
private $referenceCompiler; |
36
|
|
|
|
37
|
6 |
|
public function __construct() |
38
|
|
|
{ |
39
|
6 |
|
$this->tableCompiler = new TableCompiler(); |
40
|
6 |
|
$this->referenceCompiler = new ReferenceCompiler(); |
41
|
6 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
34 |
|
public function compile(BuilderInterface $qb) |
47
|
|
|
{ |
48
|
34 |
|
$compiledSQL = $this->compileNativeSQL($qb); |
49
|
|
|
|
50
|
34 |
|
if ($qb instanceof AbstractBuilder) { |
51
|
34 |
|
$references = $qb->getParameterManager()->getReferences(); |
52
|
34 |
|
$compiledSQL = $this->referenceCompiler->compile($compiledSQL, $references); |
53
|
34 |
|
} |
54
|
|
|
|
55
|
34 |
|
return $compiledSQL; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param BuilderInterface $qb |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
34 |
|
private function compileNativeSQL(BuilderInterface $qb) |
64
|
|
|
{ |
65
|
34 |
|
return implode(' ', array_filter([ |
66
|
34 |
|
$this->compileDelete($qb), |
67
|
34 |
|
$this->compileInsert($qb), |
68
|
34 |
|
$this->compileUpdate($qb), |
69
|
34 |
|
$this->compileSelect($qb), |
70
|
34 |
|
$this->tableCompiler->compileRootTables($qb), |
71
|
34 |
|
$this->tableCompiler->compileJoinTables($qb), |
72
|
34 |
|
$this->compileInsertColumns($qb), |
73
|
34 |
|
$this->compileInsertSelect($qb), |
74
|
34 |
|
$this->compileSet($qb), |
75
|
34 |
|
$this->compileWhere($qb), |
76
|
34 |
|
$this->compileInsertValues($qb), |
77
|
34 |
|
$this->compileGroupBy($qb), |
78
|
34 |
|
$this->compileHaving($qb), |
79
|
34 |
|
$this->compileOrderBy($qb), |
80
|
34 |
|
$this->compileLimit($qb) |
81
|
34 |
|
])); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param BuilderInterface $qb |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
34 |
|
private function compileDelete(BuilderInterface $qb) |
90
|
|
|
{ |
91
|
34 |
|
if ($qb instanceof Clause\DeleteClauseInterface) { |
92
|
2 |
|
return $qb->getDeleteClauses() ? 'DELETE ' . implode(', ', $qb->getDeleteClauses()) : 'DELETE'; |
93
|
|
|
} |
94
|
|
|
|
95
|
32 |
|
return ''; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param BuilderInterface $qb |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
34 |
|
private function compileInsert(BuilderInterface $qb) |
104
|
|
|
{ |
105
|
34 |
|
if ($qb instanceof AbstractInsertBuilder) { |
106
|
3 |
|
return 'INSERT INTO'; |
107
|
|
|
} |
108
|
|
|
|
109
|
32 |
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param BuilderInterface $qb |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
34 |
|
private function compileUpdate(BuilderInterface $qb) |
118
|
|
|
{ |
119
|
34 |
|
if ($qb instanceof UpdateBuilder) { |
120
|
4 |
|
return $qb->isIgnore() ? 'UPDATE IGNORE' : 'UPDATE'; |
121
|
|
|
} |
122
|
|
|
|
123
|
30 |
|
return ''; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param BuilderInterface $qb |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
34 |
|
private function compileSelect(BuilderInterface $qb) |
132
|
|
|
{ |
133
|
34 |
|
if ($qb instanceof Clause\SelectClauseInterface) { |
134
|
26 |
|
return 'SELECT ' . implode(', ', $qb->getSelectClauses()); |
135
|
|
|
} |
136
|
|
|
|
137
|
9 |
|
return ''; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param BuilderInterface $qb |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
34 |
|
private function compileSet(BuilderInterface $qb) |
146
|
|
|
{ |
147
|
34 |
|
if ($qb instanceof Clause\SetClauseInterface && $qb->getSetClauses()) { |
148
|
4 |
|
return 'SET ' . implode(', ', $qb->getSetClauses()); |
149
|
|
|
} |
150
|
|
|
|
151
|
30 |
|
return ''; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param BuilderInterface $qb |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
34 |
|
private function compileWhere(BuilderInterface $qb) |
160
|
|
|
{ |
161
|
34 |
|
if ($qb instanceof Clause\WhereClauseInterface && $qb->getWhereClauses()) { |
162
|
9 |
|
return 'WHERE ' . implode(' AND ', $qb->getWhereClauses()); |
163
|
|
|
} |
164
|
|
|
|
165
|
25 |
|
return ''; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param BuilderInterface $qb |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
34 |
|
private function compileGroupBy(BuilderInterface $qb) |
174
|
|
|
{ |
175
|
34 |
|
if ($qb instanceof Clause\GroupByClauseInterface && $qb->getGroupByClauses()) { |
176
|
5 |
|
$clause = 'GROUP BY ' . implode(', ', $qb->getGroupByClauses()); |
177
|
|
|
|
178
|
5 |
|
return $qb->isGroupByWithRollUp() ? $clause . ' WITH ROLLUP' : $clause; |
179
|
|
|
} |
180
|
|
|
|
181
|
31 |
|
return ''; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param BuilderInterface $qb |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
34 |
|
private function compileHaving(BuilderInterface $qb) |
190
|
|
|
{ |
191
|
34 |
|
if ($qb instanceof Clause\HavingClauseInterface && $qb->getHavingClauses()) { |
192
|
1 |
|
return 'HAVING ' . implode(' AND ', $qb->getHavingClauses()); |
193
|
|
|
} |
194
|
|
|
|
195
|
33 |
|
return ''; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param BuilderInterface $qb |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
34 |
|
private function compileOrderBy(BuilderInterface $qb) |
204
|
|
|
{ |
205
|
34 |
|
if ($qb instanceof Clause\OrderByClauseInterface && $qb->getOrderByClauses()) { |
206
|
2 |
|
return 'ORDER BY ' . implode(', ', $qb->getOrderByClauses()); |
207
|
|
|
} |
208
|
|
|
|
209
|
32 |
|
return ''; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param BuilderInterface $qb |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
34 |
|
private function compileLimit(BuilderInterface $qb) |
218
|
|
|
{ |
219
|
34 |
|
if ($qb instanceof Clause\LimitClauseInterface && $qb->getLimitClause()) { |
220
|
2 |
|
return 'LIMIT ' . $qb->getLimitClause(); |
221
|
|
|
} |
222
|
|
|
|
223
|
32 |
|
return ''; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param BuilderInterface $qb |
228
|
|
|
* |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
34 |
|
private function compileInsertValues(BuilderInterface $qb) |
232
|
|
|
{ |
233
|
34 |
|
if ($qb instanceof Component\InsertValuesComponentInterface) { |
234
|
2 |
|
return 'VALUES ' . implode(', ', array_map(function (array $values) { |
235
|
2 |
|
return '('. implode(', ', $values) . ')'; |
236
|
2 |
|
}, $qb->getValues())); |
237
|
|
|
} |
238
|
|
|
|
239
|
32 |
|
return ''; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param BuilderInterface $qb |
244
|
|
|
* |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
34 |
|
private function compileInsertSelect(BuilderInterface $qb) |
248
|
|
|
{ |
249
|
34 |
|
if ($qb instanceof InsertSelectBuilder) { |
250
|
1 |
|
return $qb->getSelectInsert()->buildSQL(); |
251
|
|
|
} |
252
|
|
|
|
253
|
34 |
|
return ''; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param BuilderInterface $qb |
258
|
|
|
* |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
34 |
|
private function compileInsertColumns(BuilderInterface $qb) |
262
|
|
|
{ |
263
|
34 |
|
if ($qb instanceof Clause\InsertColumnsClauseInterface) { |
264
|
3 |
|
return '(' . implode(', ', $qb->getColumns()) . ')'; |
265
|
|
|
} |
266
|
|
|
|
267
|
32 |
|
return ''; |
268
|
|
|
} |
269
|
|
|
} |