Commit 17f50b86 authored by Marco Pivetta's avatar Marco Pivetta

Merge branch 'fix/#2428-sql-server-2012-newline-before-order-by'

Close #2428
parents 69b57859 842bbb91
......@@ -115,7 +115,13 @@ class SQLServer2012Platform extends SQLServer2008Platform
// 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)
......
......@@ -363,4 +363,12 @@ class SQLServer2012PlatformTest extends AbstractSQLServerPlatformTestCase
$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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment