11require 'active_record/connection_adapters/abstract_adapter'
2- require_library_or_gem 'dbi ' unless defined? ( DBI )
3- require 'active_record/connection_adapters/ sqlserver_adapter/core_ext/dbi '
2+ require_library_or_gem 'odbc ' unless defined? ( ODBC )
3+ require File . dirname ( __FILE__ ) + '/ sqlserver_adapter/core_ext/odbc '
44require 'active_record/connection_adapters/sqlserver_adapter/core_ext/active_record'
55require 'base64'
66
@@ -10,20 +10,11 @@ class Base
1010
1111 def self . sqlserver_connection ( config ) #:nodoc:
1212 config . symbolize_keys!
13- mode = config [ :mode ] ? config [ :mode ] . to_s . upcase : 'ADO'
1413 username = config [ :username ] ? config [ :username ] . to_s : 'sa'
1514 password = config [ :password ] ? config [ :password ] . to_s : ''
16- if mode == "ODBC"
17- raise ArgumentError , "Missing DSN. Argument ':dsn' must be set in order for this adapter to work." unless config . has_key? ( :dsn )
18- dsn = config [ :dsn ]
19- driver_url = "DBI:ODBC:#{ dsn } "
20- else
21- raise ArgumentError , "Missing Database. Argument ':database' must be set in order for this adapter to work." unless config . has_key? ( :database )
22- database = config [ :database ]
23- host = config [ :host ] ? config [ :host ] . to_s : 'localhost'
24- driver_url = "DBI:ADO:Provider=SQLOLEDB;Data Source=#{ host } ;Initial Catalog=#{ database } ;User ID=#{ username } ;Password=#{ password } ;"
25- end
26- ConnectionAdapters ::SQLServerAdapter . new ( logger , [ driver_url , username , password ] )
15+ raise ArgumentError , "Missing DSN. Argument ':dsn' must be set in order for this adapter to work." unless config . has_key? ( :dsn )
16+ dsn = config [ :dsn ]
17+ ConnectionAdapters ::SQLServerAdapter . new ( logger , [ dsn , username , password ] )
2718 end
2819
2920 protected
@@ -147,53 +138,28 @@ def simplified_datetime
147138
148139 end #SQLServerColumn
149140
150- # In ADO mode, this adapter will ONLY work on Windows systems, since it relies on
151- # Win32OLE, which, to my knowledge, is only available on Windows.
152- #
153- # This mode also relies on the ADO support in the DBI module. If you are using the
154- # one-click installer of Ruby, then you already have DBI installed, but the ADO module
155- # is *NOT* installed. You will need to get the latest source distribution of Ruby-DBI
156- # from http://ruby-dbi.sourceforge.net/ unzip it, and copy the file from
157- # <tt>src/lib/dbd_ado/ADO.rb</tt> to <tt>X:/Ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb</tt>
158- #
159- # You will more than likely need to create the ADO directory. Once you've installed
160- # that file, you are ready to go.
161- #
162141 # In ODBC mode, the adapter requires the ODBC support in the DBI module which requires
163142 # the Ruby ODBC module. Ruby ODBC 0.996 was used in development and testing,
164143 # and it is available at http://www.ch-werner.de/rubyodbc/
165144 #
166145 # Options:
167146 #
168- # * <tt>:mode</tt> -- ADO or ODBC. Defaults to ADO.
169147 # * <tt>:username</tt> -- Defaults to sa.
170148 # * <tt>:password</tt> -- Defaults to empty string.
171- # * <tt>:windows_auth</tt> -- Defaults to "User ID=#{username};Password=#{password}"
172- #
173- # ADO specific options:
174- #
175- # * <tt>:host</tt> -- Defaults to localhost.
176- # * <tt>:database</tt> -- The name of the database. No default, must be provided.
177- # * <tt>:windows_auth</tt> -- Use windows authentication instead of username/password.
178- #
179- # ODBC specific options:
180- #
181149 # * <tt>:dsn</tt> -- Defaults to nothing.
182150 #
183151 class SQLServerAdapter < AbstractAdapter
184152
185153 ADAPTER_NAME = 'SQLServer' . freeze
186- VERSION = '2.3 ' . freeze
154+ VERSION = '2.4-mmi ' . freeze
187155 DATABASE_VERSION_REGEXP = /Microsoft SQL Server\s +(\d {4})/
188156 SUPPORTED_VERSIONS = [ 2000 , 2005 , 2008 ] . freeze
189157 LIMITABLE_TYPES = [ 'string' , 'integer' , 'float' , 'char' , 'nchar' , 'varchar' , 'nvarchar' ] . freeze
190158
191- LOST_CONNECTION_EXCEPTIONS = [ DBI ::DatabaseError , DBI ::InterfaceError ]
159+ LOST_CONNECTION_EXCEPTIONS = [ ODBC :: Error ] #[ DBI::DatabaseError, DBI::InterfaceError]
192160 LOST_CONNECTION_MESSAGES = [
193- 'Communication link failure' ,
194- 'Read from the server failed' ,
195- 'Write to the server failed' ,
196- 'Database connection was already closed' ]
161+ 'Invalid handle'
162+ ]
197163
198164 cattr_accessor :native_text_database_type , :native_binary_database_type , :native_string_database_type ,
199165 :log_info_schema_queries , :enable_default_unicode_types , :auto_connect
@@ -349,7 +315,7 @@ def disable_referential_integrity(&block)
349315 # CONNECTION MANAGEMENT ====================================#
350316
351317 def active?
352- raw_connection . execute ( "SELECT 1" ) . finish
318+ raw_connection . run ( "SELECT 1" ) . drop
353319 true
354320 rescue *LOST_CONNECTION_EXCEPTIONS
355321 false
@@ -366,7 +332,7 @@ def disconnect!
366332 end
367333
368334 def finish_statement_handle ( handle )
369- handle . finish if handle && handle . respond_to? ( :finish ) && !handle . finished?
335+ handle . drop if handle && handle . respond_to? ( :drop ) && !handle . finished?
370336 handle
371337 end
372338
@@ -771,15 +737,15 @@ def remove_database_connections_and_rollback(name)
771737 # CONNECTION MANAGEMENT ====================================#
772738
773739 def connect
774- driver_url , username , password = @connection_options
775- @connection = DBI . connect ( driver_url , username , password )
740+ dsn , username , password = @connection_options
741+ @connection = ODBC . connect ( dsn , username , password )
776742 configure_connection
777743 rescue
778744 raise unless @auto_connecting
779745 end
780746
781747 def configure_connection
782- raw_connection [ 'AutoCommit' ] = true
748+ # raw_connection['AutoCommit'] = true
783749 end
784750
785751 def with_auto_reconnect
@@ -839,9 +805,9 @@ def info_schema_query
839805 def raw_execute ( sql , name = nil , &block )
840806 log ( sql , name ) do
841807 if block_given?
842- with_auto_reconnect { raw_connection . execute ( sql ) { |handle | yield ( handle ) } }
808+ with_auto_reconnect { raw_connection . run_block ( sql ) { |handle | yield ( handle ) } }
843809 else
844- with_auto_reconnect { raw_connection . execute ( sql ) }
810+ with_auto_reconnect { raw_connection . run ( sql ) }
845811 end
846812 end
847813 end
@@ -861,12 +827,12 @@ def do_execute(sql,name=nil)
861827
862828 def raw_select ( sql , name = nil )
863829 handle = raw_execute ( sql , name )
864- fields = handle . column_names
830+ fields = handle . columns ( true ) . map { | c | c . name }
865831 results = handle_as_array ( handle )
866832 rows = results . inject ( [ ] ) do |rows , row |
867833 row . each_with_index do |value , i |
868834 # DEPRECATED in DBI 0.4.0 and above. Remove when 0.2.2 and lower is no longer supported.
869- if value . is_a? DBI :: Timestamp
835+ if value . is_a? ODBC :: TimeStamp
870836 row [ i ] = value . to_sqlserver_string
871837 end
872838 end
0 commit comments