Skip to content

Commit 6314d55

Browse files
Benjamin Nolandeeky666
authored andcommitted
Minor code cleanup for efficiency, and tests to check the order by clause scrubbing is working correctly and not entering an infinite loop
1 parent 9eb014a commit 6314d55

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,11 +1224,11 @@ protected function doModifyLimitQuery($query, $limit, $offset = null)
12241224
*/
12251225
private function scrubInnerOrderBy($query)
12261226
{
1227-
$count = substr_count(strtoupper($query), "ORDER BY");
1227+
$count = substr_count(strtoupper($query), 'ORDER BY');
12281228
$offset = 0;
12291229

12301230
while ($count-- > 0) {
1231-
$orderByPos = stripos($query, " ORDER BY", $offset);
1231+
$orderByPos = stripos($query, ' ORDER BY', $offset);
12321232
if ($orderByPos === false) {
12331233
break;
12341234
}
@@ -1285,7 +1285,7 @@ private function isOrderByInTopNSubquery($query, $currentPosition)
12851285
}
12861286

12871287
// Only yank query text on the same nesting level as the ORDER BY clause.
1288-
$subQueryBuffer = ($parenCount === 0 ? $query[$currentPosition] : " ") . $subQueryBuffer;
1288+
$subQueryBuffer = ($parenCount === 0 ? $query[$currentPosition] : ' ') . $subQueryBuffer;
12891289

12901290
$currentPosition--;
12911291
}

tests/Doctrine/Tests/DBAL/Platforms/SQLServerPlatformTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class SQLServerPlatformTest extends AbstractSQLServerPlatformTestCase
99
{
10+
1011
public function createPlatform()
1112
{
1213
return new SQLServerPlatform;
@@ -24,6 +25,15 @@ public function testAppendsLockHint($lockMode, $lockHint)
2425
$this->assertSame($expectedResult, $this->_platform->appendLockHint($fromClause, $lockMode));
2526
}
2627

28+
/**
29+
* @group DBAL-2408
30+
* @dataProvider getModifyLimitQueries
31+
*/
32+
public function testScrubInnerOrderBy($query, $limit, $offset, $expectedResult)
33+
{
34+
$this->assertSame($expectedResult, $this->_platform->modifyLimitQuery($query, $limit, $offset));
35+
}
36+
2737
public function getLockHints()
2838
{
2939
return array(
@@ -36,4 +46,16 @@ public function getLockHints()
3646
array(LockMode::PESSIMISTIC_WRITE, ' WITH (UPDLOCK, ROWLOCK)'),
3747
);
3848
}
49+
50+
public function getModifyLimitQueries()
51+
{
52+
return array(
53+
// Test re-ordered query with correctly-scrubbed ORDER BY clause
54+
array('SELECT id_0, MIN(sclr_2) AS dctrn_minrownum FROM (SELECT c0_.id AS id_0, c0_.title AS title_1, ROW_NUMBER() OVER(ORDER BY c0_.title ASC) AS sclr_2 FROM TestTable c0_ ORDER BY c0_.title ASC) dctrn_result GROUP BY id_0 ORDER BY dctrn_minrownum ASC', 30, null, 'WITH dctrn_cte AS (SELECT TOP 30 id_0, MIN(sclr_2) AS dctrn_minrownum FROM (SELECT c0_.id AS id_0, c0_.title AS title_1, ROW_NUMBER() OVER(ORDER BY c0_.title ASC) AS sclr_2 FROM TestTable c0_) dctrn_result GROUP BY id_0 ORDER BY dctrn_minrownum ASC) SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM dctrn_cte) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 1 AND 30 ORDER BY doctrine_rownum ASC'),
55+
56+
// Test re-ordered query with no scrubbed ORDER BY clause
57+
array('SELECT id_0, MIN(sclr_2) AS dctrn_minrownum FROM (SELECT c0_.id AS id_0, c0_.title AS title_1, ROW_NUMBER() OVER(ORDER BY c0_.title ASC) AS sclr_2 FROM TestTable c0_) dctrn_result GROUP BY id_0 ORDER BY dctrn_minrownum ASC', 30, null, 'WITH dctrn_cte AS (SELECT TOP 30 id_0, MIN(sclr_2) AS dctrn_minrownum FROM (SELECT c0_.id AS id_0, c0_.title AS title_1, ROW_NUMBER() OVER(ORDER BY c0_.title ASC) AS sclr_2 FROM TestTable c0_) dctrn_result GROUP BY id_0 ORDER BY dctrn_minrownum ASC) SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM dctrn_cte) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 1 AND 30 ORDER BY doctrine_rownum ASC'),
58+
);
59+
}
60+
3961
}

0 commit comments

Comments
 (0)