jdbc-helper 0.2.1 → 0.3.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.
@@ -5,11 +5,13 @@ class TestObjectWrapper < Test::Unit::TestCase
5
5
 
6
6
  def setup
7
7
  @table_name = "tmp_jdbc_helper"
8
+ @procedure_name = "tmp_jdbc_helper_test_proc"
8
9
  end
9
10
 
10
11
  def teardown
11
12
  each_connection do |conn|
12
13
  drop_table conn
14
+ conn.update "drop procedure #{@procedure_name}" rescue nil
13
15
  end
14
16
  end
15
17
 
@@ -39,19 +41,29 @@ class TestObjectWrapper < Test::Unit::TestCase
39
41
  # With symbol
40
42
  assert_kind_of JDBCHelper::ObjectWrapper, conn.table(:some_table)
41
43
  assert_instance_of JDBCHelper::TableWrapper, conn.table(:some_table)
44
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.function(:some_func)
45
+ assert_instance_of JDBCHelper::FunctionWrapper, conn.function(:some_func)
46
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.procedure(:some_proc)
47
+ assert_instance_of JDBCHelper::ProcedureWrapper, conn.procedure(:some_proc)
42
48
  assert_equal 'some_table', conn.table(:some_table).name
43
49
 
44
50
  # With string
45
- assert_kind_of JDBCHelper::ObjectWrapper, conn.table('db.table')
51
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.table('table')
46
52
  assert_instance_of JDBCHelper::TableWrapper, conn.table('db.table')
53
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.function('db.some_func')
54
+ assert_instance_of JDBCHelper::FunctionWrapper, conn.function('some_func')
55
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.procedure('some_proc')
56
+ assert_instance_of JDBCHelper::ProcedureWrapper, conn.procedure('db.some_proc')
47
57
  assert_equal 'db.table', conn.table('db.table').name
48
58
 
49
- # Invalid table name
50
- assert_raise(ArgumentError) { conn.table('table;') }
51
- assert_raise(ArgumentError) { conn.table('table -- ') }
52
- assert_raise(ArgumentError) { conn.table("tab'le") }
53
- assert_raise(ArgumentError) { conn.table('tab"le') }
54
- assert_raise(ArgumentError) { conn.table("tab`le") }
59
+ # Invalid object name
60
+ [ ' ', 'object;', 'object -- ', "obj'ect",
61
+ 'obj"ect', 'obj`ect', 'obje(t', 'ob)ect' ].each do |inv|
62
+ assert_raise(ArgumentError) { conn.table(inv) }
63
+ assert_raise(ArgumentError) { conn.function(inv) }
64
+ assert_raise(ArgumentError) { conn.table(inv.to_sym) }
65
+ assert_raise(ArgumentError) { conn.function(inv.to_sym) }
66
+ end
55
67
 
56
68
  # Abstract class
57
69
  assert_raise(Exception) { JDBCHelper::ObjectWrapper.new(conn, 'table') }
@@ -79,6 +91,36 @@ class TestObjectWrapper < Test::Unit::TestCase
79
91
  end
80
92
  end
81
93
 
94
+ def test_function_wrapper
95
+ each_connection do |conn|
96
+ assert_equal 2.to_i, conn.function(:mod).call(5, 3).to_i
97
+ assert_equal 'yeah', conn.function(:coalesce).call(nil, nil, 'yeah', 'no')
98
+ end
99
+ end
100
+
101
+ def test_procedure_wrapper
102
+ each_connection do |conn|
103
+ create_test_procedure conn, @procedure_name
104
+
105
+ pr = conn.procedure(@procedure_name)
106
+
107
+ result = pr.call 'hello', 10, [100, Fixnum], [Time.now, Time], Float, String
108
+ assert_instance_of Hash, result
109
+ assert_equal 1000, result[3]
110
+ assert_equal 'hello', result[6]
111
+
112
+ if @type != :oracle
113
+ result = pr.call(
114
+ :i1 => 'hello', :i2 => 10,
115
+ :io1 => [100, Fixnum], 'io2' => [Time.now, Time],
116
+ :o1 => Float, 'o2' => String)
117
+ assert_instance_of Hash, result
118
+ assert_equal 1000, result[:io1]
119
+ assert_equal 'hello', result['o2']
120
+ end
121
+ end
122
+ end
123
+
82
124
  def test_insert_count
83
125
  each_connection do |conn|
84
126
  create_table conn
@@ -98,9 +140,10 @@ class TestObjectWrapper < Test::Unit::TestCase
98
140
  end
99
141
  end
100
142
 
101
- # This will fail if the database doesn't support insert ignore syntax
102
143
  def test_insert_ignore
103
144
  each_connection do |conn|
145
+ next unless @type == :mysql
146
+
104
147
  create_table conn
105
148
  table = conn.table(@table_name)
106
149
  params = {
@@ -117,9 +160,10 @@ class TestObjectWrapper < Test::Unit::TestCase
117
160
  end
118
161
  end
119
162
 
120
- # This will fail if the database doesn't support replace syntax
121
163
  def test_replace
122
164
  each_connection do |conn|
165
+ next unless @type == :mysql
166
+
123
167
  create_table conn
124
168
  table = conn.table(@table_name)
125
169
  params = {
@@ -7,7 +7,7 @@ class TestPerformance < Test::Unit::TestCase
7
7
  def setup
8
8
  @table = 'tmp_jdbc_helper'
9
9
  @range = 'aaa'..'aaz'
10
- @count = 10000
10
+ @count = 10000 # Increase this for performance measurement
11
11
  end
12
12
 
13
13
  def teardown
@@ -86,6 +86,26 @@ class TestPerformance < Test::Unit::TestCase
86
86
  end
87
87
  end
88
88
  }.real}"
89
+
90
+ puts "Chaining enumerators with query: #{Benchmark.measure {
91
+ conn.query("select * from #{@table}").each_slice(50) do |slice|
92
+ slice.each do |row|
93
+ @range.each_with_index do |r,i|
94
+ row[i]
95
+ end
96
+ end
97
+ end
98
+ }.real}"
99
+
100
+ puts "Chaining enumerators with enumerate: #{Benchmark.measure {
101
+ conn.enumerate("select * from #{@table}").each_slice(50) do |slice|
102
+ slice.each do |row|
103
+ @range.each_with_index do |r,i|
104
+ row[i]
105
+ end
106
+ end
107
+ end
108
+ }.real}"
89
109
  end
90
110
  end
91
111
 
data/test/test_sql.rb CHANGED
@@ -18,22 +18,36 @@ class TestSQL < Test::Unit::TestCase
18
18
 
19
19
  end
20
20
 
21
+ def test_order_by
22
+ assert_equal "", SQL.order_by()
23
+ assert_equal "", SQL.order_by(nil)
24
+ assert_equal "", SQL.order_by(nil, nil)
25
+ assert_equal "order by a", SQL.order_by(:a)
26
+ assert_equal "order by a", SQL.order_by('a')
27
+ assert_equal "order by a desc", SQL.order_by('a desc')
28
+ assert_equal "order by a asc", SQL.order_by('a asc')
29
+ assert_equal "order by a, b asc, c desc", SQL.order_by(:a, 'b asc', 'c desc')
30
+
31
+ assert_raise(ArgumentError) { SQL.order_by(" -- ") }
32
+ assert_raise(ArgumentError) { SQL.order_by(:a, :b, "c 'd") }
33
+ end
34
+
21
35
  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(" ")
36
+ assert_equal "where a = 1", SQL.where(:a => 1)
37
+ assert_equal "where a = 1.2", SQL.where(:a => 1.2)
38
+ assert_equal "where a = 9999999999999999999", SQL.where(:a => 9999999999999999999)
39
+ assert_equal "where a >= 1 and a <= 2", SQL.where(:a => 1..2)
40
+ assert_equal "where a >= 1 and a < 2", SQL.where(:a => 1...2)
41
+ assert_equal "where a = 'A''s'", SQL.where(:a => "A's")
42
+ assert_equal "where a is null", SQL.where(:a => nil)
43
+ assert_equal "where a is not null", SQL.where(:a => SQL.not_nil)
44
+ assert_equal "where a = sysdate", SQL.where(:a => SQL.expr('sysdate'))
45
+ assert_equal "where sysdate = sysdate", SQL.where(SQL.expr('sysdate') => SQL.expr('sysdate'))
46
+ assert_equal "where a in ('aa', 'bb', 'cc')", SQL.where(:a => %w[aa bb cc])
47
+ assert_equal "where a = 1 and b = 'A''s'", SQL.where(:a => 1, :b => "A's")
48
+ assert_equal "where a = 1 or b = 1", SQL.where("a = 1 or b = 1")
49
+ assert_equal '', SQL.where(nil)
50
+ assert_equal '', SQL.where(" ")
37
51
 
38
52
  # Non-primitive datatypes not implemented (TODO?)
39
53
  assert_raise(NotImplementedError) { SQL.where(:a => Time.now) }
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jdbc-helper
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Junegunn Choi
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-19 00:00:00 +09:00
13
+ date: 2011-05-21 00:00:00 +09:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -46,6 +46,17 @@ dependencies:
46
46
  requirement: *id003
47
47
  prerelease: false
48
48
  type: :development
49
+ - !ruby/object:Gem::Dependency
50
+ name: test-unit
51
+ version_requirements: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.0
57
+ requirement: *id004
58
+ prerelease: false
59
+ type: :development
49
60
  description: A JDBC helper for JRuby/Database developers.
50
61
  email: junegunn.c@gmail.com
51
62
  executables: []
@@ -58,16 +69,22 @@ extra_rdoc_files:
58
69
  files:
59
70
  - lib/jdbc-helper.rb
60
71
  - lib/jdbc-helper/connection.rb
72
+ - lib/jdbc-helper/connection/callable_statement.rb
73
+ - lib/jdbc-helper/connection/parameterized_statement.rb
61
74
  - lib/jdbc-helper/connection/prepared_statement.rb
62
75
  - lib/jdbc-helper/connection/result_set_enumerator.rb
63
76
  - lib/jdbc-helper/connection/row.rb
64
77
  - lib/jdbc-helper/connection/statement_pool.rb
78
+ - lib/jdbc-helper/connection/type_map.rb
65
79
  - lib/jdbc-helper/connector.rb
66
80
  - lib/jdbc-helper/connector/mysql_connector.rb
67
81
  - lib/jdbc-helper/connector/oracle_connector.rb
68
82
  - lib/jdbc-helper/constants.rb
69
- - lib/jdbc-helper/object_wrapper.rb
70
83
  - lib/jdbc-helper/sql.rb
84
+ - lib/jdbc-helper/wrapper/function_wrapper.rb
85
+ - lib/jdbc-helper/wrapper/object_wrapper.rb
86
+ - lib/jdbc-helper/wrapper/procedure_wrapper.rb
87
+ - lib/jdbc-helper/wrapper/table_wrapper.rb
71
88
  - LICENSE.txt
72
89
  - README.rdoc
73
90
  - test/database.yml