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.
@@ -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
- - Junegunn Choi
7
+ authors:
8
+ - Junegunn Choi
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-04-22 00:00:00 +09:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
17
- name: bundler
18
- version_requirements: &id001 !ruby/object:Gem::Requirement
19
- none: false
20
- requirements:
21
- - - ~>
22
- - !ruby/object:Gem::Version
23
- version: 1.0.0
24
- requirement: *id001
25
- prerelease: false
26
- type: :development
27
- - !ruby/object:Gem::Dependency
28
- name: jeweler
29
- version_requirements: &id002 !ruby/object:Gem::Requirement
30
- none: false
31
- requirements:
32
- - - ~>
33
- - !ruby/object:Gem::Version
34
- version: 1.5.2
35
- requirement: *id002
36
- prerelease: false
37
- type: :development
38
- - !ruby/object:Gem::Dependency
39
- name: rcov
40
- version_requirements: &id003 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: "0"
46
- requirement: *id003
47
- prerelease: false
48
- type: :development
49
- description: JDBCHelper::Connection object wraps around a JDBC connection and provides much nicer interface to crucial database operations from primitive selects and updates to more complex ones involving batch updates, prepared statements and transactions.
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
- extra_rdoc_files:
56
- - LICENSE.txt
57
- - README.rdoc
58
- files:
59
- - lib/jdbc-helper.rb
60
- - lib/jdbc-helper/connection.rb
61
- - lib/jdbc-helper/connection/prepared_statement.rb
62
- - lib/jdbc-helper/connection/result_set_enumerator.rb
63
- - lib/jdbc-helper/connection/row.rb
64
- - lib/jdbc-helper/connection/statement_pool.rb
65
- - lib/jdbc-helper/connector.rb
66
- - lib/jdbc-helper/connector/mysql_connector.rb
67
- - lib/jdbc-helper/connector/oracle_connector.rb
68
- - lib/jdbc-helper/constants.rb
69
- - LICENSE.txt
70
- - README.rdoc
71
- - test/database.yml
72
- - test/helper.rb
73
- - test/test_jdbc-helper.rb
74
- has_rdoc: true
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
- - MIT
79
+ licenses:
80
+ - MIT
78
81
  post_install_message:
79
82
  rdoc_options: []
80
-
81
- require_paths:
82
- - lib
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
- - !ruby/object:Gem::Version
88
- hash: 2
89
- segments:
90
- - 0
91
- version: "0"
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
- - !ruby/object:Gem::Version
97
- version: "0"
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
98
100
  requirements: []
99
-
100
101
  rubyforge_project:
101
- rubygems_version: 1.5.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
- - test/database.yml
107
- - test/helper.rb
108
- - test/test_jdbc-helper.rb
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