<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Fixing Rails Pagination for SQL Server</title>
	<link>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/</link>
	<description>Semantic Web, Ruby on Rails, and Massive Data</description>
	<pubDate>Fri, 21 Nov 2008 04:26:52 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>

	<item>
		<title>By: Alex Le</title>
		<link>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-735</link>
		<dc:creator>Alex Le</dc:creator>
		<pubDate>Fri, 26 Sep 2008 18:32:02 +0000</pubDate>
		<guid>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-735</guid>
		<description>Hi everyone,

I posted an updated version of the patch for SQL Server 2005 at my blog

http://alexle.net/archives/292

Cheers!

Alex</description>
		<content:encoded><![CDATA[<p>Hi everyone,</p>
<p>I posted an updated version of the patch for SQL Server 2005 at my blog</p>
<p><a href="http://alexle.net/archives/292" rel="nofollow">http://alexle.net/archives/292</a></p>
<p>Cheers!</p>
<p>Alex</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sydney</title>
		<link>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-429</link>
		<dc:creator>Sydney</dc:creator>
		<pubDate>Sat, 29 Dec 2007 00:37:39 +0000</pubDate>
		<guid>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-429</guid>
		<description>Bless you!  I was going crazy with this function adding extra back-slashes to the query, among other things.  I did have to make a couple changes to the snippet you posted - there seemed to be a couple extra "n"s in there:

      def add_limit_offset!(sql, options)
        if options[:limit] and options[:offset]
          options[:order] &#124;&#124;= sql.match('FROM (.*) ')[1] + '.id'
          sql.sub!(/ORDER BY.*$/i, '')
          sql.sub!(/SELECT/i,
                  "SELECT row_number() over( order by #{options[:order]} ) as row_num, ")
          sql.replace("SELECT TOP #{options[:limit]} * FROM (#{sql}) as tmp1 " +
                "WHERE row_num &#62; #{options[:offset]}")
        end
      end</description>
		<content:encoded><![CDATA[<p>Bless you!  I was going crazy with this function adding extra back-slashes to the query, among other things.  I did have to make a couple changes to the snippet you posted - there seemed to be a couple extra &#8220;n&#8221;s in there:</p>
<p>      def add_limit_offset!(sql, options)<br />
        if options[:limit] and options[:offset]<br />
          options[:order] ||= sql.match(&#8217;FROM (.*) &#8216;)[1] + &#8216;.id&#8217;<br />
          sql.sub!(/ORDER BY.*$/i, &#8221;)<br />
          sql.sub!(/SELECT/i,<br />
                  &#8220;SELECT row_number() over( order by #{options[:order]} ) as row_num, &#8220;)<br />
          sql.replace(&#8221;SELECT TOP #{options[:limit]} * FROM (#{sql}) as tmp1 &#8221; +<br />
                &#8220;WHERE row_num &gt; #{options[:offset]}&#8221;)<br />
        end<br />
      end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nicholas a. evans</title>
		<link>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-311</link>
		<dc:creator>nicholas a. evans</dc:creator>
		<pubDate>Wed, 17 Oct 2007 17:57:22 +0000</pubDate>
		<guid>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-311</guid>
		<description>Thanks for this!  I've been banging my head against the wall, trying to get some intelligent support for :offset on SQL Server.

I'm not sure what the deal is with the "n"s at the end of lines 10 and 11 (inside the strings).  I assumed they were typos, and it seems to work for me without them (but not with them).

Also, your monkey-patch broke two things (that I could find): the ability to use :join or :include, and the ability to use just :limit (without :offset).

I *think* that I fixed both of those bugs here: http://pastie.caboo.se/108142


module ActiveRecord
  module ConnectionAdapters
    class SQLServerAdapter

      def add_limit_offset!(sql, options)
        if options[:limit] and options[:offset]
          options[:order] &#124;&#124;= sql.match('FROM (\w*) ')[1] + '.id'
          sql.sub!(/ORDER BY.*$/i, '')
          sql.sub!(/SELECT/i,
                   "SELECT row_number() over( order by #{options[:order]} ) as row_num, ")
          sql.replace("select top #{options[:limit]} * from (#{sql}) as tmp_table1 " +
                      "where row_num &#62; #{options[:offset]}")
        elsif sql !~ /^\s*SELECT (@@&#124;COUNT\()/i
          sql.sub!(/^\s*SELECT(\s+DISTINCT)?/i) do
            "SELECT#{$1} TOP #{options[:limit]}"
          end unless options[:limit].nil?
        end
      end

    end
  end
end</description>
		<content:encoded><![CDATA[<p>Thanks for this!  I&#8217;ve been banging my head against the wall, trying to get some intelligent support for :offset on SQL Server.</p>
<p>I&#8217;m not sure what the deal is with the &#8220;n&#8221;s at the end of lines 10 and 11 (inside the strings).  I assumed they were typos, and it seems to work for me without them (but not with them).</p>
<p>Also, your monkey-patch broke two things (that I could find): the ability to use :join or :include, and the ability to use just :limit (without :offset).</p>
<p>I *think* that I fixed both of those bugs here: <a href="http://pastie.caboo.se/108142" rel="nofollow">http://pastie.caboo.se/108142</a></p>
<p>module ActiveRecord<br />
  module ConnectionAdapters<br />
    class SQLServerAdapter</p>
<p>      def add_limit_offset!(sql, options)<br />
        if options[:limit] and options[:offset]<br />
          options[:order] ||= sql.match(&#8217;FROM (\w*) &#8216;)[1] + &#8216;.id&#8217;<br />
          sql.sub!(/ORDER BY.*$/i, &#8221;)<br />
          sql.sub!(/SELECT/i,<br />
                   &#8220;SELECT row_number() over( order by #{options[:order]} ) as row_num, &#8220;)<br />
          sql.replace(&#8221;select top #{options[:limit]} * from (#{sql}) as tmp_table1 &#8221; +<br />
                      &#8220;where row_num &gt; #{options[:offset]}&#8221;)<br />
        elsif sql !~ /^\s*SELECT (@@|COUNT\()/i<br />
          sql.sub!(/^\s*SELECT(\s+DISTINCT)?/i) do<br />
            &#8220;SELECT#{$1} TOP #{options[:limit]}&#8221;<br />
          end unless options[:limit].nil?<br />
        end<br />
      end</p>
<p>    end<br />
  end<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-306</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Fri, 12 Oct 2007 16:04:42 +0000</pubDate>
		<guid>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-306</guid>
		<description>Tried it against ActiveScaffold and it breaks on some JOINS

Also tried  
"WITH MyTmp AS
(SELECT ROW_NUMBER() OVER( order by #{options[:order]} ) as row_num , * FROM (#{sql})
SELECT * FROM MyTmp where row-num between  #{options[:offset]} AND ( #{options[:offset]} +  #{options[:limit]} - 1)"

But the OLE connector couldn't understand - probably needs a native 2005 connector

Cool idea though -</description>
		<content:encoded><![CDATA[<p>Tried it against ActiveScaffold and it breaks on some JOINS</p>
<p>Also tried<br />
&#8220;WITH MyTmp AS<br />
(SELECT ROW_NUMBER() OVER( order by #{options[:order]} ) as row_num , * FROM (#{sql})<br />
SELECT * FROM MyTmp where row-num between  #{options[:offset]} AND ( #{options[:offset]} +  #{options[:limit]} - 1)&#8221;</p>
<p>But the OLE connector couldn&#8217;t understand - probably needs a native 2005 connector</p>
<p>Cool idea though -</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joshua</title>
		<link>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-195</link>
		<dc:creator>Joshua</dc:creator>
		<pubDate>Wed, 05 Sep 2007 17:14:52 +0000</pubDate>
		<guid>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-195</guid>
		<description>&lt;p&gt;Where exactly do i implement his patch?&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Where exactly do i implement his patch?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim</title>
		<link>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-194</link>
		<dc:creator>Tim</dc:creator>
		<pubDate>Wed, 01 Aug 2007 14:20:32 +0000</pubDate>
		<guid>http://www.semergence.com/2007/07/31/fixing-rails-pagination-for-sql-server/#comment-194</guid>
		<description>&lt;p&gt;An alternative track: how about investigating the &lt;a href="http://odbc-rails.rubyforge.org/" rel="nofollow"&gt;ODBC Adapter for Rails&lt;/a&gt; instead?&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>An alternative track: how about investigating the <a href="http://odbc-rails.rubyforge.org/" rel="nofollow">ODBC Adapter for Rails</a> instead?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
