From 55692003d4a230e2fc666420ce94c3b98d098f30 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 3 Jul 2005 12:34:22 +0000 Subject: Fixed Base.content_columns call for SQL Server adapter #1450 [DeLynn Berry] And a bug with the offset rules git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1647 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../lib/active_record/connection_adapters/sqlserver_adapter.rb | 6 +++++- activerecord/test/finder_test.rb | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index dbcf50a08b..1f03b2a427 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed Base.content_columns call for SQL Server adapter #1450 [DeLynn Berry] + * Fixed Base#write_attribute to work with both symbols and strings #1190 [Paul Legato] * Fixed that has_and_belongs_to_many didn't respect single table inheritance types #1081 [Florian Weber] diff --git a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb index 36177bc895..a5dc5a0faf 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb @@ -195,7 +195,7 @@ module ActiveRecord end def columns(table_name, name = nil) - sql = "SELECT COLUMN_NAME as ColName, COLUMN_DEFAULT as DefaultValue, DATA_TYPE as ColType, COL_LENGTH('#{table_name}', COLUMN_NAME) as Length, COLUMNPROPERTY(OBJECT_ID('#{table_name}'), COLUMN_NAME, 'IsIdentity') as IsIdentity, NUMERIC_SCALE as Scale FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME = '#{table_name}'" + sql = "SELECT COLUMN_NAME as ColName, COLUMN_DEFAULT as DefaultValue, DATA_TYPE as ColType, COL_LENGTH('#{table_name}', COLUMN_NAME) as Length, COLUMNPROPERTY(OBJECT_ID('#{table_name}'), COLUMN_NAME, 'IsIdentity') as IsIdentity, NUMERIC_SCALE as Scale FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '#{table_name}'" # Comment out if you want to have the Columns select statment logged. # Personnally, I think it adds unneccessary bloat to the log. # If you do comment it out, make sure to un-comment the "result" line that follows @@ -302,6 +302,10 @@ module ActiveRecord def add_limit_offset!(sql, options) if options.has_key?(:limit) and options.has_key?(:offset) and !options[:limit].nil? and !options[:offset].nil? options[:order] ||= "id ASC" + total_rows = @connection.select_all("SELECT count(*) as TotalRows from #{get_table_name(sql)}")[0][:TotalRows].to_i + if (options[:limit] + options[:offset]) > total_rows + options[:limit] = (total_rows - options[:offset] > 0) ? (total_rows - options[:offset]) : 1 + end sql.gsub!(/SELECT/i, "SELECT * FROM ( SELECT TOP #{options[:limit]} * FROM ( SELECT TOP #{options[:limit] + options[:offset]}")<<" ) AS tmp1 ORDER BY #{change_order_direction(options[:order])} ) AS tmp2 ORDER BY #{options[:order]}" else sql.gsub!(/SELECT/i, "SELECT TOP #{options[:limit]}") unless options[:limit].nil? diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb index 12330b6f7c..61400f2772 100644 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -55,6 +55,10 @@ class FinderTest < Test::Unit::TestCase assert_equal(2, entrants.size) assert_equal(entrants(:second).name, entrants.first.name) + + entrants = Entrant.find(:all, :order => "id ASC", :limit => 2, :offset => 2) + assert_equal(1, entrants.size) + assert_equal(entrants(:third).name, entrants.first.name) end end @@ -276,7 +280,7 @@ class FinderTest < Test::Unit::TestCase no_developers = Developer.find :all, :order => 'id ASC', :limit => 0 assert_equal 0, no_developers.length end - + def test_find_all_with_limit_and_offset first_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 0 second_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 3 -- cgit v1.2.3