@@ -192,7 +192,7 @@ def initialize(logger,config)
192192 end
193193 end
194194
195- # ABSTRACT ADAPTER =========================================#
195+ # === Abstract Adapter ========================================== #
196196
197197 def adapter_name
198198 ADAPTER_NAME
@@ -214,6 +214,54 @@ def supports_savepoints?
214214 true
215215 end
216216
217+ def disable_referential_integrity
218+ do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'"
219+ yield
220+ ensure
221+ do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'"
222+ end
223+
224+ # === Abstract Adapter (Connection Management) ================== #
225+
226+ def active?
227+ raw_connection_do ( "SELECT 1" )
228+ true
229+ rescue *lost_connection_exceptions
230+ false
231+ end
232+
233+ def reconnect!
234+ disconnect!
235+ connect
236+ active?
237+ end
238+
239+ def disconnect!
240+ case connection_mode
241+ when :odbc
242+ raw_connection . disconnect rescue nil
243+ else :adonet
244+ raw_connection . close rescue nil
245+ end
246+ end
247+
248+ def reset!
249+ remove_database_connections_and_rollback { }
250+ end
251+
252+ # === Abstract Adapter (Misc Support) =========================== #
253+
254+ def pk_and_sequence_for ( table_name )
255+ idcol = identity_column ( table_name )
256+ idcol ? [ idcol . name , nil ] : nil
257+ end
258+
259+ def primary_key ( table_name )
260+ identity_column ( table_name ) . try ( :name )
261+ end
262+
263+ # === SQLServer Specific (DB Reflection) ======================== #
264+
217265 def database_version
218266 @database_version ||= info_schema_query { select_value ( 'SELECT @@version' ) }
219267 end
@@ -275,39 +323,6 @@ def native_binary_database_type
275323 @@native_binary_database_type || ( ( sqlserver_2005? || sqlserver_2008? ) ? 'varbinary(max)' : 'image' )
276324 end
277325
278- # REFERENTIAL INTEGRITY ====================================#
279-
280- def disable_referential_integrity
281- do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'"
282- yield
283- ensure
284- do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'"
285- end
286-
287- # CONNECTION MANAGEMENT ====================================#
288-
289- def active?
290- raw_connection_do ( "SELECT 1" )
291- true
292- rescue *lost_connection_exceptions
293- false
294- end
295-
296- def reconnect!
297- disconnect!
298- connect
299- active?
300- end
301-
302- def disconnect!
303- case connection_mode
304- when :odbc
305- raw_connection . disconnect rescue nil
306- else :adonet
307- raw_connection . close rescue nil
308- end
309- end
310-
311326 # RAKE UTILITY METHODS =====================================#
312327
313328 def recreate_database
@@ -357,24 +372,13 @@ def current_database
357372 def charset
358373 select_value "SELECT SERVERPROPERTY('SqlCharSetName')"
359374 end
360-
361- # This should disconnect all other users and rollback any transactions for SQL 2000 and 2005
362- # http://sqlserver2000.databases.aspfaq.com/how-do-i-drop-a-sql-server-database.html
363- def remove_database_connections_and_rollback ( database = nil )
364- database ||= current_database
365- do_execute "ALTER DATABASE #{ quote_table_name ( database ) } SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
366- begin
367- yield
368- ensure
369- do_execute "ALTER DATABASE #{ quote_table_name ( database ) } SET MULTI_USER"
370- end if block_given?
371- end
375+
372376
373377
374378
375379 protected
376380
377- # CONNECTION MANAGEMENT ==================================== #
381+ # === SQLServer Specific (Connection Management) ================ #
378382
379383 def connect
380384 config = @connection_options
@@ -402,8 +406,14 @@ def connect
402406 raise unless @auto_connecting
403407 end
404408
405- def connection_mode
406- @connection_options [ :mode ]
409+ def remove_database_connections_and_rollback ( database = nil )
410+ database ||= current_database
411+ do_execute "ALTER DATABASE #{ quote_table_name ( database ) } SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
412+ begin
413+ yield
414+ ensure
415+ do_execute "ALTER DATABASE #{ quote_table_name ( database ) } SET MULTI_USER"
416+ end if block_given?
407417 end
408418
409419 def lost_connection_exceptions
@@ -442,35 +452,8 @@ def auto_reconnected?
442452 @auto_connecting = false
443453 end
444454
445- def raw_connection_run ( sql )
446- with_auto_reconnect do
447- case connection_mode
448- when :odbc
449- block_given? ? raw_connection . run_block ( sql ) { |handle | yield ( handle ) } : raw_connection . run ( sql )
450- else :adonet
451- raw_connection . create_command . tap { |cmd | cmd . command_text = sql } . execute_reader
452- end
453- end
454- end
455-
456- def raw_connection_do ( sql )
457- case connection_mode
458- when :odbc
459- raw_connection . do ( sql )
460- else :adonet
461- raw_connection . create_command . tap { |cmd | cmd . command_text = sql } . execute_non_query
462- end
463- end
464-
465- def finish_statement_handle ( handle )
466- case connection_mode
467- when :odbc
468- handle . drop if handle && handle . respond_to? ( :drop ) && !handle . finished?
469- when :adonet
470- handle . close if handle && handle . respond_to? ( :close ) && !handle . is_closed
471- handle . dispose if handle && handle . respond_to? ( :dispose )
472- end
473- handle
455+ def connection_mode
456+ @connection_options [ :mode ]
474457 end
475458
476459 end #class SQLServerAdapter < AbstractAdapter
0 commit comments