Class: ActiveRecord::ConnectionAdapters::SQLServerAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_server_adapter.rb

Instance Method Summary collapse

Instance Method Details

#active_hostObject



55
56
57
# File 'lib/sql_server_adapter.rb', line 55

def active_host
  @active_host ||= @connection_options[:host]
end

#active_host=(host) ⇒ Object



51
52
53
# File 'lib/sql_server_adapter.rb', line 51

def active_host=(host)
  @active_host = host
end

#change_active_hostObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sql_server_adapter.rb', line 59

def change_active_host
  slave_host = @connection_options[:slaves][0]['host'] if @connection_options[:slaves]
  return unless slave_host
  if using_master?
    self.active_host = slave_host
    # After x minutes, try using the master database again
    @switch_back_next_attempt = Time.now + 5.minute
  else
    self.active_host = @connection_options[:host]
    @switch_back_next_attempt = nil
  end
end

#connectObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sql_server_adapter.rb', line 17

def connect
  config = @connection_options
  # If using host database, try connect to master again after x minutes
  if using_slave? && @switch_back_next_attempt.present? && @switch_back_next_attempt <= Time.now
    change_active_host
  end
  @connection = case config[:mode]
                when :dblib
                  begin
                    # Attempt first connect
                    dblib_connect(config)
                  rescue
                    begin
                      dblib_connect(config)
                    rescue
                      change_active_host
                      dblib_connect(config)
                    end
                  end
                when :odbc
                  odbc_connect(config)
                end
  @spid = _raw_select('SELECT @@SPID', fetch: :rows).first.first
  configure_connection
end

#dblib_connect(config) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sql_server_adapter.rb', line 72

def dblib_connect(config)
  TinyTds::Client.new(
      dataserver: config[:dataserver],
      host: active_host,
      port: config[:port],
      username: config[:username],
      password: config[:password],
      database: config[:database],
      tds_version: config[:tds_version],
      appname: config_appname(config),
      login_timeout: (config),
      timeout: config_timeout(config),
      encoding:  config_encoding(config),
      azure: config[:azure]
  ).tap do |client|
    if config[:azure]
      client.execute('SET ANSI_NULLS ON').do
      client.execute('SET CURSOR_CLOSE_ON_COMMIT OFF').do
      client.execute('SET ANSI_NULL_DFLT_ON ON').do
      client.execute('SET IMPLICIT_TRANSACTIONS OFF').do
      client.execute('SET ANSI_PADDING ON').do
      client.execute('SET QUOTED_IDENTIFIER ON').do
      client.execute('SET ANSI_WARNINGS ON').do
    else
      client.execute('SET ANSI_DEFAULTS ON').do
      client.execute('SET CURSOR_CLOSE_ON_COMMIT OFF').do
      client.execute('SET IMPLICIT_TRANSACTIONS OFF').do
    end
    client.execute('SET TEXTSIZE 2147483647').do
    client.execute('SET CONCAT_NULL_YIELDS_NULL ON').do
  end
end

#using_master?Boolean



43
44
45
# File 'lib/sql_server_adapter.rb', line 43

def using_master?
  @active_host == @connection_options[:host]
end

#using_slave?Boolean



47
48
49
# File 'lib/sql_server_adapter.rb', line 47

def using_slave?
  !using_master?
end