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\Connection\ConnectionInterface; |
15
|
|
|
use Phuria\QueryBuilder\Parameter\ParameterManager; |
16
|
|
|
use Phuria\QueryBuilder\Parameter\ParameterManagerInterface; |
17
|
|
|
use Phuria\QueryBuilder\QueryCompiler\QueryCompiler; |
18
|
|
|
use Phuria\QueryBuilder\QueryCompiler\QueryCompilerInterface; |
19
|
|
|
use Phuria\QueryBuilder\Table\AbstractTable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class QueryBuilder |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TableFactory $tableFactory |
28
|
|
|
*/ |
29
|
|
|
private $tableFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var QueryCompilerInterface $queryCompiler |
33
|
|
|
*/ |
34
|
|
|
private $queryCompiler; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var QueryClauses |
38
|
|
|
*/ |
39
|
|
|
private $queryClauses; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var AbstractTable[] $tables |
43
|
|
|
*/ |
44
|
|
|
private $tables = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ReferenceManager $referenceManager |
48
|
|
|
*/ |
49
|
|
|
private $referenceManager; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ParameterManagerInterface $parameterManager |
53
|
|
|
*/ |
54
|
|
|
private $parameterManager; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param TableFactory $tableFactory |
|
|
|
|
58
|
|
|
* @param QueryCompilerInterface $queryCompiler |
|
|
|
|
59
|
|
|
*/ |
60
|
27 |
|
public function __construct( |
61
|
|
|
TableFactory $tableFactory = null, |
62
|
|
|
QueryCompilerInterface $queryCompiler = null |
63
|
|
|
) { |
64
|
27 |
|
$this->tableFactory = $tableFactory ?: new TableFactory(); |
65
|
27 |
|
$this->queryCompiler = $queryCompiler ?: new QueryCompiler(); |
66
|
27 |
|
$this->queryClauses = new QueryClauses(); |
67
|
27 |
|
$this->referenceManager = new ReferenceManager(); |
68
|
27 |
|
$this->parameterManager = new ParameterManager(); |
69
|
27 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $clause |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
22 |
|
public function addSelect($clause) |
77
|
|
|
{ |
78
|
22 |
|
$this->queryClauses->addSelect($clause); |
79
|
|
|
|
80
|
22 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $clause |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
6 |
|
public function andWhere($clause) |
89
|
|
|
{ |
90
|
6 |
|
$this->queryClauses->andWhere($clause); |
91
|
|
|
|
92
|
6 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $clause |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
1 |
|
public function andHaving($clause) |
101
|
|
|
{ |
102
|
1 |
|
$this->queryClauses->andHaving($clause); |
103
|
|
|
|
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $clause |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
1 |
|
public function addOrderBy($clause) |
113
|
|
|
{ |
114
|
1 |
|
$this->queryClauses->addOrderBy($clause); |
115
|
|
|
|
116
|
1 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $clause |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
2 |
|
public function addSet($clause) |
125
|
|
|
{ |
126
|
2 |
|
$this->queryClauses->addSet($clause); |
127
|
|
|
|
128
|
2 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $clause |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
3 |
|
public function addGroupBy($clause) |
137
|
|
|
{ |
138
|
3 |
|
$this->queryClauses->addGroupBy($clause); |
139
|
|
|
|
140
|
3 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return QueryClauses |
145
|
|
|
*/ |
146
|
24 |
|
public function getQueryClauses() |
147
|
|
|
{ |
148
|
24 |
|
return $this->queryClauses; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $table |
153
|
|
|
* |
154
|
|
|
* @return AbstractTable |
155
|
|
|
*/ |
156
|
25 |
|
private function addRootTable($table) |
157
|
|
|
{ |
158
|
25 |
|
$table = $this->tableFactory->createNewTable($table, $this); |
159
|
25 |
|
$table->setRoot(true); |
160
|
|
|
|
161
|
25 |
|
$this->tables[] = $table; |
162
|
|
|
|
163
|
25 |
|
return $table; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param mixed $table |
168
|
|
|
* |
169
|
|
|
* @return AbstractTable |
170
|
|
|
*/ |
171
|
23 |
|
public function from($table) |
172
|
|
|
{ |
173
|
23 |
|
return $this->addFrom($table); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param mixed $table |
178
|
|
|
* |
179
|
|
|
* @return AbstractTable |
180
|
|
|
*/ |
181
|
23 |
|
public function addFrom($table) |
182
|
|
|
{ |
183
|
23 |
|
return $this->addRootTable($table); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param mixed $table |
188
|
|
|
* |
189
|
|
|
* @return AbstractTable |
190
|
|
|
*/ |
191
|
2 |
|
public function update($table) |
192
|
|
|
{ |
193
|
2 |
|
return $this->addRootTable($table); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $joinType |
198
|
|
|
* @param mixed $table |
199
|
|
|
* |
200
|
|
|
* @return AbstractTable |
201
|
|
|
*/ |
202
|
3 |
|
public function join($joinType, $table) |
203
|
|
|
{ |
204
|
3 |
|
$table = $this->tableFactory->createNewTable($table, $this); |
205
|
3 |
|
$table->setJoinType($joinType); |
206
|
|
|
|
207
|
3 |
|
$this->tables[] = $table; |
208
|
|
|
|
209
|
3 |
|
return $table; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param mixed $table |
214
|
|
|
* |
215
|
|
|
* @return AbstractTable |
216
|
|
|
*/ |
217
|
1 |
|
public function crossJoin($table) |
218
|
|
|
{ |
219
|
1 |
|
return $this->join(AbstractTable::CROSS_JOIN, $table); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param mixed $table |
224
|
|
|
* |
225
|
|
|
* @return AbstractTable |
226
|
|
|
*/ |
227
|
2 |
|
public function leftJoin($table) |
228
|
|
|
{ |
229
|
2 |
|
return $this->join(AbstractTable::LEFT_JOIN, $table); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $table |
234
|
|
|
* |
235
|
|
|
* @return AbstractTable |
236
|
|
|
*/ |
237
|
1 |
|
public function innerJoin($table) |
238
|
|
|
{ |
239
|
1 |
|
return $this->join(AbstractTable::INNER_JOIN, $table); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $clause |
244
|
|
|
* |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
1 |
|
public function limit($clause) |
248
|
|
|
{ |
249
|
1 |
|
$this->queryClauses->setLimit($clause); |
250
|
|
|
|
251
|
1 |
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param int $hint |
256
|
|
|
* @param mixed $value |
257
|
|
|
* |
258
|
|
|
* @return $this |
259
|
|
|
*/ |
260
|
1 |
|
public function addQueryHint($hint, $value = null) |
261
|
|
|
{ |
262
|
1 |
|
$this->queryClauses->addQueryHint($hint, $value); |
263
|
|
|
|
264
|
1 |
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
24 |
|
public function buildSQL() |
271
|
|
|
{ |
272
|
24 |
|
return $this->queryCompiler->compile($this); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param ConnectionInterface $connection |
|
|
|
|
277
|
|
|
* |
278
|
|
|
* @return Query |
279
|
|
|
*/ |
280
|
13 |
|
public function buildQuery(ConnectionInterface $connection = null) |
281
|
|
|
{ |
282
|
13 |
|
return new Query( |
283
|
13 |
|
$this->buildSQL(), |
284
|
13 |
|
$this->parameterManager, |
285
|
|
|
$connection |
286
|
13 |
|
); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return AbstractTable[] |
291
|
|
|
*/ |
292
|
24 |
|
public function getTables() |
293
|
|
|
{ |
294
|
24 |
|
return $this->tables; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return AbstractTable[] |
299
|
|
|
*/ |
300
|
24 |
|
public function getRootTables() |
301
|
|
|
{ |
302
|
|
|
return array_filter($this->getTables(), function(AbstractTable $table) { |
303
|
23 |
|
return $table->isRoot(); |
304
|
24 |
|
}); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return AbstractTable[] |
309
|
|
|
*/ |
310
|
|
|
public function getJoinTables() |
311
|
|
|
{ |
312
|
22 |
|
return array_filter($this->getTables(), function(AbstractTable $table) { |
313
|
21 |
|
return $table->isJoin(); |
314
|
22 |
|
}); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param int|string $name |
319
|
|
|
* @param mixed $value |
320
|
|
|
* |
321
|
|
|
* @return $this |
322
|
|
|
*/ |
323
|
1 |
|
public function setParameter($name, $value) |
324
|
|
|
{ |
325
|
1 |
|
$this->parameterManager->createOrGetParameter($name)->setValue($value); |
326
|
|
|
|
327
|
1 |
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return ReferenceManager |
332
|
|
|
*/ |
333
|
24 |
|
public function getReferenceManager() |
334
|
|
|
{ |
335
|
24 |
|
return $this->referenceManager; |
336
|
|
|
} |
337
|
|
|
} |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.