-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from 1 commit
8fbd32b
b68c93c
827d53f
43d7ef7
fca3767
a09f1cb
dfc1264
c674a15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
if ($this->platform->getName() == 'postgresql' || $this->platform->getName() == 'oracle') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use |
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cs, missing spaces |
||
$selectFields = array_merge($sqlIdentifier); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid the |
||
$selectFields = array_merge($sqlIdentifier, $sqlOrderColumns); | ||
} | ||
$sql = sprintf( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment.
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?