Class: JDBC::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/jdbc/db.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, host, port, user, password, schema) ⇒ DB

Returns a new instance of DB.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jdbc/db.rb', line 11

def initialize(engine, host, port, user, password, schema)
  adapter = get_adapter(engine, host, port, user, password, schema)
  
  begin
    java.lang.Class.forName(adapter.class_name).newInstance()

    @conn = JavaSql::DriverManager.getConnection(
                              adapter.connection_string)
  rescue JavaSql::SQLException => e
    raise RuntimeError.new(e.message)
  end
end

Class Method Details

.start(db_class, host, port, user, password, database) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jdbc/db.rb', line 24

def self.start(db_class, host, port, user, password, database)
  db = nil
  
  begin
      db = DB.new(db_class, host, port, user, password, database)
  
      yield(db)
  rescue JavaSql::SQLException => e
    raise RuntimeError.new(e.message)
  ensure
    db.close unless db.nil?
  end
end

Instance Method Details

#closeObject



62
63
64
# File 'lib/jdbc/db.rb', line 62

def close
  @conn.close unless @conn.nil?
end

#prepare(sql) ⇒ Object



58
59
60
# File 'lib/jdbc/db.rb', line 58

def prepare(sql)
  return PreparedStatement.new(@conn.prepareStatement(sql))
end

#query(sql) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jdbc/db.rb', line 38

def query(sql)
  stmt = nil
  result = nil
  
  begin
    stmt = @conn.createStatement
    
    res = stmt.execute(sql)
    
    if res == false
      return stmt.getUpdateCount
    end
    
    return Result.new(stmt.getResultSet, stmt)
  rescue JavaSql::SQLException => e
    stmt.close unless stmt.nil?
    raise RuntimeError.new(e.message)
  end
end