diff --git a/src/Platform/DB2IBMiPlatform.php b/src/Platform/DB2IBMiPlatform.php index 5940160..c3a43a7 100644 --- a/src/Platform/DB2IBMiPlatform.php +++ b/src/Platform/DB2IBMiPlatform.php @@ -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; diff --git a/tests/Platform/DB2IBMiPlatformTest.php b/tests/Platform/DB2IBMiPlatformTest.php index 158c788..e5080f3 100644 --- a/tests/Platform/DB2IBMiPlatformTest.php +++ b/tests/Platform/DB2IBMiPlatformTest.php @@ -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, @@ -149,7 +149,7 @@ public function limitQueryProvider(): iterable ]; yield [ - 'SELECT * FROM mytable', + 'SELECT * FROM mytable OFFSET 1 ROWS', 'SELECT * FROM mytable', null, 1, @@ -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, @@ -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,