Class: DBI::DBD::Jdbc::Database

Inherits:
BaseDatabase
  • Object
show all
Includes:
TypeConversions
Defined in:
lib/dbd/jdbc/database.rb

Overview

Models the DBI::BaseDatabase API to create DBI::DatabaseHandle objects.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypeConversions

#date_to_jdbcdate, #jdbc_to_dbi_sqltype, #jdbcdate_to_date, #jdbctime_to_time, #jdbctimestamp_to_timestamp, #time_to_jdbctime, #timestamp_to_jdbctimestamp

Constructor Details

#initialize(connection) ⇒ Database

Returns a new instance of Database.



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

def initialize(connection)
  @connection = connection
  @attributes = {
    #works with Sybase and Mysql.
    "nulltype" => java.sql.Types::VARCHAR,
    "allow_scroll" => false
  }
end

Class Method Details

.from_java_connection(java_connection, type_coercion = true) ⇒ Object



164
165
166
167
168
# File 'lib/dbd/jdbc/database.rb', line 164

def self.from_java_connection(java_connection, type_coercion = true)
  dbh = DBI::DatabaseHandle.new(Database.new(java_connection), type_coercion)
  dbh.driver_name = "Jdbc"
  dbh
end

Instance Method Details

#[](attribute) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dbd/jdbc/database.rb', line 134

def [](attribute)
  attribute = attribute.downcase
  check_attribute(attribute)
  case attribute
  when "autocommit" then @connection.getAutoCommit()
  when "isolation", "isolation_level" then @connection.getTransactionIsolation()
  else
    @attributes[attribute]
  end
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#[]=(attribute, value) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dbd/jdbc/database.rb', line 147

def []=(attribute, value)
  attribute = attribute.downcase
  check_attribute(attribute)
  case attribute
  when "autocommit" then @connection.setAutoCommit(value)
  when "isolation", "isolation_level" then @connection.setTransactionIsolation(value)
  else
    @attributes[attribute] = value
  end
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#columns(table) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dbd/jdbc/database.rb', line 110

def columns(table)
  (table,schema,db) = table.split(".").reverse
    
   = @connection.()
  rs = .getColumns(db, schema, table, nil)
  columns = []
  while(rs.next())
    type_name, dbi_type = jdbc_to_dbi_sqltype(rs.getShort(5))
    columns << {
      "name" => rs.getString(4),
      "sql_type" => type_name,
      "type_name" => rs.getString(6),
      "precision" => rs.getInt(7),
      "scale" => rs.getInt(9),
      "default" => rs.getString(13),
      "nullable" => (rs.getInt(11) == 1)
    }
    columns[-1]["dbi_type"] = dbi_type if dbi_type
  end
  return columns
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#commitObject



87
88
89
90
91
# File 'lib/dbd/jdbc/database.rb', line 87

def commit
  @connection.commit()
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#disconnectObject



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

def disconnect
  @connection.rollback unless self["autocommit"]
  @connection.close
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#do(statement, *bindvars) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dbd/jdbc/database.rb', line 62

def do(statement, *bindvars)
  res = nil
  if bindvars.nil? || bindvars.empty?
    stmt = @connection.createStatement()
    begin
      stmt.execute(statement)
    ensure
      stmt.close rescue NativeException
    end
  else
    stmt = execute(statement, *bindvars)
    res = stmt.rows
    stmt.finish
  end
  return res
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#java_connectionObject



160
161
162
# File 'lib/dbd/jdbc/database.rb', line 160

def java_connection
  return @connection
end

#pingObject



81
82
83
84
85
# File 'lib/dbd/jdbc/database.rb', line 81

def ping
  return !@connection.isClosed
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#prepare(sql) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/dbd/jdbc/database.rb', line 52

def prepare(sql)
  if self["allow_scroll"]
    return Statement.new(@connection.prepareStatement(sql,ResultSet::TYPE_SCROLL_INSENSITIVE, ResultSet::CONCUR_READ_ONLY), self["nulltype"], self["allow_scroll"])
  else
    return Statement.new(@connection.prepareStatement(sql), self["nulltype"], self["allow_scroll"])
  end
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#rollbackObject



93
94
95
96
97
# File 'lib/dbd/jdbc/database.rb', line 93

def rollback
  @connection.rollback()
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#tablesObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/dbd/jdbc/database.rb', line 99

def tables
  rs = @connection..getTables(nil, nil, nil, nil)
  tables = []
  while(rs.next())
    tables << rs.getString(3)
  end
  return tables
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end