Hoa central
SelectCore.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Database\Query;
38 
47 abstract class SelectCore extends Where
48 {
54  protected $_columns = null;
55 
61  protected $_distinctOrAll = null;
62 
68  protected $_from = [];
69 
75  protected $_groupBy = [];
76 
82  protected $_having = null;
83 
84 
85 
92  public function __construct(Array $columns = [])
93  {
94  $this->_columns = $columns;
95 
96  return;
97  }
98 
104  public function distinct()
105  {
106  $this->_distinctOrAll = 'DISTINCT';
107 
108  return $this;
109  }
110 
116  public function all()
117  {
118  $this->_distinctOrAll = 'ALL';
119 
120  return $this;
121  }
122 
130  public function select($column)
131  {
132  foreach (func_get_args() as $column) {
133  $this->_columns[] = $column;
134  }
135 
136  return $this;
137  }
138 
146  public function groupBy($expression)
147  {
148  foreach (func_get_args() as $expression) {
149  $this->_groupBy[] = $expression;
150  }
151 
152  return $this;
153  }
154 
161  public function having($expression)
162  {
163  $this->_having = $expression;
164 
165  return $this;
166  }
167 
175  public function from($source)
176  {
177  foreach (func_get_args() as $source) {
178  if ($source instanceof self) {
179  $source = '(' . $source . ')';
180  }
181 
182  $this->_from[] = $source;
183  }
184 
185  return $this;
186  }
187 
194  public function _as($alias)
195  {
196  if (empty($this->_from)) {
197  return $this;
198  }
199 
200  $this->_from[$alias] = array_pop($this->_from);
201 
202  return $this;
203  }
204 
211  public function join($source)
212  {
213  return $this->_join('JOIN', $source);
214  }
215 
222  public function naturalJoin($source)
223  {
224  return $this->_join('NATURAL JOIN', $source);
225  }
226 
233  public function leftJoin($source)
234  {
235  return $this->_join('LEFT JOIN', $source);
236  }
237 
244  public function naturalLeftJoin($source)
245  {
246  return $this->_join('NATURAL LEFT JOIN', $source);
247  }
248 
255  public function leftOuterJoin($source)
256  {
257  return $this->_join('LEFT OUTER JOIN', $source);
258  }
259 
266  public function naturalLeftOuterJoin($source)
267  {
268  return $this->_join('NATURAL LEFT OUTER JOIN', $source);
269  }
270 
277  public function innerJoin($source)
278  {
279  return $this->_join('INNER JOIN', $source);
280  }
281 
288  public function naturalInnerJoin($source)
289  {
290  return $this->_join('NATURAL INNER JOIN', $source);
291  }
292 
299  public function crossJoin($source)
300  {
301  return $this->_join('CROSS JOIN', $source);
302  }
303 
310  public function naturalCrossJoin($source)
311  {
312  return $this->_join('NATURAL CROSS JOIN', $source);
313  }
314 
322  protected function _join($type, $source)
323  {
324  if (empty($this->_from)) {
325  return $this;
326  }
327 
328  if ($source instanceof self) {
329  $source = '(' . $source . ')';
330  }
331 
332  end($this->_from);
333  $key = key($this->_from);
334  $value = current($this->_from);
335  $this->_from[$key] = $value . ' ' . $type . ' ' . $source;
336 
337  return new Join($this, $this->_from);
338  }
339 
345  public function reset()
346  {
347  parent::reset();
348  $this->_columns = [];
349  $this->_distinctOrAll = null;
350  $this->_groupBy = [];
351  $this->_having = [];
352  $this->_from = [];
353 
354  return $this;
355  }
356 
362  public function __toString()
363  {
364  $out = 'SELECT';
365 
366  if (null !== $this->_distinctOrAll) {
367  $out .= ' ' . $this->_distinctOrAll;
368  }
369 
370  if (!empty($this->_columns)) {
371  $out .= ' ' . implode(', ', $this->_columns);
372  } else {
373  $out .= ' *';
374  }
375 
376  if (!empty($this->_from)) {
377  $out .= ' FROM ';
378  $handle = [];
379 
380  foreach ($this->_from as $alias => $from) {
381  if (is_int($alias)) {
382  $handle[] = $from;
383  } else {
384  $handle[] = $from . ' AS ' . $alias;
385  }
386  }
387 
388  $out .= implode(', ', $handle);
389  }
390 
391  $out .= parent::__toString();
392 
393  if (!empty($this->_groupBy)) {
394  $out .= ' GROUP BY ' . implode(', ', $this->_groupBy);
395 
396  if (!empty($this->_having)) {
397  $out .= ' HAVING ' . $this->_having;
398  }
399  }
400 
401  return $out;
402  }
403 }
__construct(Array $columns=[])
Definition: SelectCore.php:92