Skip to content

Commit 8d4db05

Browse files
committed
Move connection management methods. Standardize on #raw_connection vs @connection so end users can stub if needed.
1 parent 28297e3 commit 8d4db05

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,33 @@ def disable_referential_integrity(&block)
373373
execute "EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'"
374374
end
375375

376-
# DATABASE STATEMENTS ======================================
376+
# CONNECTION MANAGEMENT ====================================#
377+
378+
def active?
379+
raw_connection.execute("SELECT 1").finish
380+
true
381+
rescue DBI::DatabaseError, DBI::InterfaceError
382+
false
383+
end
384+
385+
def reconnect!
386+
disconnect!
387+
@connection = DBI.connect(*@connection_options)
388+
rescue DBI::DatabaseError => e
389+
@logger.warn "#{adapter_name} reconnection failed: #{e.message}" if @logger
390+
false
391+
end
392+
393+
def disconnect!
394+
raw_connection.disconnect rescue nil
395+
end
396+
397+
def finish_statement_handle(handle)
398+
handle.finish if handle && handle.respond_to?(:finish) && !handle.finished?
399+
handle
400+
end
401+
402+
# DATABASE STATEMENTS ======================================#
377403

378404
def select_rows(sql, name = nil)
379405
raw_select(sql,name).last
@@ -422,7 +448,7 @@ def add_limit_offset!(sql, options)
422448
end
423449

424450
if options[:limit] and options[:offset]
425-
total_rows = @connection.select_all("SELECT count(*) as TotalRows from (#{sql.gsub(/\bSELECT(\s+DISTINCT)?\b/i, "SELECT#{$1} TOP 1000000000")}) tally")[0][:TotalRows].to_i
451+
total_rows = raw_connection.select_all("SELECT count(*) as TotalRows from (#{sql.gsub(/\bSELECT(\s+DISTINCT)?\b/i, "SELECT#{$1} TOP 1000000000")}) tally")[0][:TotalRows].to_i
426452
if (options[:limit] + options[:offset]) >= total_rows
427453
options[:limit] = (total_rows - options[:offset] >= 0) ? (total_rows - options[:offset]) : 0
428454
end
@@ -638,32 +664,6 @@ def pk_and_sequence_for(table_name)
638664
idcol ? [idcol.name,nil] : nil
639665
end
640666

641-
# CONNECTION MANAGEMENT ====================================#
642-
643-
def active?
644-
@connection.execute("SELECT 1").finish
645-
true
646-
rescue DBI::DatabaseError, DBI::InterfaceError
647-
false
648-
end
649-
650-
def reconnect!
651-
disconnect!
652-
@connection = DBI.connect(*@connection_options)
653-
rescue DBI::DatabaseError => e
654-
@logger.warn "#{adapter_name} reconnection failed: #{e.message}" if @logger
655-
false
656-
end
657-
658-
def disconnect!
659-
@connection.disconnect rescue nil
660-
end
661-
662-
def finish_statement_handle(handle)
663-
handle.finish if handle && handle.respond_to?(:finish) && !handle.finished?
664-
handle
665-
end
666-
667667
# RAKE UTILITY METHODS =====================================#
668668

669669
def recreate_database(name)
@@ -742,9 +742,9 @@ def update_sql(sql, name = nil)
742742
def raw_execute(sql, name = nil, &block)
743743
log(sql, name) do
744744
if block_given?
745-
@connection.execute(sql) { |handle| yield(handle) }
745+
raw_connection.execute(sql) { |handle| yield(handle) }
746746
else
747-
@connection.execute(sql)
747+
raw_connection.execute(sql)
748748
end
749749
end
750750
end

0 commit comments

Comments
 (0)