Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed doModifyLimitQuery for SQLServerPlatform #249

Merged
merged 1 commit into from

3 participants

@norzechowicz

It seems that there is a problem in SQLServerPlatform doModifyLimitQuery method.
I can't use Doctrine ORM Paginator because of wrong position of DISTINCT statement in final limit subquery, here is the exception message I'm getting.
SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near the keyword 'DISTINCT'.

My proposition is to change doModifyLimitQuery result from:

SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY username DESC) AS doctrine_rownum, * FROM user) ...

into:

SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY username DESC) AS doctrine_rownum FROM user) ...

@doctrinebot
Collaborator

Hello,

thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

http://doctrine-project.org/jira/browse/DBAL-416

@norzechowicz

@beberlei this PR works fine with simple Doctrine Paginator and it fix first point from this list (second point seems to be already done), I can also check those 2 other points and eventually think about some solution if you want.

@beberlei
Owner

@norzechowicz thanks, help is very much appreciated with SQL Server, we don't have a core member that is using it in production and could always use somebody that offers some time for maintaining the complicated parts :-)

@beberlei beberlei merged commit f3374cb into from
@norzechowicz norzechowicz deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 18, 2013
  1. Fixed doModifyLimitQuery for SQLServerPlatform

    Norbert Orzechowicz authored
This page is out of date. Refresh to see the latest.
View
4 lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
@@ -687,12 +687,12 @@ protected function doModifyLimitQuery($query, $limit, $offset = null)
// Remove ORDER BY clause from $query
$query = preg_replace('/\s+ORDER BY(.*)/', '', $query);
- $query = preg_replace('/^SELECT\s/', '', $query);
+ $query = preg_replace('/\sFROM/i', ", ROW_NUMBER() OVER ($over) AS doctrine_rownum FROM", $query);
$start = $offset + 1;
$end = $offset + $limit;
- $query = "SELECT * FROM (SELECT ROW_NUMBER() OVER ($over) AS doctrine_rownum, $query) AS doctrine_tbl WHERE doctrine_rownum BETWEEN $start AND $end";
+ $query = "SELECT * FROM ($query) AS doctrine_tbl WHERE doctrine_rownum BETWEEN $start AND $end";
}
}
View
2  tests/Doctrine/Tests/DBAL/Platforms/SQLServerPlatformTest.php
@@ -154,7 +154,7 @@ public function testModifyLimitQueryWithEmptyOffset()
public function testModifyLimitQueryWithOffset()
{
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10, 5);
- $this->assertEquals('SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY username DESC) AS doctrine_rownum, * FROM user) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 6 AND 15', $sql);
+ $this->assertEquals('SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY username DESC) AS doctrine_rownum FROM user) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 6 AND 15', $sql);
}
public function testModifyLimitQueryWithAscOrderBy()
Something went wrong with that request. Please try again.