diff --git a/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php b/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php index a8fbfeb46e4..3090b8d45a0 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php @@ -1308,25 +1308,20 @@ protected function _getTransactionIsolationLevelSQL($level) */ protected function doModifyLimitQuery($query, $limit, $offset) { - $limitOffsetClause = ''; + $limitOffsetClause = $this->getTopClauseSQL($limit, $offset); - if ($limit > 0) { - $limitOffsetClause = 'TOP ' . $limit . ' '; - } + return $limitOffsetClause === '' + ? $query + : preg_replace('/^\s*(SELECT\s+(DISTINCT\s+)?)/i', '\1' . $limitOffsetClause . ' ', $query); + } + private function getTopClauseSQL(?int $limit, ?int $offset) : string + { if ($offset > 0) { - if ($limit === 0) { - $limitOffsetClause = 'TOP ALL '; - } - - $limitOffsetClause .= 'START AT ' . ($offset + 1) . ' '; - } - - if ($limitOffsetClause) { - return preg_replace('/^\s*(SELECT\s+(DISTINCT\s+)?)/i', '\1' . $limitOffsetClause, $query); + return sprintf('TOP %s START AT %d', $limit === null ? 'ALL' : $limit, $offset + 1); } - return $query; + return $limit === null ? '' : 'TOP ' . $limit; } /** diff --git a/tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php index 294b9836e2e..219119c2cc1 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php @@ -646,7 +646,7 @@ public function testModifiesLimitQueryWithOffset() $this->platform->modifyLimitQuery('SELECT * FROM user', 10, 5) ); self::assertEquals( - 'SELECT TOP ALL START AT 6 * FROM user', + 'SELECT TOP 0 START AT 6 * FROM user', $this->platform->modifyLimitQuery('SELECT * FROM user', 0, 5) ); } @@ -659,6 +659,14 @@ public function testModifiesLimitQueryWithSubSelect() ); } + public function testModifiesLimitQueryWithoutLimit() + { + self::assertEquals( + 'SELECT TOP ALL START AT 11 n FROM Foo', + $this->platform->modifyLimitQuery('SELECT n FROM Foo', null, 10) + ); + } + public function testPrefersIdentityColumns() { self::assertTrue($this->platform->prefersIdentityColumns());