Skip to content

Commit

Permalink
Core extensions for ActiveRecord now reflect on the connection before…
Browse files Browse the repository at this point in the history
… doing SQL Server things. Now this adapter is compatible for using with other adapters.
  • Loading branch information
metaskills committed Jan 2, 2009
1 parent 65c6681 commit ef97131
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
@@ -1,7 +1,8 @@

MASTER

*
* Core extensions for ActiveRecord now reflect on the connection before doing SQL Server things. Now
this adapter is compatible for using with other adapters. [Ken Collins]


* 2.2.4 (December 5th, 2008)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_record/connection_adapters/sqlserver_adapter.rb
Expand Up @@ -196,6 +196,10 @@ def database_year
DATABASE_VERSION_REGEXP.match(database_version)[1].to_i
end

def sqlserver?
true
end

def sqlserver_2000?
database_year == 2000
end
Expand Down
52 changes: 28 additions & 24 deletions lib/core_ext/active_record.rb
Expand Up @@ -29,36 +29,40 @@ def coerced_sqlserver_time_columns
end

def reset_column_information_with_sqlserver_cache_support
connection.send(:initialize_sqlserver_caches)
connection.send(:initialize_sqlserver_caches) if connection.respond_to?(:sqlserver?)
reset_column_information_without_sqlserver_cache_support
end

private

def add_order_with_sqlserver_unique_checking!(sql, order, scope = :auto)
order_sql = ''
add_order_without_sqlserver_unique_checking!(order_sql, order, scope)
unless order_sql.blank?
unique_order_hash = {}
select_table_name = connection.send(:get_table_name,sql)
select_table_name.tr!('[]','') if select_table_name
orders_and_dirs_set = connection.send(:orders_and_dirs_set,order_sql)
unique_order_sql = orders_and_dirs_set.inject([]) do |array,order_dir|
ord, dir = order_dir
ord_tn_and_cn = ord.to_s.split('.').map{|o|o.tr('[]','')}
ord_table_name, ord_column_name = if ord_tn_and_cn.size > 1
ord_tn_and_cn
else
[nil, ord_tn_and_cn.first]
end
if (ord_table_name && ord_table_name == select_table_name && unique_order_hash[ord_column_name]) || unique_order_hash[ord_column_name]
array
else
unique_order_hash[ord_column_name] = true
array << "#{ord} #{dir}".strip
end
end.join(', ')
sql << " ORDER BY #{unique_order_sql}"
if connection.respond_to?(:sqlserver?)
order_sql = ''
add_order_without_sqlserver_unique_checking!(order_sql, order, scope)
unless order_sql.blank?
unique_order_hash = {}
select_table_name = connection.send(:get_table_name,sql)
select_table_name.tr!('[]','') if select_table_name
orders_and_dirs_set = connection.send(:orders_and_dirs_set,order_sql)
unique_order_sql = orders_and_dirs_set.inject([]) do |array,order_dir|
ord, dir = order_dir
ord_tn_and_cn = ord.to_s.split('.').map{|o|o.tr('[]','')}
ord_table_name, ord_column_name = if ord_tn_and_cn.size > 1
ord_tn_and_cn
else
[nil, ord_tn_and_cn.first]
end
if (ord_table_name && ord_table_name == select_table_name && unique_order_hash[ord_column_name]) || unique_order_hash[ord_column_name]
array
else
unique_order_hash[ord_column_name] = true
array << "#{ord} #{dir}".strip
end
end.join(', ')
sql << " ORDER BY #{unique_order_sql}"
end
else
add_order_without_sqlserver_unique_checking!(order_sql, order, scope)
end
end

Expand Down

0 comments on commit ef97131

Please sign in to comment.