<!-- 
RSS generated by JIRA (5.2.7#850-sha1:b2af0c8dc8537b36121c6a579fabbdf79fc919e5) at Sun May 26 04:57:48 UTC 2013

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary add field=key&field=summary to the URL of your request.
For example:
http://www.doctrine-project.org/jira/si/jira.issueviews:issue-xml/DBAL-82/DBAL-82.xml?field=key&field=summary
-->
<rss version="0.92" >
<channel>
    <title>Doctrine Project</title>
    <link>http://www.doctrine-project.org/jira</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>5.2.7</version>
        <build-number>850</build-number>
        <build-date>21-02-2013</build-date>
    </build-info>

<item>
            <title>[DBAL-82] orderBy(), setFirstResult() bug with MSSQL Server</title>
                <link>http://www.doctrine-project.org/jira/browse/DBAL-82</link>
                <project id="10040" key="DBAL">Doctrine DBAL</project>
                        <description>&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeHeader panelHeader&quot; style=&quot;border-bottom-width: 1px;&quot;&gt;&lt;b&gt;QueryBuilder&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;		$query = $&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;createQueryBuilder(&apos;account&apos;)
					  -&amp;gt;select(&apos;account&apos;)
					  -&amp;gt;orderBy(&apos;account.id&apos;, &apos;DESC&apos;)
					  -&amp;gt;getQuery();

                $result = $query-&amp;gt;setMaxResults($&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;resultsPerPage+1)
        							-&amp;gt;setFirstResult($&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;offset)
        							-&amp;gt;getResult();
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The above, when using &quot;setFirstResult&quot; and &quot;orderBy&quot; (like that), results in an error like so:&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]The multi-part identifier &lt;span class=&quot;code-quote&quot;&gt;&quot;t0_.id&quot;&lt;/span&gt; could not be bound.&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With a statement that looks something like so:&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;WITH outer_tbl AS (SELECT ROW_NUMBER() OVER (ORDER BY t0_.id DESC) AS &lt;span class=&quot;code-quote&quot;&gt;&quot;doctrine_rownum&quot;&lt;/span&gt;,   ...&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The reason the error is occurring is because you apparently need to use the &quot;alias&quot; (e.g, SELECT t0_.id AS id0) &quot;id0&quot; in the &quot;ORDER BY&quot; clause.&lt;/p&gt;

&lt;p&gt;So query will run with no problems if t0_.id as id0&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;WITH outer_tbl AS (SELECT ROW_NUMBER() OVER (ORDER BY id0 DESC) AS &lt;span class=&quot;code-quote&quot;&gt;&quot;doctrine_rownum&quot;&lt;/span&gt;,   ...&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It looks like this behavior might only occur with Microsoft SQL Server, but it is a bug.&lt;/p&gt;

&lt;p&gt;---------------&lt;/p&gt;

&lt;p&gt;If you go into: &lt;br/&gt;
Doctrine\DBAL\Platforms\MsSqlPlatform&lt;/p&gt;

&lt;p&gt;Find: &lt;/p&gt;
 &lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;$over = preg_replace(&apos;/\&lt;span class=&quot;code-quote&quot;&gt;&quot;[^,]*\&quot;&lt;/span&gt;.\&lt;span class=&quot;code-quote&quot;&gt;&quot;([^,]*)\&quot;&lt;/span&gt;/i&apos;, &apos;&lt;span class=&quot;code-quote&quot;&gt;&quot;inner_tbl&quot;&lt;/span&gt;.&lt;span class=&quot;code-quote&quot;&gt;&quot;$1&quot;&lt;/span&gt;&apos;, $orderby);&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add before:&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;                    # Get Columns
                    $columns = array();
                    &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt;(preg_match_all(&apos;/([a-zA-Z][0-9]+_\.[a-zA-Z0-9\-_]+)\sAS\s([a-zA-Z0-9\-\_]+)/&apos;, $query, $matched)) {
                        &lt;span class=&quot;code-keyword&quot;&gt;for&lt;/span&gt;($i=0; $i&amp;lt;count($matched[1]); ++$i)
                        {
                            $columns[$matched[1][$i]] = $matched[2][$i];
                        }
                    }
                    
                    # Replace columns with their alias in the &lt;span class=&quot;code-quote&quot;&gt;&quot;orderby&quot;&lt;/span&gt; statement
                    &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt;(preg_match_all(&apos;/([a-zA-Z][0-9]+_\.[a-zA-Z0-9\-_]+)\s/i&apos;, $orderby, $matches)) {
                        foreach($matches[1] as $column) 
                        {
                            $orderby = preg_replace(&apos;/&apos;.$column.&apos;/&apos;, $columns[$column], $orderby);
                        }
                    }
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Obviously this is a really ugly hack, but this resolves it.&lt;/p&gt;</description>
                <environment>Windows 7, Apache2, PHP 5.3.5, Microsoft SQL Server 2008</environment>
            <key id="12306">DBAL-82</key>
            <summary>orderBy(), setFirstResult() bug with MSSQL Server</summary>
                <type id="1" iconUrl="http://www.doctrine-project.org/jira/images/icons/issuetypes/bug.png">Bug</type>
                                <priority id="3" iconUrl="http://www.doctrine-project.org/jira/images/icons/priorities/major.png">Major</priority>
                    <status id="5" iconUrl="http://www.doctrine-project.org/jira/images/icons/statuses/resolved.png">Resolved</status>
                    <resolution id="1">Fixed</resolution>
                                <assignee username="juokaz">Juozas Kaziukenas</assignee>
                                <reporter username="aarondm">Aaron DM</reporter>
                        <labels>
                    </labels>
                <created>Sun, 16 Jan 2011 13:34:04 +0000</created>
                <updated>Sun, 31 Jul 2011 10:20:54 +0000</updated>
                    <resolved>Sun, 31 Jul 2011 10:20:54 +0000</resolved>
                            <version>2.0</version>
                                <fixVersion>2.0.7</fixVersion>
                <fixVersion>2.1.1</fixVersion>
                                <component>Platforms</component>
                        <due></due>
                    <votes>1</votes>
                        <watches>2</watches>
                        <comments>
                    <comment id="15146" author="aarondm" created="Sun, 16 Jan 2011 14:09:16 +0000"  >&lt;p&gt;Added the ugly hack fix.&lt;/p&gt;</comment>
                    <comment id="15172" author="beberlei" created="Sun, 23 Jan 2011 14:29:05 +0000"  >&lt;p&gt;Assigned to Juozas.&lt;/p&gt;</comment>
                    <comment id="15807" author="minxuan.guo" created="Thu, 5 May 2011 07:53:21 +0000"  >&lt;p&gt;Thanks Aaron DM&lt;/p&gt;

&lt;p&gt;Your code works perfectly&lt;/p&gt;</comment>
                    <comment id="16033" author="jmfontaine" created="Mon, 20 Jun 2011 08:54:27 +0000"  >&lt;p&gt;I am experiencing this bug too. Is there a way to get it fixed anytime soon? Maybe in the 2.1 release.&lt;/p&gt;</comment>
                    <comment id="16052" author="aarondm" created="Fri, 24 Jun 2011 18:50:21 +0000"  >&lt;p&gt;Here is what I think, a proper fix for this.&lt;/p&gt;

&lt;p&gt;I re-wrote the query however it works the same and is pretty much the exact same in performance (from couple of tests I&apos;ve done looking at profiler) + it fixes the issues regarding this ticket.&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;    /**
     * Adds an adapter-specific LIMIT clause to the SELECT statement.
     *
     * @param string $query
     * @param mixed $limit
     * @param mixed $offset
     * @link http:&lt;span class=&quot;code-comment&quot;&gt;//lists.bestpractical.com/pipermail/rt-devel/2005-June/007339.html
&lt;/span&gt;     * @&lt;span class=&quot;code-keyword&quot;&gt;return&lt;/span&gt; string
     */
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; function doModifyLimitQuery($query, $limit, $offset = &lt;span class=&quot;code-keyword&quot;&gt;null&lt;/span&gt;)
    {
        &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; ($limit &amp;gt; 0) {
            $count = intval($limit);
            $offset = intval($offset);

            &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; ($offset &amp;lt; 0) {
                &lt;span class=&quot;code-keyword&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;code-keyword&quot;&gt;new&lt;/span&gt; DBALException(&lt;span class=&quot;code-quote&quot;&gt;&quot;LIMIT argument offset=$offset is not valid&quot;&lt;/span&gt;);
            }

            &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; ($offset == 0) {
                $query = preg_replace(&apos;/^SELECT\s/i&apos;, &apos;SELECT TOP &apos; . $count . &apos; &apos;, $query);
            } &lt;span class=&quot;code-keyword&quot;&gt;else&lt;/span&gt; {
                $orderby = stristr($query, &apos;ORDER BY&apos;);

                &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; (!$orderby) {
                    $over = &apos;ORDER BY (SELECT 0)&apos;;
                } &lt;span class=&quot;code-keyword&quot;&gt;else&lt;/span&gt; {
                    $over = preg_replace(&apos;/\&lt;span class=&quot;code-quote&quot;&gt;&quot;[^,]*\&quot;&lt;/span&gt;.\&lt;span class=&quot;code-quote&quot;&gt;&quot;([^,]*)\&quot;&lt;/span&gt;/i&apos;, &apos;&lt;span class=&quot;code-quote&quot;&gt;&quot;inner_tbl&quot;&lt;/span&gt;.&lt;span class=&quot;code-quote&quot;&gt;&quot;$1&quot;&lt;/span&gt;&apos;, $orderby);
                }

                &lt;span class=&quot;code-comment&quot;&gt;// Remove ORDER BY clause from $query
&lt;/span&gt;                $query = preg_replace(&apos;/\s+ORDER BY(.*)/&apos;, &apos;&apos;, $query);
                $query = preg_replace(&apos;/SELECT\s/&apos;, &apos;&apos;, $query);

                $start = $offset + 1;
                $end = $offset + $count;

                &lt;span class=&quot;code-comment&quot;&gt;// Limit query
&lt;/span&gt;                $query = &lt;span class=&quot;code-quote&quot;&gt;&quot;SELECT * FROM (SELECT ROW_NUMBER() OVER ($over) AS \&quot;&lt;/span&gt;doctrine_rownum\&lt;span class=&quot;code-quote&quot;&gt;&quot;, $query) AS doctrine_tbl WHERE \&quot;&lt;/span&gt;doctrine_rownum\&lt;span class=&quot;code-quote&quot;&gt;&quot; BETWEEN $start AND $end&quot;&lt;/span&gt;;
            }
        }

        &lt;span class=&quot;code-keyword&quot;&gt;return&lt;/span&gt; $query;
    }
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I had found this query a long time ago and found it to be fairly fast, however I&apos;m not sure where exactly I found it.&lt;br/&gt;
I&apos;m going to take a guess and assume it might be this comment:&lt;br/&gt;
&lt;a href=&quot;http://josephlindsay.com/archives/2005/05/27/paging-results-in-ms-sql-server/comment-page-1/#comment-28594&quot; class=&quot;external-link&quot;&gt;http://josephlindsay.com/archives/2005/05/27/paging-results-in-ms-sql-server/comment-page-1/#comment-28594&lt;/a&gt;&lt;/p&gt;</comment>
                    <comment id="16053" author="aarondm" created="Fri, 24 Jun 2011 19:29:42 +0000"  >&lt;p&gt;Pull request: &lt;a href=&quot;https://github.com/doctrine/dbal/pull/37&quot; class=&quot;external-link&quot;&gt;https://github.com/doctrine/dbal/pull/37&lt;/a&gt;&lt;/p&gt;</comment>
                    <comment id="16250" author="beberlei" created="Sun, 31 Jul 2011 10:20:54 +0000"  >&lt;p&gt;Fixed, merged pull request&lt;/p&gt;</comment>
                </comments>
                    <attachments>
                </attachments>
            <subtasks>
        </subtasks>
        </item>
</channel>
</rss>