Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SQL Anywhere] Fix query limit values "0" and "null" #3378

Merged
merged 1 commit into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? 'ALL', $offset + 1);
}

return $query;
return $limit === null ? '' : 'TOP ' . $limit;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand All @@ -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());
Expand Down