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

Inherits:
BaseDatabase
  • Object
show all
Includes:
JdbcTypeConversions
Defined in:
lib/dbd/Jdbc.rb

Overview

Driver

Instance Method Summary collapse

Methods included from JdbcTypeConversions

#dbidate_to_jdbcdate, #dbitime_to_jdbctime, #dbitimestamp_to_jdbctimestamp, #jdbc_to_dbi_sqltype, #jdbcdate_to_dbidate, #jdbctime_to_dbitime, #jdbctimestamp_to_dbitimestamp

Constructor Details

#initialize(connection) ⇒ Database

Returns a new instance of Database.



62
63
64
65
66
67
68
# File 'lib/dbd/Jdbc.rb', line 62

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

Instance Method Details

#[](attribute) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/dbd/Jdbc.rb', line 132

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

#[]=(attribute, value) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dbd/Jdbc.rb', line 144

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

#__get_java_connectionObject



156
157
158
# File 'lib/dbd/Jdbc.rb', line 156

def __get_java_connection
  return @connection
end

#columns(table) ⇒ Object



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

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

#commitObject



89
90
91
92
93
# File 'lib/dbd/Jdbc.rb', line 89

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

#disconnectObject



70
71
72
73
74
75
# File 'lib/dbd/Jdbc.rb', line 70

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

#pingObject



83
84
85
86
87
# File 'lib/dbd/Jdbc.rb', line 83

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

#prepare(sql) ⇒ Object



77
78
79
80
81
# File 'lib/dbd/Jdbc.rb', line 77

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

#rollbackObject



95
96
97
98
99
# File 'lib/dbd/Jdbc.rb', line 95

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

#tablesObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/dbd/Jdbc.rb', line 101

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