1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @link http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
namespace JAQB\Query; |
12
|
|
|
|
13
|
|
|
use JAQB\Operations\Executable; |
14
|
|
|
use JAQB\Operations\Fetchable; |
15
|
|
|
use JAQB\Statement\SelectStatement; |
16
|
|
|
use JAQB\Statement\FromStatement; |
17
|
|
|
use JAQB\Statement\WhereStatement; |
18
|
|
|
use JAQB\Statement\OrderStatement; |
19
|
|
|
use JAQB\Statement\LimitStatement; |
20
|
|
|
use JAQB\Statement\UnionStatement; |
21
|
|
|
|
22
|
|
|
class SelectQuery extends AbstractQuery |
23
|
|
|
{ |
24
|
|
|
use Executable, Fetchable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var SelectStatement |
28
|
|
|
*/ |
29
|
|
|
protected $select; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FromStatement |
33
|
|
|
*/ |
34
|
|
|
protected $from; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var WhereStatement |
38
|
|
|
*/ |
39
|
|
|
protected $where; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var WhereStatement |
43
|
|
|
*/ |
44
|
|
|
protected $having; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var OrderStatement |
48
|
|
|
*/ |
49
|
|
|
protected $orderBy; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var OrderStatement |
53
|
|
|
*/ |
54
|
|
|
protected $groupBy; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var LimitStatement |
58
|
|
|
*/ |
59
|
|
|
protected $limit; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var UnionStatement |
63
|
|
|
*/ |
64
|
|
|
protected $union; |
65
|
|
|
|
66
|
|
|
public function __construct() |
67
|
|
|
{ |
68
|
|
|
$this->select = new SelectStatement(); |
69
|
|
|
$this->from = new FromStatement(); |
70
|
|
|
$this->where = new WhereStatement(); |
71
|
|
|
$this->having = new WhereStatement(true); |
72
|
|
|
$this->orderBy = new OrderStatement(); |
73
|
|
|
$this->groupBy = new OrderStatement(true); |
74
|
|
|
$this->limit = new LimitStatement(); |
75
|
|
|
$this->union = new UnionStatement(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets the fields to be selected for the query. |
80
|
|
|
* |
81
|
|
|
* @param array|string $fields fields |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
|
|
public function select($fields) |
86
|
|
|
{ |
87
|
|
|
$this->select->addFields($fields); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets the table for the query. |
94
|
|
|
* |
95
|
|
|
* @param string $table table name |
96
|
|
|
* |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
|
|
public function from($table) |
100
|
|
|
{ |
101
|
|
|
$this->from->addTable($table); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Adds a join to the query. |
108
|
|
|
* |
109
|
|
|
* @param string $table table name |
110
|
|
|
* @param string $on ON condition |
111
|
|
|
* @param string $using USING columns |
112
|
|
|
* @param string $type optional join type if not JOIN |
113
|
|
|
* |
114
|
|
|
* @return self |
115
|
|
|
*/ |
116
|
|
|
public function join($table, $on = null, $using = null, $type = 'JOIN') |
117
|
|
|
{ |
118
|
|
|
$this->from->addJoin($table, $on, $using, $type); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Adds a where condition to the query. |
125
|
|
|
* |
126
|
|
|
* @param array|string $field |
127
|
|
|
* @param string $condition condition value (optional) |
128
|
|
|
* @param string $operator operator (optional) |
129
|
|
|
* |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function where($field, $condition = false, $operator = '=') |
133
|
|
|
{ |
134
|
|
|
if (func_num_args() >= 2) { |
135
|
|
|
$this->where->addCondition($field, $condition, $operator); |
136
|
|
|
} else { |
137
|
|
|
$this->where->addCondition($field); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Adds a where between condition to the query. |
145
|
|
|
* |
146
|
|
|
* @param string $field |
147
|
|
|
* @param mixed $a first between value |
148
|
|
|
* @param mixed $b second between value |
149
|
|
|
* |
150
|
|
|
* @return self |
151
|
|
|
*/ |
152
|
|
|
public function between($field, $a, $b) |
153
|
|
|
{ |
154
|
|
|
$this->where->addBetweenCondition($field, $a, $b); |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sets the limit for the query. |
161
|
|
|
* |
162
|
|
|
* @param int $limit |
163
|
|
|
* @param int $offset |
164
|
|
|
* |
165
|
|
|
* @return self |
166
|
|
|
*/ |
167
|
|
|
public function limit($limit, $offset = 0) |
168
|
|
|
{ |
169
|
|
|
$this->limit->setLimit($limit, $offset); |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Sets the group by fields for the query. |
176
|
|
|
* |
177
|
|
|
* @param string|array $fields |
178
|
|
|
* @param string $direction |
179
|
|
|
* |
180
|
|
|
* @return self |
181
|
|
|
*/ |
182
|
|
|
public function groupBy($fields, $direction = false) |
183
|
|
|
{ |
184
|
|
|
$this->groupBy->addFields($fields, $direction); |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Sets the having conditions for the query. |
191
|
|
|
* |
192
|
|
|
* @param array|string $field |
193
|
|
|
* @param string|bool $condition condition value (optional) |
194
|
|
|
* @param string $operator operator (optional) |
195
|
|
|
* |
196
|
|
|
* @return self |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function having($field, $condition = false, $operator = '=') |
199
|
|
|
{ |
200
|
|
|
if (func_num_args() >= 2) { |
201
|
|
|
$this->having->addCondition($field, $condition, $operator); |
202
|
|
|
} else { |
203
|
|
|
$this->having->addCondition($field); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Sets the order for the query. |
211
|
|
|
* |
212
|
|
|
* @param string|array $fields |
213
|
|
|
* @param string $direction |
214
|
|
|
* |
215
|
|
|
* @return self |
216
|
|
|
*/ |
217
|
|
|
public function orderBy($fields, $direction = false) |
218
|
|
|
{ |
219
|
|
|
$this->orderBy->addFields($fields, $direction); |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Unions another select query with this query. |
226
|
|
|
* |
227
|
|
|
* @param SelectQuery $query |
228
|
|
|
* @param string $type optional union type |
229
|
|
|
* |
230
|
|
|
* @return self |
231
|
|
|
*/ |
232
|
|
|
public function union(SelectQuery $query, $type = false) |
233
|
|
|
{ |
234
|
|
|
$this->union->addQuery($query, $type); |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Gets the select statement for the query. |
241
|
|
|
* |
242
|
|
|
* @return SelectStatement |
243
|
|
|
*/ |
244
|
|
|
public function getSelect() |
245
|
|
|
{ |
246
|
|
|
return $this->select; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Gets the from statement for the query. |
251
|
|
|
* |
252
|
|
|
* @return FromStatement |
253
|
|
|
*/ |
254
|
|
|
public function getFrom() |
255
|
|
|
{ |
256
|
|
|
return $this->from; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Gets the where statement for the query. |
261
|
|
|
* |
262
|
|
|
* @return WhereStatement |
263
|
|
|
*/ |
264
|
|
|
public function getWhere() |
265
|
|
|
{ |
266
|
|
|
return $this->where; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Gets the limit statement for the query. |
271
|
|
|
* |
272
|
|
|
* @return LimitStatement |
273
|
|
|
*/ |
274
|
|
|
public function getLimit() |
275
|
|
|
{ |
276
|
|
|
return $this->limit; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Gets the group by statement for the query. |
281
|
|
|
* |
282
|
|
|
* @return GroupByStatement |
283
|
|
|
*/ |
284
|
|
|
public function getGroupBy() |
285
|
|
|
{ |
286
|
|
|
return $this->groupBy; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Gets the having statement for the query. |
291
|
|
|
* |
292
|
|
|
* @return HavingStatement |
293
|
|
|
*/ |
294
|
|
|
public function getHaving() |
295
|
|
|
{ |
296
|
|
|
return $this->having; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Gets the order by statement for the query. |
301
|
|
|
* |
302
|
|
|
* @return OrderByStatement |
303
|
|
|
*/ |
304
|
|
|
public function getOrderBy() |
305
|
|
|
{ |
306
|
|
|
return $this->orderBy; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Gets the union statement for the query. |
311
|
|
|
* |
312
|
|
|
* @return UnionStatement |
313
|
|
|
*/ |
314
|
|
|
public function getUnion() |
315
|
|
|
{ |
316
|
|
|
return $this->union; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Generates the raw SQL string for the query. |
321
|
|
|
* |
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
|
|
public function build() |
325
|
|
|
{ |
326
|
|
|
$sql = [ |
327
|
|
|
$this->select->build(), |
328
|
|
|
$this->from->build(), |
329
|
|
|
$this->where->build(), |
330
|
|
|
$this->groupBy->build(), |
331
|
|
|
$this->having->build(), |
332
|
|
|
$this->orderBy->build(), |
333
|
|
|
$this->limit->build(), |
334
|
|
|
$this->union->build(), |
335
|
|
|
]; |
336
|
|
|
|
337
|
|
|
$this->values = array_merge( |
338
|
|
|
$this->where->getValues(), |
339
|
|
|
$this->having->getValues(), |
340
|
|
|
$this->union->getValues()); |
341
|
|
|
|
342
|
|
|
return implode(' ', array_filter($sql)); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function __clone() |
346
|
|
|
{ |
347
|
|
|
$this->select = clone $this->select; |
348
|
|
|
$this->from = clone $this->from; |
349
|
|
|
$this->where = clone $this->where; |
350
|
|
|
$this->groupBy = clone $this->groupBy; |
351
|
|
|
$this->having = clone $this->having; |
352
|
|
|
$this->orderBy = clone $this->orderBy; |
353
|
|
|
$this->limit = clone $this->limit; |
354
|
|
|
$this->union = clone $this->union; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|