Skip to content

Commit

Permalink
Support queries with multiple same named placeholder (#141)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Staab <[email protected]>
  • Loading branch information
staabm and clxmstaab authored Jan 18, 2022
1 parent 8fa75b3 commit cac63a6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
21 changes: 21 additions & 0 deletions .phpunit-phpstan-dba.cache
Original file line number Diff line number Diff line change
Expand Up @@ -2349,6 +2349,11 @@ Simulated query: SELECT email, adaid FROM ada . WHERE email=\'my_other_table\' L
)),
),
),
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada
WHERE (gesperrt=\'1\' AND freigabe1u1=1) OR (gesperrt=\'1\' AND freigabe1u1=0)' =>
array (
'error' => NULL,
),
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email=\'my_other_table\' LIMIT 1' =>
array (
'error' => NULL,
Expand Down Expand Up @@ -2787,6 +2792,14 @@ Simulated query: SELECT email, adaid, gesperrt, freigabe1u1 FROM ada . WHERE ema
'code' => 1054,
)),
),
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE asdsa=?' =>
array (
'error' =>
staabm\PHPStanDba\Error::__set_state(array(
'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',
'code' => 1064,
)),
),
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email = \'[email protected]\'' =>
array (
'error' => NULL,
Expand Down Expand Up @@ -3017,5 +3030,13 @@ Simulated query: SELECT email, adaid, gesperrt, freigabe1u1 FROM ada . WHERE ema
array (
'error' => NULL,
),
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE gesperrt=?' =>
array (
'error' =>
staabm\PHPStanDba\Error::__set_state(array(
'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',
'code' => 1064,
)),
),
),
);
10 changes: 2 additions & 8 deletions src/QueryReflection/QueryReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PHPStan\Analyser\Scope;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -249,12 +248,7 @@ public function countPlaceholders(string $queryString): int
return $numPlaceholders;
}

$numPlaceholders = preg_match_all(self::REGEX_PLACEHOLDER, $queryString);
if (false === $numPlaceholders || $numPlaceholders < 0) {
throw new ShouldNotHappenException();
}

return $numPlaceholders;
return \count($this->extractNamedPlaceholders($queryString));
}

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

if (preg_match_all(self::REGEX_PLACEHOLDER, $queryString, $matches) > 0) {
return $matches[0];
return array_unique($matches[0]);
}

return [];
Expand Down
7 changes: 7 additions & 0 deletions tests/data/syntax-error-in-prepared-statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,11 @@ public function placeholderValidation(Connection $connection)

$connection->preparedQuery($query, [':gesperrt' => 1]);
}

public function samePlaceholderMultipleTimes(Connection $connection)
{
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada
WHERE (gesperrt=:gesperrt AND freigabe1u1=1) OR (gesperrt=:gesperrt AND freigabe1u1=0)';
$connection->preparedQuery($query, [':gesperrt' => 1]);
}
}

0 comments on commit cac63a6

Please sign in to comment.