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\QueryBuilder; |
13
|
|
|
|
14
|
|
|
use Phuria\QueryBuilder\Expression\EmptyExpression; |
15
|
|
|
use Phuria\QueryBuilder\Expression\ExpressionCollection; |
16
|
|
|
use Phuria\QueryBuilder\Expression\ExpressionInterface; |
17
|
|
|
use Phuria\QueryBuilder\Expression\QueryClauseExpression; |
18
|
|
|
use Phuria\QueryBuilder\Table\AbstractTable; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class QueryBuilder |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TableFactory $tableFactory |
27
|
|
|
*/ |
28
|
|
|
private $tableFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var CompilerManager $compilerManager |
32
|
|
|
*/ |
33
|
|
|
private $compilerManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var QueryClauses |
37
|
|
|
*/ |
38
|
|
|
private $queryClauses; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var AbstractTable[] $tables |
42
|
|
|
*/ |
43
|
|
|
private $tables = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ExpressionInterface $limit |
47
|
|
|
*/ |
48
|
|
|
private $limit; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param TableFactory $tableFactory |
52
|
|
|
* @param CompilerManager $compilerManager |
53
|
|
|
*/ |
54
|
26 |
|
public function __construct(TableFactory $tableFactory = null, CompilerManager $compilerManager = null) |
55
|
|
|
{ |
56
|
26 |
|
$this->tableFactory = $tableFactory ?: new TableFactory(); |
57
|
26 |
|
$this->compilerManager = $compilerManager ?: new CompilerManager(); |
58
|
26 |
|
$this->queryClauses = new QueryClauses(); |
59
|
26 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return ExprBuilder |
63
|
|
|
*/ |
64
|
2 |
|
public function expr() |
65
|
|
|
{ |
66
|
2 |
|
return new ExprBuilder(func_get_args()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
22 |
|
public function addSelect() |
73
|
|
|
{ |
74
|
22 |
|
$this->queryClauses->addSelect(...func_get_args()); |
75
|
|
|
|
76
|
22 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
3 |
|
public function andWhere() |
83
|
|
|
{ |
84
|
3 |
|
$this->queryClauses->andWhere(...func_get_args()); |
85
|
|
|
|
86
|
3 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
1 |
|
public function andHaving() |
93
|
|
|
{ |
94
|
1 |
|
$this->queryClauses->andHaving(...func_get_args()); |
95
|
|
|
|
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
1 |
|
public function addOrderBy() |
103
|
|
|
{ |
104
|
1 |
|
$this->queryClauses->addOrderBy(...func_get_args()); |
105
|
|
|
|
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
1 |
|
public function addSet() |
113
|
|
|
{ |
114
|
1 |
|
$this->queryClauses->addSet(...func_get_args()); |
115
|
|
|
|
116
|
1 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
2 |
|
public function addGroupBy() |
123
|
|
|
{ |
124
|
2 |
|
$this->queryClauses->addGroupBy(...func_get_args()); |
125
|
|
|
|
126
|
2 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return QueryClauses |
131
|
|
|
*/ |
132
|
23 |
|
public function getQueryClauses() |
133
|
|
|
{ |
134
|
23 |
|
return $this->queryClauses; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $hint |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
1 |
|
public function addHint($hint) |
|
|
|
|
143
|
|
|
{ |
144
|
1 |
|
$this->queryClauses->addHint(...func_get_args()); |
145
|
|
|
|
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $table |
151
|
|
|
* |
152
|
|
|
* @return AbstractTable |
153
|
|
|
*/ |
154
|
24 |
|
private function addRootTable($table) |
155
|
|
|
{ |
156
|
24 |
|
$table = $this->tableFactory->createNewTable($table, $this); |
157
|
24 |
|
$table->setRoot(true); |
158
|
|
|
|
159
|
24 |
|
$this->tables[] = $table; |
160
|
|
|
|
161
|
24 |
|
return $table; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param mixed $table |
166
|
|
|
* |
167
|
|
|
* @return AbstractTable |
168
|
|
|
*/ |
169
|
23 |
|
public function from($table) |
170
|
|
|
{ |
171
|
23 |
|
return $this->addFrom($table); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param mixed $table |
176
|
|
|
* |
177
|
|
|
* @return AbstractTable |
178
|
|
|
*/ |
179
|
23 |
|
public function addFrom($table) |
180
|
|
|
{ |
181
|
23 |
|
return $this->addRootTable($table); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param mixed $table |
186
|
|
|
* |
187
|
|
|
* @return AbstractTable |
188
|
|
|
*/ |
189
|
1 |
|
public function update($table) |
190
|
|
|
{ |
191
|
1 |
|
return $this->addRootTable($table); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $joinType |
196
|
|
|
* @param mixed $table |
197
|
|
|
* |
198
|
|
|
* @return AbstractTable |
199
|
|
|
*/ |
200
|
3 |
|
public function join($joinType, $table) |
201
|
|
|
{ |
202
|
3 |
|
$table = $this->tableFactory->createNewTable($table, $this); |
203
|
3 |
|
$table->setJoinType($joinType); |
204
|
|
|
|
205
|
3 |
|
$this->tables[] = $table; |
206
|
|
|
|
207
|
3 |
|
return $table; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param mixed $table |
212
|
|
|
* |
213
|
|
|
* @return AbstractTable |
214
|
|
|
*/ |
215
|
1 |
|
public function crossJoin($table) |
216
|
|
|
{ |
217
|
1 |
|
return $this->join(AbstractTable::CROSS_JOIN, $table); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param mixed $table |
222
|
|
|
* |
223
|
|
|
* @return AbstractTable |
224
|
|
|
*/ |
225
|
2 |
|
public function leftJoin($table) |
226
|
|
|
{ |
227
|
2 |
|
return $this->join(AbstractTable::LEFT_JOIN, $table); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $table |
232
|
|
|
* |
233
|
|
|
* @return AbstractTable |
234
|
|
|
*/ |
235
|
1 |
|
public function innerJoin($table) |
236
|
|
|
{ |
237
|
1 |
|
return $this->join(AbstractTable::INNER_JOIN, $table); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return $this |
242
|
|
|
*/ |
243
|
1 |
|
public function limit() |
244
|
|
|
{ |
245
|
1 |
|
$this->limit = ExprNormalizer::normalizeExpression(func_get_args()); |
246
|
|
|
|
247
|
1 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
23 |
|
public function buildSQL() |
254
|
|
|
{ |
255
|
23 |
|
return $this->compilerManager->compile($this); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return Query |
260
|
|
|
*/ |
261
|
11 |
|
public function buildQuery() |
262
|
|
|
{ |
263
|
11 |
|
return new Query($this->buildSQL()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return AbstractTable[] |
268
|
|
|
*/ |
269
|
23 |
|
public function getTables() |
270
|
|
|
{ |
271
|
23 |
|
return $this->tables; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return ExpressionCollection |
276
|
|
|
*/ |
277
|
23 |
|
public function getRootTables() |
278
|
|
|
{ |
279
|
|
|
return new ExpressionCollection(array_filter($this->getTables(), function (AbstractTable $table) { |
280
|
22 |
|
return $table->isRoot(); |
281
|
23 |
|
}), ', '); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return ExpressionCollection |
286
|
|
|
*/ |
287
|
|
|
public function getJoinTables() |
288
|
|
|
{ |
289
|
22 |
|
return new ExpressionCollection(array_filter($this->getTables(), function (AbstractTable $table) { |
290
|
21 |
|
return $table->isJoin(); |
291
|
22 |
|
}), ' '); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return ExpressionInterface |
296
|
|
|
*/ |
297
|
22 |
|
public function getLimitExpression() |
298
|
|
|
{ |
299
|
22 |
|
$expr = $this->limit; |
300
|
|
|
|
301
|
22 |
|
if (!$expr) { |
302
|
21 |
|
return new EmptyExpression(); |
303
|
|
|
} |
304
|
|
|
|
305
|
1 |
|
if ($expr instanceof ExpressionCollection) { |
306
|
1 |
|
$expr = $expr->changeSeparator(', '); |
307
|
1 |
|
} |
308
|
|
|
|
309
|
1 |
|
return new QueryClauseExpression( |
310
|
1 |
|
QueryClauseExpression::CLAUSE_LIMIT, |
311
|
|
|
$expr |
312
|
1 |
|
); |
313
|
|
|
} |
314
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.