Class: JDBC::DB

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

Overview

A database connection. The starting point for all interaction with the database. The JDBC drivers are not provided, so you are responsible for making sure the jar file is in the classpath so that JRuby will be able class load it.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new database connection, you are responsible for making sure the connection gets closed. The database engine param should be a symbol. Supported: :h2, :h2_mem, :hsqldb, :hsqldb_mem, :derby, :mysql, :postgresql



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jdbc/db.rb', line 19

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 java.lang.ClassNotFoundException => e
    raise RuntimeError.new(e.message)
  rescue JavaSql::SQLException => e
    raise RuntimeError.new(e.message)
  end
end

Class Method Details

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

Takes a block, and provides an open database connection to the block. Will safely close the connection automatically.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jdbc/db.rb', line 36

def self.start(engine, host, port, user, password, database)
  db = nil
  
  begin
      db = DB.new(engine, 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

Closes the database connection.



91
92
93
# File 'lib/jdbc/db.rb', line 91

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

#prepare(sql) ⇒ Object

Takes a valid SQL string. Returns a PreparedStatement object if no block is given (you are required to close it). If a block is provided it will pass the statement to the block, and it will automatically close upon block exit.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jdbc/db.rb', line 76

def prepare(sql)
  stmt = PreparedStatement.new(@conn.prepareStatement(sql))
  
  if block_given?
    yield(stmt)
    
    stmt.close
    
    return
  end
  
  return stmt
end

#query(sql) ⇒ Object

Takes a valid SQL string. Will return a Result object for a query, or the number of rows affected for an update.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jdbc/db.rb', line 52

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