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.
- data/README.markdown +251 -0
- data/lib/jdbc-helper.rb +1 -10
- data/lib/jdbc-helper/connection.rb +347 -351
- data/lib/jdbc-helper/connection/callable_statement.rb +56 -56
- data/lib/jdbc-helper/connection/parameterized_statement.rb +57 -46
- data/lib/jdbc-helper/connection/prepared_statement.rb +88 -88
- data/lib/jdbc-helper/connection/result_set_enumerator.rb +102 -91
- data/lib/jdbc-helper/connection/row.rb +106 -115
- data/lib/jdbc-helper/connection/statement_pool.rb +44 -44
- data/lib/jdbc-helper/connection/type_map.rb +41 -66
- data/lib/jdbc-helper/connector.rb +10 -10
- data/lib/jdbc-helper/connector/mysql_connector.rb +24 -24
- data/lib/jdbc-helper/connector/oracle_connector.rb +33 -32
- data/lib/jdbc-helper/constants.rb +20 -17
- data/lib/jdbc-helper/sql.rb +168 -175
- data/lib/jdbc-helper/wrapper/function_wrapper.rb +12 -12
- data/lib/jdbc-helper/wrapper/object_wrapper.rb +17 -17
- data/lib/jdbc-helper/wrapper/procedure_wrapper.rb +120 -123
- data/lib/jdbc-helper/wrapper/sequence_wrapper.rb +43 -43
- data/lib/jdbc-helper/wrapper/table_wrapper.rb +172 -169
- data/test/helper.rb +2 -10
- data/test/performance.rb +141 -0
- data/test/test_connection.rb +443 -389
- data/test/test_connectors.rb +47 -47
- data/test/test_object_wrapper.rb +541 -432
- data/test/test_sql.rb +143 -135
- metadata +119 -123
- data/README.rdoc +0 -223
- data/test/test_performance.rb +0 -138
data/test/test_connectors.rb
CHANGED
@@ -1,52 +1,52 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestConnectors < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
50
|
-
|
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
|
|
data/test/test_object_wrapper.rb
CHANGED
@@ -1,442 +1,506 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestObjectWrapper < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
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
|
-
|
436
|
+
assert_equal 50, table.count
|
368
437
|
|
369
|
-
|
370
|
-
|
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
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
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
|
-
|
439
|
-
|
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
|
-
|
463
|
-
|
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
|
|