jdbc-helper 0.1.3 → 0.2.0
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.rdoc +17 -1
- data/lib/jdbc-helper/connection/prepared_statement.rb +28 -16
- data/lib/jdbc-helper/connection/row.rb +37 -9
- data/lib/jdbc-helper/connection/statement_pool.rb +4 -3
- data/lib/jdbc-helper/connection.rb +57 -19
- data/lib/jdbc-helper/connector/mysql_connector.rb +7 -0
- data/lib/jdbc-helper/connector/oracle_connector.rb +12 -0
- data/lib/jdbc-helper/object_wrapper.rb +126 -0
- data/lib/jdbc-helper/sql.rb +168 -0
- data/lib/jdbc-helper.rb +2 -0
- data/test/database.yml +4 -2
- data/test/helper.rb +22 -0
- data/test/{test_jdbc-helper.rb → test_connection.rb} +5 -39
- data/test/test_connectors.rb +34 -0
- data/test/test_object_wrapper.rb +221 -0
- data/test/test_performance.rb +105 -0
- data/test/test_sql.rb +77 -0
- metadata +94 -89
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'benchmark'
|
3
|
+
|
4
|
+
class TestPerformance < Test::Unit::TestCase
|
5
|
+
include JDBCHelperTestHelper
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@table = 'tmp_jdbc_helper'
|
9
|
+
@range = 'aaa'..'aaz'
|
10
|
+
@count = 10000
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
each_connection do | conn |
|
15
|
+
drop_table conn
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# No assertion here.
|
20
|
+
def test_performance
|
21
|
+
each_connection do | conn |
|
22
|
+
reset conn
|
23
|
+
puts "Normal inserts: #{Benchmark.measure {
|
24
|
+
@count.times do |i|
|
25
|
+
conn.update "insert into #{@table} values (#{@range.map{rand @count}.join ','})"
|
26
|
+
end
|
27
|
+
}.real}"
|
28
|
+
|
29
|
+
puts "Prepared inserts: #{Benchmark.measure {
|
30
|
+
pins = conn.prepare "insert into #{@table} values (#{@range.map{'?'}.join ','})"
|
31
|
+
@count.times do |i|
|
32
|
+
pins.update *(@range.map {rand @count})
|
33
|
+
end
|
34
|
+
pins.close
|
35
|
+
}.real}"
|
36
|
+
|
37
|
+
puts "Prepared inserts (batch & chuck-transactional): #{Benchmark.measure {
|
38
|
+
pins = conn.prepare "insert into #{@table} values (#{@range.map{'?'}.join ','})"
|
39
|
+
(0...@count).each_slice(50) do |slice|
|
40
|
+
conn.transaction do
|
41
|
+
slice.each do |i|
|
42
|
+
pins.add_batch *(@range.map {rand @count})
|
43
|
+
end
|
44
|
+
pins.execute_batch
|
45
|
+
end
|
46
|
+
end
|
47
|
+
pins.close
|
48
|
+
}.real}"
|
49
|
+
|
50
|
+
puts "Inserts with hash: #{Benchmark.measure {
|
51
|
+
table = conn.table(@table)
|
52
|
+
@count.times do |i|
|
53
|
+
table.insert @range.inject({}) { |hash, key| hash[key] = rand; hash }
|
54
|
+
end
|
55
|
+
}.real}"
|
56
|
+
|
57
|
+
puts "Inserts with hash (chunk-transactional): #{Benchmark.measure {
|
58
|
+
table = conn.table(@table)
|
59
|
+
(0...@count).each_slice(50) do |slice|
|
60
|
+
conn.transaction do
|
61
|
+
slice.each do |i|
|
62
|
+
table.insert @range.inject({}) { |hash, key| hash[key] = rand; hash }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
}.real}"
|
67
|
+
|
68
|
+
assert_equal @count * 5, conn.table(@table).count
|
69
|
+
|
70
|
+
conn.query("select * from #{@table}") do |row|
|
71
|
+
# ...
|
72
|
+
end
|
73
|
+
|
74
|
+
puts "Accessing records using dot notation: #{Benchmark.measure {
|
75
|
+
conn.query("select * from #{@table}") do |row|
|
76
|
+
@range.each do |r|
|
77
|
+
row.send r
|
78
|
+
end
|
79
|
+
end
|
80
|
+
}.real}"
|
81
|
+
|
82
|
+
puts "Accessing records using numeric indexes: #{Benchmark.measure {
|
83
|
+
conn.query("select * from #{@table}") do |row|
|
84
|
+
@range.each_with_index do |r,i|
|
85
|
+
row[i]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
}.real}"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def create_table conn
|
93
|
+
conn.update("create table #{@table} (#{@range.map { |e| "#{e} int" }.join(', ')})")
|
94
|
+
end
|
95
|
+
|
96
|
+
def drop_table conn
|
97
|
+
conn.update("drop table #{@table}") rescue nil
|
98
|
+
end
|
99
|
+
|
100
|
+
def reset conn
|
101
|
+
drop_table conn
|
102
|
+
create_table conn
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
data/test/test_sql.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'helper'
|
2
|
+
include JDBCHelper
|
3
|
+
|
4
|
+
class TestSQL < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
end
|
7
|
+
|
8
|
+
def teardown
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_value
|
12
|
+
assert_equal 1, SQL.value(1)
|
13
|
+
assert_equal 1.2, SQL.value(1.2)
|
14
|
+
assert_equal 9999999999999999999, SQL.value(9999999999999999999)
|
15
|
+
assert_equal "'sysdate'", SQL.value('sysdate')
|
16
|
+
assert_equal "'A''s'", SQL.value("A's")
|
17
|
+
assert_equal "sysdate", SQL.value(SQL.expr('sysdate'))
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_where
|
22
|
+
assert_equal "a = 1", SQL.where(:a => 1)
|
23
|
+
assert_equal "a = 1.2", SQL.where(:a => 1.2)
|
24
|
+
assert_equal "a = 9999999999999999999", SQL.where(:a => 9999999999999999999)
|
25
|
+
assert_equal "a >= 1 and a <= 2", SQL.where(:a => 1..2)
|
26
|
+
assert_equal "a >= 1 and a < 2", SQL.where(:a => 1...2)
|
27
|
+
assert_equal "a = 'A''s'", SQL.where(:a => "A's")
|
28
|
+
assert_equal "a is null", SQL.where(:a => nil)
|
29
|
+
assert_equal "a is not null", SQL.where(:a => SQL.not_nil)
|
30
|
+
assert_equal "a = sysdate", SQL.where(:a => SQL.expr('sysdate'))
|
31
|
+
assert_equal "sysdate = sysdate", SQL.where(SQL.expr('sysdate') => SQL.expr('sysdate'))
|
32
|
+
assert_equal "a in ('aa', 'bb', 'cc')", SQL.where(:a => %w[aa bb cc])
|
33
|
+
assert_equal "a = 1 and b = 'A''s'", SQL.where(:a => 1, :b => "A's")
|
34
|
+
assert_equal "a = 1 or b = 1", SQL.where("a = 1 or b = 1")
|
35
|
+
assert_equal nil, SQL.where(nil)
|
36
|
+
assert_equal nil, SQL.where(" ")
|
37
|
+
|
38
|
+
# Non-primitive datatypes not implemented (TODO?)
|
39
|
+
assert_raise(NotImplementedError) { SQL.where(:a => Time.now) }
|
40
|
+
|
41
|
+
# Invalid SQL detection
|
42
|
+
assert_raise(ArgumentError) { SQL.where(:a => SQL.expr(" 'a--b' -- cde")) }
|
43
|
+
assert_raise(ArgumentError) { SQL.where(:a => SQL.expr(" 'aabbb''dd")) }
|
44
|
+
assert_raise(ArgumentError) { SQL.where(:a => SQL.expr(" 'aabbb''dd' /* aaa */")) }
|
45
|
+
assert_raise(ArgumentError) { SQL.where(:a => SQL.expr(' aabbb""" ')) }
|
46
|
+
assert_raise(ArgumentError) { SQL.where(:a => SQL.expr(' aab`bb`` ')) }
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_select
|
50
|
+
assert_equal "select * from a.b", SQL.select('a.b')
|
51
|
+
assert_equal "select * from a.b where a is not null", SQL.select('a.b', :a => SQL.not_nil)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_count
|
55
|
+
assert_equal "select count(*) from a.b", SQL.count('a.b')
|
56
|
+
assert_equal "select count(*) from a.b where a is not null", SQL.count('a.b', :a => SQL.not_nil)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_delete
|
60
|
+
assert_equal "delete from a.b", SQL.delete('a.b')
|
61
|
+
assert_equal "delete from a.b where a is not null", SQL.delete('a.b', :a => SQL.not_nil)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_update
|
65
|
+
assert_equal "update a.b set a = 1, b = 'A''s', c = now()",
|
66
|
+
SQL.update('a.b', :a => 1, :b => "A's", :c => SQL.expr('now()'))
|
67
|
+
|
68
|
+
assert_equal "update a.b set a = 1, b = 'A''s', c = now() where a is not null",
|
69
|
+
SQL.update('a.b', :a => 1, :b => "A's", :c => SQL.expr('now()'), :where => { :a => SQL.not_nil })
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_insert
|
73
|
+
assert_equal "insert into a.b (a, b, c) values (1, 'A''s', null)",
|
74
|
+
SQL.insert('a.b', :a => 1, :b => "A's", :c => nil)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
metadata
CHANGED
@@ -1,108 +1,113 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: jdbc-helper
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.1.3
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
|
7
|
+
authors:
|
8
|
+
- Junegunn Choi
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
12
|
+
date: 2011-05-19 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: &2156345260 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2156345260
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: jeweler
|
27
|
+
requirement: &2156344780 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.5.2
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2156344780
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rcov
|
38
|
+
requirement: &2156344300 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2156344300
|
47
|
+
description: JDBCHelper::Connection object wraps around a JDBC connection and provides
|
48
|
+
much nicer interface to crucial database operations from primitive selects and updates
|
49
|
+
to more complex ones involving batch updates, prepared statements and transactions.
|
50
50
|
email: junegunn.c@gmail.com
|
51
51
|
executables: []
|
52
|
-
|
53
52
|
extensions: []
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
53
|
+
extra_rdoc_files:
|
54
|
+
- LICENSE.txt
|
55
|
+
- README.rdoc
|
56
|
+
files:
|
57
|
+
- lib/jdbc-helper.rb
|
58
|
+
- lib/jdbc-helper/connection.rb
|
59
|
+
- lib/jdbc-helper/connection/prepared_statement.rb
|
60
|
+
- lib/jdbc-helper/connection/result_set_enumerator.rb
|
61
|
+
- lib/jdbc-helper/connection/row.rb
|
62
|
+
- lib/jdbc-helper/connection/statement_pool.rb
|
63
|
+
- lib/jdbc-helper/connector.rb
|
64
|
+
- lib/jdbc-helper/connector/mysql_connector.rb
|
65
|
+
- lib/jdbc-helper/connector/oracle_connector.rb
|
66
|
+
- lib/jdbc-helper/constants.rb
|
67
|
+
- lib/jdbc-helper/object_wrapper.rb
|
68
|
+
- lib/jdbc-helper/sql.rb
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.rdoc
|
71
|
+
- test/database.yml
|
72
|
+
- test/helper.rb
|
73
|
+
- test/test_connection.rb
|
74
|
+
- test/test_connectors.rb
|
75
|
+
- test/test_object_wrapper.rb
|
76
|
+
- test/test_performance.rb
|
77
|
+
- test/test_sql.rb
|
75
78
|
homepage: http://github.com/junegunn/jdbc-helper
|
76
|
-
licenses:
|
77
|
-
|
79
|
+
licenses:
|
80
|
+
- MIT
|
78
81
|
post_install_message:
|
79
82
|
rdoc_options: []
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
86
|
none: false
|
85
|
-
requirements:
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
hash: -1455612613663735519
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
95
|
none: false
|
94
|
-
requirements:
|
95
|
-
|
96
|
-
|
97
|
-
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
98
100
|
requirements: []
|
99
|
-
|
100
101
|
rubyforge_project:
|
101
|
-
rubygems_version: 1.
|
102
|
+
rubygems_version: 1.7.2
|
102
103
|
signing_key:
|
103
104
|
specification_version: 3
|
104
105
|
summary: A JDBC helper for Ruby/Database developers
|
105
|
-
test_files:
|
106
|
-
|
107
|
-
|
108
|
-
|
106
|
+
test_files:
|
107
|
+
- test/database.yml
|
108
|
+
- test/helper.rb
|
109
|
+
- test/test_connection.rb
|
110
|
+
- test/test_connectors.rb
|
111
|
+
- test/test_object_wrapper.rb
|
112
|
+
- test/test_performance.rb
|
113
|
+
- test/test_sql.rb
|