Skip to content

Commit

Permalink
Fix Oracle subquery ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
raykolbe committed Apr 9, 2013
1 parent b8b7afe commit 4bafcc5
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;

/**
* Wraps the query in order to select root entity IDs for pagination.
Expand Down Expand Up @@ -138,19 +139,13 @@ public function walkSelectStatement(SelectStatement $AST)
}

// Build the counter query
if ($this->platform instanceof OraclePlatform) {
// Ordering is lost in Oracle with subqueries
// http://www.doctrine-project.org/jira/browse/DDC-1800
$sql = sprintf('SELECT DISTINCT %s, ROWNUM FROM (%s) dctrn_result ORDER BY ROWNUM ASC',
implode(', ', $sqlIdentifier), $innerSql);
} else {
$sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result',
implode(', ', $sqlIdentifier), $innerSql);
}
$sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result',
implode(', ', $sqlIdentifier), $innerSql);

if ($this->platform instanceof PostgreSqlPlatform) {
if ($this->platform instanceof PostgreSqlPlatform ||
$this->platform instanceof OraclePlatform) {
//http://www.doctrine-project.org/jira/browse/DDC-1958
$this->getPostgresqlSql($AST, $sqlIdentifier, $innerSql, $sql);
$this->preserveSqlOrdering($AST, $sqlIdentifier, $innerSql, $sql);
}

// Apply the limit and offset.
Expand All @@ -168,9 +163,9 @@ public function walkSelectStatement(SelectStatement $AST)

return $sql;
}

/**
* Generates new SQL for postgresql if necessary.
* Generates new SQL for Postgresql or Oracle if necessary.
*
* @param SelectStatement $AST
* @param array $sqlIdentifier
Expand All @@ -179,7 +174,7 @@ public function walkSelectStatement(SelectStatement $AST)
*
* @return void
*/
public function getPostgresqlSql(SelectStatement $AST, array $sqlIdentifier, $innerSql, &$sql)
public function preserveSqlOrdering(SelectStatement $AST, array $sqlIdentifier, $innerSql, &$sql)
{
// For every order by, find out the SQL alias by inspecting the ResultSetMapping.
$sqlOrderColumns = array();
Expand Down

0 comments on commit 4bafcc5

Please sign in to comment.