Skip to content

Commit cac63a6

Browse files
staabmclxmstaab
andauthored
Support queries with multiple same named placeholder (#141)
Co-authored-by: Markus Staab <[email protected]>
1 parent 8fa75b3 commit cac63a6

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

.phpunit-phpstan-dba.cache

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,11 @@ Simulated query: SELECT email, adaid FROM ada . WHERE email=\'my_other_table\' L
23492349
)),
23502350
),
23512351
),
2352+
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada
2353+
WHERE (gesperrt=\'1\' AND freigabe1u1=1) OR (gesperrt=\'1\' AND freigabe1u1=0)' =>
2354+
array (
2355+
'error' => NULL,
2356+
),
23522357
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email=\'my_other_table\' LIMIT 1' =>
23532358
array (
23542359
'error' => NULL,
@@ -2787,6 +2792,14 @@ Simulated query: SELECT email, adaid, gesperrt, freigabe1u1 FROM ada . WHERE ema
27872792
'code' => 1054,
27882793
)),
27892794
),
2795+
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE asdsa=?' =>
2796+
array (
2797+
'error' =>
2798+
staabm\PHPStanDba\Error::__set_state(array(
2799+
'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 \'? LIMIT 0\' at line 1',
2800+
'code' => 1064,
2801+
)),
2802+
),
27902803
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email = \'[email protected]\'' =>
27912804
array (
27922805
'error' => NULL,
@@ -3017,5 +3030,13 @@ Simulated query: SELECT email, adaid, gesperrt, freigabe1u1 FROM ada . WHERE ema
30173030
array (
30183031
'error' => NULL,
30193032
),
3033+
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE gesperrt=?' =>
3034+
array (
3035+
'error' =>
3036+
staabm\PHPStanDba\Error::__set_state(array(
3037+
'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 \'? LIMIT 0\' at line 1',
3038+
'code' => 1064,
3039+
)),
3040+
),
30203041
),
30213042
);

src/QueryReflection/QueryReflection.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpParser\Node\Expr;
88
use PhpParser\Node\Expr\BinaryOp\Concat;
99
use PHPStan\Analyser\Scope;
10-
use PHPStan\ShouldNotHappenException;
1110
use PHPStan\Type\Constant\ConstantArrayType;
1211
use PHPStan\Type\Constant\ConstantIntegerType;
1312
use PHPStan\Type\Constant\ConstantStringType;
@@ -249,12 +248,7 @@ public function countPlaceholders(string $queryString): int
249248
return $numPlaceholders;
250249
}
251250

252-
$numPlaceholders = preg_match_all(self::REGEX_PLACEHOLDER, $queryString);
253-
if (false === $numPlaceholders || $numPlaceholders < 0) {
254-
throw new ShouldNotHappenException();
255-
}
256-
257-
return $numPlaceholders;
251+
return \count($this->extractNamedPlaceholders($queryString));
258252
}
259253

260254
/**
@@ -270,7 +264,7 @@ public function extractNamedPlaceholders(string $queryString): array
270264
}
271265

272266
if (preg_match_all(self::REGEX_PLACEHOLDER, $queryString, $matches) > 0) {
273-
return $matches[0];
267+
return array_unique($matches[0]);
274268
}
275269

276270
return [];

tests/data/syntax-error-in-prepared-statement.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,11 @@ public function placeholderValidation(Connection $connection)
136136

137137
$connection->preparedQuery($query, [':gesperrt' => 1]);
138138
}
139+
140+
public function samePlaceholderMultipleTimes(Connection $connection)
141+
{
142+
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada
143+
WHERE (gesperrt=:gesperrt AND freigabe1u1=1) OR (gesperrt=:gesperrt AND freigabe1u1=0)';
144+
$connection->preparedQuery($query, [':gesperrt' => 1]);
145+
}
139146
}

0 commit comments

Comments
 (0)