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
|
|
|
* Adds a where not between condition to the query. |
161
|
|
|
* |
162
|
|
|
* @param string $field |
163
|
|
|
* @param mixed $a first between value |
164
|
|
|
* @param mixed $b second between value |
165
|
|
|
* |
166
|
|
|
* @return self |
167
|
|
|
*/ |
168
|
|
|
public function notBetween($field, $a, $b) |
169
|
|
|
{ |
170
|
|
|
$this->where->addNotBetweenCondition($field, $a, $b); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Sets the limit for the query. |
177
|
|
|
* |
178
|
|
|
* @param int $limit |
179
|
|
|
* @param int $offset |
180
|
|
|
* |
181
|
|
|
* @return self |
182
|
|
|
*/ |
183
|
|
|
public function limit($limit, $offset = 0) |
184
|
|
|
{ |
185
|
|
|
$this->limit->setLimit($limit, $offset); |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Sets the group by fields for the query. |
192
|
|
|
* |
193
|
|
|
* @param string|array $fields |
194
|
|
|
* @param string $direction |
195
|
|
|
* |
196
|
|
|
* @return self |
197
|
|
|
*/ |
198
|
|
|
public function groupBy($fields, $direction = false) |
199
|
|
|
{ |
200
|
|
|
$this->groupBy->addFields($fields, $direction); |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Sets the having conditions for the query. |
207
|
|
|
* |
208
|
|
|
* @param array|string $field |
209
|
|
|
* @param string|bool $condition condition value (optional) |
210
|
|
|
* @param string $operator operator (optional) |
211
|
|
|
* |
212
|
|
|
* @return self |
213
|
|
|
*/ |
214
|
|
View Code Duplication |
public function having($field, $condition = false, $operator = '=') |
215
|
|
|
{ |
216
|
|
|
if (func_num_args() >= 2) { |
217
|
|
|
$this->having->addCondition($field, $condition, $operator); |
218
|
|
|
} else { |
219
|
|
|
$this->having->addCondition($field); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Sets the order for the query. |
227
|
|
|
* |
228
|
|
|
* @param string|array $fields |
229
|
|
|
* @param string $direction |
230
|
|
|
* |
231
|
|
|
* @return self |
232
|
|
|
*/ |
233
|
|
|
public function orderBy($fields, $direction = false) |
234
|
|
|
{ |
235
|
|
|
$this->orderBy->addFields($fields, $direction); |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Unions another select query with this query. |
242
|
|
|
* |
243
|
|
|
* @param SelectQuery $query |
244
|
|
|
* @param string $type optional union type |
245
|
|
|
* |
246
|
|
|
* @return self |
247
|
|
|
*/ |
248
|
|
|
public function union(SelectQuery $query, $type = false) |
249
|
|
|
{ |
250
|
|
|
$this->union->addQuery($query, $type); |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Gets the select statement for the query. |
257
|
|
|
* |
258
|
|
|
* @return SelectStatement |
259
|
|
|
*/ |
260
|
|
|
public function getSelect() |
261
|
|
|
{ |
262
|
|
|
return $this->select; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Gets the from statement for the query. |
267
|
|
|
* |
268
|
|
|
* @return FromStatement |
269
|
|
|
*/ |
270
|
|
|
public function getFrom() |
271
|
|
|
{ |
272
|
|
|
return $this->from; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Gets the where statement for the query. |
277
|
|
|
* |
278
|
|
|
* @return WhereStatement |
279
|
|
|
*/ |
280
|
|
|
public function getWhere() |
281
|
|
|
{ |
282
|
|
|
return $this->where; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Gets the limit statement for the query. |
287
|
|
|
* |
288
|
|
|
* @return LimitStatement |
289
|
|
|
*/ |
290
|
|
|
public function getLimit() |
291
|
|
|
{ |
292
|
|
|
return $this->limit; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Gets the group by statement for the query. |
297
|
|
|
* |
298
|
|
|
* @return GroupByStatement |
299
|
|
|
*/ |
300
|
|
|
public function getGroupBy() |
301
|
|
|
{ |
302
|
|
|
return $this->groupBy; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Gets the having statement for the query. |
307
|
|
|
* |
308
|
|
|
* @return HavingStatement |
309
|
|
|
*/ |
310
|
|
|
public function getHaving() |
311
|
|
|
{ |
312
|
|
|
return $this->having; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Gets the order by statement for the query. |
317
|
|
|
* |
318
|
|
|
* @return OrderByStatement |
319
|
|
|
*/ |
320
|
|
|
public function getOrderBy() |
321
|
|
|
{ |
322
|
|
|
return $this->orderBy; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Gets the union statement for the query. |
327
|
|
|
* |
328
|
|
|
* @return UnionStatement |
329
|
|
|
*/ |
330
|
|
|
public function getUnion() |
331
|
|
|
{ |
332
|
|
|
return $this->union; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Generates the raw SQL string for the query. |
337
|
|
|
* |
338
|
|
|
* @return string |
339
|
|
|
*/ |
340
|
|
|
public function build() |
341
|
|
|
{ |
342
|
|
|
$sql = [ |
343
|
|
|
$this->select->build(), |
344
|
|
|
$this->from->build(), |
345
|
|
|
$this->where->build(), |
346
|
|
|
$this->groupBy->build(), |
347
|
|
|
$this->having->build(), |
348
|
|
|
$this->orderBy->build(), |
349
|
|
|
$this->limit->build(), |
350
|
|
|
$this->union->build(), |
351
|
|
|
]; |
352
|
|
|
|
353
|
|
|
$this->values = array_merge( |
354
|
|
|
$this->where->getValues(), |
355
|
|
|
$this->having->getValues(), |
356
|
|
|
$this->union->getValues()); |
357
|
|
|
|
358
|
|
|
return implode(' ', array_filter($sql)); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
public function __clone() |
362
|
|
|
{ |
363
|
|
|
$this->select = clone $this->select; |
364
|
|
|
$this->from = clone $this->from; |
365
|
|
|
$this->where = clone $this->where; |
366
|
|
|
$this->groupBy = clone $this->groupBy; |
367
|
|
|
$this->having = clone $this->having; |
368
|
|
|
$this->orderBy = clone $this->orderBy; |
369
|
|
|
$this->limit = clone $this->limit; |
370
|
|
|
$this->union = clone $this->union; |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|