1 | <?php |
||
21 | class Select extends AbstractBuilder |
||
22 | { |
||
23 | use Traits\From; |
||
24 | use Traits\Where; |
||
25 | use Traits\Order; |
||
26 | use Traits\Limit; |
||
27 | |||
28 | /** |
||
29 | * <code> |
||
30 | * [ |
||
31 | * 'u.id', 'u.name', |
||
32 | * 'p.id', 'p.title' |
||
33 | * ] |
||
34 | * </code> |
||
35 | * |
||
36 | * @var array[] |
||
37 | */ |
||
38 | protected $select = []; |
||
39 | protected $groupBy = []; |
||
40 | protected $having = null; |
||
41 | |||
42 | /** |
||
43 | * @var mixed PDO fetch types or object class |
||
44 | */ |
||
45 | protected $fetchType = \PDO::FETCH_ASSOC; |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | * |
||
50 | * @param integer|string|object $fetchType |
||
51 | * |
||
52 | * @return integer|string|array |
||
53 | */ |
||
54 | 3 | public function execute($fetchType = null) |
|
70 | |||
71 | /** |
||
72 | * Setup fetch type, any of PDO, or any Class |
||
73 | * |
||
74 | * @param string $fetchType |
||
75 | * |
||
76 | * @return Select instance |
||
77 | */ |
||
78 | 2 | public function setFetchType($fetchType) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 8 | public function getSql() : string |
|
97 | |||
98 | /** |
||
99 | * Specifies an item that is to be returned in the query result |
||
100 | * Replaces any previously specified selections, if any |
||
101 | * |
||
102 | * Example |
||
103 | * <code> |
||
104 | * $sb = new Select(); |
||
105 | * $sb |
||
106 | * ->select('u.id', 'p.id') |
||
107 | * ->from('users', 'u') |
||
108 | * ->leftJoin('u', 'phone', 'p', 'u.id = p.user_id'); |
||
109 | * </code> |
||
110 | * |
||
111 | * @param string[] $select the selection expressions |
||
112 | * |
||
113 | * @return Select instance |
||
114 | */ |
||
115 | 9 | public function select(...$select) : Select |
|
120 | |||
121 | /** |
||
122 | * Adds an item that is to be returned in the query result. |
||
123 | * |
||
124 | * Example |
||
125 | * <code> |
||
126 | * $sb = new Select(); |
||
127 | * $sb |
||
128 | * ->select('u.id') |
||
129 | * ->addSelect('p.id') |
||
130 | * ->from('users', 'u') |
||
131 | * ->leftJoin('u', 'phone', 'u.id = p.user_id'); |
||
132 | * </code> |
||
133 | * |
||
134 | * @param string[] $select the selection expression |
||
135 | * |
||
136 | * @return Select instance |
||
137 | */ |
||
138 | 1 | public function addSelect(string ...$select) : Select |
|
143 | |||
144 | /** |
||
145 | * Get current select query part |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | 3 | public function getSelect() : array |
|
153 | |||
154 | /** |
||
155 | * Specifies a grouping over the results of the query. |
||
156 | * Replaces any previously specified groupings, if any. |
||
157 | * |
||
158 | * Example |
||
159 | * <code> |
||
160 | * $sb = new Select(); |
||
161 | * $sb |
||
162 | * ->select('u.name') |
||
163 | * ->from('users', 'u') |
||
164 | * ->groupBy('u.id'); |
||
165 | * </code> |
||
166 | * |
||
167 | * @param string[] $groupBy the grouping expression |
||
168 | * |
||
169 | * @return Select instance |
||
170 | */ |
||
171 | 1 | public function groupBy(string ...$groupBy) : Select |
|
176 | |||
177 | /** |
||
178 | * Adds a grouping expression to the query. |
||
179 | * |
||
180 | * Example |
||
181 | * <code> |
||
182 | * $sb = new Select(); |
||
183 | * $sb |
||
184 | * ->select('u.name') |
||
185 | * ->from('users', 'u') |
||
186 | * ->groupBy('u.lastLogin'); |
||
187 | * ->addGroupBy('u.createdAt') |
||
188 | * </code> |
||
189 | * |
||
190 | * @param string[] $groupBy the grouping expression |
||
191 | * |
||
192 | * @return Select instance |
||
193 | */ |
||
194 | 1 | public function addGroupBy(string ...$groupBy) : Select |
|
199 | |||
200 | /** |
||
201 | * Specifies a restriction over the groups of the query. |
||
202 | * Replaces any previous having restrictions, if any |
||
203 | * |
||
204 | * @param string[] $conditions the query restriction predicates |
||
205 | * |
||
206 | * @return Select |
||
207 | */ |
||
208 | 1 | public function having(...$conditions) : Select |
|
213 | |||
214 | /** |
||
215 | * Adds a restriction over the groups of the query, forming a logical |
||
216 | * conjunction with any existing having restrictions |
||
217 | * |
||
218 | * @param string[] $conditions the query restriction predicates |
||
219 | * |
||
220 | * @return Select |
||
221 | */ |
||
222 | 1 | public function andHaving(...$conditions) : Select |
|
234 | |||
235 | /** |
||
236 | * Adds a restriction over the groups of the query, forming a logical |
||
237 | * disjunction with any existing having restrictions |
||
238 | * |
||
239 | * @param string[] $conditions the query restriction predicates |
||
240 | * |
||
241 | * @return Select |
||
242 | */ |
||
243 | 1 | public function orHaving(...$conditions) : Select |
|
255 | |||
256 | /** |
||
257 | * Setup offset like a page number, start from 1 |
||
258 | * |
||
259 | * @param integer $page |
||
260 | * |
||
261 | * @return Select |
||
262 | * @throws DbException |
||
263 | */ |
||
264 | 1 | public function setPage(int $page = 1) : Select |
|
272 | |||
273 | /** |
||
274 | * Prepare Select query part |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 8 | protected function prepareSelect() : string |
|
282 | |||
283 | /** |
||
284 | * prepareWhere |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 8 | protected function prepareGroupBy() : string |
|
292 | |||
293 | /** |
||
294 | * prepareWhere |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | 8 | protected function prepareHaving() : string |
|
302 | } |
||
303 |