Commit 842bbb91 authored by stchr's avatar stchr Committed by Marco Pivetta

Fix Modify Limit Query with newline before ORDER BY

parent 69b57859
...@@ -115,7 +115,13 @@ class SQLServer2012Platform extends SQLServer2008Platform ...@@ -115,7 +115,13 @@ class SQLServer2012Platform extends SQLServer2008Platform
// Queries using OFFSET... FETCH MUST have an ORDER BY clause // 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 // 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 if ($orderByPos === false
|| substr_count($query, "(", $orderByPos) - substr_count($query, ")", $orderByPos) || substr_count($query, "(", $orderByPos) - substr_count($query, ")", $orderByPos)
......
...@@ -363,4 +363,12 @@ class SQLServer2012PlatformTest extends AbstractSQLServerPlatformTestCase ...@@ -363,4 +363,12 @@ class SQLServer2012PlatformTest extends AbstractSQLServerPlatformTestCase
$sql = $this->_platform->modifyLimitQuery($querySql, 10); $sql = $this->_platform->modifyLimitQuery($querySql, 10);
$this->assertEquals($expectedSql, $sql); $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