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 --- .../lib/active_record/connection_adapters/sqlserver_adapter.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb') 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? -- cgit v1.2.3