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