Skip to content

Commit 9e12104

Browse files
staabmclxmstaab
andauthored
fixed error with placeholder in LIMIT clause (#99)
Co-authored-by: Markus Staab <[email protected]>
1 parent 6e0aac3 commit 9e12104

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

.phpunit-phpstan-dba.cache

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,55 @@
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\' AND email LIKE \'%@example%\'
40+
LIMIT 1
41+
' =>
42+
array (
43+
'error' => NULL,
44+
),
45+
'
46+
SELECT email, adaid
47+
FROM ada
48+
WHERE gesperrt = \'1\' AND email LIKE NULL
49+
LIMIT 1
50+
' =>
51+
array (
52+
'error' => NULL,
53+
),
554
'
655
SELECT email adaid
756
WHERE gesperrt = \'1\' AND email LIKE \'%@example.com\'

src/QueryReflection/QuerySimulation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public static function simulate(string $queryString): ?string
6666

6767
private static function stripTraillingLimit(string $queryString): ?string
6868
{
69-
return preg_replace('/\s*LIMIT\s+\d+\s*(,\s*\d*)?$/i', '', $queryString);
69+
// XXX someday we will use a proper SQL parser,
70+
// which would also allow us to support even more complex expressions like SELECT .. LIMIT X, Y FOR UPDATE
71+
return preg_replace('/\s*LIMIT\s+["\']?\d+["\']?\s*(,\s*["\']?\d*["\']?)?\s*$/i', '', $queryString);
7072
}
7173
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ public function noErrorOnMixedParams(Connection $connection, $unknownType)
4444
', [1, $unknownType]);
4545
}
4646

47+
public function noErrorOnPlaceholderInLimit(Connection $connection, int $limit)
48+
{
49+
$connection->preparedQuery('
50+
SELECT email, adaid
51+
FROM ada
52+
WHERE gesperrt = ?
53+
LIMIT ?
54+
', [1, $limit]);
55+
}
56+
57+
public function noErrorOnPlaceholderInOffsetAndLimit(Connection $connection, int $offset, int $limit)
58+
{
59+
$connection->preparedQuery('
60+
SELECT email, adaid
61+
FROM ada
62+
WHERE gesperrt = ?
63+
LIMIT ?, ?
64+
', [1, $offset, $limit]);
65+
}
66+
4767
public function preparedParams(Connection $connection)
4868
{
4969
$connection->preparedQuery('SELECT email, adaid FROM ada WHERE gesperrt = ?', [1]);

0 commit comments

Comments
 (0)