2 module ConnectionAdapters
8 @config = config.symbolize_keys
11 def configure_connection
12 config[:retry_count] ||= 5
13 config[:connection_alive_sql] ||= "select 1"
16 @jndi_connection = true
23 warn "JNDI data source unavailable: #{e.message}; trying straight JDBC"
32 jndi = config[:jndi].to_s
33 ctx = javax.naming.InitialContext.new
35 @connection_factory = JdbcConnectionFactory.impl do
38 unless config[:driver]
39 config[:driver] = connection.meta_data.connection.java_class.name
41 @jndi_connection = true
45 url = config[:url].to_s
46 if Hash === config[:options]
48 config[:options].each do |k,v|
49 options << '&' unless options.empty?
50 options << "#{k}=#{v}"
52 url = url['?'] ? "#{url}&#{options}" : "#{url}?#{options}" unless options.empty?
54 config[:options] = nil
60 unless config[:driver] && config[:url]
61 raise ::ActiveRecord::ConnectionNotEstablished, "jdbc adapter requires driver class and url"
64 driver = config[:driver].to_s
65 user = config[:username].to_s
66 pass = config[:password].to_s
69 #jdbc_driver = (config[:driver_instance] ||= JdbcDriver.new(driver))
71 @connection_factory = JdbcConnectionFactory.impl do
73 #jdbc_driver.connection(url, user, pass)
74 ::Java::OrgSonarServerUi::JRubyFacade.getInstance().getConnection()
80 attr_reader :adapter, :connection_factory
82 # @native_database_types - setup properly by adapter= versus set_native_database_types.
83 # This contains type information for the adapter. Individual adapters can make tweaks
84 # by defined modify_types
86 # @native_types - This is the default type settings sans any modifications by the
87 # individual adapter. My guess is that if we loaded two adapters of different types
88 # then this is used as a base to be tweaked by each adapter to create @native_database_types
90 def initialize(config)
93 connection # force the connection to load
94 set_native_database_types
96 rescue ::ActiveRecord::ActiveRecordError
99 raise ::ActiveRecord::JDBCError.new("The driver encountered an unknown error: #{e}").tap { |err|
101 err.sql_exception = e
105 def adapter=(adapter)
107 @native_database_types = dup_native_types
108 @adapter.modify_types(@native_database_types)
109 @adapter.config.replace(config)
112 # Duplicate all native types into new hash structure so it can be modified
113 # without destroying original structure.
116 @native_types.each_pair do |k, v|
117 types[k] = v.inject({}) do |memo, kv|
118 memo[kv.first] = begin kv.last.dup rescue kv.last end
124 private :dup_native_types