Skip to content

Commit

Permalink
refactor #adapter_spec
Browse files Browse the repository at this point in the history
- 2.times code seemed really hard to grasp
- predictable order of ArJdbc (adapter) DB vendor matching
  • Loading branch information
kares committed Dec 19, 2012
1 parent fd870c5 commit b7f55eb
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/arjdbc/jdbc/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,34 @@ def jdbc_connection

# Locate specialized adapter specification if one exists based on config data
def adapter_spec(config)
2.times do
dialect = (config[:dialect] || config[:driver]).to_s
::ArJdbc.constants.map { |name| ::ArJdbc.const_get name }.each do |constant|
if constant.respond_to? :adapter_matcher
spec = constant.adapter_matcher(dialect, config)
return spec if spec
end
dialect = (config[:dialect] || config[:driver]).to_s
::ArJdbc.constants.sort.each do |constant|
constant = ::ArJdbc.const_get(constant) # e.g. ArJdbc::MySQL

if constant.respond_to?(:adapter_matcher)
spec = constant.adapter_matcher(dialect, config)
return spec if spec
end
end

# If nothing matches and we're using jndi, try to automatically detect the database.
break unless config[:jndi] and !config[:dialect]
if config[:jndi] && ! config[:dialect]
begin
conn = Java::javax.naming.InitialContext.new.lookup(config[:jndi]).getConnection
config[:dialect] = conn.getMetaData.getDatabaseProductName
data_source = Java::JavaxNaming::InitialContext.new.lookup(config[:jndi])
connection = data_source.getConnection
config[:dialect] = connection.getMetaData.getDatabaseProductName

# Derby-specific hack
if ::ArJdbc::Derby.adapter_matcher(config[:dialect], config)
# Needed to set the correct database schema name
config[:username] ||= conn.getMetaData.getUserName
config[:username] ||= connection.getMetaData.getUserName
end
rescue
conn.close if conn
connection.close if connection
else
return adapter_spec(config) # re-try matching a spec with set config[:dialect]
end
end

nil
end

Expand Down

0 comments on commit b7f55eb

Please sign in to comment.