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
|
|
|
|
21
|
|
|
class SelectQuery extends Query |
22
|
|
|
{ |
23
|
|
|
use Executable, Fetchable; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var SelectStatement |
27
|
|
|
*/ |
28
|
|
|
protected $select; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var FromStatement |
32
|
|
|
*/ |
33
|
|
|
protected $from; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var WhereStatement |
37
|
|
|
*/ |
38
|
|
|
protected $where; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var WhereStatement |
42
|
|
|
*/ |
43
|
|
|
protected $having; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var OrderStatement |
47
|
|
|
*/ |
48
|
|
|
protected $orderBy; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var OrderStatement |
52
|
|
|
*/ |
53
|
|
|
protected $groupBy; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var LimitStatement |
57
|
|
|
*/ |
58
|
|
|
protected $limit; |
59
|
|
|
|
60
|
|
|
public function __construct() |
61
|
|
|
{ |
62
|
|
|
$this->select = new SelectStatement(); |
63
|
|
|
$this->from = new FromStatement(); |
64
|
|
|
$this->where = new WhereStatement(); |
65
|
|
|
$this->having = new WhereStatement(true); |
66
|
|
|
$this->orderBy = new OrderStatement(); |
67
|
|
|
$this->groupBy = new OrderStatement(true); |
68
|
|
|
$this->limit = new LimitStatement(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Sets the fields to be selected for the query. |
73
|
|
|
* |
74
|
|
|
* @param array|string $fields fields |
75
|
|
|
* |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public function select($fields) |
79
|
|
|
{ |
80
|
|
|
$this->select->addFields($fields); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Sets the table for the query. |
87
|
|
|
* |
88
|
|
|
* @param string $table table name |
89
|
|
|
* |
90
|
|
|
* @return self |
91
|
|
|
*/ |
92
|
|
|
public function from($table) |
93
|
|
|
{ |
94
|
|
|
$this->from->addTable($table); |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Adds a join to the query. |
101
|
|
|
* |
102
|
|
|
* @param string $table table name |
103
|
|
|
* @param string $on ON condition |
104
|
|
|
* @param string $using USING columns |
105
|
|
|
* @param string $type optional join type if not JOIN |
106
|
|
|
* |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
|
|
public function join($table, $on = null, $using = null, $type = 'JOIN') |
110
|
|
|
{ |
111
|
|
|
$this->from->addJoin($table, $on, $using, $type); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sets the where conditions for the query. |
118
|
|
|
* |
119
|
|
|
* @param array|string $field |
120
|
|
|
* @param string $condition condition value (optional) |
121
|
|
|
* @param string $operator operator (optional) |
122
|
|
|
* |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function where($field, $condition = false, $operator = '=') |
126
|
|
|
{ |
127
|
|
|
if (func_num_args() >= 2) { |
128
|
|
|
$this->where->addCondition($field, $condition, $operator); |
129
|
|
|
} else { |
130
|
|
|
$this->where->addCondition($field); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Sets the limit for the query. |
138
|
|
|
* |
139
|
|
|
* @param int $limit |
140
|
|
|
* @param int $offset |
141
|
|
|
* |
142
|
|
|
* @return self |
143
|
|
|
*/ |
144
|
|
|
public function limit($limit, $offset = 0) |
145
|
|
|
{ |
146
|
|
|
$this->limit->setLimit($limit, $offset); |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sets the group by fields for the query. |
153
|
|
|
* |
154
|
|
|
* @param string|array $fields |
155
|
|
|
* @param string $direction |
156
|
|
|
* |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
|
|
public function groupBy($fields, $direction = false) |
160
|
|
|
{ |
161
|
|
|
$this->groupBy->addFields($fields, $direction); |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Sets the having conditions for the query. |
168
|
|
|
* |
169
|
|
|
* @param array|string $field |
170
|
|
|
* @param string|bool $condition condition value (optional) |
171
|
|
|
* @param string $operator operator (optional) |
172
|
|
|
* |
173
|
|
|
* @return self |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
public function having($field, $condition = false, $operator = '=') |
176
|
|
|
{ |
177
|
|
|
if (func_num_args() >= 2) { |
178
|
|
|
$this->having->addCondition($field, $condition, $operator); |
179
|
|
|
} else { |
180
|
|
|
$this->having->addCondition($field); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Sets the order for the query. |
188
|
|
|
* |
189
|
|
|
* @param string|array $fields |
190
|
|
|
* @param string $direction |
191
|
|
|
* |
192
|
|
|
* @return self |
193
|
|
|
*/ |
194
|
|
|
public function orderBy($fields, $direction = false) |
195
|
|
|
{ |
196
|
|
|
$this->orderBy->addFields($fields, $direction); |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Gets the select statement for the query. |
203
|
|
|
* |
204
|
|
|
* @return SelectStatement |
205
|
|
|
*/ |
206
|
|
|
public function getSelect() |
207
|
|
|
{ |
208
|
|
|
return $this->select; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Gets the from statement for the query. |
213
|
|
|
* |
214
|
|
|
* @return FromStatement |
215
|
|
|
*/ |
216
|
|
|
public function getFrom() |
217
|
|
|
{ |
218
|
|
|
return $this->from; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Gets the where statement for the query. |
223
|
|
|
* |
224
|
|
|
* @return WhereStatement |
225
|
|
|
*/ |
226
|
|
|
public function getWhere() |
227
|
|
|
{ |
228
|
|
|
return $this->where; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Gets the limit statement for the query. |
233
|
|
|
* |
234
|
|
|
* @return LimitStatement |
235
|
|
|
*/ |
236
|
|
|
public function getLimit() |
237
|
|
|
{ |
238
|
|
|
return $this->limit; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Gets the group by statement for the query. |
243
|
|
|
* |
244
|
|
|
* @return GroupByStatement |
245
|
|
|
*/ |
246
|
|
|
public function getGroupBy() |
247
|
|
|
{ |
248
|
|
|
return $this->groupBy; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Gets the having statement for the query. |
253
|
|
|
* |
254
|
|
|
* @return HavingStatement |
255
|
|
|
*/ |
256
|
|
|
public function getHaving() |
257
|
|
|
{ |
258
|
|
|
return $this->having; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Gets the order by statement for the query. |
263
|
|
|
* |
264
|
|
|
* @return OrderByStatement |
265
|
|
|
*/ |
266
|
|
|
public function getOrderBy() |
267
|
|
|
{ |
268
|
|
|
return $this->orderBy; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Generates the raw SQL string for the query. |
273
|
|
|
* |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
|
|
public function build() |
277
|
|
|
{ |
278
|
|
|
$sql = [ |
279
|
|
|
$this->select->build(), |
280
|
|
|
$this->from->build(), |
281
|
|
|
$this->where->build(), |
282
|
|
|
$this->groupBy->build(), |
283
|
|
|
$this->having->build(), |
284
|
|
|
$this->orderBy->build(), |
285
|
|
|
$this->limit->build(), |
286
|
|
|
]; |
287
|
|
|
|
288
|
|
|
$this->values = array_merge($this->where->getValues(), |
289
|
|
|
$this->having->getValues()); |
290
|
|
|
|
291
|
|
|
return implode(' ', array_filter($sql)); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|