Skip to content

Commit 88e3749

Browse files
staabmclxmstaab
andauthored
prevent unnecessary work in union type queries (#147)
Co-authored-by: Markus Staab <[email protected]>
1 parent 22c9e4a commit 88e3749

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

.phpunit-phpstan-dba.cache

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,64 @@
22
'schemaVersion' => 'v3-rename-props',
33
'records' =>
44
array (
5+
'
6+
SELECT email adaid
7+
WHERE gesperrt = \'1\' AND email LIKE \'%@example.com\'
8+
FROM ada
9+
LIMIT 1
10+
' =>
11+
array (
12+
'error' =>
13+
staabm\PHPStanDba\Error::__set_state(array(
14+
'message' => 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near \'FROM ada LIMIT 0\' at line 3',
15+
'code' => 1064,
16+
)),
17+
),
18+
'
19+
SELECT email, adaid
20+
FROM ada
21+
WHERE gesperrt = \'1\'
22+
LIMIT \'1\'
23+
' =>
24+
array (
25+
'error' => NULL,
26+
),
27+
'
28+
SELECT email, adaid
29+
FROM ada
30+
WHERE gesperrt = \'1\'
31+
LIMIT \'1\', \'1\'
32+
' =>
33+
array (
34+
'error' => NULL,
35+
),
36+
'
37+
SELECT email, adaid
38+
FROM ada
39+
WHERE gesperrt = \'1\'
40+
LIMIT \'1\', \'1\'
41+
' =>
42+
array (
43+
'error' => NULL,
44+
),
45+
'
46+
SELECT email, adaid
47+
FROM ada
48+
WHERE gesperrt = \'1\' AND email LIKE \'%@example%\'
49+
LIMIT 1
50+
' =>
51+
array (
52+
'error' => NULL,
53+
),
54+
'
55+
SELECT email, adaid
56+
FROM ada
57+
WHERE gesperrt = \'1\' AND email LIKE NULL
58+
LIMIT 1
59+
' =>
60+
array (
61+
'error' => NULL,
62+
),
563
'
664
SELECT email adaid
765
WHERE gesperrt = \'1\' AND email LIKE \'%@example.com\'
@@ -2717,6 +2775,11 @@ Simulated query: SELECT email, adaid FROM ada . WHERE email=\'my_other_table\' L
27172775
array (
27182776
'error' => NULL,
27192777
),
2778+
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada
2779+
WHERE (gesperrt=\'1\' AND freigabe1u1=1) OR (gesperrt=\'1\' AND freigabe1u1=0)' =>
2780+
array (
2781+
'error' => NULL,
2782+
),
27202783
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email=\'my_other_table\' LIMIT 1' =>
27212784
array (
27222785
'error' => NULL,

src/QueryReflection/QueryReflection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public function resolvePreparedQueryStrings(Expr $queryExpr, Type $parameterType
8484
$queryString = $this->replaceParameters($queryString, $parameters);
8585
yield $queryString;
8686
}
87+
88+
return;
8789
}
8890

8991
$queryString = $this->resolvePreparedQueryString($queryExpr, $parameterTypes, $scope);
@@ -119,6 +121,8 @@ public function resolveQueryStrings(Expr $queryExpr, Scope $scope): iterable
119121
foreach (TypeUtils::getConstantStrings($type) as $constantString) {
120122
yield $constantString->getValue();
121123
}
124+
125+
return;
122126
}
123127

124128
$queryString = $this->resolveQueryString($queryExpr, $scope);
@@ -258,7 +262,7 @@ public function countPlaceholders(string $queryString): int
258262
if (preg_match_all(self::REGEX_UNNAMED_PLACEHOLDER, $queryString, $matches) > 0) {
259263
$candidates = $matches[0];
260264

261-
// filter placeholders within quotes strings
265+
// filter placeholders within quoted strings
262266
$candidates = array_filter($candidates, function ($candidate) {
263267
return '"' !== $candidate[0] && "'" !== $candidate[0];
264268
});
@@ -277,7 +281,7 @@ public function extractNamedPlaceholders(string $queryString): array
277281
if (preg_match_all(self::REGEX_NAMED_PLACEHOLDER, $queryString, $matches) > 0) {
278282
$candidates = $matches[0];
279283

280-
// filter placeholders within quotes strings
284+
// filter placeholders within quoted strings
281285
$candidates = array_filter($candidates, function ($candidate) {
282286
return '"' !== $candidate[0] && "'" !== $candidate[0];
283287
});

0 commit comments

Comments
 (0)