Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
9f3cb437
Commit
9f3cb437
authored
Jan 14, 2014
by
Steve Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix modifying limit and offset for statements with subqueries on SQL Server
parent
8ac0db03
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
93 additions
and
3 deletions
+93
-3
SQLServerPlatform.php
lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
+11
-3
ModifyLimitQueryTest.php
...s/Doctrine/Tests/DBAL/Functional/ModifyLimitQueryTest.php
+14
-0
AbstractSQLServerPlatformTestCase.php
...ests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php
+68
-0
No files found.
lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
View file @
9f3cb437
...
...
@@ -1145,9 +1145,17 @@ class SQLServerPlatform extends AbstractPlatform
$query
=
preg_replace
(
'/\s+ORDER\s+BY\s+([^\)]*)/'
,
''
,
$query
);
//Remove ORDER BY from $query
$format
=
'SELECT * FROM (%s) AS doctrine_tbl WHERE doctrine_rownum BETWEEN %d AND %d'
;
// Pattern to match "main" SELECT ... FROM clause (including nested parentheses in select list).
$selectFromPattern
=
'/^(\s*SELECT\s+(?:\((?>[^()]+)|(?:R)*\)|[^(])+)\sFROM\s/i'
;
if
(
!
$orderBy
)
{
//Replace only first occurrence of FROM with OVER to prevent changing FROM also in subqueries.
$query
=
preg_replace
(
'/\sFROM\s/i'
,
', ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM '
,
$query
,
1
);
//Replace only "main" FROM with OVER to prevent changing FROM also in subqueries.
$query
=
preg_replace
(
$selectFromPattern
,
'$1, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM '
,
$query
,
1
);
return
sprintf
(
$format
,
$query
,
$start
,
$end
);
}
...
...
@@ -1197,7 +1205,7 @@ class SQLServerPlatform extends AbstractPlatform
//Replace only first occurrence of FROM with $over to prevent changing FROM also in subqueries.
$over
=
'ORDER BY '
.
implode
(
', '
,
$overColumns
);
$query
=
preg_replace
(
'/\sFROM\s/i'
,
"
, ROW_NUMBER() OVER (
$over
) AS doctrine_rownum FROM "
,
$query
,
1
);
$query
=
preg_replace
(
$selectFromPattern
,
"$1
, ROW_NUMBER() OVER (
$over
) AS doctrine_rownum FROM "
,
$query
,
1
);
return
sprintf
(
$format
,
$query
,
$start
,
$end
);
}
...
...
tests/Doctrine/Tests/DBAL/Functional/ModifyLimitQueryTest.php
View file @
9f3cb437
...
...
@@ -102,6 +102,20 @@ class ModifyLimitQueryTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this
->
assertLimitResult
(
array
(
2
),
$sql
,
1
,
1
);
}
public
function
testModifyLimitQuerySubSelect
()
{
$this
->
_conn
->
insert
(
'modify_limit_table'
,
array
(
'test_int'
=>
1
));
$this
->
_conn
->
insert
(
'modify_limit_table'
,
array
(
'test_int'
=>
2
));
$this
->
_conn
->
insert
(
'modify_limit_table'
,
array
(
'test_int'
=>
3
));
$this
->
_conn
->
insert
(
'modify_limit_table'
,
array
(
'test_int'
=>
4
));
$sql
=
"SELECT *, (SELECT COUNT(*) FROM modify_limit_table) AS cnt FROM modify_limit_table"
;
$this
->
assertLimitResult
(
array
(
4
,
3
,
2
,
1
),
$sql
,
10
,
0
,
false
);
$this
->
assertLimitResult
(
array
(
4
,
3
),
$sql
,
2
,
0
,
false
);
$this
->
assertLimitResult
(
array
(
2
,
1
),
$sql
,
2
,
2
,
false
);
}
public
function
assertLimitResult
(
$expectedResults
,
$sql
,
$limit
,
$offset
,
$deterministic
=
true
)
{
$p
=
$this
->
_conn
->
getDatabasePlatform
();
...
...
tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php
View file @
9f3cb437
...
...
@@ -256,6 +256,74 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
$this
->
assertEquals
(
$expected
,
$actual
);
}
/**
* @group DBAL-713
*/
public
function
testModifyLimitQueryWithSubSelectInSelectList
()
{
$sql
=
$this
->
_platform
->
modifyLimitQuery
(
"SELECT "
.
"u.id, "
.
"(u.foo/2) foodiv, "
.
"CONCAT(u.bar, u.baz) barbaz, "
.
"(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) FROM profile p WHERE p.user_id = u.id) login_count "
.
"FROM user u "
.
"WHERE u.status = 'disabled'"
,
10
);
$this
->
assertEquals
(
"SELECT * FROM ("
.
"SELECT "
.
"u.id, "
.
"(u.foo/2) foodiv, "
.
"CONCAT(u.bar, u.baz) barbaz, "
.
"(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) FROM profile p WHERE p.user_id = u.id) login_count, "
.
"ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum "
.
"FROM user u "
.
"WHERE u.status = 'disabled'"
.
") AS doctrine_tbl WHERE doctrine_rownum BETWEEN 1 AND 10"
,
$sql
);
}
/**
* @group DBAL-713
*/
public
function
testModifyLimitQueryWithSubSelectInSelectListAndOrderByClause
()
{
if
(
!
$this
->
_platform
->
supportsLimitOffset
())
{
$this
->
markTestSkipped
(
sprintf
(
'Platform "%s" does not support offsets in result limiting.'
,
$this
->
_platform
->
getName
()));
}
$sql
=
$this
->
_platform
->
modifyLimitQuery
(
"SELECT "
.
"u.id, "
.
"(u.foo/2) foodiv, "
.
"CONCAT(u.bar, u.baz) barbaz, "
.
"(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) FROM profile p WHERE p.user_id = u.id) login_count "
.
"FROM user u "
.
"WHERE u.status = 'disabled' "
.
"ORDER BY u.username DESC"
,
10
,
5
);
$this
->
assertEquals
(
"SELECT * FROM ("
.
"SELECT "
.
"u.id, "
.
"(u.foo/2) foodiv, "
.
"CONCAT(u.bar, u.baz) barbaz, "
.
"(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) FROM profile p WHERE p.user_id = u.id) login_count, "
.
"ROW_NUMBER() OVER (ORDER BY username DESC) AS doctrine_rownum "
.
"FROM user u "
.
"WHERE u.status = 'disabled'"
.
") AS doctrine_tbl WHERE doctrine_rownum BETWEEN 6 AND 15"
,
$sql
);
}
/**
* @group DDC-1360
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment