1 | <?php |
||
24 | class SelectQuery extends AbstractSelect implements \JsonSerializable, \Countable |
||
25 | { |
||
26 | //See SQL generation below |
||
27 | use LoggerTrait; |
||
28 | |||
29 | /** |
||
30 | * Table names to select data from. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $tables = []; |
||
35 | |||
36 | /** |
||
37 | * Select queries represented by sql fragments or query builders to be united. Stored as |
||
38 | * [UNION TYPE, SELECT QUERY]. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $unionTokens = []; |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | * |
||
47 | * @param array $from Initial set of table names. |
||
48 | * @param array $columns Initial set of columns to fetch. |
||
49 | */ |
||
50 | public function __construct( |
||
63 | |||
64 | /** |
||
65 | * Set table names SELECT query should be performed for. Table names can be provided with |
||
66 | * specified alias (AS construction). |
||
67 | * |
||
68 | * @param array|string|mixed $tables Array of names, comma separated string or set of |
||
69 | * parameters. |
||
70 | * |
||
71 | * @return self|$this |
||
72 | */ |
||
73 | public function from($tables): SelectQuery |
||
79 | |||
80 | /** |
||
81 | * Tables to be loaded. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getTables(): array |
||
89 | |||
90 | /** |
||
91 | * Set columns should be fetched as result of SELECT query. Columns can be provided with |
||
92 | * specified alias (AS construction). |
||
93 | * |
||
94 | * @param array|string|mixed $columns Array of names, comma separated string or set of |
||
95 | * parameters. |
||
96 | * |
||
97 | * @return self|$this |
||
98 | */ |
||
99 | public function columns($columns): SelectQuery |
||
105 | |||
106 | /** |
||
107 | * Set of columns to be selected. |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public function getColumns(): array |
||
115 | |||
116 | /** |
||
117 | * Add select query to be united with. |
||
118 | * |
||
119 | * @param FragmentInterface $query |
||
120 | * |
||
121 | * @return self|$this |
||
122 | */ |
||
123 | public function union(FragmentInterface $query): SelectQuery |
||
129 | |||
130 | /** |
||
131 | * Add select query to be united with. Duplicate values will be included in result. |
||
132 | * |
||
133 | * @param FragmentInterface $query |
||
134 | * |
||
135 | * @return self|$this |
||
136 | */ |
||
137 | public function unionAll(FragmentInterface $query): SelectQuery |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function getParameters(): array |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | * |
||
171 | * @param bool $paginate Apply pagination to result, can be disabled in honor of count method. |
||
172 | * |
||
173 | * @return QueryStatement |
||
174 | */ |
||
175 | public function run(bool $paginate = true) |
||
204 | |||
205 | /** |
||
206 | * Iterate thought result using smaller data chinks with defined size and walk function. |
||
207 | * |
||
208 | * Example: |
||
209 | * $select->chunked(100, function(PDOResult $result, $offset, $count) { |
||
210 | * dump($result); |
||
211 | * }); |
||
212 | * |
||
213 | * You must return FALSE from walk function to stop chunking. |
||
214 | * |
||
215 | * @param int $limit |
||
216 | * @param callable $callback |
||
217 | */ |
||
218 | public function runChunks(int $limit, callable $callback) |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | * |
||
245 | * Count number of rows in query. Limit, offset, order by, group by values will be ignored. Do |
||
246 | * not count united queries, or queries in complex joins. |
||
247 | * |
||
248 | * @param string $column Column to count by (every column by default). |
||
249 | * |
||
250 | * @return int |
||
251 | */ |
||
252 | public function count(string $column = '*'): int |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | * |
||
270 | * Shortcut to execute one of aggregation methods (AVG, MAX, MIN, SUM) using method name as |
||
271 | * reference. |
||
272 | * |
||
273 | * Example: |
||
274 | * echo $select->sum('user.balance'); |
||
275 | * |
||
276 | * @param string $method |
||
277 | * @param array $arguments |
||
278 | * |
||
279 | * @return int|float |
||
280 | * |
||
281 | * @throws BuilderException |
||
282 | * @throws QueryException |
||
283 | */ |
||
284 | public function __call($method, array $arguments) |
||
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | * |
||
316 | * @return \PDOStatement|QueryStatement |
||
317 | */ |
||
318 | public function getIterator() |
||
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | public function sqlStatement(QueryCompiler $compiler = null): string |
||
353 | |||
354 | /** |
||
355 | * {@inheritdoc} |
||
356 | */ |
||
357 | public function jsonSerialize() |
||
361 | |||
362 | /** |
||
363 | * Request all results as array. |
||
364 | * |
||
365 | * @return array |
||
366 | */ |
||
367 | public function fetchAll(): array |
||
371 | } |
||
372 |