Hoa central
Select.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Database\Query;
38 
47 class Select extends SelectCore implements Dml
48 {
54  protected $_select = [];
55 
61  protected $_orderBy = [];
62 
68  protected $_limit = [];
69 
75  protected $_offset = null;
76 
77 
78 
84  public function union()
85  {
86  return $this->compose('UNION');
87  }
88 
94  public function unionAll()
95  {
96  return $this->compose('UNION ALL');
97  }
98 
104  public function intersect()
105  {
106  return $this->compose('INTERSECT');
107  }
108 
114  public function except()
115  {
116  return $this->compose('EXCEPT');
117  }
118 
125  protected function compose($operator)
126  {
127  $this->_select[] = parent::__toString() . ' ' . $operator;
128  $this->reset();
129 
130  return $this;
131  }
132 
140  public function orderBy($term)
141  {
142  foreach (func_get_args() as $term) {
143  $this->_orderBy[] = $term;
144  }
145 
146  return $this;
147  }
148 
156  public function limit($expression)
157  {
158  foreach (func_get_args() as $expression) {
159  $this->_limit[] = $expression;
160  }
161 
162  return $this;
163  }
164 
171  public function offset($expression)
172  {
173  $this->_offset = $expression;
174 
175  return $this;
176  }
177 
183  public function __toString()
184  {
185  $out = null;
186  $select = implode(' ', $this->_select);
187 
188  if (!empty($select)) {
189  $out .= $select . ' ';
190  }
191 
192  $out .= parent::__toString();
193 
194  if (!empty($this->_orderBy)) {
195  $out .= ' ORDER BY ' . implode(', ', $this->_orderBy);
196  }
197 
198  if (!empty($this->_limit)) {
199  $out .= ' LIMIT';
200 
201  if (null !== $this->_offset) {
202  $out .= ' ' . $this->_limit[0] .
203  ' OFFSET ' . $this->_offset;
204  } else {
205  $out .= ' ' . implode(', ', $this->_limit);
206  }
207  }
208 
209  return $out;
210  }
211 }
limit($expression)
Definition: Select.php:156
offset($expression)
Definition: Select.php:171