Permalink
Browse files

Merge pull request #383 from flip111/patch-5

SQL Server Paginator bug fix
  • Loading branch information...
2 parents 899c98d + ee06a30 commit b4bcc18fe0d1b99e37ad51a4a25a04a83ab9d99a @guilhermeblanco guilhermeblanco committed Oct 1, 2013
@@ -868,14 +868,22 @@ protected function doModifyLimitQuery($query, $limit, $offset = null)
}
}
+ $isWrapped = (preg_match('/SELECT DISTINCT .* FROM \(.*\) dctrn_result/', $query)) ? true : false;
+
//Find alias for each colum used in ORDER BY
if ( ! empty($orderbyColumns)) {
foreach ($orderbyColumns as $column) {
- $pattern = sprintf('/%s\.(%s)\s*(AS)?\s*([^,\s\)]*)/i', $column['table'], $column['column']);
- $overColumn = preg_match($pattern, $query, $matches)
- ? ($column['hasTable'] ? $column['table'] . '.' : '') . $column['column']
- : $column['column'];
+ $pattern = sprintf('/%s\.%s\s+(?:AS\s+)?([^,\s)]+)/i', $column['table'], $column['column']);
+
+ if ($isWrapped) {
+ $overColumn = preg_match($pattern, $query, $matches)
+ ? $matches[1] : '';
+ } else {
+ $overColumn = preg_match($pattern, $query, $matches)
+ ? ($column['hasTable'] ? $column['table'] . '.' : '') . $column['column']
+ : $column['column'];
+ }
if (isset($column['sort'])) {
$overColumn .= ' ' . $column['sort'];
@@ -205,6 +205,9 @@ public function testModifyLimitQueryWithSubSelectAndOrder()
{
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM (SELECT u.id as uid, u.name as uname ORDER BY u.name DESC) dctrn_result', 10);
$this->assertEquals('SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY u.name DESC) AS doctrine_rownum FROM (SELECT u.id as uid, u.name as uname) dctrn_result) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 1 AND 10', $sql);
+
+ $sql = $this->_platform->modifyLimitQuery('SELECT * FROM (SELECT u.id, u.name ORDER BY u.name DESC) dctrn_result', 10);
+ $this->assertEquals('SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY name DESC) AS doctrine_rownum FROM (SELECT u.id, u.name) dctrn_result) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 1 AND 10', $sql);
}
public function testModifyLimitQueryWithSubSelectAndMultipleOrder()
@@ -214,6 +217,9 @@ public function testModifyLimitQueryWithSubSelectAndMultipleOrder()
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM (SELECT u.id uid, u.name uname ORDER BY u.name DESC, id ASC) dctrn_result', 10, 5);
$this->assertEquals('SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY u.name DESC, id ASC) AS doctrine_rownum FROM (SELECT u.id uid, u.name uname) dctrn_result) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 6 AND 15', $sql);
+
+ $sql = $this->_platform->modifyLimitQuery('SELECT * FROM (SELECT u.id, u.name ORDER BY u.name DESC, id ASC) dctrn_result', 10, 5);
+ $this->assertEquals('SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY name DESC, id ASC) AS doctrine_rownum FROM (SELECT u.id, u.name) dctrn_result) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 6 AND 15', $sql);
}
public function testModifyLimitQueryWithFromColumnNames()

0 comments on commit b4bcc18

Please sign in to comment.