jdbc-wrapper 0.2 → 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -1,13 +1,29 @@
1
- Minimum Requirements
2
- ====================
1
+ = JDBC Wrapper (for JRuby)
2
+
3
+ == Minimum Requirements
3
4
 
4
5
  * Java 1.5
5
6
  * JRuby 1.1 (tested with RC2)
6
7
  * JDBC Drivers
7
8
 
8
- How to Install
9
- ==============
9
+ == How to Install
10
10
 
11
11
  * gem install jdbc-wrapper
12
12
  * get your jdbc driver jars on your classpath
13
13
  (the easier way to do this is drop them in $JRUBY_HOME/lib)
14
+
15
+ == How to Use
16
+
17
+ require 'rubygems'
18
+ require 'jdbc'
19
+
20
+ JDBC::DB.start(:h2,nil,nil,nil,nil,'foo') do |db|
21
+ db.query("CREATE TABLE records (name VARCHAR(80))")
22
+
23
+ db.query("INSERT INTO records (name) VALUES ('foo')")
24
+
25
+ db.query("SELECT * FROM records").each_hash do |row|
26
+ row.each { |key, value| puts "#{key} = #{value}" }
27
+ end
28
+ end
29
+
data/Rakefile CHANGED
@@ -4,20 +4,29 @@ task :test do
4
4
  Rake.run_tests 'test/test_*.rb'
5
5
  end
6
6
 
7
+ require 'rake/rdoctask'
8
+
9
+ Rake::RDocTask.new do |rd|
10
+ rd.main = "README"
11
+ rd.rdoc_files.include("README", "lib/**/*.rb")
12
+ end
13
+
7
14
  require 'rubygems'
8
15
  Gem::manage_gems
9
16
  require 'rake/gempackagetask'
10
17
 
11
18
  gemspec = Gem::Specification.new do |s|
12
19
  s.name = "jdbc-wrapper"
13
- s.version = "0.2"
20
+ s.version = "0.3"
14
21
  s.author = "Larry Myers"
15
- s.email = "larry@larrymyers.com"
22
+ s.email = "larry@larrymyers.com"
23
+ s.homepage = "http://jdbc-wrapper.rubyforge.org/"
24
+ s.rubyforge_project = "JDBC Wrapper"
16
25
  s.platform = Gem::Platform::RUBY
17
26
  s.summary = "A basic JDBC wrapper for JRuby"
18
27
  s.files = FileList["{examples,lib,test}/**/*","Rakefile","README"].to_a
19
28
  s.require_path = "lib"
20
- s.has_rdoc = false
29
+ s.has_rdoc = true
21
30
  end
22
31
 
23
32
  Rake::GemPackageTask.new(gemspec) do |pkg|
data/lib/jdbc.rb CHANGED
@@ -2,6 +2,9 @@ $:.unshift File.join(File.dirname(__FILE__),'.')
2
2
 
3
3
  require 'java'
4
4
 
5
+ # Wraps the java.sql package in
6
+ # a module and makes it available
7
+ # to the JDBC module.
5
8
  module JavaSql
6
9
  include_package 'java.sql'
7
10
  end
data/lib/jdbc/adapters.rb CHANGED
@@ -5,3 +5,7 @@ require 'adapters/h2'
5
5
  require 'adapters/hsqldb'
6
6
  require 'adapters/mysql'
7
7
  require 'adapters/postgresql'
8
+
9
+ # A set of wrapper classes to encapsulate each database engine. Each adapter
10
+ # requires the standard connection parameters on instantiation. Two methods
11
+ # are expected to exist on each adapter: class_name and connection_string.
data/lib/jdbc/db.rb CHANGED
@@ -7,7 +7,15 @@ require 'fileutils'
7
7
  require 'adapters'
8
8
 
9
9
  module JDBC
10
+ # A database connection. The starting point for all interaction with the
11
+ # database. The JDBC drivers are not provided, so you are responsible for
12
+ # making sure the jar file is in the classpath so that JRuby will be able
13
+ # class load it.
10
14
  class DB
15
+ # Creates a new database connection, you are responsible for
16
+ # making sure the connection gets closed. The database engine
17
+ # param should be a symbol. Supported: :h2, :hsqldb, :derby
18
+ # :mysql, :postgresql
11
19
  def initialize(engine, host, port, user, password, schema)
12
20
  adapter = get_adapter(engine, host, port, user, password, schema)
13
21
 
@@ -21,11 +29,13 @@ module JDBC
21
29
  end
22
30
  end
23
31
 
24
- def self.start(db_class, host, port, user, password, database)
32
+ # Takes a block, and provides an open database connection to the
33
+ # block. Will safely close the connection automatically.
34
+ def self.start(engine, host, port, user, password, database)
25
35
  db = nil
26
36
 
27
37
  begin
28
- db = DB.new(db_class, host, port, user, password, database)
38
+ db = DB.new(engine, host, port, user, password, database)
29
39
 
30
40
  yield(db)
31
41
  rescue JavaSql::SQLException => e
@@ -35,6 +45,8 @@ module JDBC
35
45
  end
36
46
  end
37
47
 
48
+ # Takes a valid SQL string. Will return a Result object for a query,
49
+ # or the number of rows affected for an update.
38
50
  def query(sql)
39
51
  stmt = nil
40
52
  result = nil
@@ -55,10 +67,25 @@ module JDBC
55
67
  end
56
68
  end
57
69
 
70
+ # Takes a valid SQL string. Returns a PreparedStatement object if
71
+ # no block is given (you are required to close it). If a block is
72
+ # provided it will pass the statement to the block, and it will
73
+ # automatically close upon block exit.
58
74
  def prepare(sql)
59
- return PreparedStatement.new(@conn.prepareStatement(sql))
75
+ stmt = PreparedStatement.new(@conn.prepareStatement(sql))
76
+
77
+ if block_given?
78
+ yield(stmt)
79
+
80
+ stmt.close
81
+
82
+ return
83
+ end
84
+
85
+ return stmt
60
86
  end
61
87
 
88
+ # Closes the database connection.
62
89
  def close
63
90
  @conn.close unless @conn.nil?
64
91
  end
@@ -1,10 +1,16 @@
1
1
  module JDBC
2
+ # Wraps a java PreparedStatement.
2
3
  class PreparedStatement
4
+ # Takes a java PreparedStatement. You should not have to call this. It
5
+ # will be instantiated by a DB connection.
3
6
  def initialize(stmt)
4
7
  @stmt = stmt
5
8
  @meta_data = stmt.getParameterMetaData
6
9
  end
7
10
 
11
+ # Executes the statement with the provided list of comma separated
12
+ # arguments. Will return a Result for queries and the number of rows
13
+ # affected for updates.
8
14
  def execute(*args)
9
15
  if args.length != @meta_data.getParameterCount
10
16
  raise RuntimeError.new("Got #{args.length} params, " +
@@ -24,6 +30,7 @@ module JDBC
24
30
  return @stmt.getUpdateCount
25
31
  end
26
32
 
33
+ # Closes the statement.
27
34
  def close
28
35
  @stmt.close
29
36
  end
data/lib/jdbc/result.rb CHANGED
@@ -1,11 +1,20 @@
1
1
  module JDBC
2
+ # Result wraps a java ResultSet, and provides methods to both fetch rows
3
+ # one at a time, as well as ruby iterators. All rows are automatically
4
+ # converted to a ruby Array or Hash when they are fetched. Each column is
5
+ # casted to the equivalent ruby object if possible, otherwise it will be a
6
+ # String.
2
7
  class Result
8
+ # Takes a java ResultSet and Statement. You should not have to call this.
9
+ # A Result will usually be instantiated by a DB or PreparedStatement.
3
10
  def initialize(resultSet, statement)
4
11
  @rs = resultSet
5
12
  @stmt = statement
6
13
  @columns = get_rs_meta_data
7
14
  end
8
15
 
16
+ # Fetches the next row and returns it as an Array. Returns nil if no more
17
+ # rows are available and closes the Result.
9
18
  def fetch
10
19
  if @rs.next
11
20
  result = []
@@ -22,6 +31,13 @@ module JDBC
22
31
  return nil
23
32
  end
24
33
 
34
+ # Fetches the next row and returns it as a Hash. The column names are
35
+ # the keys. Returns nil is no more rows are available and closes the Result.
36
+ #
37
+ # Note: All column names are automatically lowercased for consistency
38
+ # since databases differ in behavior on this aspect. (ex: Derby is
39
+ # uppercase, Postgres is lowercase, and Mysql depends on how the table
40
+ # was created.)
25
41
  def fetch_hash
26
42
  if @rs.next
27
43
  result = {}
@@ -38,18 +54,23 @@ module JDBC
38
54
  return nil
39
55
  end
40
56
 
57
+ # Returns each row as an Array to the provided block. Will automatically
58
+ # close the Result after the block exits.
41
59
  def each
42
60
  while(result = fetch)
43
61
  yield(result)
44
62
  end
45
63
  end
46
64
 
65
+ # Returns each row as an Hash to the provided block. The column names
66
+ # are the keys. Will automatically close the Result after the block exits.
47
67
  def each_hash
48
68
  while(result = fetch_hash)
49
69
  yield(result)
50
70
  end
51
71
  end
52
72
 
73
+ # Closes the Result
53
74
  def close
54
75
  @rs.close unless @rs.nil?
55
76
  @stmt.close unless @stmt.nil?
@@ -25,6 +25,8 @@ class TestDB < Test::Unit::TestCase
25
25
 
26
26
  assert_equal('bar', row[0])
27
27
  assert_equal(2, row[1])
28
+
29
+ stmt.close
28
30
  end
29
31
 
30
32
  def test_runtime_error_on_invalid_arg_count
@@ -38,6 +40,8 @@ class TestDB < Test::Unit::TestCase
38
40
  rescue RuntimeError => e
39
41
  assert_equal("Got 2 params, expected 1.", e.message)
40
42
  end
43
+
44
+ stmt.close
41
45
  end
42
46
 
43
47
  def create_and_populate_records_table
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  extensions: []
3
- homepage:
3
+ homepage: http://jdbc-wrapper.rubyforge.org/
4
4
  executables: []
5
5
  version: !ruby/object:Gem::Version
6
- version: !str 0.2
6
+ version: !str 0.3
7
7
  post_install_message:
8
- date: 2008-03-13 04:00:00 +00:00
8
+ date: 2008-03-18 04:00:00 +00:00
9
9
  files:
10
10
  - examples/derby.rb
11
11
  - examples/h2.rb
@@ -31,7 +31,7 @@ rdoc_options: []
31
31
  signing_key:
32
32
  cert_chain: []
33
33
  name: jdbc-wrapper
34
- has_rdoc: false
34
+ has_rdoc: true
35
35
  platform: ruby
36
36
  summary: A basic JDBC wrapper for JRuby
37
37
  default_executable:
@@ -59,5 +59,5 @@ authors:
59
59
  - Larry Myers
60
60
  extra_rdoc_files: []
61
61
  requirements: []
62
- rubyforge_project:
62
+ rubyforge_project: JDBC Wrapper
63
63
  autorequire: