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
81dd0b7a
Commit
81dd0b7a
authored
Apr 21, 2016
by
Sergei Morozov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle arbitrary whitespaces when parsing SQL in order to apply LIMIT for MS SQL Server
parent
a6d3d162
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
3 deletions
+23
-3
SQLServer2012Platform.php
lib/Doctrine/DBAL/Platforms/SQLServer2012Platform.php
+2
-2
SQLServerPlatform.php
lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
+1
-1
ModifyLimitQueryTest.php
...s/Doctrine/Tests/DBAL/Functional/ModifyLimitQueryTest.php
+20
-0
No files found.
lib/Doctrine/DBAL/Platforms/SQLServer2012Platform.php
View file @
81dd0b7a
...
...
@@ -117,7 +117,7 @@ class SQLServer2012Platform extends SQLServer2008Platform
// Find the position of the last instance of ORDER BY and ensure it is not within a parenthetical statement
// but can be in a newline
$matches
=
array
();
$matchesCount
=
preg_match_all
(
"/[
\\
s]+order
by /i
"
,
$query
,
$matches
,
PREG_OFFSET_CAPTURE
);
$matchesCount
=
preg_match_all
(
"/[
\\
s]+order
\\
s+by
\\
s/im
"
,
$query
,
$matches
,
PREG_OFFSET_CAPTURE
);
$orderByPos
=
false
;
if
(
$matchesCount
>
0
)
{
$orderByPos
=
$matches
[
0
][(
$matchesCount
-
1
)][
1
];
...
...
@@ -126,7 +126,7 @@ class SQLServer2012Platform extends SQLServer2008Platform
if
(
$orderByPos
===
false
||
substr_count
(
$query
,
"("
,
$orderByPos
)
-
substr_count
(
$query
,
")"
,
$orderByPos
)
)
{
if
(
stripos
(
$query
,
'SELECT DISTINCT'
)
===
0
)
{
if
(
preg_match
(
'/^SELECT\s+DISTINCT/im'
,
$query
)
)
{
// SQL Server won't let us order by a non-selected column in a DISTINCT query,
// so we have to do this madness. This says, order by the first column in the
// result. SQL Server's docs say that a nonordered query's result order is non-
...
...
lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
View file @
81dd0b7a
...
...
@@ -1192,7 +1192,7 @@ class SQLServerPlatform extends AbstractPlatform
// Even if the TOP n is very large, the use of a CTE will
// allow the SQL Server query planner to optimize it so it doesn't
// actually scan the entire range covered by the TOP clause.
$selectPattern
=
'/^(\s*SELECT\s+(?:DISTINCT\s+)?)(.*)$/i'
;
$selectPattern
=
'/^(\s*SELECT\s+(?:DISTINCT\s+)?)(.*)$/i
m
'
;
$replacePattern
=
sprintf
(
'$1%s $2'
,
"TOP
$end
"
);
$query
=
preg_replace
(
$selectPattern
,
$replacePattern
,
$query
);
...
...
tests/Doctrine/Tests/DBAL/Functional/ModifyLimitQueryTest.php
View file @
81dd0b7a
...
...
@@ -124,6 +124,26 @@ class ModifyLimitQueryTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this
->
assertLimitResult
(
array
(
2
,
1
),
$sql
,
2
,
2
);
}
public
function
testModifyLimitQueryLineBreaks
()
{
$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
));
$sql
=
<<<SQL
SELECT
*
FROM
modify_limit_table
ORDER
BY
test_int
ASC
SQL;
$this
->
assertLimitResult
(
array
(
2
),
$sql
,
1
,
1
);
}
public
function
assertLimitResult
(
$expectedResults
,
$sql
,
$limit
,
$offset
,
$deterministic
=
true
)
{
$p
=
$this
->
_conn
->
getDatabasePlatform
();
...
...
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