Skip to content
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
31 changes: 18 additions & 13 deletions lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,24 +977,29 @@ public function getName()
*/
protected function doModifyLimitQuery($query, $limit, $offset = null)
{
$limit = (int) $limit;
$offset = (int) $offset;
if ($limit === null && $offset === null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove the integer casting entirely and just rely on the method signature integer|null for both $limit and $offset? Then just check everywhere against null. Also please remove the $has* variables as they are unnecessary.

Copy link
Member Author

@morozov morozov Jan 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deeky666 The confusing part here is how is $offset = null different from $offset = 0 from the SQL semantics standpoint? Should the DBAL produce additional sub-query in the latter case? Right now it doesn't which makes more sense to me.

UPD: comparing $offset to null will break unit test:

1) Doctrine\Tests\DBAL\Platforms\OraclePlatformTest::testModifyLimitQuery
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'SELECT a.* FROM (SELECT * FROM user) a WHERE ROWNUM <= 10'
+'SELECT * FROM (SELECT a.*, ROWNUM AS doctrine_rownum FROM (SELECT * FROM user) a WHERE ROWNUM <= 10) WHERE doctrine_rownum >= 1'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morozov yeah good point. But then leave it as before casting both parameters to int and checking for 0 instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deeky666 but unlike OFFSET, for LIMIT 0 and null have different meanings — "0 records" and "all records" respectively. There's no complete consistency between existing adapters but all of them except SQL Anywhere only omit $limit when it's null. Technically, it is possible to execute a query and retrieve 0 records from it, so the DBAL shouldn't be a limiting factor here. Also from the API consumer standpoint, if there's a bug in the calculation of $limit on the application end and it's calculated as 0, I'd better expect 0 records to be returned instead of all of them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morozov sounds reasonable. Thanks for clearing that up. I'll accept it as is now.

return $query;
}

if (preg_match('/^\s*SELECT/i', $query)) {
if (!preg_match('/\sFROM\s/i', $query)) {
$query .= " FROM dual";
}
if ($limit > 0) {
$max = $offset + $limit;
$column = '*';
if ($offset > 0) {
$min = $offset + 1;
$query = 'SELECT * FROM (SELECT a.' . $column . ', rownum AS doctrine_rownum FROM (' .
$query .
') a WHERE rownum <= ' . $max . ') WHERE doctrine_rownum >= ' . $min;
} else {
$query = 'SELECT a.' . $column . ' FROM (' . $query . ') a WHERE ROWNUM <= ' . $max;
}

$columns = array('a.*');

if ($offset > 0) {
$columns[] = 'ROWNUM AS doctrine_rownum';
}

$query = sprintf('SELECT %s FROM (%s) a', implode(', ', $columns), $query);

if ($limit !== null) {
$query .= sprintf(' WHERE ROWNUM <= %d', $offset + $limit);
}

if ($offset > 0) {
$query = sprintf('SELECT * FROM (%s) WHERE doctrine_rownum >= %d', $query, $offset + 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testModifyLimitQuerySimpleQuery()
$this->assertLimitResult(array(1, 2, 3, 4), $sql, 10, 0);
$this->assertLimitResult(array(1, 2), $sql, 2, 0);
$this->assertLimitResult(array(3, 4), $sql, 2, 2);
$this->assertLimitResult(array(2, 3, 4), $sql, null, 1);
}

public function testModifyLimitQueryJoinQuery()
Expand Down
12 changes: 12 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ public function testModifyLimitQueryWithEmptyOffset()
$this->assertEquals('SELECT a.* FROM (SELECT * FROM user) a WHERE ROWNUM <= 10', $sql);
}

public function testModifyLimitQueryWithNonEmptyOffset()
{
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 10);
$this->assertEquals('SELECT * FROM (SELECT a.*, ROWNUM AS doctrine_rownum FROM (SELECT * FROM user) a WHERE ROWNUM <= 20) WHERE doctrine_rownum >= 11', $sql);
}

public function testModifyLimitQueryWithEmptyLimit()
{
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', null, 10);
$this->assertEquals('SELECT * FROM (SELECT a.*, ROWNUM AS doctrine_rownum FROM (SELECT * FROM user) a) WHERE doctrine_rownum >= 11', $sql);
}

public function testModifyLimitQueryWithAscOrderBy()
{
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username ASC', 10);
Expand Down