aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-07-03 12:34:22 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-07-03 12:34:22 +0000
commit55692003d4a230e2fc666420ce94c3b98d098f30 (patch)
tree1f74ea28e65ab28a8fdcb33b0b3c4e335066475b /activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
parentc52edf2319eccf0feafcec7ac4fd0164011a06db (diff)
downloadrails-55692003d4a230e2fc666420ce94c3b98d098f30.tar.gz
rails-55692003d4a230e2fc666420ce94c3b98d098f30.tar.bz2
rails-55692003d4a230e2fc666420ce94c3b98d098f30.zip
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
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb6
1 files changed, 5 insertions, 1 deletions
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?