-
Notifications
You must be signed in to change notification settings - Fork 9
/
SelectQuery.php
279 lines (238 loc) · 7.47 KB
/
SelectQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 enc=utf8: */
/**
* @author Alexey Zakhlestin
* @package mysql-query-builder
**/
/*
MySQL Query Builder
Copyright © 2005-2009 Alexey Zakhlestin <indeyets@gmail.com>
Copyright © 2005-2006 Konstantin Sedov <kostya.online@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* This class contains logic of "SELECT" queries
*
* @package mysql-query-builder
* @author Alexey Zakhlestin
*/
class SelectQuery extends BasicQuery
{
private $limit = null;
private $selects = null;
private $groupby = null;
private $havings = null;
private $indices = null;
private $distinct = false;
/**
* Creates new SELECT-query object.
* By default, it is equivalent of "SELECT t0.* FROM t0, t1, t2, tN", where t0-tN are tables given to this constructor
*
* @param mixed $tables
*/
public function __construct($tables)
{
parent::__construct($tables);
$this->setSelect(array(new AllFields()));
}
/**
* Specifies, which columns should be selected.
* $_select can be instance of AllFields, MQB_Field or array of such instances.
* If $distinct is set to TRUE, 'SELECT DISTINCT …' query shall be used
*
* @param mixed $_selects
* @param bool $distinct
* @return void
* @throws InvalidArgumentException, RangeException
*/
public function setSelect($_selects, $distinct = false)
{
if (!is_array($_selects)) {
$_selects = array($_selects);
}
if (count($_selects) == 0) {
throw new InvalidArgumentException('Nothing to select');
}
if (!is_bool($distinct)) {
throw new InvalidArgumentException('"distinct" parameter should be boolean');
}
foreach ($_selects as $s) {
if (!($s instanceof MQB_Field) and !($s instanceof AllFields))
throw new RangeException('Allowed values are objects of the following classes: Field, AllFields, sqlFunction and Aggregate');
}
$this->selects = $_selects;
$this->distinct = $distinct;
$this->reset();
}
/**
* Specifies, which index(es) should be preferred for the first table of query
* $indices should be either string or array of strings
*
* @param mixed $indices
* @return void
*/
public function setIndices($indices)
{
if (!is_array($indices)) {
$indices = array($indices);
}
$this->indices = $indices;
}
/**
* Specifies, if the query should use "GROUP BY" clause.
* MQB_Field of array of MQB_Fields is allowed as parameter
*
* @param mixed $orderlist
* @return void
* @throws InvalidArgumentException
*/
public function setGroupby($orderlist)
{
if (!is_array($orderlist))
$orderlist = array($orderlist);
foreach ($orderlist as $field)
if (!($field instanceof MQB_Field))
throw new InvalidArgumentException('setGroupBy takes only [array of] MQB_Fields as parameter');
$this->groupby = $orderlist;
$this->reset();
}
/**
* setup "LIMIT" clause of Query.
*
* @param integer $limit
* @param integer $offset
* @return void
* @throws InvalidArgumentException
*/
public function setLimit($limit, $offset=0)
{
if (!is_numeric($limit) or !is_numeric($offset))
throw new InvalidArgumentException('Limit should be specified using numerics');
$this->limit = array($limit, $offset);
}
/**
* Accessor, which returns internal "GROUP BY" array
*
* @return array
*/
public function showGroupBy()
{
return $this->groupby;
}
protected function getSql(array &$parameters)
{
return $this->getSelect($parameters).
$this->getFrom($parameters).
// $this->getIndices().
$this->getWhere($parameters).
$this->getGroupby($parameters).
$this->getHaving($parameters).
$this->getOrderby($parameters).
$this->getLimit($parameters);
}
/**
* Returns "LIMIT" clause which can be used in various queries
*
* @param array $parameters
* @return void
*/
protected function getLimit(array &$parameters)
{
if (null === $this->limit)
return "";
return " LIMIT ".$this->limit[0].' OFFSET '.$this->limit[1];
}
/**
* Returns number of columns, which will be returned by query
*
* @return integer
*/
public function countSelects()
{
return count($this->selects);
}
private function getSelect(&$parameters)
{
$res = 'SELECT ';
if (true === $this->distinct) {
$res .= 'DISTINCT ';
}
$sqls = array();
foreach ($this->selects as $s) {
$sqls[] = $s->getSql($parameters, true);
}
return $res.implode(", ", $sqls);
}
protected function getFrom(array &$parameters)
{
$froms = array();
for ($i = 0; $i < count($this->from); $i++) {
$_str = $this->from[$i]->__toString().' AS `t'.$i.'`';
if (0 == $i)
$_str .= $this->getIndices();
$froms[] = $_str;
}
$sql = ' FROM '.implode(", ", $froms);
return $sql;
}
private function getGroupby(&$parameters)
{
if ($this->groupby === null)
return "";
foreach ($this->groupby as $groupby) {
if (null !== $alias = $groupby->getAlias())
$sqls[] = $alias;
else
$sqls[] = $groupby->getSql($parameters);
}
return " GROUP BY ".implode(", ", $sqls);
}
/**
* Specifies "HAVING" clause of query
*
* @param MQB_Condition $conditions
* @return void
* @author Jimi Dini
*/
public function setHaving(MQB_Condition $conditions = null)
{
if (null === $conditions) {
$this->havings = null;
} elseif ($conditions instanceof MQB_Condition) {
$this->havings = clone $conditions;
}
$this->reset();
}
protected function getHaving(&$parameters)
{
if (null == $this->havings)
return "";
return " HAVING ".$this->havings->getSql($parameters);
}
protected function getIndices()
{
if (null === $this->indices)
return '';
$res = ' USE INDEX (';
$first = true;
foreach ($this->indices as $idx) {
if (true === $first)
$first = false;
else
$res .= ', ';
$res .= '`'.$idx.'`';
}
$res .= ')';
return $res;
}
}