Skip to content

Commit 91f413c

Browse files
author
Aidan Haran
committed
Refactored to use new_client connection pattern
1 parent 0e0da50 commit 91f413c

File tree

2 files changed

+88
-75
lines changed

2 files changed

+88
-75
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 75 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,67 @@ class SQLServerAdapter < AbstractAdapter
5959
self.use_output_inserted = true
6060
self.exclude_output_inserted_table_names = Concurrent::Map.new { false }
6161

62-
def initialize(connection, logger = nil, config = {})
62+
class << self
63+
def new_client(config)
64+
case config[:mode]
65+
when :dblib
66+
dblib_connect(config)
67+
end
68+
end
69+
70+
def dblib_connect(config)
71+
TinyTds::Client.new(
72+
dataserver: config[:dataserver],
73+
host: config[:host],
74+
port: config[:port],
75+
username: config[:username],
76+
password: config[:password],
77+
database: config[:database],
78+
tds_version: config[:tds_version] || "7.3",
79+
appname: config_appname(config),
80+
login_timeout: config_login_timeout(config),
81+
timeout: config_timeout(config),
82+
encoding: config_encoding(config),
83+
azure: config[:azure],
84+
contained: config[:contained]
85+
).tap do |client|
86+
if config[:azure]
87+
client.execute("SET ANSI_NULLS ON").do
88+
client.execute("SET ANSI_NULL_DFLT_ON ON").do
89+
client.execute("SET ANSI_PADDING ON").do
90+
client.execute("SET ANSI_WARNINGS ON").do
91+
else
92+
client.execute("SET ANSI_DEFAULTS ON").do
93+
end
94+
client.execute("SET QUOTED_IDENTIFIER ON").do
95+
client.execute("SET CURSOR_CLOSE_ON_COMMIT OFF").do
96+
client.execute("SET IMPLICIT_TRANSACTIONS OFF").do
97+
client.execute("SET TEXTSIZE 2147483647").do
98+
client.execute("SET CONCAT_NULL_YIELDS_NULL ON").do
99+
end
100+
end
101+
102+
def config_appname(config)
103+
config[:appname] || configure_application_name || Rails.application.class.name.split("::").first rescue nil
104+
end
105+
106+
def config_login_timeout(config)
107+
config[:login_timeout].present? ? config[:login_timeout].to_i : nil
108+
end
109+
110+
def config_timeout(config)
111+
config[:timeout].present? ? config[:timeout].to_i / 1000 : nil
112+
end
113+
114+
def config_encoding(config)
115+
config[:encoding].present? ? config[:encoding] : nil
116+
end
117+
end
118+
119+
def initialize(connection, logger, _connection_options, config)
63120
super(connection, logger, config)
64-
# Our Responsibility
65121
@connection_options = config
66-
connect
67-
initialize_dateformatter
68-
use_database
122+
configure_connection
69123
end
70124

71125
# === Abstract Adapter ========================================== #
@@ -226,6 +280,14 @@ def reset!
226280
do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
227281
end
228282

283+
def configure_connection
284+
@spid = _raw_select("SELECT @@SPID", fetch: :rows).first.first
285+
@version_year = version_year
286+
287+
initialize_dateformatter
288+
use_database
289+
end
290+
229291
# === Abstract Adapter (Misc Support) =========================== #
230292

231293
def tables_with_referential_integrity
@@ -408,78 +470,18 @@ def translate_exception(e, message:, sql:, binds:)
408470

409471
# === SQLServer Specific (Connection Management) ================ #
410472

411-
def connect
412-
config = @connection_options
413-
@connection = case config[:mode]
414-
when :dblib
415-
dblib_connect(config)
416-
end
417-
@spid = _raw_select("SELECT @@SPID", fetch: :rows).first.first
418-
@version_year = version_year
419-
configure_connection
420-
end
421-
422473
def connection_errors
423474
@connection_errors ||= [].tap do |errors|
424475
errors << TinyTds::Error if defined?(TinyTds::Error)
425476
end
426477
end
427478

428-
def dblib_connect(config)
429-
TinyTds::Client.new(
430-
dataserver: config[:dataserver],
431-
host: config[:host],
432-
port: config[:port],
433-
username: config[:username],
434-
password: config[:password],
435-
database: config[:database],
436-
tds_version: config[:tds_version] || "7.3",
437-
appname: config_appname(config),
438-
login_timeout: config_login_timeout(config),
439-
timeout: config_timeout(config),
440-
encoding: config_encoding(config),
441-
azure: config[:azure],
442-
contained: config[:contained]
443-
).tap do |client|
444-
if config[:azure]
445-
client.execute("SET ANSI_NULLS ON").do
446-
client.execute("SET ANSI_NULL_DFLT_ON ON").do
447-
client.execute("SET ANSI_PADDING ON").do
448-
client.execute("SET ANSI_WARNINGS ON").do
449-
else
450-
client.execute("SET ANSI_DEFAULTS ON").do
451-
end
452-
client.execute("SET QUOTED_IDENTIFIER ON").do
453-
client.execute("SET CURSOR_CLOSE_ON_COMMIT OFF").do
454-
client.execute("SET IMPLICIT_TRANSACTIONS OFF").do
455-
client.execute("SET TEXTSIZE 2147483647").do
456-
client.execute("SET CONCAT_NULL_YIELDS_NULL ON").do
457-
end
458-
end
459-
460-
def config_appname(config)
461-
config[:appname] || configure_application_name || Rails.application.class.name.split("::").first rescue nil
462-
end
463-
464-
def config_login_timeout(config)
465-
config[:login_timeout].present? ? config[:login_timeout].to_i : nil
466-
end
467-
468-
def config_timeout(config)
469-
config[:timeout].present? ? config[:timeout].to_i / 1000 : nil
470-
end
471-
472-
def config_encoding(config)
473-
config[:encoding].present? ? config[:encoding] : nil
474-
end
475-
476-
def configure_connection; end
477-
478479
def configure_application_name; end
479480

480481
def initialize_dateformatter
481482
@database_dateformat = user_options_dateformat
482483
a, b, c = @database_dateformat.each_char.to_a
484+
483485
[a, b, c].each { |f| f.upcase! if f == "y" }
484486
dateformat = "%#{a}-%#{b}-%#{c}"
485487
::Date::DATE_FORMATS[:_sqlserver_dateformat] = dateformat
@@ -502,6 +504,13 @@ def version_year
502504
def sqlserver_version
503505
@sqlserver_version ||= _raw_select("SELECT @@version", fetch: :rows).first.first.to_s
504506
end
507+
508+
private
509+
510+
def connect
511+
@connection = self.class.new_client(@connection_options)
512+
configure_connection
513+
end
505514
end
506515
end
507516
end

lib/active_record/sqlserver_base.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ module ActiveRecord
44
module ConnectionHandling
55
def sqlserver_connection(config) #:nodoc:
66
config = config.symbolize_keys
7-
config.reverse_merge! mode: :dblib
8-
mode = config[:mode].to_s.downcase.underscore.to_sym
9-
case mode
7+
config.reverse_merge!(mode: :dblib)
8+
config[:mode] = config[:mode].to_s.downcase.underscore.to_sym
9+
10+
case config[:mode]
1011
when :dblib
1112
require "tiny_tds"
1213
else
1314
raise ArgumentError, "Unknown connection mode in #{config.inspect}."
1415
end
15-
ConnectionAdapters::SQLServerAdapter.new(nil, nil, config.merge(mode: mode))
16+
17+
ConnectionAdapters::SQLServerAdapter.new(
18+
ConnectionAdapters::SQLServerAdapter.new_client(config),
19+
logger,
20+
nil,
21+
config
22+
)
1623
rescue TinyTds::Error => e
17-
if e.message.match(/database .* does not exist/i)
18-
raise ActiveRecord::NoDatabaseError
19-
else
20-
raise
21-
end
24+
raise ActiveRecord::NoDatabaseError if e.message.match(/database .* does not exist/i)
25+
raise e
2226
end
2327
end
2428
end

0 commit comments

Comments
 (0)