Skip to content

Commit

Permalink
Use the recommended FETCH NEXT x ROWS clause instead of LIMIT x
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Apr 10, 2023
1 parent 78b7abf commit 9f7a1f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
23 changes: 7 additions & 16 deletions src/Platform/DB2IBMiPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,25 +333,16 @@ protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
*/
protected function doModifyLimitQuery($query, $limit, $offset = null)
{
if (null === $limit) {
return $query;
}

if ($limit < 0) {
throw new Exception(sprintf('Limit must be a positive integer or zero, %d given', $limit));
}

if (0 === $offset && false === strpos($query, 'ORDER BY')) {
// In cases where an offset isn't required and there's no "ORDER BY" clause,
// we can use the much simpler "FETCH FIRST".

return sprintf('%s FETCH FIRST %u ROWS ONLY', $query, $limit);
if ($offset > 0) {
$query .= sprintf(' OFFSET %u ROWS', $offset);
}

$query .= sprintf(' LIMIT %u', $limit);
if (null !== $limit) {
if ($limit < 0) {
throw new Exception(sprintf('Limit must be a positive integer or zero, %d given', $limit));
}

if ($offset > 0) {
$query .= sprintf(' OFFSET %u', $offset);
$query .= sprintf(' FETCH %s %u ROWS ONLY', 0 === $offset ? 'FIRST' : 'NEXT', $limit);
}

return $query;
Expand Down
16 changes: 8 additions & 8 deletions tests/Platform/DB2IBMiPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ public function testVarcharTypeDeclarationSQLSnippet(string $expectedSql, array
public function limitQueryProvider(): iterable
{
yield [
'SELECT * FROM mytable LIMIT 5 OFFSET 2',
'SELECT * FROM mytable OFFSET 2 ROWS FETCH NEXT 5 ROWS ONLY',
'SELECT * FROM mytable',
5,
2,
];

yield [
'SELECT * FROM mytable LIMIT 1 OFFSET 1',
'SELECT * FROM mytable OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY',
'SELECT * FROM mytable',
1,
1,
];

yield [
'SELECT * FROM mytable LIMIT 1 OFFSET 2',
'SELECT * FROM mytable OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY',
'SELECT * FROM mytable',
1,
2,
Expand Down Expand Up @@ -149,7 +149,7 @@ public function limitQueryProvider(): iterable
];

yield [
'SELECT * FROM mytable',
'SELECT * FROM mytable OFFSET 1 ROWS',
'SELECT * FROM mytable',
null,
1,
Expand All @@ -163,21 +163,21 @@ public function limitQueryProvider(): iterable
];

yield [
'SELECT * FROM mytable ORDER BY created_at LIMIT 1',
'SELECT * FROM mytable ORDER BY created_at FETCH FIRST 1 ROWS ONLY',
'SELECT * FROM mytable ORDER BY created_at',
1,
0,
];

yield [
'SELECT * FROM mytable ORDER BY created_at LIMIT 1',
'SELECT * FROM mytable ORDER BY created_at FETCH FIRST 1 ROWS ONLY',
'SELECT * FROM mytable ORDER BY created_at',
1,
null,
];

yield [
'SELECT * FROM mytable ORDER BY created_at LIMIT 0',
'SELECT * FROM mytable ORDER BY created_at FETCH FIRST 0 ROWS ONLY',
'SELECT * FROM mytable ORDER BY created_at',
0,
0,
Expand All @@ -191,7 +191,7 @@ public function limitQueryProvider(): iterable
];

yield [
'SELECT * FROM mytable ORDER BY created_at LIMIT 0',
'SELECT * FROM mytable ORDER BY created_at FETCH FIRST 0 ROWS ONLY',
'SELECT * FROM mytable ORDER BY created_at',
0,
null,
Expand Down

0 comments on commit 9f7a1f5

Please sign in to comment.