Skip to content

Commit 0e0125b

Browse files
committed
Move ActiveRecord extensions/changes to core_ext module.
1 parent e14b6a1 commit 0e0125b

File tree

2 files changed

+99
-82
lines changed

2 files changed

+99
-82
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'active_record/connection_adapters/abstract_adapter'
22
require_library_or_gem 'dbi' unless defined?(DBI)
33
require 'core_ext/dbi'
4+
require 'core_ext/active_record'
45
require 'base64'
56

67
module ActiveRecord
@@ -26,88 +27,6 @@ def self.sqlserver_connection(config) #:nodoc:
2627
conn["AutoCommit"] = true
2728
ConnectionAdapters::SQLServerAdapter.new(conn, logger, [driver_url, username, password])
2829
end
29-
30-
class << self
31-
32-
def reset_column_information_with_sqlserver_columns_cache_support
33-
connection.instance_variable_set :@sqlserver_columns_cache, {}
34-
reset_column_information_without_sqlserver_columns_cache_support
35-
end
36-
alias_method_chain :reset_column_information, :sqlserver_columns_cache_support
37-
38-
end
39-
40-
private
41-
42-
# Add basic support for SQL server locking hints
43-
# In the case of SQL server, the lock value must follow the FROM clause
44-
# Mysql: SELECT * FROM tst where testID = 10 LOCK IN share mode
45-
# SQLServer: SELECT * from tst WITH (HOLDLOCK, ROWLOCK) where testID = 10
46-
# h-lame: OK, so these 2 methods should be a patch to rails ideally, so we don't
47-
# have to play catch up against rails itself should construct_finder_sql ever
48-
# change
49-
def self.construct_finder_sql(options)
50-
scope = scope(:find)
51-
sql = "SELECT #{options[:select] || (scope && scope[:select]) || ((options[:joins] || (scope && scope[:joins])) && quoted_table_name + '.*') || '*'} "
52-
sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} "
53-
54-
add_lock!(sql, options, scope) if ActiveRecord::Base.connection.adapter_name == "SQLServer" && !options[:lock].blank? # SQLServer
55-
56-
# merge_joins isn't defined in 2.1.1, but appears in edge
57-
if defined?(merge_joins)
58-
# The next line may fail with a nil error under 2.1.1 or other non-edge rails versions - Use this instead: add_joins!(sql, options, scope)
59-
add_joins!(sql, options[:joins], scope)
60-
else
61-
add_joins!(sql, options, scope)
62-
end
63-
64-
add_conditions!(sql, options[:conditions], scope)
65-
66-
add_group!(sql, options[:group], scope)
67-
add_order!(sql, options[:order], scope)
68-
add_limit!(sql, options, scope)
69-
add_lock!(sql, options, scope) unless ActiveRecord::Base.connection.adapter_name == "SQLServer" # Not SQLServer
70-
sql
71-
end
72-
73-
# Overwrite the ActiveRecord::Base method for SQL server.
74-
# GROUP BY is necessary for distinct orderings
75-
def self.construct_finder_sql_for_association_limiting(options, join_dependency)
76-
scope = scope(:find)
77-
is_distinct = !options[:joins].blank? || include_eager_conditions?(options) || include_eager_order?(options)
78-
79-
sql = "SELECT #{table_name}.#{connection.quote_column_name(primary_key)} FROM #{table_name} "
80-
81-
if is_distinct
82-
sql << join_dependency.join_associations.collect(&:association_join).join
83-
# merge_joins isn't defined in 2.1.1, but appears in edge
84-
if defined?(merge_joins)
85-
# The next line may fail with a nil error under 2.1.1 or other non-edge rails versions - Use this instead: add_joins!(sql, options, scope)
86-
add_joins!(sql, options[:joins], scope)
87-
else
88-
add_joins!(sql, options, scope)
89-
end
90-
end
91-
92-
add_conditions!(sql, options[:conditions], scope)
93-
add_group!(sql, options[:group], scope)
94-
95-
if options[:order] && is_distinct
96-
if sql =~ /GROUP\s+BY/i
97-
sql << ", #{table_name}.#{connection.quote_column_name(primary_key)}"
98-
else
99-
sql << " GROUP BY #{table_name}.#{connection.quote_column_name(primary_key)}"
100-
end #if sql =~ /GROUP BY/i
101-
102-
connection.add_order_by_for_association_limiting!(sql, options)
103-
else
104-
add_order!(sql, options[:order], scope)
105-
end
106-
107-
add_limit!(sql, options, scope)
108-
109-
return sanitize_sql(sql)
110-
end
11130

11231
end
11332

lib/core_ext/active_record.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
module ActiveRecord
2+
module ConnectionAdapters
3+
module SQLServerActiveRecordExtensions
4+
5+
def self.included(klass)
6+
klass.extend ClassMethods
7+
class << klass
8+
alias_method_chain :reset_column_information, :sqlserver_columns_cache_support
9+
end
10+
end
11+
12+
module ClassMethods
13+
14+
def reset_column_information_with_sqlserver_columns_cache_support
15+
connection.instance_variable_set :@sqlserver_columns_cache, {}
16+
reset_column_information_without_sqlserver_columns_cache_support
17+
end
18+
19+
private
20+
21+
# Add basic support for SQL server locking hints
22+
# In the case of SQL server, the lock value must follow the FROM clause
23+
# Mysql: SELECT * FROM tst where testID = 10 LOCK IN share mode
24+
# SQLServer: SELECT * from tst WITH (HOLDLOCK, ROWLOCK) where testID = 10
25+
# h-lame: OK, so these 2 methods should be a patch to rails ideally, so we don't
26+
# have to play catch up against rails itself should construct_finder_sql ever
27+
# change
28+
def construct_finder_sql(options)
29+
scope = scope(:find)
30+
sql = "SELECT #{options[:select] || (scope && scope[:select]) || ((options[:joins] || (scope && scope[:joins])) && quoted_table_name + '.*') || '*'} "
31+
sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} "
32+
33+
add_lock!(sql, options, scope) if ActiveRecord::Base.connection.adapter_name == "SQLServer" && !options[:lock].blank? # SQLServer
34+
35+
# merge_joins isn't defined in 2.1.1, but appears in edge
36+
if defined?(merge_joins)
37+
# The next line may fail with a nil error under 2.1.1 or other non-edge rails versions - Use this instead: add_joins!(sql, options, scope)
38+
add_joins!(sql, options[:joins], scope)
39+
else
40+
add_joins!(sql, options, scope)
41+
end
42+
43+
add_conditions!(sql, options[:conditions], scope)
44+
45+
add_group!(sql, options[:group], scope)
46+
add_order!(sql, options[:order], scope)
47+
add_limit!(sql, options, scope)
48+
add_lock!(sql, options, scope) unless ActiveRecord::Base.connection.adapter_name == "SQLServer" # Not SQLServer
49+
sql
50+
end
51+
52+
# Overwrite the ActiveRecord::Base method for SQL server.
53+
# GROUP BY is necessary for distinct orderings
54+
def construct_finder_sql_for_association_limiting(options, join_dependency)
55+
scope = scope(:find)
56+
is_distinct = !options[:joins].blank? || include_eager_conditions?(options) || include_eager_order?(options)
57+
58+
sql = "SELECT #{table_name}.#{connection.quote_column_name(primary_key)} FROM #{table_name} "
59+
60+
if is_distinct
61+
sql << join_dependency.join_associations.collect(&:association_join).join
62+
# merge_joins isn't defined in 2.1.1, but appears in edge
63+
if defined?(merge_joins)
64+
# The next line may fail with a nil error under 2.1.1 or other non-edge rails versions - Use this instead: add_joins!(sql, options, scope)
65+
add_joins!(sql, options[:joins], scope)
66+
else
67+
add_joins!(sql, options, scope)
68+
end
69+
end
70+
71+
add_conditions!(sql, options[:conditions], scope)
72+
add_group!(sql, options[:group], scope)
73+
74+
if options[:order] && is_distinct
75+
if sql =~ /GROUP\s+BY/i
76+
sql << ", #{table_name}.#{connection.quote_column_name(primary_key)}"
77+
else
78+
sql << " GROUP BY #{table_name}.#{connection.quote_column_name(primary_key)}"
79+
end #if sql =~ /GROUP BY/i
80+
81+
connection.add_order_by_for_association_limiting!(sql, options)
82+
else
83+
add_order!(sql, options[:order], scope)
84+
end
85+
86+
add_limit!(sql, options, scope)
87+
88+
return sanitize_sql(sql)
89+
end
90+
91+
end
92+
93+
94+
end
95+
end
96+
end
97+
98+
ActiveRecord::Base.send :include, ActiveRecord::ConnectionAdapters::SQLServerActiveRecordExtensions

0 commit comments

Comments
 (0)