1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 12/25/14 |
5
|
|
|
* Time: 12:12 PM. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Sql\QueryBuilder\Manipulation; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\Column; |
14
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy; |
15
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ColumnQuery. |
19
|
|
|
*/ |
20
|
|
|
class ColumnQuery |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $columns = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $columnSelects = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $columnValues = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $columnFuncs = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
protected $isCount = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Select |
49
|
|
|
*/ |
50
|
|
|
protected $select; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var JoinQuery |
54
|
|
|
*/ |
55
|
|
|
protected $joinQuery; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Select $select |
59
|
|
|
* @param JoinQuery $joinQuery |
60
|
|
|
* @param array $columns |
61
|
|
|
*/ |
62
|
|
|
public function __construct(Select $select, JoinQuery $joinQuery, array $columns = null) |
63
|
|
|
{ |
64
|
|
|
$this->select = $select; |
65
|
|
|
$this->joinQuery = $joinQuery; |
66
|
|
|
|
67
|
|
|
if (!isset($columns)) { |
68
|
|
|
$columns = array(Column::ALL); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (\count($columns)) { |
72
|
|
|
$this->setColumns($columns); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $start |
78
|
|
|
* @param int $count |
79
|
|
|
* |
80
|
|
|
* @return Select |
81
|
|
|
*/ |
82
|
|
|
public function limit($start, $count = 0) |
83
|
|
|
{ |
84
|
|
|
return $this->select->limit($start, $count); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $whereOperator |
89
|
|
|
* |
90
|
|
|
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where |
91
|
|
|
*/ |
92
|
|
|
public function where($whereOperator = 'AND') |
93
|
|
|
{ |
94
|
|
|
return $this->select->where($whereOperator); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $column |
99
|
|
|
* @param string $direction |
100
|
|
|
* @param null $table |
101
|
|
|
* |
102
|
|
|
* @return Select |
103
|
|
|
*/ |
104
|
|
|
public function orderBy($column, $direction = OrderBy::ASC, $table = null) |
105
|
|
|
{ |
106
|
|
|
return $this->select->orderBy($column, $direction, $table); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string[] $columns |
111
|
|
|
* |
112
|
|
|
* @return Select |
113
|
|
|
*/ |
114
|
|
|
public function groupBy(array $columns) |
115
|
|
|
{ |
116
|
|
|
return $this->select->groupBy($columns); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Allows setting a Select query as a column value. |
121
|
|
|
* |
122
|
|
|
* @param array $column |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setSelectAsColumn(array $column) |
127
|
|
|
{ |
128
|
|
|
$this->columnSelects[] = $column; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getColumnSelects() |
137
|
|
|
{ |
138
|
|
|
return $this->columnSelects; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Allows setting a value to the select statement. |
143
|
|
|
* |
144
|
|
|
* @param string $value |
145
|
|
|
* @param string $alias |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function setValueAsColumn($value, $alias) |
150
|
|
|
{ |
151
|
|
|
$this->columnValues[$alias] = $value; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function getColumnValues() |
160
|
|
|
{ |
161
|
|
|
return $this->columnValues; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Allows calculation on columns using predefined SQL functions. |
166
|
|
|
* |
167
|
|
|
* @param string $funcName |
168
|
|
|
* @param string[] $arguments |
169
|
|
|
* @param string $alias |
170
|
|
|
* |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
public function setFunctionAsColumn($funcName, array $arguments, $alias) |
174
|
|
|
{ |
175
|
|
|
$this->columnFuncs[$alias] = ['func' => $funcName, 'args' => $arguments]; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
public function getColumnFuncs() |
184
|
|
|
{ |
185
|
|
|
return $this->columnFuncs; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $columnName |
190
|
|
|
* @param string $alias |
191
|
|
|
* |
192
|
|
|
* @return $this |
193
|
|
|
*/ |
194
|
|
|
public function count($columnName = '*', $alias = '') |
195
|
|
|
{ |
196
|
|
|
$table = $this->select->getTable(); |
197
|
|
|
|
198
|
|
|
$count = 'COUNT('; |
199
|
|
|
$count .= ($columnName !== '*') ? "$table.{$columnName}" : '*'; |
200
|
|
|
$count .= ')'; |
201
|
|
|
|
202
|
|
|
if (isset($alias) && \strlen($alias) > 0) { |
203
|
|
|
$count .= " AS '{$alias}'"; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$this->columns = array($count); |
207
|
|
|
$this->isCount = true; |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
public function isCount() |
216
|
|
|
{ |
217
|
|
|
return $this->isCount; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
public function getAllColumns() |
224
|
|
|
{ |
225
|
|
|
$columns = $this->getColumns(); |
226
|
|
|
|
227
|
|
|
foreach ($this->joinQuery->getJoins() as $join) { |
228
|
|
|
$joinCols = $join->getAllColumns(); |
229
|
|
|
$columns = \array_merge($columns, $joinCols); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $columns; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Column |
237
|
|
|
* |
238
|
|
|
* @throws QueryException |
239
|
|
|
*/ |
240
|
|
|
public function getColumns() |
241
|
|
|
{ |
242
|
|
|
if (\is_null($this->select->getTable())) { |
243
|
|
|
throw new QueryException('No table specified for the Select instance'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return SyntaxFactory::createColumns($this->columns, $this->select->getTable()); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Sets the column names used to write the SELECT statement. |
251
|
|
|
* If key is set, key is the column's alias. Value is always the column names. |
252
|
|
|
* |
253
|
|
|
* @param array $columns |
254
|
|
|
* |
255
|
|
|
* @return $this |
256
|
|
|
*/ |
257
|
|
|
public function setColumns(array $columns) |
258
|
|
|
{ |
259
|
|
|
$this->columns = $columns; |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|