1 | <?php |
||
24 | class SelectQuery extends AbstractSelect implements \JsonSerializable |
||
25 | { |
||
26 | use LoggerTrait; |
||
27 | |||
28 | /** |
||
29 | * Table names to select data from. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $tables = []; |
||
34 | |||
35 | /** |
||
36 | * Select queries represented by sql fragments or query builders to be united. Stored as |
||
37 | * [UNION TYPE, SELECT QUERY]. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $unionTokens = []; |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | * |
||
46 | * @param array $from Initial set of table names. |
||
47 | * @param array $columns Initial set of columns to fetch. |
||
48 | */ |
||
49 | public function __construct( |
||
62 | |||
63 | /** |
||
64 | * Set table names SELECT query should be performed for. Table names can be provided with |
||
65 | * specified alias (AS construction). |
||
66 | * |
||
67 | * @param array|string|mixed $tables Array of names, comma separated string or set of |
||
68 | * parameters. |
||
69 | * |
||
70 | * @return self|$this |
||
71 | */ |
||
72 | public function from($tables): SelectQuery |
||
78 | |||
79 | /** |
||
80 | * Tables to be loaded. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | public function getTables(): array |
||
88 | |||
89 | /** |
||
90 | * Set columns should be fetched as result of SELECT query. Columns can be provided with |
||
91 | * specified alias (AS construction). |
||
92 | * |
||
93 | * @param array|string|mixed $columns Array of names, comma separated string or set of |
||
94 | * parameters. |
||
95 | * |
||
96 | * @return self|$this |
||
97 | */ |
||
98 | public function columns($columns): SelectQuery |
||
104 | |||
105 | /** |
||
106 | * Set of columns to be selected. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function getColumns(): array |
||
114 | |||
115 | /** |
||
116 | * Alias for columns() method. |
||
117 | * |
||
118 | * @param array|string|mixed $columns Array of names, comma separated string or set of |
||
119 | * parameters. |
||
120 | * |
||
121 | * @return self|$this |
||
122 | */ |
||
123 | public function select($columns): SelectQuery |
||
129 | |||
130 | /** |
||
131 | * Add select query to be united with. |
||
132 | * |
||
133 | * @param FragmentInterface $query |
||
134 | * |
||
135 | * @return self|$this |
||
136 | */ |
||
137 | public function union(FragmentInterface $query): SelectQuery |
||
143 | |||
144 | /** |
||
145 | * Add select query to be united with. Duplicate values will be included in result. |
||
146 | * |
||
147 | * @param FragmentInterface $query |
||
148 | * |
||
149 | * @return self|$this |
||
150 | */ |
||
151 | public function unionAll(FragmentInterface $query): SelectQuery |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function getParameters(): array |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | * |
||
178 | * @param bool $paginate Apply pagination to result, can be disabled in honor of count method. |
||
179 | * |
||
180 | * @return QueryStatement |
||
181 | */ |
||
182 | public function run(bool $paginate = true) |
||
211 | |||
212 | /** |
||
213 | * Iterate thought result using smaller data chinks with defined size and walk function. |
||
214 | * |
||
215 | * Example: |
||
216 | * $select->chunked(100, function(PDOResult $result, $offset, $count) { |
||
217 | * dump($result); |
||
218 | * }); |
||
219 | * |
||
220 | * You must return FALSE from walk function to stop chunking. |
||
221 | * |
||
222 | * @param int $limit |
||
223 | * @param callable $callback |
||
224 | */ |
||
225 | public function runChunks(int $limit, callable $callback) |
||
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | * |
||
253 | * Count number of rows in query. Limit, offset, order by, group by values will be ignored. Do |
||
254 | * not count united queries, or queries in complex joins. |
||
255 | * |
||
256 | * @param string $column Column to count by (every column by default). |
||
257 | * |
||
258 | * @return int |
||
259 | */ |
||
260 | public function count(string $column = '*'): int |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | * |
||
276 | * Shortcut to execute one of aggregation methods (AVG, MAX, MIN, SUM) using method name as |
||
277 | * reference. |
||
278 | * |
||
279 | * Example: |
||
280 | * echo $select->sum('user.balance'); |
||
281 | * |
||
282 | * @param string $method |
||
283 | * @param array $arguments |
||
284 | * |
||
285 | * @return int|float |
||
286 | * |
||
287 | * @throws BuilderException |
||
288 | * @throws QueryException |
||
289 | */ |
||
290 | public function __call(string $method, array $arguments) |
||
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | * |
||
320 | * @return \PDOStatement|QueryStatement |
||
321 | */ |
||
322 | public function getIterator() |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function sqlStatement(QueryCompiler $compiler = null): string |
||
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | */ |
||
361 | public function jsonSerialize() |
||
365 | |||
366 | /** |
||
367 | * Request all results as array. |
||
368 | * |
||
369 | * @return array |
||
370 | */ |
||
371 | public function fetchAll(): array |
||
375 | } |
||
376 |