Class: DBI::DBD::Jdbc::Driver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/dbd/jdbc/driver.rb

Overview

Models the DBI::BaseDriver API to create DBI::DriverHandle objects.

Instance Method Summary collapse

Constructor Details

#initializeDriver

Returns a new instance of Driver.



35
36
37
38
39
40
41
42
# File 'lib/dbd/jdbc/driver.rb', line 35

def initialize
  super("0.4.0")
  #attributes specific to this class.  All attributes in the
  #connect attributes that aren't in this list will be
  #applied to the database handle
  @driverAttributes = [ "driver" ]
  @loaded_drivers = Collections.synchronizedMap(HashMap.new)
end

Instance Method Details

#connect(dbname, user, auth, attr) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dbd/jdbc/driver.rb', line 53

def connect(dbname, user, auth, attr)
  driverClass = attr["driver"]
  raise InterfaceError.new('driver class name must be specified as "driver" in connection attributes') unless driverClass

  load(driverClass)

  connection = java.sql.DriverManager.getConnection("jdbc:"+dbname, user, auth)
  dbh = Database.new(connection)

  (attr.keys - @driverAttributes).each { |key| dbh[key] = attr[key] }

  return dbh
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#load(name) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/dbd/jdbc/driver.rb', line 44

def load(name)
  unless @loaded_drivers.containsKey(name)
    clazz = java.lang.Class.forName(name,true,JRuby.runtime.jruby_class_loader)
    java.sql.DriverManager.registerDriver(clazz.newInstance)
    
    @loaded_drivers.put(name,true)
  end
end