Skip to content

Commit

Permalink
Fix ArgumentError: Unknown connection mode error
Browse files Browse the repository at this point in the history
  • Loading branch information
Lavika committed Oct 5, 2023
1 parent bf4cab2 commit f571df2
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def dbconsole(config, options = {})
end

def new_client(config)
case config[:mode]
case config[:mode].to_sym
when :dblib
require "tiny_tds"
dblib_connect(config)
Expand Down Expand Up @@ -124,6 +124,25 @@ def dblib_connect(config)
raise e
end

def odbc_connect(config)
if config[:dsn].include?(';')
driver = ODBC::Driver.new.tap do |d|
d.name = config[:dsn_name] || 'Driver1'
d.attrs = config[:dsn].split(';').map { |atr| atr.split('=') }.reject { |kv| kv.size != 2 }.reduce({}) { |a, e| k, v = e ; a[k] = v ; a }
end
ODBC::Database.new.drvconnect(driver)
else
ODBC.connect config[:dsn], config[:username], config[:password]
end.tap do |c|
begin
c.use_time = true
c.use_utc = ActiveRecord::Base.default_timezone == :utc
rescue Exception
warn 'Ruby ODBC v0.99992 or higher is required.'
end
end
end

def config_appname(config)
if instance_methods.include?(:configure_application_name)
ActiveSupport::Deprecation.warn <<~MSG.squish
Expand Down Expand Up @@ -158,6 +177,7 @@ def config_encoding(config)
end

def initialize(connection, logger, _connection_options, config)
config[:mode] = config[:mode].to_s.downcase.underscore.to_sym
super(connection, logger, config)
@connection_options = config
perform_connection_configuration
Expand Down Expand Up @@ -537,25 +557,6 @@ def connection_errors
end
end

def odbc_connect(config)
if config[:dsn].include?(';')
driver = ODBC::Driver.new.tap do |d|
d.name = config[:dsn_name] || 'Driver1'
d.attrs = config[:dsn].split(';').map { |atr| atr.split('=') }.reject { |kv| kv.size != 2 }.reduce({}) { |a, e| k, v = e ; a[k] = v ; a }
end
ODBC::Database.new.drvconnect(driver)
else
ODBC.connect config[:dsn], config[:username], config[:password]
end.tap do |c|
begin
c.use_time = true
c.use_utc = ActiveRecord::Base.default_timezone == :utc
rescue Exception
warn 'Ruby ODBC v0.99992 or higher is required.'
end
end
end

def config_appname(config)
config[:appname] || configure_application_name || Rails.application.class.name.split("::").first rescue nil
end
Expand Down

0 comments on commit f571df2

Please sign in to comment.