Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 55692003d4a230e2fc666420ce94c3b98d098f30 1 parent c52edf2
@dhh dhh authored
View
2  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]
View
6 activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
@@ -195,7 +195,7 @@ def select_one(sql, name = nil)
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 @@ def quote_column_name(name)
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?
View
6 activerecord/test/finder_test.rb
@@ -55,6 +55,10 @@ def test_find_all_with_prepared_limit_and_offset
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 @@ def test_find_all_with_limit
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
Please sign in to comment.
Something went wrong with that request. Please try again.