Total Complexity | 42 |
Total Lines | 417 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | Features | 1 |
Complex classes like FindObjectsFromRawSqlQueryFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FindObjectsFromRawSqlQueryFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class FindObjectsFromRawSqlQueryFactory implements QueryFactory |
||
19 | { |
||
20 | /** |
||
21 | * @var array[] |
||
22 | */ |
||
23 | protected $columnDescriptors; |
||
24 | /** |
||
25 | * @var Schema |
||
26 | */ |
||
27 | private $schema; |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $processedSql; |
||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $processedSqlCount; |
||
36 | /** |
||
37 | * @var TDBMService |
||
38 | */ |
||
39 | private $tdbmService; |
||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $mainTable; |
||
44 | |||
45 | /** |
||
46 | * FindObjectsFromRawSqlQueryFactory constructor. |
||
47 | * @param TDBMService $tdbmService |
||
48 | * @param Schema $schema |
||
49 | * @param string $mainTable |
||
50 | * @param string $sql |
||
51 | * @param string $sqlCount |
||
52 | */ |
||
53 | public function __construct(TDBMService $tdbmService, Schema $schema, string $mainTable, string $sql, string $sqlCount = null) |
||
60 | } |
||
61 | |||
62 | public function sort($orderBy): void |
||
63 | { |
||
64 | throw new TDBMException('sort not supported for raw sql queries'); |
||
65 | } |
||
66 | |||
67 | public function getMagicSql(): string |
||
68 | { |
||
69 | return $this->processedSql; |
||
70 | } |
||
71 | |||
72 | public function getMagicSqlCount(): string |
||
73 | { |
||
74 | return $this->processedSqlCount; |
||
75 | } |
||
76 | |||
77 | public function getColumnDescriptors(): array |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param string $sql |
||
84 | * @param null|string $sqlCount |
||
85 | * @return mixed[] An array of 3 elements: [$processedSql, $processedSqlCount, $columnDescriptors] |
||
86 | * @throws TDBMException |
||
87 | */ |
||
88 | private function compute(string $sql, ?string $sqlCount): array |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param mixed[] $parsedSql |
||
106 | * @param null|string $sqlCount |
||
107 | * @return mixed[] An array of 3 elements: [$processedSql, $processedSqlCount, $columnDescriptors] |
||
108 | * @throws \PHPSQLParser\exceptions\UnsupportedFeatureException|\PHPSQLParser\exceptions\UnableToCreateSQLException |
||
109 | */ |
||
110 | private function processParsedUnionQuery(array $parsedSql, ?string $sqlCount): array |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param mixed[] $parsed |
||
150 | */ |
||
151 | private function buildUnion(array $parsed): string |
||
152 | { |
||
153 | $selectBuilder = new SelectStatementBuilder(); |
||
154 | |||
155 | return implode(' UNION ', array_map(function ($clause) use ($selectBuilder) { |
||
156 | return '(' . $selectBuilder->build($clause) . ')'; |
||
157 | }, $parsed['UNION'])); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param mixed[] $parsedSql |
||
162 | * @param null|string $sqlCount |
||
163 | * @return mixed[] An array of 3 elements: [$processedSql, $processedSqlCount, $columnDescriptors] |
||
164 | */ |
||
165 | private function processParsedSelectQuery(array $parsedSql, ?string $sqlCount): array |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param mixed[] $baseSelect |
||
188 | * @return mixed[] An array of 2 elements: [$formattedSelect, $columnDescriptors] |
||
189 | * @throws TDBMException |
||
190 | * @throws \Doctrine\DBAL\Schema\SchemaException |
||
191 | */ |
||
192 | private function formatSelect(array $baseSelect): array |
||
193 | { |
||
194 | $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable); |
||
195 | $tableGroup = $this->getTableGroupName($relatedTables); |
||
196 | |||
197 | $connection = $this->tdbmService->getConnection(); |
||
198 | $formattedSelect = []; |
||
199 | $formattedCountSelect = []; |
||
200 | $columnDescriptors = []; |
||
201 | $fetchedTables = []; |
||
202 | |||
203 | foreach ($baseSelect as $entry) { |
||
204 | if ($entry['expr_type'] !== 'colref') { |
||
205 | $formattedSelect[] = $entry; |
||
206 | $formattedCountSelect[] = $entry; |
||
207 | continue; |
||
208 | } |
||
209 | |||
210 | $noQuotes = $entry['no_quotes']; |
||
211 | if ($noQuotes['delim'] !== '.' || count($noQuotes['parts']) !== 2) { |
||
212 | $formattedSelect[] = $entry; |
||
213 | $formattedCountSelect[] = $entry; |
||
214 | continue; |
||
215 | } |
||
216 | |||
217 | $tableName = $noQuotes['parts'][0]; |
||
218 | if (!in_array($tableName, $relatedTables)) { |
||
219 | $formattedSelect[] = $entry; |
||
220 | $formattedCountSelect[] = $entry; |
||
221 | continue; |
||
222 | } |
||
223 | |||
224 | $columnName = $noQuotes['parts'][1]; |
||
225 | if ($columnName !== '*') { |
||
226 | $formattedSelect[] = $entry; |
||
227 | $formattedCountSelect[] = $entry; |
||
228 | continue; |
||
229 | } |
||
230 | |||
231 | $table = $this->schema->getTable($tableName); |
||
232 | $pkColumns = $table->getPrimaryKeyColumns(); |
||
233 | foreach ($table->getColumns() as $column) { |
||
234 | $columnName = $column->getName(); |
||
235 | $alias = AbstractQueryFactory::getColumnAlias($tableName, $columnName); |
||
236 | $astColumn = [ |
||
237 | 'expr_type' => 'colref', |
||
238 | 'base_expr' => $connection->quoteIdentifier($tableName) . '.' . $connection->quoteIdentifier($columnName), |
||
239 | 'no_quotes' => [ |
||
240 | 'delim' => '.', |
||
241 | 'parts' => [ |
||
242 | $tableName, |
||
243 | $columnName |
||
244 | ] |
||
245 | ], |
||
246 | 'alias' => [ |
||
247 | 'as' => true, |
||
248 | 'name' => $alias, |
||
249 | ] |
||
250 | ]; |
||
251 | $formattedSelect[] = $astColumn; |
||
252 | if (in_array($columnName, $pkColumns)) { |
||
253 | $formattedCountSelect[] = $astColumn; |
||
254 | } |
||
255 | $columnDescriptors[$alias] = [ |
||
256 | 'as' => $alias, |
||
257 | 'table' => $tableName, |
||
258 | 'column' => $columnName, |
||
259 | 'type' => $column->getType(), |
||
260 | 'tableGroup' => $tableGroup, |
||
261 | ]; |
||
262 | } |
||
263 | $fetchedTables[] = $tableName; |
||
264 | } |
||
265 | |||
266 | $missingTables = array_diff($relatedTables, $fetchedTables); |
||
267 | if (!empty($missingTables)) { |
||
268 | throw new TDBMException('Missing tables '.implode(', ', $missingTables).' in SELECT statement'); |
||
269 | } |
||
270 | |||
271 | for ($i = 0; $i < count($formattedSelect) - 1; $i++) { |
||
272 | if (!isset($formattedSelect[$i]['delim'])) { |
||
273 | $formattedSelect[$i]['delim'] = ','; |
||
274 | } |
||
275 | } |
||
276 | |||
277 | for ($i = 0; $i < count($formattedCountSelect) - 1; $i++) { |
||
278 | if (!isset($formattedCountSelect[$i]['delim'])) { |
||
279 | $formattedCountSelect[$i]['delim'] = ','; |
||
280 | } |
||
281 | } |
||
282 | return [$formattedSelect, $formattedCountSelect, $columnDescriptors]; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param mixed[] $parsedSql |
||
287 | * @return mixed[] |
||
288 | */ |
||
289 | private function generateParsedSqlCount(array $parsedSql): array |
||
290 | { |
||
291 | if (isset($parsedSql['ORDER'])) { |
||
292 | unset($parsedSql['ORDER']); |
||
293 | } |
||
294 | |||
295 | if (!isset($parsedSql['GROUP'])) { |
||
296 | // most simple case:no GROUP BY in query |
||
297 | return $this->generateSimpleSqlCount($parsedSql); |
||
298 | } elseif (!isset($parsedSql['HAVING'])) { |
||
299 | // GROUP BY without HAVING statement: let's COUNT the DISTINCT grouped columns |
||
300 | return $this->generateGroupedSqlCount($parsedSql); |
||
301 | } else { |
||
302 | // GROUP BY with a HAVING statement: we'll have to wrap the query |
||
303 | return $this->generateWrappedSqlCount($parsedSql); |
||
304 | } |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @param mixed[] $parsedSql The AST of the SQL query |
||
309 | * @return mixed[] An AST representing the matching COUNT query |
||
310 | */ |
||
311 | private function generateSimpleSqlCount(array $parsedSql): array |
||
312 | { |
||
313 | // If the query is a DISTINCT, we need to deal with the count. |
||
314 | |||
315 | |||
316 | // We need to count on the same columns: COUNT(DISTINCT country.id, country.label) .... |
||
317 | // but we need to remove the "alias" bit. |
||
318 | |||
319 | if ($this->isDistinctQuery($parsedSql)) { |
||
320 | // Only MySQL can do DISTINCT counts. |
||
321 | // Other databases should wrap the query |
||
322 | if (!$this->tdbmService->getConnection()->getSchemaManager()->getDatabasePlatform() instanceof MySqlPlatform) { |
||
323 | return $this->generateWrappedSqlCount($parsedSql); |
||
324 | } |
||
325 | |||
326 | $countSubExpr = array_map(function (array $item) { |
||
327 | unset($item['alias']); |
||
328 | return $item; |
||
329 | }, $parsedSql['SELECT']); |
||
330 | } else { |
||
331 | $countSubExpr = [ |
||
332 | [ |
||
333 | 'expr_type' => 'colref', |
||
334 | 'base_expr' => '*', |
||
335 | 'sub_tree' => false |
||
336 | ] |
||
337 | ]; |
||
338 | } |
||
339 | |||
340 | $parsedSql['SELECT'] = [[ |
||
341 | 'expr_type' => 'aggregate_function', |
||
342 | 'alias' => [ |
||
343 | 'as' => true, |
||
344 | 'name' => 'cnt', |
||
345 | ], |
||
346 | 'base_expr' => 'COUNT', |
||
347 | 'sub_tree' => $countSubExpr, |
||
348 | 'delim' => false, |
||
349 | ]]; |
||
350 | |||
351 | return $parsedSql; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @param mixed[] $parsedSql AST to analyze |
||
356 | * @return bool |
||
357 | */ |
||
358 | private function isDistinctQuery(array $parsedSql): bool |
||
359 | { |
||
360 | foreach ($parsedSql['SELECT'] as $item) { |
||
361 | if ($item['expr_type'] === 'reserved' && $item['base_expr'] === 'DISTINCT') { |
||
362 | return true; |
||
363 | } |
||
364 | } |
||
365 | return false; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @param mixed[] $parsedSql The AST of the SQL query |
||
370 | * @return mixed[] An AST representing the matching COUNT query |
||
371 | */ |
||
372 | private function generateGroupedSqlCount(array $parsedSql): array |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @param mixed[] $parsedSql The AST of the SQL query |
||
395 | * @return mixed[] An AST representing the matching COUNT query |
||
396 | */ |
||
397 | private function generateWrappedSqlCount(array $parsedSql): array |
||
398 | { |
||
399 | return [ |
||
400 | 'SELECT' => [[ |
||
401 | 'expr_type' => 'aggregate_function', |
||
402 | 'alias' => [ |
||
403 | 'as' => true, |
||
404 | 'name' => 'cnt', |
||
405 | ], |
||
406 | 'base_expr' => 'COUNT', |
||
407 | 'sub_tree' => [ |
||
408 | [ |
||
409 | 'expr_type' => 'colref', |
||
410 | 'base_expr' => '*', |
||
411 | 'sub_tree' => false |
||
412 | ] |
||
413 | ], |
||
414 | 'delim' => false, |
||
415 | ]], |
||
416 | 'FROM' => [[ |
||
417 | 'expr_type' => 'subquery', |
||
418 | 'alias' => [ |
||
419 | 'as' => true, |
||
420 | 'name' => '____query' |
||
421 | ], |
||
422 | 'sub_tree' => $parsedSql, |
||
423 | ]] |
||
424 | ]; |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * @param string[] $relatedTables |
||
429 | * @return string |
||
430 | */ |
||
431 | protected function getTableGroupName(array $relatedTables): string |
||
435 | } |
||
436 | } |
||
437 |