Class: RDBI::Driver::JDBC::Database

Inherits:
RDBI::Database
  • Object
show all
Defined in:
lib/caruby/rdbi/driver/jdbc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Database

Returns a new instance of Database.

Raises:

  • (DatabaseError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 47

def initialize(*args)
  super *args

  database = @connect_args[:database] || @connect_args[:dbname] ||
    @connect_args[:db]
  username = @connect_args[:username] || @connect_args[:user]
  password = @connect_args[:password] || @connect_args[:pass]
  
  # the driver class
  driver_class = @connect_args[:driver_class]
  raise DatabaseError.new('Missing JDBC driver class') unless driver_class
  clazz = java.lang.Class.forName(driver_class, true, JRuby.runtime.jruby_class_loader)
  java.sql.DriverManager.registerDriver(clazz.newInstance)

  @handle = java.sql.DriverManager.getConnection(
              "#{database}",
              username,
              password
            )

  self.database_name = @handle.getCatalog
end

Instance Attribute Details

#handleObject

Returns the value of attribute handle.



45
46
47
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 45

def handle
  @handle
end

Instance Method Details

#commitObject



90
91
92
93
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 90

def commit
  @handle.commit if @handle.getAutoCommit == false
  super
end

#disconnectObject



70
71
72
73
74
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 70

def disconnect
  @handle.rollback if @handle.getAutoCommit == false
  @handle.close
  super
end

#new_statement(query) ⇒ Object



95
96
97
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 95

def new_statement(query)
  Statement.new(query, self)
end

#pingObject



114
115
116
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 114

def ping
  !@handle.isClosed
end

#quote(item) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 118

def quote(item)
  case item
  when Numeric
    item.to_s
  when TrueClass
    "1"
  when FalseClass
    "0"
  when NilClass
    "NULL"
  else
    "'#{item.to_s}'"
  end
end

#rollbackObject



85
86
87
88
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 85

def rollback
  @handle.rollback if @handle.getAutoCommit == false
  super
end

#schemaObject



105
106
107
108
109
110
111
112
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 105

def schema
  rs = @handle..getTables(nil, nil, nil, nil)
  tables = []
  while rs.next
    tables << table_schema(rs.getString(3))
  end
  tables
end

#table_schema(table_name) ⇒ Object



99
100
101
102
103
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 99

def table_schema(table_name)
  new_statement(
    "SELECT * FROM #{table_name} WHERE 1=2"
  ).new_execution[1]
end

#transaction(&block) ⇒ Object

Raises:

  • (RDBI::TransactionError)


76
77
78
79
80
81
82
83
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 76

def transaction(&block)
  raise RDBI::TransactionError, "Already in transaction" if in_transaction?
  @handle.setAutoCommit false
  @handle.setSavepoint
  super
  @handle.commit
  @handle.setAutoCommit true
end