Skip to content

Commit c1fe064

Browse files
committed
throw unresolvable query while query resolving
1 parent 75d3dcb commit c1fe064

4 files changed

+28
-26
lines changed

src/QueryReflection/QueryReflection.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use staabm\PHPStanDba\SchemaReflection\SchemaReflection;
3535
use staabm\PHPStanDba\SqlAst\ParserInference;
3636
use staabm\PHPStanDba\UnresolvableQueryException;
37+
use staabm\PHPStanDba\UnresolvableQueryDynamicFromException;
3738

3839
final class QueryReflection
3940
{
@@ -280,13 +281,7 @@ public function resolveQueryStrings(Expr $queryExpr, Scope $scope): iterable
280281

281282
$queryString = $this->resolveQueryExpr($queryExpr, $scope);
282283
if (null !== $queryString) {
283-
$normalizedQuery = QuerySimulation::stripComments($this->normalizeQueryString($queryString));
284-
285-
// query simulation might lead in a invalid query, skip those
286-
$error = $this->validateQueryString($normalizedQuery);
287-
if ($error === null) {
288-
yield $normalizedQuery;
289-
}
284+
yield QuerySimulation::stripComments($this->normalizeQueryString($queryString));
290285
}
291286
}
292287

@@ -360,6 +355,13 @@ private function resolveQueryStringExpr(Expr $queryExpr, Scope $scope, bool $res
360355
$leftString = $this->resolveQueryStringExpr($left, $scope);
361356
$rightString = $this->resolveQueryStringExpr($right, $scope);
362357

358+
// queries with a dynamic FROM are not resolvable
359+
if (QueryReflection::getRuntimeConfiguration()->isDebugEnabled()) {
360+
if (str_ends_with(rtrim($leftString), 'FROM') && is_numeric(trim($rightString, '"\''))) {
361+
throw new UnresolvableQueryDynamicFromException('Seems the query is too dynamic to be resolved by query simulation');
362+
}
363+
}
364+
363365
if (null === $leftString || null === $rightString) {
364366
return null;
365367
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace staabm\PHPStanDba;
6+
7+
/**
8+
* @api
9+
*/
10+
final class UnresolvableQueryDynamicFromException extends UnresolvableQueryException
11+
{
12+
public static function getTip(): string
13+
{
14+
return 'Consider to simplify the FROM-clause construction.';
15+
}
16+
}

src/UnresolvableQueryInvalidAfterSimulationException.php

-16
This file was deleted.

tests/rules/UnresolvableQueryMethodRuleTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PHPStan\Testing\RuleTestCase;
99
use staabm\PHPStanDba\QueryReflection\QueryReflection;
1010
use staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule;
11-
use staabm\PHPStanDba\UnresolvableQueryInvalidAfterSimulationException;
11+
use staabm\PHPStanDba\UnresolvableQueryDynamicFromException;
1212
use staabm\PHPStanDba\UnresolvableQueryMixedTypeException;
1313
use staabm\PHPStanDba\UnresolvableQueryStringTypeException;
1414

@@ -76,7 +76,7 @@ public function testBug548(): void
7676
[
7777
'Unresolvable Query: Seems the query is too dynamic to be resolved by query simulation.',
7878
10,
79-
UnresolvableQueryInvalidAfterSimulationException::getTip(),
79+
UnresolvableQueryDynamicFromException::getTip(),
8080
],
8181
]);
8282
}
@@ -87,7 +87,7 @@ public function testBug547(): void
8787
[
8888
'Unresolvable Query: Seems the query is too dynamic to be resolved by query simulation.',
8989
10,
90-
UnresolvableQueryInvalidAfterSimulationException::getTip(),
90+
UnresolvableQueryDynamicFromException::getTip(),
9191
],
9292
]);
9393
}

0 commit comments

Comments
 (0)