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
d7b098ce
Commit
d7b098ce
authored
Jun 29, 2016
by
stchr
Committed by
Marco Pivetta
Jul 11, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix Modify Limit Query with newline before ORDER BY
parent
e05f26d8
Changes
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
382 additions
and
0 deletions
+382
-0
SQLServer2012Platform.php
lib/Doctrine/DBAL/Platforms/SQLServer2012Platform.php
+53
-0
SQLServer2012PlatformTest.php
...ctrine/Tests/DBAL/Platforms/SQLServer2012PlatformTest.php
+329
-0
No files found.
lib/Doctrine/DBAL/Platforms/SQLServer2012Platform.php
View file @
d7b098ce
...
@@ -102,4 +102,57 @@ class SQLServer2012Platform extends SQLServer2008Platform
...
@@ -102,4 +102,57 @@ class SQLServer2012Platform extends SQLServer2008Platform
{
{
return
'Doctrine\DBAL\Platforms\Keywords\SQLServer2012Keywords'
;
return
'Doctrine\DBAL\Platforms\Keywords\SQLServer2012Keywords'
;
}
}
/**
* {@inheritdoc}
*/
protected
function
doModifyLimitQuery
(
$query
,
$limit
,
$offset
=
null
)
{
if
(
$limit
===
null
&&
$offset
===
null
)
{
return
$query
;
}
// 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
// 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
)
)
{
if
(
stripos
(
$query
,
'SELECT DISTINCT'
)
===
0
)
{
// 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-
// deterministic anyway, so this won't do anything that a bunch of update and
// deletes to the table wouldn't do anyway.
$query
.=
" ORDER BY 1"
;
}
else
{
// In another DBMS, we could do ORDER BY 0, but SQL Server gets angry if you
// use constant expressions in the order by list.
$query
.=
" ORDER BY (SELECT 0)"
;
}
}
if
(
$offset
===
null
)
{
$offset
=
0
;
}
// This looks somewhat like MYSQL, but limit/offset are in inverse positions
// Supposedly SQL:2008 core standard.
// Per TSQL spec, FETCH NEXT n ROWS ONLY is not valid without OFFSET n ROWS.
$query
.=
" OFFSET "
.
(
int
)
$offset
.
" ROWS"
;
if
(
$limit
!==
null
)
{
$query
.=
" FETCH NEXT "
.
(
int
)
$limit
.
" ROWS ONLY"
;
}
return
$query
;
}
}
}
tests/Doctrine/Tests/DBAL/Platforms/SQLServer2012PlatformTest.php
View file @
d7b098ce
This diff is collapsed.
Click to expand it.
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