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

fixed PostgreSQL and Oracle pagination issues #1325

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
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
48 changes: 42 additions & 6 deletions lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,48 @@ public function preserveSqlOrdering(SelectStatement $AST, array $sqlIdentifier,
}

if (count($orderBy)) {
$sql = sprintf(
'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s',
implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)),
$innerSql,
implode(', ', $orderBy)
);

// http://www.doctrine-project.org/jira/browse/DDC-1958
Copy link
Member

Choose a reason for hiding this comment

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

Can this entire block be moved to a private method?

if ($this->platform->getName() == 'postgresql' || $this->platform->getName() == 'oracle') {
Copy link
Member

Choose a reason for hiding this comment

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

We should use instanceof here

if (preg_match('/SELECT (.*?) FROM /i', $innerSql, $regs)) {
$selectedParts = explode(",", $regs[1]);
}
$globalOrderByStmt = SqlWalker::walkOrderByClause($AST->orderByClause);

/*
* Adding row_number in select statement, instead of selecting all fields in order clause
* being away from the DISTINCTION leaks, (e.g. 1-a, a 1-b)
*/
$selectedParts[] = sprintf("row_number() OVER(%s) as rownum ", $globalOrderByStmt);

$selectClause = implode(',', $selectedParts);
$innerSql = preg_replace("/^\s*select .+ from (.*)$/im", "SELECT {$selectClause} FROM $1", $innerSql);

// Grouping by primary key and min(rownumber) for correct result
$sql = sprintf(
Copy link
Member

Choose a reason for hiding this comment

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

Return directly

'SELECT %s FROM (%s) dctrn_result GROUP BY %s ORDER BY %s',
implode(', ', array_merge($sqlIdentifier, ['min(rownum) as minrow'])),
$innerSql, implode(',', $sqlIdentifier),
'minrow asc');
} else {

/*
* MySQL does'nt reset the ordering of subselect even when the fields
* which are participated in order by statement, arent selected in
* main select statement.
*/
if($this->platform->getName() == 'mysql'){
Copy link
Contributor

Choose a reason for hiding this comment

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

cs, missing spaces if (, ) {

$selectFields = array_merge($sqlIdentifier);
Copy link
Member

Choose a reason for hiding this comment

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

This array_merge() call is not needed

} else {
Copy link
Member

Choose a reason for hiding this comment

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

Avoid the else clause if not needed

$selectFields = array_merge($sqlIdentifier, $sqlOrderColumns);
}
$sql = sprintf(
Copy link
Member

Choose a reason for hiding this comment

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

Return directly

'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s',
implode(', ', $selectFields),
$innerSql,
implode(', ', $orderBy)
);
}
}

return $sql;
Expand Down