Module: ActiveRecord::ConnectionAdapters::JdbcConnection::ConfigHelper

Included in:
ActiveRecord::ConnectionAdapters::JdbcConnection
Defined in:
lib/arjdbc/jdbc/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/arjdbc/jdbc/connection.rb', line 5

def config
  @config
end

Instance Method Details

#configure_connectionObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/arjdbc/jdbc/connection.rb', line 11

def configure_connection
  config[:retry_count] ||= 5
  config[:connection_alive_sql] ||= "select 1"
  @jndi_connection = false
  @connection = nil
  if config[:jndi]
    begin
      configure_jndi
    rescue => e
      warn "JNDI data source unavailable: #{e.message}; trying straight JDBC"
      configure_jdbc
    end
  else
    configure_jdbc
  end
end

#configure_jdbcObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/arjdbc/jdbc/connection.rb', line 56

def configure_jdbc
  unless config[:driver] && config[:url]
    raise ::ActiveRecord::ConnectionNotEstablished, "jdbc adapter requires driver class and url"
  end

  driver = config[:driver].to_s
  user   = config[:username].to_s
  pass   = config[:password].to_s
  url    = configure_url

  jdbc_driver = JdbcDriver.new(driver)
  jdbc_driver.load
  @connection_factory = JdbcConnectionFactory.impl do
    jdbc_driver.connection(url, user, pass)
  end
end

#configure_jndiObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/arjdbc/jdbc/connection.rb', line 28

def configure_jndi
  jndi = config[:jndi].to_s
  ctx = javax.naming.InitialContext.new
  ds = ctx.lookup(jndi)
  @connection_factory = JdbcConnectionFactory.impl do
    ds.connection
  end
  unless config[:driver]
    config[:driver] = connection..connection.java_class.name
  end
  @jndi_connection = true
end

#configure_urlObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/arjdbc/jdbc/connection.rb', line 41

def configure_url
  url = config[:url].to_s
  if Hash === config[:options]
    options = ''
    config[:options].each do |k,v|
      options << '&' unless options.empty?
      options << "#{k}=#{v}"
    end
    url = url['?'] ? "#{url}&#{options}" : "#{url}?#{options}" unless options.empty?
    config[:url] = url
    config[:options] = nil
  end
  url
end