Bugs item #13355, was opened at 2007-08-25 19:39
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=7857&aid=13355&group_id=2014

Category: AR-JDBC
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Nobody (None)
Assigned to: Nobody (None)
Summary: add_limit_offset in SQL Server 2005

Initial Comment:
I'm using ActiveRecord-JDBC with SQL Server 2005. I found the attached 
implementation of add_limit_offset to be more efficient than the one included 
in version 0.5. The downside is that it will not work with SQL Server 2000.

Maybe there's some way to distribute both versions of the adapter.


    def add_limit_offset!(sql, options)
      if limit = options[:limit] and offset = options[:offset]        
        if options[:order]
          sql.sub!(/(SELECT.*)(\s+ORDER BY.*)/i) do
            "SELECT B.* FROM (SELECT A.*, ROW_NUMBER() over (ORDER BY 
#{options[:order]}) AS internal$rownum FROM (#{$1}) A ) B WHERE 
B.internal$rownum > #{offset} AND B.internal$rownum <= #{limit + offset}#{$2}"
          end
        else
          sql.sub!(/(SELECT.*)/i) do
            "SELECT B.* FROM (SELECT A.*, ROW_NUMBER() over (ORDER BY 
internal$const) AS internal$rownum FROM (SELECT C.*, 1 AS internal$const FROM ( 
#{$1} ) C ) A ) B WHERE B.internal$rownum > #{offset} AND B.internal$rownum <= 
#{limit + offset}"
          end
        end
      elsif sql !~ /^\s*SELECT (@@|COUNT\()/i
        sql.sub!(/^\s*SELECT([\s]*distinct)?/i) do
          "SELECT#{$1} TOP #{options[:limit]}"
        end unless options[:limit].nil?
      end
    end

----------------------------------------------------------------------

Comment By: Leigh Kennedy (lkennedy)
Date: 2008-06-02 12:31

Message:
Don't have access to 2005, but in 2000,

select @@version

returns:
Microsoft SQL Server  2000 - 8.00.2039 (Intel X86) 
        May  3 2005 23:18:38 
        Copyright (c) 1988-2003 Microsoft Corporation
        Standard Edition on Windows NT 5.2 (Build 3790: )


You would just need to parse the output.
(1 row(s) affected)

----------------------------------------------------------------------

Comment By: Nick Sieger (nicksieger)
Date: 2008-04-17 12:40

Message:
Is there a way to detect which server version you're running?

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=7857&aid=13355&group_id=2014
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to