11require 'active_record/connection_adapters/abstract_adapter'
22require_library_or_gem 'dbi' unless defined? ( DBI )
33require 'core_ext/dbi'
4+ require 'core_ext/active_record'
45require 'base64'
56
67module 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
0 commit comments