jdbc-helper 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,52 +1,52 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestConnectors < Test::Unit::TestCase
4
- include JDBCHelperTestHelper
5
-
6
- def setup
7
- end
8
-
9
- def teardown
10
- end
11
-
12
- def test_connectors
13
- config.each do | db, conn_info |
14
- if conn_info['driver'] =~ /mysql/
15
- host = conn_info['url'].match(%r{//(.*?)/})[1]
16
- db = conn_info['url'].match(%r{/([^/?]*?)(\?.*)?$})[1]
17
- conn = JDBCHelper::MySQLConnector.connect(host, conn_info['user'], conn_info['password'], db)
18
-
19
- assert conn.closed? == false
20
- conn.close
21
- assert conn.closed?
22
-
23
- @conn = nil
24
- ret = JDBCHelper::MySQLConnector.connect(host, conn_info['user'], conn_info['password'], db) do |conn|
25
- assert conn.closed? == false
26
- @conn = conn
27
- 1
28
- end
29
- assert @conn.closed?
30
- assert_equal 1, ret
31
- elsif conn_info['driver'] =~ /oracle/
32
- host = conn_info['url'].match(%r{@(.*?)/})[1]
33
- svc = conn_info['url'].match(%r{/([^/?]*?)(\?.*)?$})[1]
34
- conn = JDBCHelper::OracleConnector.connect(host, conn_info['user'], conn_info['password'], svc)
35
-
36
- assert conn.closed? == false
37
- conn.close
38
- assert conn.closed?
39
-
40
- @conn = nil
41
- ret = JDBCHelper::OracleConnector.connect(host, conn_info['user'], conn_info['password'], svc) do |conn|
42
- assert conn.closed? == false
43
- @conn = conn
44
- 1
45
- end
46
- assert @conn.closed?
47
- assert_equal 1, ret
48
- end
49
- end
50
- end
4
+ include JDBCHelperTestHelper
5
+
6
+ def setup
7
+ end
8
+
9
+ def teardown
10
+ end
11
+
12
+ def test_connectors
13
+ config.each do | db, conn_info |
14
+ if conn_info['driver'] =~ /mysql/
15
+ host = conn_info['url'].match(%r{//(.*?)/})[1]
16
+ db = conn_info['url'].match(%r{/([^/?]*?)(\?.*)?$})[1]
17
+ conn = JDBCHelper::MySQLConnector.connect(host, conn_info['user'], conn_info['password'], db)
18
+
19
+ assert conn.closed? == false
20
+ conn.close
21
+ assert conn.closed?
22
+
23
+ @conn = nil
24
+ ret = JDBCHelper::MySQLConnector.connect(host, conn_info['user'], conn_info['password'], db) do |conn|
25
+ assert conn.closed? == false
26
+ @conn = conn
27
+ 1
28
+ end
29
+ assert @conn.closed?
30
+ assert_equal 1, ret
31
+ elsif conn_info['driver'] =~ /oracle/
32
+ host = conn_info['url'].match(%r{@(.*?)/})[1]
33
+ svc = conn_info['url'].match(%r{/([^/?]*?)(\?.*)?$})[1]
34
+ conn = JDBCHelper::OracleConnector.connect(host, conn_info['user'], conn_info['password'], svc)
35
+
36
+ assert conn.closed? == false
37
+ conn.close
38
+ assert conn.closed?
39
+
40
+ @conn = nil
41
+ ret = JDBCHelper::OracleConnector.connect(host, conn_info['user'], conn_info['password'], svc) do |conn|
42
+ assert conn.closed? == false
43
+ @conn = conn
44
+ 1
45
+ end
46
+ assert @conn.closed?
47
+ assert_equal 1, ret
48
+ end
49
+ end
50
+ end
51
51
  end
52
52
 
@@ -1,442 +1,506 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestObjectWrapper < Test::Unit::TestCase
4
- include JDBCHelperTestHelper
5
-
6
- def setup
7
- @table_name = "tmp_jdbc_helper"
8
- @procedure_name = "tmp_jdbc_helper_test_proc"
9
- end
10
-
11
- def teardown
12
- each_connection do |conn|
13
- drop_table conn
14
- conn.update "drop procedure #{@procedure_name}" rescue nil
15
- end
16
- end
17
-
18
- def create_table conn
19
- drop_table conn
20
- conn.update "
21
- create table #{@table_name} (
22
- id int primary key,
23
- alpha int,
24
- beta float,
25
- gamma varchar(100)
26
- )
27
- "
28
- end
29
-
30
- def drop_table conn
31
- begin
32
- conn.update "drop table #{@table_name}"
33
- return true
34
- rescue Exception
35
- return false
36
- end
37
- end
38
-
39
- def test_wrapper
40
- each_connection do |conn|
41
- # With symbol
42
- assert_kind_of JDBCHelper::ObjectWrapper, conn.table(:some_table)
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)
48
- assert_equal 'some_table', conn.table(:some_table).name
49
-
50
- # With string
51
- assert_kind_of JDBCHelper::ObjectWrapper, conn.table('table')
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')
57
- assert_equal 'db.table', conn.table('db.table').name
58
-
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
67
-
68
- # Abstract class
69
- assert_raise(Exception) { JDBCHelper::ObjectWrapper.new(conn, 'table') }
70
- end
71
- end
72
-
73
- def insert table, cnt = 100
74
- params = {
75
- :alpha => 100,
76
- :beta => JDBCHelper::SQL('0.1 + 0.2'),
77
- }
78
-
79
- (1..cnt).each do |pk|
80
- icnt = table.
81
- default(:gamma => 'hello world').
82
- default(:alpha => 200).
83
- insert(params.merge(:id => pk))
84
- assert_equal 1, icnt unless table.batch?
85
- end
86
- end
87
-
88
- def test_empty
89
- each_connection do |conn|
90
- create_table conn
91
- table = conn.table(@table_name)
92
-
93
- assert table.empty?
94
- end
95
- end
96
-
97
- def test_function_wrapper
98
- each_connection do |conn|
99
- assert_equal 2.to_i, conn.function(:mod).call(5, 3).to_i
100
- assert_equal 'yeah', conn.function(:coalesce).call(nil, nil, 'yeah', 'no')
101
- end
102
- end
103
-
104
- def test_procedure_wrapper
105
- each_connection do |conn, conn_info|
106
- {
107
- :proc => @procedure_name,
108
- :db_proc => [conn_info['database'], @procedure_name].join('.')
109
- }.each do |mode, prname|
110
- create_test_procedure_simple conn, prname
111
-
112
- pr = conn.procedure(prname)
113
- pr.call # should be ok without any arguments
114
-
115
- # Complex case
116
- create_test_procedure conn, prname
117
- pr.refresh
118
-
119
- result = pr.call 'hello', 10, [100, Fixnum], [Time.now, Time], nil, Float, String
120
- assert_instance_of Hash, result
121
- assert_equal 1000, result[3]
122
- assert_equal 'hello', result[7]
123
-
124
- result = pr.call(
125
- :io1 => [100, Fixnum],
126
- 'io2' => [Time.now, Time],
127
- :i2 => 10,
128
- :i1 => 'hello',
129
- :o1 => Float, 'o2' => String)
130
- assert_instance_of Hash, result
131
- assert_equal 1000, result[:io1]
132
- assert_equal 'hello', result['o2']
133
-
134
- # Test default values
135
- # - MySQL does not support default values
136
- # - Oracle JDBC does not fully implement getProcedureColumns
137
- # => Cannot get default values with standard interface => Pending
138
- if @type != :mysql
139
- pend("Not tested") do
140
- result = pr.call(
141
- :io1 => [100, Fixnum],
142
- 'io2' => [Time.now, Time],
143
- #:i2 => 10,
144
- :i1 => 'hello',
145
- :o1 => Float, 'o2' => String)
146
- assert_instance_of Hash, result
147
- assert_equal 100, result[:io1]
148
- assert_equal 'hello', result['o2']
149
-
150
- result = pr.call 'hello', [100, Fixnum], [Time.now, Time], nil, Float, String
151
- assert_instance_of Hash, result
152
- assert_equal 100, result[3]
153
- assert_equal 'hello', result[7]
154
- end
155
- end
156
- end#prname
157
- end
158
- end
159
-
160
- def test_insert_count
161
- each_connection do |conn|
162
- create_table conn
163
- table = conn.table(@table_name)
164
-
165
- # Count
166
- assert_equal 0, table.count
167
- assert table.empty?
168
-
169
- # Insert
170
- insert table
171
-
172
- # Empty?
173
- assert_equal false, table.empty?
174
- assert_equal true, table.empty?(:alpha => 999)
175
- assert_equal true, table.where(:alpha => 999).empty?
176
-
177
- # Count
178
- assert_equal 100, table.count
179
- assert_equal 100, table.count(:alpha => 100)
180
- assert_equal 1, table.where(:alpha => 100).count(:id => 1) # scoped
181
- assert_equal 0, table.where(:alpha => 200).count(:id => 1) # scoped
182
- assert_equal 0, table.count(:beta => nil)
183
-
184
- assert_equal 100, table.where(:alpha => 100).count
185
- assert_equal 0, table.where(:beta => nil).count
186
- assert_equal 40, table.where('id >= 11', 'id <= 50').count
187
- assert_equal 40, table.where('id >= 11').count('id <= 50')
188
- assert_equal 40, table.where('id >= 11').where('id <= 50').count
189
- assert_equal 40, table.where('id >= 11').where('id <= 50').where('1 = 1').count
190
- assert_equal 0, table.where(:alpha => 100).count(:beta => nil)
191
-
192
- assert_equal true, table.empty?(:beta => nil)
193
- assert_equal true, table.where(:beta => nil).empty?
194
- end
195
- end
196
-
197
- def test_insert_ignore
198
- each_connection do |conn|
199
- next unless @type == :mysql
200
-
201
- create_table conn
202
- table = conn.table(@table_name)
203
- params = {
204
- :id => 1,
205
- :alpha => 100,
206
- :beta => JDBCHelper::SQL('0.1 + 0.2'),
207
- :gamma => 'hello world' }
208
-
209
- 100.times do
210
- table.insert_ignore(params)
211
- end
212
-
213
- assert_equal 1, table.count
214
- end
215
- end
216
-
217
- def test_replace
218
- each_connection do |conn|
219
- next unless @type == :mysql
220
-
221
- create_table conn
222
- table = conn.table(@table_name)
223
- params = {
224
- :id => 1,
225
- :beta => JDBCHelper::SQL('0.1 + 0.2'),
226
- :gamma => 'hello world' }
227
-
228
- 100.times do |i|
229
- table.replace(params.merge(:alpha => i))
230
- end
231
-
232
- assert_equal 1, table.count
233
- assert_equal 99, table.select.first.alpha
234
- end
235
- end
236
-
237
- def test_select
238
- each_connection do |conn|
239
- create_table conn
240
- table = conn.table(@table_name)
241
- insert table
242
- assert_equal 100, table.count
243
-
244
- def check_row row
245
- assert_equal 100, row.alpha
246
- assert_equal 'hello world', row.gamma
247
- end
248
-
249
- cnt = 0
250
- table.select do |row|
251
- cnt += 1
252
- check_row row
253
- end
254
- assert_equal 100, cnt
255
-
256
- cnt = 0
257
- table.each do |row|
258
- cnt += 1
259
- check_row row
260
- end
261
- assert_equal 100, cnt
262
-
263
- cnt = 0
264
- table.each_slice(10) do |rows|
265
- cnt += rows.length
266
- end
267
- assert_equal 100, cnt
268
-
269
- cnt = 0
270
- table.select('alpha omega') do |row|
271
- cnt += 1
272
- assert_equal 100, row.omega
273
- assert_equal ['omega'], row.labels.map(&:downcase)
274
- end
275
- assert_equal 100, cnt
276
-
277
- cnt = 0
278
- prev_id = 100
279
- table.where(:id => 11..20).order('id desc') do |row|
280
- cnt += 1
281
- check_row row
282
-
283
- assert row.id.to_i < prev_id
284
- prev_id = row.id.to_i
285
- end
286
- assert_equal 10, cnt
287
-
288
- assert_equal "select a, b, c cc from tmp_jdbc_helper " +
289
- "where id >= 11 and id <= 20 order by id desc, name asc",
290
- table.where(:id => 11..20).
291
- select(:a, :b, 'c cc').
292
- order('id desc', 'name asc').sql
293
-
294
- assert_equal "select a, b, c cc from tmp_jdbc_helper " +
295
- "where (id != 15) and id >= 11 and id <= 20 order by id desc, name asc",
296
- table.where("id != 15", :id => 11..20).
297
- select(:a, :b, 'c cc').
298
- order('id desc', 'name asc').sql
299
-
300
- assert_raise(ArgumentError) { table.order }
301
- assert_raise(ArgumentError) { table.order.where }
302
- assert_raise(ArgumentError) { table.where.order }
303
- assert_raise(ArgumentError) { table.select.order }
304
- end
305
- end
306
-
307
- def test_delete
308
- each_connection do |conn|
309
- create_table conn
310
- table = conn.table(@table_name)
311
- insert table
312
-
313
- # Count
314
- assert_equal 100, table.count
315
-
316
- # Delete
317
- assert_equal 10, table.delete(:id => (1...11))
318
- assert_equal 10, table.delete(:id => (11..20))
319
- assert_equal 1, table.delete(:id => 21)
320
- assert_equal 4, table.delete(:id => [22, 23, 24, 25])
321
- assert_equal 5, table.delete("id <= 30")
322
- assert_equal 10, table.where("id <= 40").delete
323
-
324
- # Could be dangerous (XXX)
325
- assert_equal 60, table.delete
326
-
327
- # Count
328
- assert_equal 0, table.count
329
- end
330
- end
331
-
332
- def test_update
333
- each_connection do |conn|
334
- create_table conn
335
- table = conn.table(@table_name)
336
- insert table
337
-
338
- assert_equal 10, table.update(:beta => 0, :where => { :id => (1..10) })
339
- assert_equal 2, table.where(:id => (55..56)).update(:beta => 0, :where => { :id => (51..60) })
340
- assert_equal 10, table.where(:id => (11..20)).update(:beta => 1)
341
-
342
- with_default = table.default(:beta => 0)
343
- assert_equal 5, with_default.where(:id => (11..15)).update
344
- assert_equal 5, with_default.where(:id => (16..20)).update
345
- with_default = table.default(:beta => 1)
346
- assert_equal 5, with_default.where(:id => (16..20)).update(:beta => 0) # override
347
- assert_equal 22, table.count(:beta => 0)
348
- assert_equal 100, table.update(:beta => 1)
349
- end
350
- end
351
-
352
- def test_batch
353
- each_connection do |conn|
354
- # Initialize test table
355
- create_table conn
356
- table = conn.table(@table_name)
357
- insert table
358
-
359
- # Duplicated calls are idempotent
360
- btable = table.batch
361
- assert_equal btable, btable.batch
362
-
363
- # Batch updates
364
- table.batch.delete
365
- assert_equal 100, table.count
4
+ include JDBCHelperTestHelper
5
+
6
+ def setup
7
+ @table_name = "tmp_jdbc_helper"
8
+ @procedure_name = "tmp_jdbc_helper_test_proc"
9
+ @blob = 'XXX'
10
+ end
11
+
12
+ def teardown
13
+ each_connection do |conn|
14
+ drop_table conn
15
+ conn.update "drop procedure #{@procedure_name}" rescue nil
16
+ end
17
+ end
18
+
19
+ def create_table conn
20
+ drop_table conn
21
+ ddl = "
22
+ create table #{@table_name} (
23
+ id int primary key,
24
+ alpha int,
25
+ beta float,
26
+ gamma varchar(100),
27
+ delta blob,
28
+ num_f decimal(15, 5),
29
+ num_fstr decimal(30, 10),
30
+ num_int decimal(9, 0),
31
+ num_long decimal(18, 0),
32
+ num_str decimal(30, 0)
33
+ #{", num_wtf number" if @type == :oracle}
34
+ )
35
+ "
36
+ ddl.gsub('decimal', 'number') if @type == :oracle
37
+ conn.update ddl
38
+ end
39
+
40
+ def drop_table conn
41
+ begin
42
+ conn.update "drop table #{@table_name}"
43
+ return true
44
+ rescue Exception
45
+ return false
46
+ end
47
+ end
48
+
49
+ def test_wrapper
50
+ each_connection do |conn|
51
+ # With symbol
52
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.table(:some_table)
53
+ assert_instance_of JDBCHelper::TableWrapper, conn.table(:some_table)
54
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.function(:some_func)
55
+ assert_instance_of JDBCHelper::FunctionWrapper, conn.function(:some_func)
56
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.procedure(:some_proc)
57
+ assert_instance_of JDBCHelper::ProcedureWrapper, conn.procedure(:some_proc)
58
+ assert_equal 'some_table', conn.table(:some_table).name
59
+
60
+ # With string
61
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.table('table')
62
+ assert_instance_of JDBCHelper::TableWrapper, conn.table('db.table')
63
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.function('db.some_func')
64
+ assert_instance_of JDBCHelper::FunctionWrapper, conn.function('some_func')
65
+ assert_kind_of JDBCHelper::ObjectWrapper, conn.procedure('some_proc')
66
+ assert_instance_of JDBCHelper::ProcedureWrapper, conn.procedure('db.some_proc')
67
+ assert_equal 'db.table', conn.table('db.table').name
68
+
69
+ # Invalid object name
70
+ [ ' ', 'object;', 'object -- ', "obj'ect",
71
+ 'obj"ect', 'obj`ect', 'obje(t', 'ob)ect' ].each do |inv|
72
+ assert_raise(ArgumentError) { conn.table(inv) }
73
+ assert_raise(ArgumentError) { conn.function(inv) }
74
+ assert_raise(ArgumentError) { conn.table(inv.to_sym) }
75
+ assert_raise(ArgumentError) { conn.function(inv.to_sym) }
76
+ end
77
+
78
+ # Abstract class
79
+ assert_raise(NotImplementedError) { JDBCHelper::ObjectWrapper.new(conn, 'table') }
80
+ end
81
+ end
82
+
83
+
84
+ def insert_params
85
+ {
86
+ :alpha => 100,
87
+ :beta => JDBCHelper::SQL('0.1 + 0.2'),
88
+ :num_f => 1234567890.12345, # 16 digits
89
+ :num_fstr => BigDecimal.new("12345678901234567890.12345"),
90
+ :num_int => 123456789,
91
+ :num_long => 123456789012345678,
92
+ :num_str => 123456789012345678901234567890,
93
+ :num_wtf => 12345.6789
94
+ }
95
+ end
96
+
97
+ def insert table, cnt = 100
98
+ require 'java'
99
+
100
+ params = insert_params.dup
101
+ params.delete(:num_wtf) unless @type == :oracle
102
+
103
+ (1..cnt).each do |pk|
104
+ icnt = table.
105
+ default(:gamma => 'hello world').
106
+ default(:alpha => 200).
107
+ insert(params.merge(
108
+ :id => pk,
109
+ :delta => java.io.ByteArrayInputStream.new( @blob.to_java_bytes ))
110
+ )
111
+ assert_equal 1, icnt unless table.batch?
112
+ end
113
+ end
114
+
115
+ def test_empty
116
+ each_connection do |conn|
117
+ create_table conn
118
+ table = conn.table(@table_name)
119
+
120
+ assert table.empty?
121
+ end
122
+ end
123
+
124
+ def test_function_wrapper
125
+ each_connection do |conn|
126
+ assert_equal 2.to_i, conn.function(:mod).call(5, 3).to_i
127
+ assert_equal 'yeah', conn.function(:coalesce).call(nil, nil, 'yeah', 'no')
128
+ end
129
+ end
130
+
131
+ def test_procedure_wrapper
132
+ each_connection do |conn, conn_info|
133
+ {
134
+ :proc => @procedure_name,
135
+ :db_proc => [conn_info['database'], @procedure_name].join('.')
136
+ }.each do |mode, prname|
137
+ create_test_procedure_simple conn, prname
138
+
139
+ pr = conn.procedure(prname)
140
+ pr.call # should be ok without any arguments
141
+
142
+ # Complex case
143
+ create_test_procedure conn, prname
144
+ pr.refresh
145
+
146
+ result = pr.call 'hello', 10, [100, Fixnum], [Time.now, Time], nil, Float, String
147
+ assert_instance_of Hash, result
148
+ assert_equal 1000, result[3]
149
+ assert_equal 'hello', result[7]
150
+
151
+ result = pr.call(
152
+ :io1 => [100, Fixnum],
153
+ 'io2' => [Time.now, Time],
154
+ :i2 => 10,
155
+ :i1 => 'hello',
156
+ :o1 => Float, 'o2' => String)
157
+ assert_instance_of Hash, result
158
+ assert_equal 1000, result[:io1]
159
+ assert_equal 'hello', result['o2']
160
+
161
+ # Test default values
162
+ # - MySQL does not support default values
163
+ # - Oracle JDBC does not fully implement getProcedureColumns
164
+ # => Cannot get default values with standard interface => Pending
165
+ if @type != :mysql
166
+ pend("Not tested") do
167
+ result = pr.call(
168
+ :io1 => [100, Fixnum],
169
+ 'io2' => [Time.now, Time],
170
+ #:i2 => 10,
171
+ :i1 => 'hello',
172
+ :o1 => Float, 'o2' => String)
173
+ assert_instance_of Hash, result
174
+ assert_equal 100, result[:io1]
175
+ assert_equal 'hello', result['o2']
176
+
177
+ result = pr.call 'hello', [100, Fixnum], [Time.now, Time], nil, Float, String
178
+ assert_instance_of Hash, result
179
+ assert_equal 100, result[3]
180
+ assert_equal 'hello', result[7]
181
+ end
182
+ end
183
+ end#prname
184
+ end
185
+ end
186
+
187
+ def test_insert_count
188
+ each_connection do |conn|
189
+ create_table conn
190
+ table = conn.table(@table_name)
191
+
192
+ # Count
193
+ assert_equal 0, table.count
194
+ assert table.empty?
195
+
196
+ # Insert
197
+ insert table
198
+
199
+ # Empty?
200
+ assert_equal false, table.empty?
201
+ assert_equal true, table.empty?(:alpha => 999)
202
+ assert_equal true, table.where(:alpha => 999).empty?
203
+
204
+ # Count
205
+ assert_equal 100, table.count
206
+ assert_equal 100, table.count(:alpha => 100)
207
+ assert_equal 1, table.where(:alpha => 100).count(:id => 1) # scoped
208
+ assert_equal 0, table.where(:alpha => 200).count(:id => 1) # scoped
209
+ assert_equal 0, table.count(:beta => nil)
210
+
211
+ assert_equal 100, table.where(:alpha => 100).count
212
+ assert_equal 0, table.where(:beta => nil).count
213
+ assert_equal 40, table.where('id >= 11', 'id <= 50').count
214
+ assert_equal 40, table.where('id >= 11').count('id <= 50')
215
+ assert_equal 40, table.where('id >= 11').where('id <= 50').count
216
+ assert_equal 40, table.where('id >= 11').where('id <= 50').where('1 = 1').count
217
+ assert_equal 0, table.where(:alpha => 100).count(:beta => nil)
218
+
219
+ assert_equal true, table.empty?(:beta => nil)
220
+ assert_equal true, table.where(:beta => nil).empty?
221
+ end
222
+ end
223
+
224
+ def test_insert_ignore
225
+ each_connection do |conn|
226
+ next unless @type == :mysql
227
+
228
+ create_table conn
229
+ table = conn.table(@table_name)
230
+ params = {
231
+ :id => 1,
232
+ :alpha => 100,
233
+ :beta => JDBCHelper::SQL('0.1 + 0.2'),
234
+ :gamma => 'hello world' }
235
+
236
+ 100.times do
237
+ table.insert_ignore(params)
238
+ end
239
+
240
+ assert_equal 1, table.count
241
+ end
242
+ end
243
+
244
+ def test_replace
245
+ each_connection do |conn|
246
+ next unless @type == :mysql
247
+
248
+ create_table conn
249
+ table = conn.table(@table_name)
250
+ params = {
251
+ :id => 1,
252
+ :beta => JDBCHelper::SQL('0.1 + 0.2'),
253
+ :gamma => 'hello world' }
254
+
255
+ 100.times do |i|
256
+ table.replace(params.merge(:alpha => i))
257
+ end
258
+
259
+ assert_equal 1, table.count
260
+ assert_equal 99, table.select.first.alpha
261
+ end
262
+ end
263
+
264
+ def test_select
265
+ each_connection do |conn|
266
+ create_table conn
267
+ table = conn.table(@table_name)
268
+ insert table
269
+ assert_equal 100, table.count
270
+
271
+ def check_row row
272
+ assert_equal 100, row.alpha
273
+ assert_equal 'hello world', row.gamma
274
+ end
275
+
276
+ cnt = 0
277
+ table.select do |row|
278
+ cnt += 1
279
+ check_row row
280
+ end
281
+ assert_equal 100, cnt
282
+
283
+ # each
284
+ cnt = 0
285
+ table.each do |row|
286
+ cnt += 1
287
+ check_row row
288
+ end
289
+ assert_equal 100, cnt
290
+
291
+ # As Enumerable
292
+ cnt = 0
293
+ table.each_slice(10) do |rows|
294
+ cnt += rows.length
295
+ end
296
+ assert_equal 100, cnt
297
+
298
+ # Alias
299
+ cnt = 0
300
+ table.select('alpha omega') do |row|
301
+ cnt += 1
302
+ assert_equal 100, row.omega
303
+ assert_equal ['omega'], row.labels.map(&:downcase)
304
+ end
305
+ assert_equal 100, cnt
306
+
307
+ # Lob, Decimals
308
+ params = insert_params
309
+ cols = [:delta, :num_f, :num_fstr, :num_int, :num_long, :num_str]
310
+ cols << :num_wtf if @type == :oracle
311
+ table.select(*cols) do |row|
312
+ blob = row.delta
313
+ assert_equal @blob, blob.getBytes(1, blob.length()).to_a.pack('U*')
314
+ assert_equal Float, row.num_f.class
315
+ assert_equal BigDecimal, row.num_fstr.class
316
+ assert_equal Fixnum, row.num_int.class
317
+ assert_equal Fixnum, row.num_long.class
318
+ assert_equal Bignum, row.num_str.class
319
+ assert_equal BigDecimal, row.num_wtf.class if @type == :oracle
320
+
321
+ assert_equal params[:num_int], row.num_int
322
+ assert_equal params[:num_long], row.num_long
323
+ assert_equal params[:num_str], row.num_str
324
+ assert_equal params[:num_fstr], row.num_fstr
325
+ assert_equal params[:num_f], row.num_f
326
+ assert_equal params[:num_wtf], row.num_wtf if @type == :oracle
327
+ end
328
+
329
+ cnt = 0
330
+ prev_id = 100
331
+ table.where(:id => 11..20).order('id desc') do |row|
332
+ cnt += 1
333
+ check_row row
334
+
335
+ assert row.id.to_i < prev_id
336
+ prev_id = row.id.to_i
337
+ end
338
+ assert_equal 10, cnt
339
+
340
+ assert_equal "select a, b, c cc from tmp_jdbc_helper " +
341
+ "where id >= 11 and id <= 20 order by id desc, name asc",
342
+ table.where(:id => 11..20).
343
+ select(:a, :b, 'c cc').
344
+ order('id desc', 'name asc').sql
345
+
346
+ assert_equal "select a, b, c cc from tmp_jdbc_helper " +
347
+ "where (id != 15) and id >= 11 and id <= 20 order by id desc, name asc",
348
+ table.where("id != 15", :id => 11..20).
349
+ select(:a, :b, 'c cc').
350
+ order('id desc', 'name asc').sql
351
+
352
+ assert_raise(ArgumentError) { table.order }
353
+ assert_raise(ArgumentError) { table.order.where }
354
+ assert_raise(ArgumentError) { table.where.order }
355
+ assert_raise(ArgumentError) { table.select.order }
356
+ end
357
+ end
358
+
359
+ def test_delete
360
+ each_connection do |conn|
361
+ create_table conn
362
+ table = conn.table(@table_name)
363
+ insert table
364
+
365
+ # Count
366
+ assert_equal 100, table.count
367
+
368
+ # Delete
369
+ assert_equal 10, table.delete(:id => (1...11))
370
+ assert_equal 10, table.delete(:id => (11..20))
371
+ assert_equal 1, table.delete(:id => 21)
372
+ assert_equal 4, table.delete(:id => [22, 23, 24, 25])
373
+ assert_equal 5, table.delete("id <= 30")
374
+ assert_equal 10, table.where("id <= 40").delete
375
+
376
+ # Could be dangerous (XXX)
377
+ assert_equal 60, table.delete
378
+
379
+ # Count
380
+ assert_equal 0, table.count
381
+ end
382
+ end
383
+
384
+ def test_update
385
+ each_connection do |conn|
386
+ create_table conn
387
+ table = conn.table(@table_name)
388
+ insert table
389
+
390
+ assert_equal 10, table.update(:beta => 0, :where => { :id => (1..10) })
391
+ assert_equal 2, table.where(:id => (55..56)).update(:beta => 0, :where => { :id => (51..60) })
392
+ assert_equal 10, table.where(:id => (11..20)).update(:beta => 1)
393
+
394
+ with_default = table.default(:beta => 0)
395
+ assert_equal 5, with_default.where(:id => (11..15)).update
396
+ assert_equal 5, with_default.where(:id => (16..20)).update
397
+ with_default = table.default(:beta => 1)
398
+ assert_equal 5, with_default.where(:id => (16..20)).update(:beta => 0) # override
399
+ assert_equal 22, table.count(:beta => 0)
400
+ assert_equal 100, table.update(:beta => 1)
401
+
402
+ # Blob-handling
403
+ first_row = table.select(:delta).first
404
+ blob = first_row.delta
405
+
406
+ table.update(:delta => nil)
407
+ table.update(:delta => blob)
408
+
409
+ table.select('delta') do |row|
410
+ blob = row.delta
411
+ assert_equal @blob, blob.getBytes(1, blob.length()).to_a.pack('U*')
412
+ end
413
+ end
414
+ end
415
+
416
+ def test_batch
417
+ each_connection do |conn|
418
+ # Initialize test table
419
+ create_table conn
420
+ table = conn.table(@table_name)
421
+ insert table
422
+
423
+ # Duplicated calls are idempotent
424
+ btable = table.batch
425
+ assert_equal btable, btable.batch
426
+
427
+ # Batch updates
428
+ table.batch.delete
429
+ assert_equal 100, table.count
430
+ conn.execute_batch
431
+ assert_equal 0, table.count
432
+
433
+ insert table.batch, 50
434
+ assert_equal 0, table.count
366
435
  conn.execute_batch
367
- assert_equal 0, table.count
436
+ assert_equal 50, table.count
368
437
 
369
- insert table.batch, 50
370
- assert_equal 0, table.count
438
+ table.batch.update(:alpha => JDBCHelper::SQL('alpha * 2'))
439
+ assert_equal 100, table.select(:alpha).to_a.first.alpha.to_i
440
+
441
+ # Independent update inbetween
442
+ table.delete(:id => 1..10)
443
+ assert_equal 40, table.count
444
+
445
+ # Finally
371
446
  conn.execute_batch
372
- assert_equal 50, table.count
373
-
374
- table.batch.update(:alpha => JDBCHelper::SQL('alpha * 2'))
375
- assert_equal 100, table.select(:alpha).to_a.first.alpha.to_i
376
-
377
- # Independent update inbetween
378
- table.delete(:id => 1..10)
379
- assert_equal 40, table.count
380
-
381
- # Finally
382
- conn.execute_batch
383
-
384
- assert_equal 200, table.select(:alpha).to_a.first.alpha.to_i
385
- end
386
- end
387
-
388
- def test_truncate_table
389
- each_connection do |conn|
390
- create_table conn
391
- table = conn.table(@table_name)
392
- insert table
393
-
394
- table.truncate!
395
- assert table.empty?
396
- end
397
- end
398
-
399
- def test_drop_table
400
- each_connection do |conn|
401
- create_table conn
402
- table = conn.table(@table_name)
403
- table.drop!
404
- assert_equal false, drop_table(conn)
405
-
406
- create_table conn
407
- table = conn.table(@table_name)
408
- table.drop_table! #alias
409
- assert_equal false, drop_table(conn)
410
- end
411
- end
412
-
413
- def test_sequence
414
- each_connection do |conn|
415
- # MySQL doesn't support sequences
416
- next if @type == :mysql
417
-
418
- seq = conn.sequence(@table_name + '_seq')
419
- seq.reset!(100)
420
- assert (prev = seq.nextval) >= 100
421
- assert_equal prev, seq.currval
422
- assert_equal 1, seq.nextval - prev
423
-
424
- seq.reset! 1, 2
425
- assert seq.nextval >= 1
426
- assert seq.nextval <= 4
427
- assert seq.nextval >= 5
428
-
429
- seq.drop!
430
- seq.create!(10)
431
- assert seq.nextval >= 10
432
- seq.drop!
433
- end
434
- end
447
+
448
+ assert_equal 200, table.select(:alpha).to_a.first.alpha.to_i
449
+ end
450
+ end
451
+
452
+ def test_truncate_table
453
+ each_connection do |conn|
454
+ create_table conn
455
+ table = conn.table(@table_name)
456
+ insert table
457
+
458
+ table.truncate!
459
+ assert table.empty?
460
+ end
461
+ end
462
+
463
+ def test_drop_table
464
+ each_connection do |conn|
465
+ create_table conn
466
+ table = conn.table(@table_name)
467
+ table.drop!
468
+ assert_equal false, drop_table(conn)
469
+
470
+ create_table conn
471
+ table = conn.table(@table_name)
472
+ table.drop_table! #alias
473
+ assert_equal false, drop_table(conn)
474
+ end
475
+ end
476
+
477
+ def test_sequence
478
+ each_connection do |conn|
479
+ # MySQL doesn't support sequences
480
+ next if @type == :mysql
481
+
482
+ seq = conn.sequence(@table_name + '_seq')
483
+ seq.reset!(100)
484
+ assert (prev = seq.nextval) >= 100
485
+ assert_equal prev, seq.currval
486
+ assert_equal 1, seq.nextval - prev
487
+
488
+ seq.reset! 1, 2
489
+ assert seq.nextval >= 1
490
+ assert seq.nextval <= 4
491
+ assert seq.nextval >= 5
492
+
493
+ seq.drop!
494
+ seq.create!(10)
495
+ assert seq.nextval >= 10
496
+ seq.drop!
497
+ end
498
+ end
435
499
 
436
500
  # 0.5.1: Too many open cursors
437
501
  def test_ora_10000
438
- each_connection do |conn|
439
- create_table conn
502
+ each_connection do |conn|
503
+ create_table conn
440
504
  insert conn.table(@table_name)
441
505
 
442
506
  # Batch-enabled object
@@ -459,8 +523,8 @@ class TestObjectWrapper < Test::Unit::TestCase
459
523
  end
460
524
 
461
525
  def test_prepared_statements
462
- each_connection do |conn|
463
- create_table conn
526
+ each_connection do |conn|
527
+ create_table conn
464
528
 
465
529
  # No duplicate preparations
466
530
  t = conn.table(@table_name)
@@ -501,5 +565,50 @@ class TestObjectWrapper < Test::Unit::TestCase
501
565
  t.batch.close
502
566
  end
503
567
  end
568
+
569
+ def test_invalidated_prepared_statements
570
+ each_connection do |conn|
571
+ create_table conn
572
+
573
+ t = conn.table(@table_name)
574
+ insert t, 100
575
+ assert_equal 100, t.count
576
+
577
+ create_table conn
578
+ insert t, 100
579
+ # SHOULD NOT FAIL
580
+ assert_equal 100, t.count
581
+ end
582
+ end
583
+
584
+ def test_closed_prepared_statements
585
+ each_connection do |conn|
586
+ create_table conn
587
+
588
+ t = conn.table(@table_name)
589
+ insert t, 100
590
+ assert_equal 100, t.count
591
+
592
+ conn.prepared_statements.each { |ps| ps.close }
593
+
594
+ # SHOULD NOT FAIL (automatic repreparation)
595
+ assert_equal 100, t.count
596
+ end
597
+ end
598
+
599
+ def test_closed_prepared_statements_java
600
+ each_connection do |conn|
601
+ create_table conn
602
+
603
+ t = conn.table(@table_name)
604
+ insert t, 100
605
+ assert_equal 100, t.count
606
+
607
+ conn.prepared_statements.each { |ps| ps.java_obj.close }
608
+
609
+ # SHOULD NOT FAIL (automatic repreparation)
610
+ assert_equal 100, t.count
611
+ end
612
+ end
504
613
  end
505
614