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\Table\AbstractTable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class QueryClauses |
20
|
|
|
{ |
21
|
|
|
const QUERY_SELECT = 1; |
22
|
|
|
const QUERY_UPDATE = 2; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var QueryBuilder $qb |
26
|
|
|
*/ |
27
|
|
|
private $qb; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array $selectClauses |
31
|
|
|
*/ |
32
|
|
|
private $selectClauses = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array $whereClauses |
36
|
|
|
*/ |
37
|
|
|
private $whereClauses = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array $orderByClauses |
41
|
|
|
*/ |
42
|
|
|
private $orderByClauses = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array $setClauses |
46
|
|
|
*/ |
47
|
|
|
private $setClauses = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array $groupByClauses |
51
|
|
|
*/ |
52
|
|
|
private $groupByClauses = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array $havingClauses |
56
|
|
|
*/ |
57
|
|
|
private $havingClauses = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string $limitClause |
61
|
|
|
*/ |
62
|
|
|
private $limitClause; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param QueryBuilder $qb |
66
|
|
|
*/ |
67
|
24 |
|
public function __construct(QueryBuilder $qb) |
68
|
|
|
{ |
69
|
24 |
|
$this->qb = $qb; |
70
|
24 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
21 |
|
public function guessQueryType() |
76
|
|
|
{ |
77
|
21 |
|
if ($this->selectClauses) { |
|
|
|
|
78
|
20 |
|
return static::QUERY_SELECT; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return static::QUERY_UPDATE; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $clause |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
20 |
|
public function addSelect($clause) |
90
|
|
|
{ |
91
|
20 |
|
$this->selectClauses[] = $clause; |
92
|
|
|
|
93
|
20 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $clause |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
3 |
|
public function andWhere($clause) |
102
|
|
|
{ |
103
|
3 |
|
$this->whereClauses[] = $clause; |
104
|
|
|
|
105
|
3 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $clause |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
1 |
|
public function andHaving($clause) |
114
|
|
|
{ |
115
|
1 |
|
$this->havingClauses[] = $clause; |
116
|
|
|
|
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $clause |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
1 |
|
public function addOrderBy($clause) |
126
|
|
|
{ |
127
|
1 |
|
$this->orderByClauses[] = $clause; |
128
|
|
|
|
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $clause |
134
|
|
|
* |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
1 |
|
public function addSet($clause) |
138
|
|
|
{ |
139
|
1 |
|
$this->setClauses[] = $clause; |
140
|
|
|
|
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $clause |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
2 |
|
public function addGroupBy($clause) |
150
|
|
|
{ |
151
|
2 |
|
$this->groupByClauses[] = $clause; |
152
|
|
|
|
153
|
2 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $clause |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
1 |
|
public function setLimit($clause) |
162
|
|
|
{ |
163
|
1 |
|
$this->limitClause = $clause; |
164
|
|
|
|
165
|
1 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
20 |
|
public function getRawSelectClause() |
172
|
|
|
{ |
173
|
20 |
|
if ($this->selectClauses) { |
|
|
|
|
174
|
20 |
|
return 'SELECT ' . implode(', ', $this->selectClauses); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return ''; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
1 |
|
public function getRawUpdateClause() |
184
|
|
|
{ |
185
|
1 |
|
$rootTables = $this->qb->getRootTables(); |
186
|
|
|
|
187
|
1 |
|
if (0 === count($rootTables)) { |
188
|
|
|
return ''; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return 'UPDATE ' . implode(', ', array_map(function (AbstractTable $table) { |
192
|
1 |
|
if ($table->getAlias()) { |
193
|
|
|
return $table->getTableName() . ' AS ' . $table->getAlias(); |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
return $table->getTableName(); |
197
|
1 |
|
}, $rootTables)); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
20 |
|
public function getRawFromClause() |
204
|
|
|
{ |
205
|
20 |
|
$rootTables = $this->qb->getRootTables(); |
206
|
|
|
|
207
|
20 |
|
if (0 === count($rootTables)) { |
208
|
1 |
|
return ''; |
209
|
|
|
} |
210
|
|
|
|
211
|
19 |
|
return 'FROM ' . implode(', ', array_map(function (AbstractTable $table) { |
212
|
19 |
|
if ($table->getAlias()) { |
213
|
9 |
|
return $table->getTableName() . ' AS ' . $table->getAlias(); |
214
|
|
|
} |
215
|
|
|
|
216
|
15 |
|
return $table->getTableName(); |
217
|
19 |
|
}, $rootTables)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
20 |
|
public function getRawJoinClause() |
224
|
|
|
{ |
225
|
20 |
|
$joinTables = $this->qb->getJoinTables(); |
226
|
|
|
|
227
|
20 |
|
if (0 === count($joinTables)) { |
228
|
17 |
|
return ''; |
229
|
|
|
} |
230
|
|
|
|
231
|
3 |
|
$joins = []; |
232
|
|
|
|
233
|
3 |
|
foreach ($joinTables as $table) { |
234
|
3 |
|
$clause = $table->getJoinType() . ' ' . $table->getTableName(); |
235
|
|
|
|
236
|
3 |
|
if ($table->getAlias()) { |
237
|
1 |
|
$clause .= ' AS ' . $table->getAlias(); |
238
|
1 |
|
} |
239
|
|
|
|
240
|
3 |
|
if ($table->getJoinOn()) { |
241
|
2 |
|
$clause .= ' ON ' . $table->getJoinOn(); |
242
|
2 |
|
} |
243
|
|
|
|
244
|
3 |
|
$joins[] = $clause; |
245
|
3 |
|
} |
246
|
|
|
|
247
|
3 |
|
return implode(' ', $joins); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
20 |
|
public function getRawWhereClause() |
254
|
|
|
{ |
255
|
20 |
|
if ($this->whereClauses) { |
|
|
|
|
256
|
3 |
|
return 'WHERE ' . implode(' AND ', $this->whereClauses); |
257
|
|
|
} |
258
|
|
|
|
259
|
17 |
|
return ''; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return string |
264
|
|
|
*/ |
265
|
20 |
|
public function getRawOrderByClause() |
266
|
|
|
{ |
267
|
20 |
|
if ($this->orderByClauses) { |
|
|
|
|
268
|
1 |
|
return 'ORDER BY ' . implode(', ', $this->orderByClauses); |
269
|
|
|
} |
270
|
|
|
|
271
|
19 |
|
return ''; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
1 |
|
public function getRawSetClause() |
278
|
|
|
{ |
279
|
1 |
|
if ($this->setClauses) { |
|
|
|
|
280
|
1 |
|
return 'SET ' . implode(', ', $this->setClauses); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return ''; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
20 |
|
public function getRawGroupByClause() |
290
|
|
|
{ |
291
|
20 |
|
if ($this->groupByClauses) { |
|
|
|
|
292
|
2 |
|
return 'GROUP BY ' . implode(', ', $this->groupByClauses); |
293
|
|
|
} |
294
|
|
|
|
295
|
18 |
|
return ''; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
20 |
|
public function getRawHavingClause() |
302
|
|
|
{ |
303
|
20 |
|
if ($this->havingClauses) { |
|
|
|
|
304
|
1 |
|
return 'HAVING ' . implode(' AND ', $this->havingClauses); |
305
|
|
|
} |
306
|
|
|
|
307
|
19 |
|
return ''; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return string |
312
|
|
|
*/ |
313
|
20 |
|
public function getRawLimitClause() |
314
|
|
|
{ |
315
|
20 |
|
if ($this->limitClause) { |
316
|
1 |
|
return 'LIMIT ' . $this->limitClause; |
317
|
|
|
} |
318
|
|
|
|
319
|
19 |
|
return ''; |
320
|
|
|
} |
321
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.