Skip to content

Commit

Permalink
Merge branch 'fix/#2428-sql-server-2012-newline-before-order-by'
Browse files Browse the repository at this point in the history
Close #2428
  • Loading branch information
Ocramius committed Jul 11, 2016
2 parents 69b5785 + 842bbb9 commit 17f50b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/Doctrine/DBAL/Platforms/SQLServer2012Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ protected function doModifyLimitQuery($query, $limit, $offset = null)

// Queries using OFFSET... FETCH MUST have an ORDER BY clause
// Find the position of the last instance of ORDER BY and ensure it is not within a parenthetical statement
$orderByPos = strripos($query, " ORDER BY ");
// but can be in a newline
$matches = array();
$matchesCount = preg_match_all("/[\\s]+order by /i", $query, $matches, PREG_OFFSET_CAPTURE);
$orderByPos = false;
if ($matchesCount > 0) {
$orderByPos = $matches[0][($matchesCount - 1)][1];
}

if ($orderByPos === false
|| substr_count($query, "(", $orderByPos) - substr_count($query, ")", $orderByPos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,12 @@ public function testModifyLimitQueryWithTopNSubQueryWithOrderBy()
$sql = $this->_platform->modifyLimitQuery($querySql, 10);
$this->assertEquals($expectedSql, $sql);
}

public function testModifyLimitQueryWithNewlineBeforeOrderBy()
{
$querySql = "SELECT * FROM test\nORDER BY col DESC";
$expectedSql = "SELECT * FROM test\nORDER BY col DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY";
$sql = $this->_platform->modifyLimitQuery($querySql, 10);
$this->assertEquals($expectedSql, $sql);
}
}

0 comments on commit 17f50b8

Please sign in to comment.