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\AliasManager\AliasManager; |
15
|
|
|
use Phuria\QueryBuilder\Table\AbstractTable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class QueryBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var TableFactory $tableFactory |
24
|
|
|
*/ |
25
|
|
|
private $tableFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var CompilerManager $compilerManager |
29
|
|
|
*/ |
30
|
|
|
private $compilerManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var QueryClauses |
34
|
|
|
*/ |
35
|
|
|
private $queryClauses; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var AbstractTable[] $tables |
39
|
|
|
*/ |
40
|
|
|
private $tables = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ReferenceManager $referenceManager |
44
|
|
|
*/ |
45
|
|
|
private $referenceManager; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param TableFactory $tableFactory |
49
|
|
|
* @param CompilerManager $compilerManager |
50
|
|
|
*/ |
51
|
24 |
|
public function __construct(TableFactory $tableFactory = null, CompilerManager $compilerManager = null) |
52
|
|
|
{ |
53
|
24 |
|
$this->tableFactory = $tableFactory ?: new TableFactory(); |
54
|
24 |
|
$this->compilerManager = $compilerManager ?: new CompilerManager(); |
55
|
24 |
|
$this->queryClauses = new QueryClauses($this); |
56
|
24 |
|
$this->aliasManager = new AliasManager(); |
|
|
|
|
57
|
24 |
|
$this->referenceManager = new ReferenceManager(); |
58
|
24 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return AliasManager |
62
|
|
|
*/ |
63
|
1 |
|
public function getAliasManager() |
64
|
|
|
{ |
65
|
1 |
|
return $this->aliasManager; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $clause |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
20 |
|
public function addSelect($clause) |
74
|
|
|
{ |
75
|
20 |
|
$this->queryClauses->addSelect($clause); |
76
|
|
|
|
77
|
20 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $clause |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
3 |
|
public function andWhere($clause) |
86
|
|
|
{ |
87
|
3 |
|
$this->queryClauses->andWhere($clause); |
88
|
|
|
|
89
|
3 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $clause |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
1 |
|
public function andHaving($clause) |
98
|
|
|
{ |
99
|
1 |
|
$this->queryClauses->andHaving($clause); |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
1 |
|
public function addOrderBy() |
108
|
|
|
{ |
109
|
1 |
|
$this->queryClauses->addOrderBy(...func_get_args()); |
|
|
|
|
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
1 |
|
public function addSet() |
118
|
|
|
{ |
119
|
1 |
|
$this->queryClauses->addSet(...func_get_args()); |
|
|
|
|
120
|
|
|
|
121
|
1 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
2 |
|
public function addGroupBy() |
128
|
|
|
{ |
129
|
2 |
|
$this->queryClauses->addGroupBy(...func_get_args()); |
|
|
|
|
130
|
|
|
|
131
|
2 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return QueryClauses |
136
|
|
|
*/ |
137
|
21 |
|
public function getQueryClauses() |
138
|
|
|
{ |
139
|
21 |
|
return $this->queryClauses; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $table |
144
|
|
|
* |
145
|
|
|
* @return AbstractTable |
146
|
|
|
*/ |
147
|
22 |
|
private function addRootTable($table) |
148
|
|
|
{ |
149
|
22 |
|
$table = $this->tableFactory->createNewTable($table, $this); |
150
|
22 |
|
$table->setRoot(true); |
151
|
|
|
|
152
|
22 |
|
$this->tables[] = $table; |
153
|
|
|
|
154
|
22 |
|
return $table; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param mixed $table |
159
|
|
|
* |
160
|
|
|
* @return AbstractTable |
161
|
|
|
*/ |
162
|
21 |
|
public function from($table) |
163
|
|
|
{ |
164
|
21 |
|
return $this->addFrom($table); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param mixed $table |
169
|
|
|
* |
170
|
|
|
* @return AbstractTable |
171
|
|
|
*/ |
172
|
21 |
|
public function addFrom($table) |
173
|
|
|
{ |
174
|
21 |
|
return $this->addRootTable($table); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param mixed $table |
179
|
|
|
* |
180
|
|
|
* @return AbstractTable |
181
|
|
|
*/ |
182
|
1 |
|
public function update($table) |
183
|
|
|
{ |
184
|
1 |
|
return $this->addRootTable($table); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $joinType |
189
|
|
|
* @param mixed $table |
190
|
|
|
* |
191
|
|
|
* @return AbstractTable |
192
|
|
|
*/ |
193
|
3 |
|
public function join($joinType, $table) |
194
|
|
|
{ |
195
|
3 |
|
$table = $this->tableFactory->createNewTable($table, $this); |
196
|
3 |
|
$table->setJoinType($joinType); |
197
|
|
|
|
198
|
3 |
|
$this->tables[] = $table; |
199
|
|
|
|
200
|
3 |
|
return $table; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param mixed $table |
205
|
|
|
* |
206
|
|
|
* @return AbstractTable |
207
|
|
|
*/ |
208
|
1 |
|
public function crossJoin($table) |
209
|
|
|
{ |
210
|
1 |
|
return $this->join(AbstractTable::CROSS_JOIN, $table); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param mixed $table |
215
|
|
|
* |
216
|
|
|
* @return AbstractTable |
217
|
|
|
*/ |
218
|
2 |
|
public function leftJoin($table) |
219
|
|
|
{ |
220
|
2 |
|
return $this->join(AbstractTable::LEFT_JOIN, $table); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $table |
225
|
|
|
* |
226
|
|
|
* @return AbstractTable |
227
|
|
|
*/ |
228
|
1 |
|
public function innerJoin($table) |
229
|
|
|
{ |
230
|
1 |
|
return $this->join(AbstractTable::INNER_JOIN, $table); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string $clause |
235
|
|
|
* |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
1 |
|
public function limit($clause) |
239
|
|
|
{ |
240
|
1 |
|
$this->queryClauses->setLimit($clause); |
241
|
|
|
|
242
|
1 |
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
21 |
|
public function buildSQL() |
249
|
|
|
{ |
250
|
21 |
|
return $this->compilerManager->compile($this); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return Query |
255
|
|
|
*/ |
256
|
10 |
|
public function buildQuery() |
257
|
|
|
{ |
258
|
10 |
|
return new Query($this->buildSQL()); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return AbstractTable[] |
263
|
|
|
*/ |
264
|
21 |
|
public function getTables() |
265
|
|
|
{ |
266
|
21 |
|
return $this->tables; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return AbstractTable[] |
271
|
|
|
*/ |
272
|
21 |
|
public function getRootTables() |
273
|
|
|
{ |
274
|
|
|
return array_filter($this->getTables(), function (AbstractTable $table) { |
275
|
20 |
|
return $table->isRoot(); |
276
|
21 |
|
}); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return AbstractTable[] |
281
|
|
|
*/ |
282
|
|
|
public function getJoinTables() |
283
|
|
|
{ |
284
|
20 |
|
return array_filter($this->getTables(), function (AbstractTable $table) { |
285
|
19 |
|
return $table->isJoin(); |
286
|
20 |
|
}); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return ReferenceManager |
291
|
|
|
*/ |
292
|
21 |
|
public function getReferenceManager() |
293
|
|
|
{ |
294
|
21 |
|
return $this->referenceManager; |
295
|
|
|
} |
296
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: