Skip to content

Commit 9f896b7

Browse files
staabmclxmstaab
andauthored
Don't report syntax error on incomplete query (#26)
Co-authored-by: Markus Staab <[email protected]>
1 parent b014c17 commit 9f896b7

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

.phpstan-dba.cache

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@
468468
)),
469469
),
470470
),
471+
'SELECT email, adaid, gesperrt, freigabe1u1 FROM LIMIT 1' =>
472+
array (
473+
'error' =>
474+
staabm\PHPStanDba\Error::__set_state(array(
475+
'message' => 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'LIMIT 0\' at line 1',
476+
'code' => 1064,
477+
)),
478+
'result' =>
479+
array (
480+
1 => NULL,
481+
),
482+
),
471483
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada' =>
472484
array (
473485
'error' => NULL,

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
],
3535
"phpstan": [
3636
"phpstan analyse -c phpstan.neon.dist"
37+
],
38+
"phpunit": [
39+
"phpunit"
3740
]
3841
},
3942
"config": {

src/QueryReflection/QueryReflection.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,11 @@ private function resolveQueryString(Expr $expr, Scope $scope): ?string
8686
$leftString = $this->resolveQueryString($left, $scope);
8787
$rightString = $this->resolveQueryString($right, $scope);
8888

89-
if ($leftString && $rightString) {
90-
return $leftString.$rightString;
91-
}
92-
if ($leftString) {
93-
return $leftString;
94-
}
95-
if ($rightString) {
96-
return $rightString;
89+
if (null === $leftString || null === $rightString) {
90+
return null;
9791
}
92+
93+
return $leftString.$rightString;
9894
}
9995

10096
$type = $scope->getType($expr);

tests/data/runMysqlQuery.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Foo
2525
public function run(string $prodHostname, DbCredentials $dbCredentials)
2626
{
2727
$prodDomains = runMysqlQuery('SELECT cmsdomainid FROM cmsdomain WHERE url ="'.$prodHostname.'" and standard=1', $dbCredentials);
28-
assertType('array<int, array{string}>|null', $prodDomains);
28+
assertType('array<int, array<int, string>>|null', $prodDomains);
29+
// XXX should be more precise, when we would be smarter in query parsing
30+
// assertType('array<int, array{string}>|null', $prodDomains);
2931
}
3032
}

tests/data/syntax-error-in-query.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function unknownTable(PDO $pdo)
4141
$pdo->query('SELECT * FROM unknownTable', PDO::FETCH_ASSOC);
4242
}
4343

44+
public function incompleteQuery(PDO $pdo, string $tableName)
45+
{
46+
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM '.$tableName.' LIMIT 1', PDO::FETCH_ASSOC);
47+
}
48+
4449
public function validQuery(PDO $pdo)
4550
{
4651
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada', PDO::FETCH_ASSOC);

0 commit comments

Comments
 (0)