jdbc-helper 0.5.1 → 0.6.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/test/helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
- require 'pry'
3
+ #require 'pry'
4
+
4
5
  begin
5
6
  Bundler.setup(:default, :development)
6
7
  rescue Bundler::BundlerError => e
@@ -71,15 +72,6 @@ module JDBCHelperTestHelper
71
72
  end
72
73
  end
73
74
 
74
- # This is crazy. But Oracle.
75
- def assert_equal *args
76
- if args.first.class == Fixnum
77
- super(args[0].to_s, args[1].to_s)
78
- else
79
- super(*args)
80
- end
81
- end
82
-
83
75
  def each_connection(&block)
84
76
  config.each do | db, conn_info |
85
77
  conn = JDBCHelper::Connection.new(conn_info.reject { |k,v| k == 'database'})
@@ -0,0 +1,141 @@
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 # Increase this for performance measurement
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, info|
22
+ # FIXME
23
+ next unless info['driver'] =~ /mysql/
24
+
25
+ reset conn
26
+ puts "Normal inserts: #{Benchmark.measure {
27
+ @count.times do |i|
28
+ conn.update "insert into #{@table} values (#{@range.map{rand @count}.join ','})"
29
+ end
30
+ }.real}"
31
+
32
+ puts "Normal inserts (batch & chuck-transactional): #{Benchmark.measure {
33
+ (0...@count).each_slice(50) do |slice|
34
+ conn.transaction do
35
+ slice.each do |i|
36
+ conn.add_batch "insert into #{@table} values (#{@range.map{rand @count}.join ','})"
37
+ end
38
+ conn.execute_batch
39
+ end
40
+ end
41
+ }.real}"
42
+
43
+ puts "Prepared inserts: #{Benchmark.measure {
44
+ pins = conn.prepare "insert into #{@table} values (#{@range.map{'?'}.join ','})"
45
+ @count.times do |i|
46
+ pins.update *(@range.map {rand @count})
47
+ end
48
+ pins.close
49
+ }.real}"
50
+
51
+ puts "Prepared inserts (batch & chuck-transactional): #{Benchmark.measure {
52
+ pins = conn.prepare "insert into #{@table} values (#{@range.map{'?'}.join ','})"
53
+ (0...@count).each_slice(50) do |slice|
54
+ conn.transaction do
55
+ slice.each do |i|
56
+ pins.add_batch *(@range.map {rand @count})
57
+ end
58
+ pins.execute_batch
59
+ end
60
+ end
61
+ pins.close
62
+ }.real}"
63
+
64
+ puts "Inserts with hash: #{Benchmark.measure {
65
+ table = conn.table(@table)
66
+ @count.times do |i|
67
+ table.insert @range.inject({}) { |hash, key| hash[key] = rand; hash }
68
+ end
69
+ }.real}"
70
+
71
+ puts "Inserts with hash (batch & chunk-transactional): #{Benchmark.measure {
72
+ table = conn.table(@table)
73
+ btable = table.batch
74
+ (0...@count).each_slice(50) do |slice|
75
+ conn.transaction do
76
+ slice.each do |i|
77
+ btable.insert @range.inject({}) { |hash, key| hash[key] = rand; hash }
78
+ end
79
+ conn.execute_batch
80
+ end
81
+ end
82
+ }.real}"
83
+
84
+ assert_equal @count * 6, conn.table(@table).count
85
+
86
+ conn.query("select * from #{@table}") do |row|
87
+ # ...
88
+ end
89
+
90
+ puts "Accessing records using dot notation: #{Benchmark.measure {
91
+ conn.query("select * from #{@table}") do |row|
92
+ @range.each do |r|
93
+ row.send r
94
+ end
95
+ end
96
+ }.real}"
97
+
98
+ puts "Accessing records using numeric indexes: #{Benchmark.measure {
99
+ conn.query("select * from #{@table}") do |row|
100
+ @range.each_with_index do |r,i|
101
+ row[i]
102
+ end
103
+ end
104
+ }.real}"
105
+
106
+ puts "Chaining enumerators with query: #{Benchmark.measure {
107
+ conn.query("select * from #{@table}").each_slice(50) do |slice|
108
+ slice.each do |row|
109
+ @range.each_with_index do |r,i|
110
+ row[i]
111
+ end
112
+ end
113
+ end
114
+ }.real}"
115
+
116
+ puts "Chaining enumerators with enumerate: #{Benchmark.measure {
117
+ conn.enumerate("select * from #{@table}").each_slice(50) do |slice|
118
+ slice.each do |row|
119
+ @range.each_with_index do |r,i|
120
+ row[i]
121
+ end
122
+ end
123
+ end
124
+ }.real}"
125
+ end
126
+ end
127
+
128
+ def create_table conn
129
+ conn.update("create table #{@table} (#{@range.map { |e| "#{e} int" }.join(', ')})")
130
+ end
131
+
132
+ def drop_table conn
133
+ conn.update("drop table #{@table}") rescue nil
134
+ end
135
+
136
+ def reset conn
137
+ drop_table conn
138
+ create_table conn
139
+ end
140
+ end
141
+
@@ -1,279 +1,331 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestConnection < Test::Unit::TestCase
4
- include JDBCHelperTestHelper
5
-
6
- def setup
7
- end
8
-
9
- def teardown
10
- conn.update "drop table #{TEST_TABLE}" rescue nil
11
- conn.update "drop procedure #{TEST_PROCEDURE}" rescue nil
12
- end
13
-
14
- TEST_TABLE = 'tmp_jdbc_helper_test'
15
- TEST_PROCEDURE = 'tmp_jdbc_helper_test_proc'
16
-
17
- def get_one_two
18
- "
19
- select 1 one, 'two' two from dual
20
- union all
21
- select 1 one, 'two' two from dual
22
- "
23
- end
24
-
25
- def check_one_two(rec)
26
- assert_equal 2, rec.length
27
-
28
- assert_equal 1, rec.one
29
- assert_equal 1, rec[0]
30
- assert_equal 1, rec['one']
31
- assert_equal 1, rec[:one]
32
- assert_equal ['1'], rec[0...1].map(&:to_s)
33
- assert_equal ['1'], rec[0, 1].map(&:to_s)
34
-
35
- assert_equal 'two', rec.two
36
- assert_equal 'two', rec[1]
37
- assert_equal 'two', rec['two']
38
- assert_equal ['two'], rec[1..-1]
39
- assert_equal ['two'], rec[1, 1]
40
-
41
- assert_equal ['1', 'two'], rec[0..1].map(&:to_s)
42
- assert_equal ['1', 'two'], rec[0..-1].map(&:to_s)
43
- assert_equal ['1', 'two'], rec[0, 2].map(&:to_s)
44
-
45
- assert_raise(NoMethodError) { rec.three }
46
- assert_raise(NameError) { rec['three'] }
47
- assert_raise(RangeError) { rec[3] }
48
- end
49
-
50
- def reset_test_table conn
51
- conn.update "drop table #{TEST_TABLE}" rescue nil
52
- cnt = conn.update "
53
- create table #{TEST_TABLE} (
54
- a int primary key,
55
- b varchar(100)
56
- )"
57
- assert_equal 0, cnt
58
- end
59
-
60
- def reset_test_table_ts conn
61
- conn.update "drop table #{TEST_TABLE}" rescue nil
62
- cnt = conn.update "
63
- create table #{TEST_TABLE} (
64
- a timestamp
65
- )"
66
- assert_equal 0, cnt
67
- end
68
-
69
- # ---------------------------------------------------------------
70
-
71
- def test_connect_clone_and_close
72
- config.each do | db, conn_info_org |
73
- 4.times do | i |
74
- conn_info = conn_info_org.reject { |k,v| k == 'database' }
75
-
76
- # With or without timeout parameter
77
- conn_info['timeout'] = 60 if i % 2 == 1
78
-
79
- # Can connect with hash with symbol keys?
80
- conn_info.keys.each do | str_key |
81
- conn_info[str_key.to_sym] = conn_info.delete str_key
82
- end if i % 2 == 0
83
-
84
- # Integers will be converted to String
85
- #conn_info['defaultRowPrefetch'] = 1000
86
-
87
- conn = JDBCHelper::Connection.new(conn_info)
88
- assert_equal(conn.closed?, false)
89
- assert_equal(conn.driver, conn_info[:driver] || conn_info['driver'])
90
- assert_equal(conn.url, conn_info[:url] || conn_info['url'])
4
+ include JDBCHelperTestHelper
5
+
6
+ def setup
7
+ end
8
+
9
+ def teardown
10
+ conn.update "drop table #{TEST_TABLE}" rescue nil
11
+ conn.update "drop procedure #{TEST_PROCEDURE}" rescue nil
12
+ end
13
+
14
+ TEST_TABLE = 'tmp_jdbc_helper_test'
15
+ TEST_PROCEDURE = 'tmp_jdbc_helper_test_proc'
16
+
17
+ def get_one_two
18
+ "
19
+ select 1 one, 'two' two from dual
20
+ union all
21
+ select 1 one, 'two' two from dual
22
+ "
23
+ end
24
+
25
+ def check_one_two(rec)
26
+ assert_equal 2, rec.length
27
+
28
+ assert_equal 1, rec.one
29
+ assert_equal 1, rec[0]
30
+ assert_equal 1, rec['one']
31
+ assert_equal 1, rec[:one]
32
+ assert_equal [1], rec[0...1]
33
+ assert_equal [1], rec[0, 1]
34
+
35
+ assert_equal 'two', rec.two
36
+ assert_equal 'two', rec[1]
37
+ assert_equal 'two', rec['two']
38
+ assert_equal ['two'], rec[1..-1]
39
+ assert_equal ['two'], rec[1, 1]
40
+
41
+ assert_equal [1, 'two'], rec[0..1]
42
+ assert_equal [1, 'two'], rec[0..-1]
43
+ assert_equal [1, 'two'], rec[0, 2]
44
+
45
+ # FIXME: Exponent field
46
+ assert rec.join('--') =~ /--two$/
47
+
48
+ assert_raise(NoMethodError) { rec.three }
49
+ assert_raise(NameError) { rec['three'] }
50
+ assert_raise(NameError) { rec[:three] }
51
+ assert_raise(RangeError) { rec[3] }
52
+ end
53
+
54
+ def reset_test_table conn
55
+ conn.update "drop table #{TEST_TABLE}" rescue nil
56
+ cnt = conn.update "
57
+ create table #{TEST_TABLE} (
58
+ a int primary key,
59
+ b varchar(100)
60
+ )"
61
+ assert_equal 0, cnt
62
+ end
63
+
64
+ def reset_test_table_ts conn
65
+ conn.update "drop table #{TEST_TABLE}" rescue nil
66
+ cnt = conn.update "
67
+ create table #{TEST_TABLE} (
68
+ a timestamp
69
+ )"
70
+ assert_equal 0, cnt
71
+ end
72
+
73
+ # ---------------------------------------------------------------
74
+
75
+ def test_invalid_driver
76
+ assert_raise(NameError) {
77
+ JDBCHelper::Connection.new(:driver => 'xxx', :url => 'localhost')
78
+ }
79
+ end
80
+
81
+ def test_connect_clone_and_close
82
+ config.each do | db, conn_info_org |
83
+ 4.times do | i |
84
+ conn_info = conn_info_org.reject { |k,v| k == 'database' }
85
+
86
+ # With or without timeout parameter
87
+ conn_info['timeout'] = 60 if i % 2 == 1
88
+
89
+ # Can connect with hash with symbol keys?
90
+ conn_info.keys.each do | str_key |
91
+ conn_info[str_key.to_sym] = conn_info.delete str_key
92
+ end if i % 2 == 0
93
+
94
+ # Integers will be converted to String
95
+ #conn_info['defaultRowPrefetch'] = 1000
96
+
97
+ conn = JDBCHelper::Connection.new(conn_info)
98
+ assert_equal(conn.closed?, false)
99
+ assert_equal(conn.driver, conn_info[:driver] || conn_info['driver'])
100
+ assert_equal(conn.url, conn_info[:url] || conn_info['url'])
91
101
 
92
102
  conn.fetch_size = 100
93
103
  assert_equal 100, conn.fetch_size
94
104
 
95
- conn.close
96
- assert_equal(conn.closed?, true)
97
- [ :query, :update, :add_batch, :prepare ].each do | met |
98
- assert_raise(RuntimeError) { conn.send met, "A" }
99
- end
100
- [ :execute_batch, :clear_batch ].each do | met |
101
- assert_raise(RuntimeError) { conn.send met }
102
- end
105
+ conn.close
106
+ assert_equal(conn.closed?, true)
107
+ [ :query, :update, :add_batch, :prepare ].each do | met |
108
+ assert_raise(RuntimeError) { conn.send met, "A" }
109
+ end
110
+ [ :execute_batch, :clear_batch ].each do | met |
111
+ assert_raise(RuntimeError) { conn.send met }
112
+ end
103
113
 
104
114
  new_conn = conn.clone
105
115
  assert new_conn.java_obj != conn.java_obj
106
- assert new_conn.closed? == false
116
+ assert new_conn.closed? == false
107
117
  assert_equal 100, new_conn.fetch_size
108
118
  new_conn.close
109
119
 
110
- # initialize with execution block
111
- conn = JDBCHelper::Connection.new(conn_info) do | c |
120
+ # initialize with execution block
121
+ conn = JDBCHelper::Connection.new(conn_info) do | c |
112
122
  c2 = c.clone
113
123
  assert c2.java_obj != c.java_obj
114
124
 
115
- c.query('select 1 from dual')
116
- c2.query('select 1 from dual')
117
- assert_equal c.closed?, false
125
+ c.query('select 1 from dual')
126
+ c2.query('select 1 from dual')
127
+ assert_equal c.closed?, false
118
128
 
119
129
  c2.close
120
- assert c2.closed?
121
- end
122
- assert conn.closed?
123
- end
124
- end
125
- end
126
-
127
- def test_fetch_size
128
- each_connection do | conn |
129
- assert_nil conn.fetch_size
130
- conn.fetch_size = 10
131
- assert_equal 10, conn.fetch_size
132
- conn.set_fetch_size 20
133
- assert_equal 20, conn.fetch_size
134
-
135
- # No way to confirm whether if this is really working as it's supposed to be.
136
- end
137
- end
138
-
139
- def test_query_enumerate
140
- each_connection do | conn |
141
- # Query without a block => Array
142
- query_result = conn.query get_one_two
143
- assert query_result.is_a? Array
144
- assert_equal 2, query_result.length
145
- check_one_two(query_result.first)
146
-
147
- # Query with a block
148
- count = 0
149
- conn.query(get_one_two) do | row |
150
- check_one_two row
151
- count += 1
152
- end
153
- assert_equal 2, count
154
-
155
- # Enumerate
156
- enum = conn.enumerate(get_one_two)
157
- assert enum.is_a? Enumerable
158
- assert enum.closed? == false
159
- a = enum.to_a
160
- assert_equal 2, a.length
161
- check_one_two a.first
162
- assert enum.closed? == true
163
- end
164
- end
165
-
166
- def test_update_batch
167
- each_connection do | conn |
168
- reset_test_table conn
169
- count = 100
170
-
171
- iq = lambda do | i |
172
- "insert into #{TEST_TABLE} values (#{i}, 'A')"
173
- end
130
+ assert c2.closed?
131
+ end
132
+ assert conn.closed?
133
+ end
134
+ end
135
+ end
136
+
137
+ def test_fetch_size
138
+ each_connection do | conn |
139
+ assert_nil conn.fetch_size
140
+ conn.fetch_size = 10
141
+ assert_equal 10, conn.fetch_size
142
+ conn.set_fetch_size 20
143
+ assert_equal 20, conn.fetch_size
144
+
145
+ # No way to confirm whether if this is really working as it's supposed to be.
146
+ end
147
+ end
148
+
149
+ def test_query_enumerate
150
+ each_connection do | conn |
151
+ # Query without a block => Array
152
+ query_result = conn.query get_one_two
153
+ assert query_result.is_a? Array
154
+ assert_equal 2, query_result.length
155
+ check_one_two(query_result.first)
156
+ assert_equal query_result.first, query_result.last
157
+
158
+ # Query with a block
159
+ count = 0
160
+ conn.query(get_one_two) do | row |
161
+ check_one_two row
162
+ count += 1
163
+ end
164
+ assert_equal 2, count
165
+
166
+ # Enumerate
167
+ enum = conn.enumerate(get_one_two)
168
+ assert enum.is_a? Enumerable
169
+ assert enum.closed? == false
170
+ a = enum.to_a
171
+ assert_equal 2, a.length
172
+ check_one_two a.first
173
+ assert enum.closed? == true
174
+ end
175
+ end
176
+
177
+ def test_enumerate_errors
178
+ each_connection do |conn|
179
+ # On error, Statement object must be returned to the StatementPool
180
+ (JDBCHelper::Constants::MAX_STATEMENT_NESTING_LEVEL * 2).times do |i|
181
+ conn.enumerate('xxx') rescue nil
182
+ end
183
+ assert_equal 'OK', conn.query("select 'OK' from dual")[0][0]
184
+ end
185
+ end
186
+
187
+ def test_deep_nesting
188
+ nest = lambda { |str, lev|
189
+ if lev > 0
190
+ "conn.query('select 1 from dual') do |r#{lev}|
191
+ #{nest.call str, lev - 1}
192
+ end"
193
+ else
194
+ str
195
+ end
196
+ }
197
+ (1..(JDBCHelper::Constants::MAX_STATEMENT_NESTING_LEVEL + 1)).each do |level|
198
+ each_connection do |conn|
199
+ str = nest.call('assert true', level)
200
+ if level > JDBCHelper::Constants::MAX_STATEMENT_NESTING_LEVEL
201
+ assert_raise(RuntimeError) { eval str }
202
+ else
203
+ eval str
204
+ end
205
+ end
206
+ end
207
+ end
208
+
209
+ def test_update_batch
210
+ each_connection do | conn |
211
+ reset_test_table conn
212
+ count = 100
213
+
214
+ iq = lambda do | i |
215
+ "insert into #{TEST_TABLE} values (#{i}, 'A')"
216
+ end
174
217
  ins1 = conn.prepare "insert into #{TEST_TABLE} values (? + #{count}, 'B')"
175
218
  ins2 = conn.prepare "insert into #{TEST_TABLE} values (? + #{count * 2}, 'C')"
176
219
 
177
- # update
178
- assert_equal 1, conn.update(iq.call 0)
179
- assert_equal 1, conn.prev_stat.success_count
220
+ # update
221
+ assert_equal 1, conn.update(iq.call 0)
222
+ assert_equal 1, conn.prev_stat.success_count
180
223
 
181
- # add_batch execute_batch
182
- reset_test_table conn
224
+ # add_batch execute_batch
225
+ reset_test_table conn
183
226
 
184
- count.times do | p |
185
- conn.add_batch iq.call(p)
227
+ count.times do | p |
228
+ conn.add_batch iq.call(p)
186
229
  ins1.add_batch p
187
230
  ins2.add_batch p
188
- end
189
- conn.execute_batch
190
- assert_equal count * 3, conn.query("select count(*) from #{TEST_TABLE}")[0][0]
231
+ end
232
+ conn.execute_batch
233
+ assert_equal count * 3, conn.table(TEST_TABLE).count
191
234
 
192
- # add_batch clear_batch
193
- reset_test_table conn
235
+ # add_batch clear_batch
236
+ reset_test_table conn
194
237
 
195
- count.times do | p |
196
- conn.add_batch iq.call(p)
238
+ count.times do | p |
239
+ conn.add_batch iq.call(p)
197
240
  ins1.add_batch p
198
241
  ins2.add_batch p
199
- end
200
- conn.clear_batch
242
+ end
243
+ conn.clear_batch
201
244
  # Already cleared, no effect
202
245
  ins1.execute_batch
203
246
  ins2.execute_batch
204
- assert_equal 0, conn.query("select count(*) from #{TEST_TABLE}")[0][0]
205
- end
206
- end
207
-
208
- def test_prepared_query_enumerate
209
- each_connection do | conn |
210
- sel = conn.prepare get_one_two
211
- assert sel.closed? == false
212
- assert_equal conn.prepared_statements.first, sel
213
-
214
- # Fetch size
215
- assert_nil conn.fetch_size
216
- assert_nil sel.fetch_size
217
- conn.fetch_size = 100
218
- sel.fetch_size = 10
219
- assert_equal 100, conn.fetch_size
220
- assert_equal 10, sel.fetch_size
221
- sel.set_fetch_size 20
222
- assert_equal 100, conn.fetch_size
223
- assert_equal 20, sel.fetch_size
224
-
225
- # Query without a block => Array
226
- query_result = sel.query
227
- assert query_result.is_a? Array
228
- assert_equal 2, query_result.length
229
- check_one_two(query_result.first)
230
-
231
- # Query with a block
232
- count = 0
233
- sel.query do | row |
234
- check_one_two row
235
- count += 1
236
- end
237
- assert_equal 2, count
238
-
239
- # Enumerate
240
- enum = sel.enumerate
241
- assert enum.is_a? Enumerable
242
- assert enum.closed? == false
243
- a = enum.to_a
244
- assert_equal 2, a.length
245
- check_one_two a.first
246
- assert enum.closed? == true
247
-
248
- sel.close
249
- assert sel.closed?
250
- [ :query, :update, :add_batch, :execute_batch, :clear_batch ].each do | met |
251
- assert_raise(RuntimeError) { sel.send met }
252
- end
253
- end
254
- end
255
-
256
- def test_prepared_update_batch
257
- each_connection do | conn |
258
- reset_test_table conn
259
- ins = conn.prepare "insert into #{TEST_TABLE} values (?, ?)"
260
- assert_equal 2, ins.parameter_count
247
+ assert_equal 0, conn.table(TEST_TABLE).count
248
+ end
249
+ end
250
+
251
+ def test_prepared_query_enumerate
252
+ each_connection do | conn |
253
+ 2.times do |iter|
254
+ sel = conn.prepare get_one_two
255
+ assert sel.closed? == false
256
+ assert_equal conn.prepared_statements.first, sel
257
+
258
+ # Fetch size
259
+ if iter == 0
260
+ assert_nil conn.fetch_size
261
+ assert_nil sel.fetch_size
262
+ end
263
+ conn.fetch_size = 100
264
+ sel.fetch_size = 10
265
+ assert_equal 100, conn.fetch_size
266
+ assert_equal 10, sel.fetch_size
267
+ sel.set_fetch_size 20
268
+ assert_equal 100, conn.fetch_size
269
+ assert_equal 20, sel.fetch_size
270
+
271
+ # Query without a block => Array
272
+ query_result = sel.query
273
+ assert query_result.is_a? Array
274
+ assert_equal 2, query_result.length
275
+ check_one_two(query_result.first)
276
+
277
+ # Query with a block
278
+ count = 0
279
+ sel.query do | row |
280
+ check_one_two row
281
+ count += 1
282
+ end
283
+ assert_equal 2, count
284
+
285
+ # Enumerate
286
+ enum = sel.enumerate
287
+ assert enum.is_a? Enumerable
288
+ assert enum.closed? == false
289
+ a = enum.to_a
290
+ assert_equal 2, a.length
291
+ check_one_two a.first
292
+ assert enum.closed? == true
293
+
294
+ if iter == 0
295
+ sel.close
296
+ else
297
+ sel.java_obj.close
298
+ end
299
+
300
+ assert sel.closed?
301
+ [ :query, :update, :add_batch, :execute_batch, :clear_batch ].each do | met |
302
+ assert_raise(RuntimeError) { sel.send met }
303
+ end
304
+ end#times
305
+ end#each_connection
306
+ end
307
+
308
+ def test_prepared_update_batch
309
+ each_connection do | conn |
310
+ reset_test_table conn
311
+ ins = conn.prepare "insert into #{TEST_TABLE} values (?, ?)"
312
+ assert_equal 2, ins.parameter_count
261
313
  assert_equal conn.prepared_statements.first, ins
262
314
 
263
- count = 100
315
+ count = 100
264
316
 
265
- # update
266
- assert ins.closed? == false
267
- assert_equal 1, ins.update(0, 'A')
268
- assert_equal 1, conn.prev_stat.success_count
269
- ins.close
317
+ # update
318
+ assert ins.closed? == false
319
+ assert_equal 1, ins.update(0, 'A')
320
+ assert_equal 1, conn.prev_stat.success_count
321
+ ins.close
270
322
  assert_equal 0, conn.prepared_statements.length
271
323
 
272
- # add_batch execute_batch
324
+ # add_batch execute_batch
273
325
  2.times do |iter|
274
326
  reset_test_table conn
275
327
  ins = conn.prepare "insert into #{TEST_TABLE} values (?, ?)"
276
- assert_equal conn.prepared_statements.first, ins
328
+ assert_equal conn.prepared_statements.first, ins if iter == 0
277
329
 
278
330
  count.times do | p |
279
331
  ins.add_batch(p + 1, 'A')
@@ -283,15 +335,16 @@ class TestConnection < Test::Unit::TestCase
283
335
  else
284
336
  conn.execute_batch
285
337
  end
286
- assert_equal count, conn.query("select count(*) from #{TEST_TABLE}")[0][0]
338
+ assert_equal count, conn.table(TEST_TABLE).count
287
339
  ins.close
288
- assert_equal 0, conn.prepared_statements.length
340
+ # 1 for count
341
+ assert_equal 1, conn.prepared_statements.length
289
342
  end
290
343
 
291
- # add_batch clear_batch
292
- reset_test_table conn
293
- ins = conn.prepare "insert into #{TEST_TABLE} values (?, ?)"
294
- assert_equal conn.prepared_statements.first, ins
344
+ # add_batch clear_batch
345
+ reset_test_table conn
346
+ ins = conn.prepare "insert into #{TEST_TABLE} values (?, ?)"
347
+ assert_equal conn.prepared_statements.last, ins # count is first
295
348
 
296
349
  # clear_batch
297
350
  2.times do |iter|
@@ -303,154 +356,155 @@ class TestConnection < Test::Unit::TestCase
303
356
  else
304
357
  conn.clear_batch
305
358
  end
306
- assert_equal 0, conn.query("select count(*) from #{TEST_TABLE}")[0][0]
359
+ assert_equal 0, conn.table(TEST_TABLE).count
307
360
  end
308
361
 
309
- # close closed?
310
- assert ins.closed? == false
311
- ins.close
312
- assert_equal 0, conn.prepared_statements.length
313
- assert ins.closed?
314
- [ :query, :update, :add_batch, :execute_batch, :clear_batch ].each do | met |
315
- assert_raise(RuntimeError) { ins.send met }
316
- end
317
- end
318
- end
319
-
320
- def test_transaction
321
- each_connection do | conn |
322
- reset_test_table conn
323
- count = 100
324
-
325
- 3.times do | i |
326
- sum = 0
327
- conn.update "delete from #{TEST_TABLE}"
328
- conn.transaction do | tx |
329
- count.times.each_slice(10) do | slice |
330
- slice.each do | p |
331
- conn.add_batch("insert into #{TEST_TABLE} values (#{p}, 'xxx')")
332
- sum += p
333
- end
334
- conn.execute_batch
335
- end
336
- result = conn.query("select count(*), sum(a) from #{TEST_TABLE}").first
337
-
338
- assert_equal count, result.first
339
- assert_equal sum, result.last.to_i
340
-
341
- case i
342
- when 0 then tx.rollback
343
- when 1 then tx.commit
344
- else
345
- nil # committed implicitly
346
- end
347
-
348
- flunk 'This should not be executed' if i < 2
349
- end
350
-
351
- assert_equal (i == 0 ? 0 : count),
352
- conn.query("select count(*) from #{TEST_TABLE}").first.first
353
- end
354
- end
355
- end
356
-
357
- def test_setter_timestamp
358
- each_connection do | conn |
359
- # Java timestamp
360
- reset_test_table_ts conn
361
- ts = java.sql.Timestamp.new(Time.now.to_i * 1000)
362
- conn.prepare("insert into #{TEST_TABLE} values (?)").update(ts)
363
- assert_equal ts, conn.query("select * from #{TEST_TABLE}")[0][0]
364
-
365
- # Ruby time
366
- reset_test_table_ts conn
367
- ts = Time.now
368
- conn.prepare("insert into #{TEST_TABLE} values (?)").update(ts)
369
- got = conn.query("select * from #{TEST_TABLE}")[0][0]
370
- assert_equal ts.to_i * 1000, got.getTime
371
- end
372
- end
373
-
374
- # Conditional testing is bad, but
375
- # Oracle and MySQL behave differently.
376
- def test_callable_statement
377
- each_connection do | conn |
378
- # Creating test procedure (Defined in JDBCHelperTestHelper)
379
- create_test_procedure conn, TEST_PROCEDURE
380
-
381
- # Array parameter
382
- cstmt_ord = conn.prepare_call "{call #{TEST_PROCEDURE}(?, ?, ?, ?, ?, ?, ?)}"
383
- result = cstmt_ord.call('hello', 10, [100, Fixnum], [Time.now, Time], nil, Float, String)
384
- assert_instance_of Hash, result
385
- assert_equal 1000, result[3]
386
- assert_equal 'hello', result[7]
387
-
388
- # Hash parameter
389
- cstmt_name = conn.prepare_call(case @type
390
- when :oracle
391
- "{call #{TEST_PROCEDURE}(:i1, :i2, :io1, :io2, :n1, :o1, :o2)}"
392
- else
393
- "{call #{TEST_PROCEDURE}(?, ?, ?, ?, ?, ?, ?)}"
394
- end)
395
- result = cstmt_name.call(
396
- :i1 => 'hello', :i2 => 10,
397
- :io1 => [100, Fixnum], 'io2' => [Time.now, Time],
398
- :n1 => nil,
399
- :o1 => Float, 'o2' => String)
400
- assert_instance_of Hash, result
401
- assert_equal 1000, result[:io1]
402
- assert_equal 'hello', result['o2']
403
-
404
- # Invalid parameters
405
- #assert_raise(NativeException) { cstmt_ord.call 1 }
406
- assert_raise(ArgumentError) { cstmt_ord.call({}, {}) }
407
- assert_raise(NativeException) { cstmt_name.call 1 }
408
- assert_raise(ArgumentError) { cstmt_name.call({}, {}) }
409
-
410
- # Close
411
- [ cstmt_ord, cstmt_name ].each do | cstmt |
412
- assert_equal false, cstmt.closed?
413
- cstmt.close
414
- assert_equal true, cstmt.closed?
415
- assert_raise(RuntimeError) { cstmt.call }
416
- end
417
-
418
- # Data truncated for column 'io1' at row 2. WHY?
419
- # http://www.herongyang.com/JDBC/MySQL-CallableStatement-INOUT-Parameters.html
420
- if @type != :mysql
421
- cstmt_ord = conn.prepare_call "{call #{TEST_PROCEDURE}('howdy', ?, ?, ?, ?, ?, ?)}"
422
- cstmt_name = conn.prepare_call(case @type
423
- when :oracle
424
- "{call #{TEST_PROCEDURE}('howdy', :i2, :io1, :io2, :n1, :o1, :o2)}"
425
- else
426
- "{call #{TEST_PROCEDURE}('howdy', ?, ?, ?, ?, ?, ?)}"
427
- end)
428
- # Hash parameter
429
- result = cstmt_name.call(
430
- #:i1 => 'hello',
431
- :i2 => 10,
432
- :io1 => [100, Fixnum], 'io2' => [Time.now, Time],
433
- :n1 => nil,
434
- :o1 => Float, 'o2' => String)
435
- assert_instance_of Hash, result
436
- assert_equal 1000, result[:io1]
437
- assert_equal 'howdy', result['o2']
438
-
439
- # Array parameter
440
- result = cstmt_ord.call(10, [100, Fixnum], [Time.now, Time], nil, Float, String)
441
- assert_instance_of Hash, result
442
- assert_equal 1000, result[2]
443
- assert_equal 'howdy', result[6]
444
-
445
- # Close
446
- [ cstmt_ord, cstmt_name ].each do | cstmt |
447
- assert_equal false, cstmt.closed?
448
- cstmt.close
449
- assert_equal true, cstmt.closed?
450
- assert_raise(RuntimeError) { cstmt.call }
451
- end
452
- end
453
- end
454
- end
362
+ # close closed?
363
+ assert ins.closed? == false
364
+ ins.close
365
+ # 1 for count
366
+ assert_equal 1, conn.prepared_statements.length
367
+ assert ins.closed?
368
+ [ :query, :update, :add_batch, :execute_batch, :clear_batch ].each do | met |
369
+ assert_raise(RuntimeError) { ins.send met }
370
+ end
371
+ end
372
+ end
373
+
374
+ def test_transaction
375
+ each_connection do | conn |
376
+ reset_test_table conn
377
+ count = 100
378
+
379
+ 3.times do | i |
380
+ sum = 0
381
+ conn.update "delete from #{TEST_TABLE}"
382
+ conn.transaction do | tx |
383
+ count.times.each_slice(10) do | slice |
384
+ slice.each do | p |
385
+ conn.add_batch("insert into #{TEST_TABLE} values (#{p}, 'xxx')")
386
+ sum += p
387
+ end
388
+ conn.execute_batch
389
+ end
390
+ result = conn.query("select count(*), sum(a) from #{TEST_TABLE}").first
391
+
392
+ assert_equal count, result.first
393
+ assert_equal sum, result.last
394
+
395
+ case i
396
+ when 0 then tx.rollback
397
+ when 1 then tx.commit
398
+ else
399
+ nil # committed implicitly
400
+ end
401
+
402
+ flunk 'This should not be executed' if i < 2
403
+ end
404
+
405
+ assert_equal (i == 0 ? 0 : count),
406
+ conn.table(TEST_TABLE).count
407
+ end
408
+ end
409
+ end
410
+
411
+ def test_setter_timestamp
412
+ each_connection do | conn |
413
+ # Java timestamp
414
+ reset_test_table_ts conn
415
+ ts = java.sql.Timestamp.new(Time.now.to_i * 1000)
416
+ conn.prepare("insert into #{TEST_TABLE} values (?)").update(ts)
417
+ assert_equal ts, conn.query("select * from #{TEST_TABLE}")[0][0]
418
+
419
+ # Ruby time
420
+ reset_test_table_ts conn
421
+ ts = Time.now
422
+ conn.prepare("insert into #{TEST_TABLE} values (?)").update(ts)
423
+ got = conn.query("select * from #{TEST_TABLE}")[0][0]
424
+ assert [ts.to_i * 1000, (ts.to_f * 1000).to_i].include?(got.getTime)
425
+ end
426
+ end
427
+
428
+ # Conditional testing is bad, but
429
+ # Oracle and MySQL behave differently.
430
+ def test_callable_statement
431
+ each_connection do | conn |
432
+ # Creating test procedure (Defined in JDBCHelperTestHelper)
433
+ create_test_procedure conn, TEST_PROCEDURE
434
+
435
+ # Array parameter
436
+ cstmt_ord = conn.prepare_call "{call #{TEST_PROCEDURE}(?, ?, ?, ?, ?, ?, ?)}"
437
+ result = cstmt_ord.call('hello', 10, [100, Fixnum], [Time.now, Time], nil, Float, String)
438
+ assert_instance_of Hash, result
439
+ assert_equal 1000, result[3]
440
+ assert_equal 'hello', result[7]
441
+
442
+ # Hash parameter
443
+ cstmt_name = conn.prepare_call(case @type
444
+ when :oracle
445
+ "{call #{TEST_PROCEDURE}(:i1, :i2, :io1, :io2, :n1, :o1, :o2)}"
446
+ else
447
+ "{call #{TEST_PROCEDURE}(?, ?, ?, ?, ?, ?, ?)}"
448
+ end)
449
+ result = cstmt_name.call(
450
+ :i1 => 'hello', :i2 => 10,
451
+ :io1 => [100, Fixnum], 'io2' => [Time.now, Time],
452
+ :n1 => nil,
453
+ :o1 => Float, 'o2' => String)
454
+ assert_instance_of Hash, result
455
+ assert_equal 1000, result[:io1]
456
+ assert_equal 'hello', result['o2']
457
+
458
+ # Invalid parameters
459
+ #assert_raise(NativeException) { cstmt_ord.call 1 }
460
+ assert_raise(ArgumentError) { cstmt_ord.call({}, {}) }
461
+ assert_raise(NativeException) { cstmt_name.call 1 }
462
+ assert_raise(ArgumentError) { cstmt_name.call({}, {}) }
463
+
464
+ # Close
465
+ [ cstmt_ord, cstmt_name ].each do | cstmt |
466
+ assert_equal false, cstmt.closed?
467
+ cstmt.close
468
+ assert_equal true, cstmt.closed?
469
+ assert_raise(RuntimeError) { cstmt.call }
470
+ end
471
+
472
+ # Data truncated for column 'io1' at row 2. WHY?
473
+ # http://www.herongyang.com/JDBC/MySQL-CallableStatement-INOUT-Parameters.html
474
+ if @type != :mysql
475
+ cstmt_ord = conn.prepare_call "{call #{TEST_PROCEDURE}('howdy', ?, ?, ?, ?, ?, ?)}"
476
+ cstmt_name = conn.prepare_call(case @type
477
+ when :oracle
478
+ "{call #{TEST_PROCEDURE}('howdy', :i2, :io1, :io2, :n1, :o1, :o2)}"
479
+ else
480
+ "{call #{TEST_PROCEDURE}('howdy', ?, ?, ?, ?, ?, ?)}"
481
+ end)
482
+ # Hash parameter
483
+ result = cstmt_name.call(
484
+ #:i1 => 'hello',
485
+ :i2 => 10,
486
+ :io1 => [100, Fixnum], 'io2' => [Time.now, Time],
487
+ :n1 => nil,
488
+ :o1 => Float, 'o2' => String)
489
+ assert_instance_of Hash, result
490
+ assert_equal 1000, result[:io1]
491
+ assert_equal 'howdy', result['o2']
492
+
493
+ # Array parameter
494
+ result = cstmt_ord.call(10, [100, Fixnum], [Time.now, Time], nil, Float, String)
495
+ assert_instance_of Hash, result
496
+ assert_equal 1000, result[2]
497
+ assert_equal 'howdy', result[6]
498
+
499
+ # Close
500
+ [ cstmt_ord, cstmt_name ].each do | cstmt |
501
+ assert_equal false, cstmt.closed?
502
+ cstmt.close
503
+ assert_equal true, cstmt.closed?
504
+ assert_raise(RuntimeError) { cstmt.call }
505
+ end
506
+ end
507
+ end
508
+ end
455
509
  end
456
510