ruby-oci8 2.0.4-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/ChangeLog +1912 -0
  2. data/Makefile +96 -0
  3. data/NEWS +223 -0
  4. data/README +86 -0
  5. data/VERSION +1 -0
  6. data/dist-files +77 -0
  7. data/doc/api.en.html +527 -0
  8. data/doc/api.en.rd +554 -0
  9. data/doc/api.ja.html +525 -0
  10. data/doc/api.ja.rd +557 -0
  11. data/doc/manual.css +35 -0
  12. data/lib/.document +1 -0
  13. data/lib/dbd/OCI8.rb +591 -0
  14. data/lib/oci8.rb +82 -0
  15. data/lib/oci8.rb.in +82 -0
  16. data/lib/oci8/.document +5 -0
  17. data/lib/oci8/bindtype.rb +319 -0
  18. data/lib/oci8/compat.rb +113 -0
  19. data/lib/oci8/datetime.rb +619 -0
  20. data/lib/oci8/encoding-init.rb +40 -0
  21. data/lib/oci8/encoding.yml +537 -0
  22. data/lib/oci8/metadata.rb +2077 -0
  23. data/lib/oci8/object.rb +562 -0
  24. data/lib/oci8/oci8.rb +571 -0
  25. data/lib/oci8/oracle_version.rb +144 -0
  26. data/lib/oci8lib_18.so +0 -0
  27. data/lib/oci8lib_191.so +0 -0
  28. data/metaconfig +142 -0
  29. data/pre-distclean.rb +7 -0
  30. data/ruby-oci8.gemspec +63 -0
  31. data/setup.rb +1331 -0
  32. data/test/README +4 -0
  33. data/test/config.rb +109 -0
  34. data/test/test_all.rb +50 -0
  35. data/test/test_appinfo.rb +63 -0
  36. data/test/test_array_dml.rb +333 -0
  37. data/test/test_bind_raw.rb +46 -0
  38. data/test/test_bind_time.rb +178 -0
  39. data/test/test_break.rb +83 -0
  40. data/test/test_clob.rb +79 -0
  41. data/test/test_connstr.rb +81 -0
  42. data/test/test_datetime.rb +622 -0
  43. data/test/test_dbi.rb +366 -0
  44. data/test/test_dbi_clob.rb +53 -0
  45. data/test/test_encoding.rb +100 -0
  46. data/test/test_metadata.rb +257 -0
  47. data/test/test_oci8.rb +434 -0
  48. data/test/test_oracle_version.rb +70 -0
  49. data/test/test_oradate.rb +256 -0
  50. data/test/test_oranumber.rb +655 -0
  51. data/test/test_rowid.rb +33 -0
  52. metadata +108 -0
@@ -0,0 +1,622 @@
1
+ require 'oci8'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/config'
4
+ require 'scanf'
5
+
6
+ class TestDateTime < Test::Unit::TestCase
7
+
8
+ def timezone_string(tzh, tzm)
9
+ if tzh >= 0
10
+ format("+%02d:%02d", tzh, tzm)
11
+ else
12
+ format("-%02d:%02d", -tzh, -tzm)
13
+ end
14
+ end
15
+
16
+ def string_to_time(str)
17
+ /(\d+)-(\d+)-(\d+) ?(?:(\d+):(\d+):(\d+))?(?:\.(\d+))? ?([+-]\d+:\d+)?/ =~ str
18
+ args = []
19
+ args << $1.to_i # year
20
+ args << $2.to_i # month
21
+ args << $3.to_i # day
22
+ args << $4.to_i if $4 # hour
23
+ args << $5.to_i if $5 # minute
24
+ if $8
25
+ args << $6.to_i + $7.to_i.to_r / ('1' + '0' * ($7.length)).to_i
26
+ args << $8
27
+ Time.new(*args)
28
+ else
29
+ if $6
30
+ args << $6.to_i
31
+ end
32
+ if $7
33
+ args << $7.to_i.to_r * 1000000 / ('1' + '0' * ($7.length)).to_i
34
+ end
35
+ # no time zone
36
+ Time.local(*args)
37
+ end
38
+ #Time.local(*str.split(/[- :\.]/).collect do |n| n.to_i; end)
39
+ end
40
+
41
+ def setup
42
+ @conn = get_oci8_connection
43
+ @local_timezone = timezone_string(*((::Time.now.utc_offset / 60).divmod 60))
44
+ end
45
+
46
+ def teardown
47
+ @conn.logoff
48
+ end
49
+
50
+ def test_date_select
51
+ ['2005-12-31 23:59:59',
52
+ '2006-01-01 00:00:00'].each do |date|
53
+ @conn.exec(<<-EOS) do |row|
54
+ SELECT TO_DATE('#{date}', 'YYYY-MM-DD HH24:MI:SS') FROM dual
55
+ EOS
56
+ assert_equal(Time.local(*date.scanf("%d-%d-%d %d:%d:%d.%06d")), row[0])
57
+ end
58
+ end
59
+ end
60
+
61
+ def test_date_out_bind
62
+ cursor = @conn.parse(<<-EOS)
63
+ BEGIN
64
+ :out := TO_DATE(:in, 'YYYY-MM-DD HH24:MI:SS');
65
+ END;
66
+ EOS
67
+ cursor.bind_param(:out, nil, DateTime)
68
+ cursor.bind_param(:in, nil, String, 36)
69
+ ['2005-12-31 23:59:59',
70
+ '2006-01-01 00:00:00'].each do |date|
71
+ cursor[:in] = date
72
+ cursor.exec
73
+ assert_equal(DateTime.parse(date + @local_timezone), cursor[:out])
74
+ end
75
+ cursor.close
76
+ end
77
+
78
+ def test_date_in_bind
79
+ cursor = @conn.parse(<<-EOS)
80
+ DECLARE
81
+ dt date;
82
+ BEGIN
83
+ dt := :in;
84
+ :out := TO_CHAR(dt, 'YYYY-MM-DD HH24:MI:SS');
85
+ END;
86
+ EOS
87
+ cursor.bind_param(:out, nil, String, 33)
88
+ cursor.bind_param(:in, nil, DateTime)
89
+ ['2005-12-31 23:59:59',
90
+ '2006-01-01 00:00:00'].each do |date|
91
+ cursor[:in] = DateTime.parse(date + @local_timezone)
92
+ cursor.exec
93
+ assert_equal(date, cursor[:out])
94
+ end
95
+ cursor.close
96
+ end
97
+
98
+ def test_timestamp_select
99
+ return if $oracle_version < OCI8::ORAVER_9_0
100
+
101
+ ['2005-12-31 23:59:59.999999000',
102
+ '2006-01-01 00:00:00.000000000'].each do |date|
103
+ @conn.exec(<<-EOS) do |row|
104
+ SELECT TO_TIMESTAMP('#{date}', 'YYYY-MM-DD HH24:MI:SS.FF') FROM dual
105
+ EOS
106
+ assert_equal(Time.local(*date.scanf("%d-%d-%d %d:%d:%d.%06d")), row[0])
107
+ end
108
+ end
109
+ end
110
+
111
+ def test_timestamp_out_bind
112
+ return if $oracle_version < OCI8::ORAVER_9_0
113
+
114
+ cursor = @conn.parse(<<-EOS)
115
+ BEGIN
116
+ :out := TO_TIMESTAMP(:in, 'YYYY-MM-DD HH24:MI:SS.FF');
117
+ END;
118
+ EOS
119
+ cursor.bind_param(:out, nil, DateTime)
120
+ cursor.bind_param(:in, nil, String, 36)
121
+ ['2005-12-31 23:59:59.999999000',
122
+ '2006-01-01 00:00:00.000000000'].each do |date|
123
+ cursor[:in] = date
124
+ cursor.exec
125
+ assert_equal(DateTime.parse(date + @local_timezone), cursor[:out])
126
+ end
127
+ cursor.close
128
+ end
129
+
130
+ def test_timestamp_in_bind
131
+ return if $oracle_version < OCI8::ORAVER_9_0
132
+
133
+ cursor = @conn.parse(<<-EOS)
134
+ BEGIN
135
+ :out := TO_CHAR(:in, 'YYYY-MM-DD HH24:MI:SS.FF');
136
+ END;
137
+ EOS
138
+ cursor.bind_param(:out, nil, String, 33)
139
+ cursor.bind_param(:in, nil, DateTime)
140
+ ['2005-12-31 23:59:59.999999000',
141
+ '2006-01-01 00:00:00.000000000'].each do |date|
142
+ cursor[:in] = DateTime.parse(date + @local_timezone)
143
+ cursor.exec
144
+ assert_equal(date, cursor[:out])
145
+ end
146
+ cursor.close
147
+ end
148
+
149
+ def test_timestamp_tz_select
150
+ return if $oracle_version < OCI8::ORAVER_9_0
151
+
152
+ ['2005-12-31 23:59:59.999999000 +08:30',
153
+ '2006-01-01 00:00:00.000000000 -08:30'].each do |date|
154
+ @conn.exec(<<-EOS) do |row|
155
+ SELECT TO_TIMESTAMP_TZ('#{date}', 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM') FROM dual
156
+ EOS
157
+ expected_val = begin
158
+ string_to_time(date)
159
+ rescue
160
+ DateTime.parse(date)
161
+ end
162
+ assert_equal(expected_val, row[0])
163
+ end
164
+ end
165
+ end
166
+
167
+ def test_timestamp_tz_out_bind
168
+ return if $oracle_version < OCI8::ORAVER_9_0
169
+
170
+ cursor = @conn.parse(<<-EOS)
171
+ BEGIN
172
+ :out := TO_TIMESTAMP_TZ(:in, 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM');
173
+ END;
174
+ EOS
175
+ cursor.bind_param(:out, nil, DateTime)
176
+ cursor.bind_param(:in, nil, String, 36)
177
+ ['2005-12-31 23:59:59.999999000 +08:30',
178
+ '2006-01-01 00:00:00.000000000 -08:30'].each do |date|
179
+ cursor[:in] = date
180
+ cursor.exec
181
+ assert_equal(DateTime.parse(date), cursor[:out])
182
+ end
183
+ cursor.close
184
+ end
185
+
186
+ def test_timestamp_tz_in_bind
187
+ return if $oracle_version < OCI8::ORAVER_9_0
188
+
189
+ cursor = @conn.parse(<<-EOS)
190
+ BEGIN
191
+ :out := TO_CHAR(:in, 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM');
192
+ END;
193
+ EOS
194
+ cursor.bind_param(:out, nil, String, 36)
195
+ cursor.bind_param(:in, nil, DateTime)
196
+ ['2005-12-31 23:59:59.999999000 +08:30',
197
+ '2006-01-01 00:00:00.000000000 -08:30'].each do |date|
198
+ cursor[:in] = DateTime.parse(date)
199
+ cursor.exec
200
+ assert_equal(date, cursor[:out])
201
+ end
202
+ cursor.close
203
+ end
204
+
205
+ def test_datetype_duck_typing
206
+ cursor = @conn.parse("BEGIN :out := :in; END;")
207
+ cursor.bind_param(:in, nil, DateTime)
208
+ cursor.bind_param(:out, nil, DateTime)
209
+ obj = Object.new
210
+ # test year, month, day
211
+ def obj.year; 2006; end
212
+ def obj.month; 12; end
213
+ def obj.day; 31; end
214
+ cursor[:in] = obj
215
+ cursor.exec
216
+ assert_equal(DateTime.parse('2006-12-31 00:00:00' + @local_timezone), cursor[:out])
217
+ # test hour
218
+ def obj.hour; 23; end
219
+ cursor[:in] = obj
220
+ cursor.exec
221
+ assert_equal(DateTime.parse('2006-12-31 23:00:00' + @local_timezone), cursor[:out])
222
+ # test min
223
+ def obj.min; 59; end
224
+ cursor[:in] = obj
225
+ cursor.exec
226
+ assert_equal(DateTime.parse('2006-12-31 23:59:00' + @local_timezone), cursor[:out])
227
+ # test sec
228
+ def obj.sec; 59; end
229
+ cursor[:in] = obj
230
+ cursor.exec
231
+ assert_equal(DateTime.parse('2006-12-31 23:59:59' + @local_timezone), cursor[:out])
232
+
233
+ # sec_fraction and timezone are available on Oracle 9i or later
234
+ return if $oracle_version < OCI8::ORAVER_9_0
235
+
236
+ # test sec_fraction
237
+ def obj.sec_fraction; DateTime.parse('0001-01-01 00:00:00.000001').sec_fraction * 999999 ; end
238
+ cursor[:in] = obj
239
+ cursor.exec
240
+ assert_equal(DateTime.parse('2006-12-31 23:59:59.999999' + @local_timezone), cursor[:out])
241
+ # test utc_offset (Time)
242
+ def obj.utc_offset; @utc_offset; end
243
+ obj.instance_variable_set(:@utc_offset, 9 * 60 * 60)
244
+ cursor[:in] = obj
245
+ cursor.exec
246
+ assert_equal(DateTime.parse('2006-12-31 23:59:59.999999 +09:00'), cursor[:out])
247
+ obj.instance_variable_set(:@utc_offset, -5 * 60 * 60)
248
+ cursor[:in] = obj
249
+ cursor.exec
250
+ assert_equal(DateTime.parse('2006-12-31 23:59:59.999999 -05:00'), cursor[:out])
251
+ # test offset (DateTime)
252
+ def obj.offset; @offset; end
253
+ obj.instance_variable_set(:@offset, 9.to_r / 24)
254
+ cursor[:in] = obj
255
+ cursor.exec
256
+ assert_equal(DateTime.parse('2006-12-31 23:59:59.999999 +09:00'), cursor[:out])
257
+ obj.instance_variable_set(:@offset, -5.to_r / 24)
258
+ cursor[:in] = obj
259
+ cursor.exec
260
+ assert_equal(DateTime.parse('2006-12-31 23:59:59.999999 -05:00'), cursor[:out])
261
+ end
262
+
263
+ def test_timezone
264
+ if $oracle_version >= OCI8::ORAVER_9_0
265
+ # temporarily change the mapping to test OCI8::BindType::Util.default_timezone.
266
+ OCI8::BindType::Mapping[:date] = OCI8::BindType::TimeViaOCIDate
267
+ end
268
+ begin
269
+ assert_raise(ArgumentError) do
270
+ OCI8::BindType::Util.default_timezone = :invalid_value
271
+ end
272
+
273
+ [:local, :utc].each do |tz|
274
+ OCI8::BindType::Util.default_timezone = tz
275
+ @conn.exec("select sysdate, to_date('2008-01-02', 'yyyy-mm-dd') from dual") do |row|
276
+ row.each do |dt|
277
+ assert_kind_of(Time, dt)
278
+ assert_equal(tz, dt.utc? ? :utc : :local)
279
+ end
280
+ assert_equal(2008, row[1].year)
281
+ assert_equal(1, row[1].month)
282
+ assert_equal(2, row[1].day)
283
+ end
284
+ end
285
+ ensure
286
+ OCI8::BindType::Util.default_timezone = :local
287
+ if $oracle_version >= OCI8::ORAVER_9_0
288
+ OCI8::BindType::Mapping[:date] = OCI8::BindType::Time
289
+ end
290
+ end
291
+
292
+ if $oracle_version >= OCI8::ORAVER_9_0
293
+ ses_tz = nil
294
+ @conn.exec('select sessiontimezone from dual') do |row|
295
+ ses_tz = row[0]
296
+ end
297
+
298
+ begin
299
+ ['+09:00', '+00:00', '-05:00'].each do |tz|
300
+ @conn.exec("alter session set time_zone = '#{tz}'")
301
+ @conn.exec("select current_timestamp, sysdate, to_timestamp('2008-01-02', 'yyyy-mm-dd') from dual") do |row|
302
+ row.each do |dt|
303
+ case dt
304
+ when Time
305
+ assert_equal(tz, timezone_string(*((dt.utc_offset / 60).divmod 60)))
306
+ when DateTime
307
+ tz = tz.gsub(/:/, '') if RUBY_VERSION <= '1.8.5'
308
+ assert_equal(tz, dt.zone)
309
+ else
310
+ flunk "unexpedted type #{dt.class}"
311
+ end
312
+ end
313
+ assert_equal(2008, row[2].year)
314
+ assert_equal(1, row[2].month)
315
+ assert_equal(2, row[2].day)
316
+ end
317
+ end
318
+ ensure
319
+ @conn.exec("alter session set time_zone = '#{ses_tz}'")
320
+ end
321
+ else
322
+ end
323
+ end
324
+
325
+ def test_interval_ym_select
326
+ return if $oracle_version < OCI8::ORAVER_9_0
327
+
328
+ [['2006-01-01', '2004-03-01'],
329
+ ['2006-01-01', '2005-03-01'],
330
+ ['2006-01-01', '2006-03-01'],
331
+ ['2006-01-01', '2007-03-01']
332
+ ].each do |date1, date2|
333
+ @conn.exec(<<-EOS) do |row|
334
+ SELECT (TO_TIMESTAMP('#{date1}', 'YYYY-MM-DD')
335
+ - TO_TIMESTAMP('#{date2}', 'YYYY-MM-DD')) YEAR TO MONTH
336
+ FROM dual
337
+ EOS
338
+ assert_equal(DateTime.parse(date1), DateTime.parse(date2) >> row[0])
339
+ end
340
+ end
341
+ end
342
+
343
+ def test_interval_ym_out_bind
344
+ return if $oracle_version < OCI8::ORAVER_9_0
345
+
346
+ cursor = @conn.parse(<<-EOS)
347
+ DECLARE
348
+ ts1 TIMESTAMP;
349
+ ts2 TIMESTAMP;
350
+ BEGIN
351
+ ts1 := TO_TIMESTAMP(:in1, 'YYYY-MM-DD');
352
+ ts2 := TO_TIMESTAMP(:in2, 'YYYY-MM-DD');
353
+ :out := (ts1 - ts2) YEAR TO MONTH;
354
+ END;
355
+ EOS
356
+ cursor.bind_param(:out, nil, :interval_ym)
357
+ cursor.bind_param(:in1, nil, String, 36)
358
+ cursor.bind_param(:in2, nil, String, 36)
359
+ [['2006-01-01', '2004-03-01'],
360
+ ['2006-01-01', '2005-03-01'],
361
+ ['2006-01-01', '2006-03-01'],
362
+ ['2006-01-01', '2007-03-01']
363
+ ].each do |date1, date2|
364
+ cursor[:in1] = date1
365
+ cursor[:in2] = date2
366
+ cursor.exec
367
+ assert_equal(DateTime.parse(date1), DateTime.parse(date2) >> cursor[:out])
368
+ end
369
+ cursor.close
370
+ end
371
+
372
+ def test_interval_ym_in_bind
373
+ return if $oracle_version < OCI8::ORAVER_9_0
374
+
375
+ cursor = @conn.parse(<<-EOS)
376
+ DECLARE
377
+ ts1 TIMESTAMP;
378
+ BEGIN
379
+ ts1 := TO_TIMESTAMP(:in1, 'YYYY-MM-DD');
380
+ :out := TO_CHAR(ts1 + :in2, 'YYYY-MM-DD');
381
+ END;
382
+ EOS
383
+ cursor.bind_param(:out, nil, String, 36)
384
+ cursor.bind_param(:in1, nil, String, 36)
385
+ cursor.bind_param(:in2, nil, :interval_ym)
386
+ [['2006-01-01', -25],
387
+ ['2006-01-01', -24],
388
+ ['2006-01-01', -23],
389
+ ['2006-01-01', -13],
390
+ ['2006-01-01', -12],
391
+ ['2006-01-01', -11],
392
+ ['2006-01-01', +2],
393
+ ['2006-01-01', -2],
394
+ ['2006-01-01', +12]
395
+ ].each do |date, interval|
396
+ cursor[:in1] = date
397
+ cursor[:in2] = interval
398
+ cursor.exec
399
+ assert_equal(DateTime.parse(date) >> interval, DateTime.parse(cursor[:out]))
400
+ end
401
+ cursor.close
402
+ end
403
+
404
+ def test_interval_ds_select
405
+ return if $oracle_version < OCI8::ORAVER_9_0
406
+
407
+ [['2006-01-01', '2004-03-01'],
408
+ ['2006-01-01', '2005-03-01'],
409
+ ['2006-01-01', '2006-03-01'],
410
+ ['2006-01-01', '2007-03-01'],
411
+ ['2006-01-01', '2006-01-01 23:00:00'],
412
+ ['2006-01-01', '2006-01-01 00:59:00'],
413
+ ['2006-01-01', '2006-01-01 00:00:59'],
414
+ ['2006-01-01', '2006-01-01 00:00:00.999999'],
415
+ ['2006-01-01', '2006-01-01 23:59:59.999999'],
416
+ ['2006-01-01', '2005-12-31 23:00:00'],
417
+ ['2006-01-01', '2005-12-31 00:59:00'],
418
+ ['2006-01-01', '2005-12-31 00:00:59'],
419
+ ['2006-01-01', '2005-12-31 00:00:00.999999'],
420
+ ['2006-01-01', '2005-12-31 23:59:59.999999']
421
+ ].each do |date1, date2|
422
+ @conn.exec(<<-EOS) do |row|
423
+ SELECT (TO_TIMESTAMP('#{date1}', 'YYYY-MM-DD HH24:MI:SS.FF')
424
+ - TO_TIMESTAMP('#{date2}', 'YYYY-MM-DD HH24:MI:SS.FF')) DAY(3) TO SECOND
425
+ FROM dual
426
+ EOS
427
+ assert_in_delta(string_to_time(date1) - string_to_time(date2), row[0], 0.0000000001)
428
+ end
429
+ end
430
+ end
431
+
432
+ def test_interval_ds_out_bind
433
+ return if $oracle_version < OCI8::ORAVER_9_0
434
+
435
+ cursor = @conn.parse(<<-EOS)
436
+ DECLARE
437
+ ts1 TIMESTAMP;
438
+ ts2 TIMESTAMP;
439
+ BEGIN
440
+ ts1 := TO_TIMESTAMP(:in1, 'YYYY-MM-DD HH24:MI:SS.FF');
441
+ ts2 := TO_TIMESTAMP(:in2, 'YYYY-MM-DD HH24:MI:SS.FF');
442
+ :out := (ts1 - ts2) DAY TO SECOND(9);
443
+ END;
444
+ EOS
445
+ cursor.bind_param(:out, nil, :interval_ds)
446
+ cursor.bind_param(:in1, nil, String, 36)
447
+ cursor.bind_param(:in2, nil, String, 36)
448
+ [['2006-01-01', '2004-03-01'],
449
+ ['2006-01-01', '2005-03-01'],
450
+ ['2006-01-01', '2006-03-01'],
451
+ ['2006-01-01', '2007-03-01'],
452
+ ['2006-01-01', '2006-01-01 23:00:00'],
453
+ ['2006-01-01', '2006-01-01 00:59:00'],
454
+ ['2006-01-01', '2006-01-01 00:00:59'],
455
+ ['2006-01-01', '2006-01-01 00:00:00.999999'],
456
+ ['2006-01-01', '2006-01-01 23:59:59.999999'],
457
+ ['2006-01-01', '2005-12-31 23:00:00'],
458
+ ['2006-01-01', '2005-12-31 00:59:00'],
459
+ ['2006-01-01', '2005-12-31 00:00:59'],
460
+ ['2006-01-01', '2005-12-31 00:00:00.999999'],
461
+ ['2006-01-01', '2005-12-31 23:59:59.999999']
462
+ ].each do |date1, date2|
463
+ cursor[:in1] = date1
464
+ cursor[:in2] = date2
465
+ cursor.exec
466
+ assert_in_delta(string_to_time(date1) - string_to_time(date2), cursor[:out], 0.0000000001)
467
+ end
468
+ cursor.close
469
+ end
470
+
471
+ def test_interval_ds_in_bind
472
+ return if $oracle_version < OCI8::ORAVER_9_0
473
+
474
+ cursor = @conn.parse(<<-EOS)
475
+ DECLARE
476
+ ts1 TIMESTAMP;
477
+ BEGIN
478
+ ts1 := TO_TIMESTAMP(:in1, 'YYYY-MM-DD HH24:MI:SS.FF');
479
+ :out := TO_CHAR(ts1 + :in2, 'YYYY-MM-DD HH24:MI:SS.FF6');
480
+ END;
481
+ EOS
482
+ cursor.bind_param(:out, nil, String, 36)
483
+ cursor.bind_param(:in1, nil, String, 36)
484
+ cursor.bind_param(:in2, nil, :interval_ds)
485
+ [['2006-01-01', -22],
486
+ ['2006-01-01', -10],
487
+ ['2006-01-01', +2],
488
+ ['2006-01-01', +12],
489
+ ['2006-01-01', -1.to_r / 24], # one hour
490
+ ['2006-01-01', -1.to_r / (24*60)], # one minute
491
+ ['2006-01-01', -1.to_r / (24*60*60)], # one second
492
+ ['2006-01-01', -999999.to_r / (24*60*60*1000000)], # 0.999999 seconds
493
+ ['2006-01-01', +1.to_r / 24], # one hour
494
+ ['2006-01-01', +1.to_r / (24*60)], # one minute
495
+ ['2006-01-01', +1.to_r / (24*60*60)], # one second
496
+ ['2006-01-01', +999999.to_r / (24*60*60*1000000)] # 0.999999 seconds
497
+ ].each do |date, interval|
498
+ interval *= 86400
499
+ cursor[:in1] = date
500
+ cursor[:in2] = interval
501
+ cursor.exec
502
+ assert_equal(string_to_time(date) + interval, string_to_time(cursor[:out]))
503
+ end
504
+ cursor.close
505
+ end
506
+
507
+ def test_days_interval_ds_select
508
+ return if $oracle_version < OCI8::ORAVER_9_0
509
+
510
+ [['2006-01-01', '2004-03-01'],
511
+ ['2006-01-01', '2005-03-01'],
512
+ ['2006-01-01', '2006-03-01'],
513
+ ['2006-01-01', '2007-03-01'],
514
+ ['2006-01-01', '2006-01-01 23:00:00'],
515
+ ['2006-01-01', '2006-01-01 00:59:00'],
516
+ ['2006-01-01', '2006-01-01 00:00:59'],
517
+ ['2006-01-01', '2006-01-01 00:00:00.999999'],
518
+ ['2006-01-01', '2006-01-01 23:59:59.999999'],
519
+ ['2006-01-01', '2005-12-31 23:00:00'],
520
+ ['2006-01-01', '2005-12-31 00:59:00'],
521
+ ['2006-01-01', '2005-12-31 00:00:59'],
522
+ ['2006-01-01', '2005-12-31 00:00:00.999999'],
523
+ ['2006-01-01', '2005-12-31 23:59:59.999999']
524
+ ].each do |date1, date2|
525
+ begin
526
+ OCI8::BindType::IntervalDS.unit = :day
527
+ @conn.exec(<<-EOS) do |row|
528
+ SELECT (TO_TIMESTAMP('#{date1}', 'YYYY-MM-DD HH24:MI:SS.FF')
529
+ - TO_TIMESTAMP('#{date2}', 'YYYY-MM-DD HH24:MI:SS.FF')) DAY(3) TO SECOND
530
+ FROM dual
531
+ EOS
532
+ assert_equal(DateTime.parse(date1) - DateTime.parse(date2), row[0])
533
+ end
534
+ ensure
535
+ OCI8::BindType::IntervalDS.unit = :second
536
+ end
537
+ end
538
+ end
539
+
540
+ def test_days_interval_ds_out_bind
541
+ return if $oracle_version < OCI8::ORAVER_9_0
542
+
543
+ cursor = @conn.parse(<<-EOS)
544
+ DECLARE
545
+ ts1 TIMESTAMP;
546
+ ts2 TIMESTAMP;
547
+ BEGIN
548
+ ts1 := TO_TIMESTAMP(:in1, 'YYYY-MM-DD HH24:MI:SS.FF');
549
+ ts2 := TO_TIMESTAMP(:in2, 'YYYY-MM-DD HH24:MI:SS.FF');
550
+ :out := (ts1 - ts2) DAY TO SECOND(9);
551
+ END;
552
+ EOS
553
+ cursor.bind_param(:out, nil, :interval_ds)
554
+ cursor.bind_param(:in1, nil, String, 36)
555
+ cursor.bind_param(:in2, nil, String, 36)
556
+ [['2006-01-01', '2004-03-01'],
557
+ ['2006-01-01', '2005-03-01'],
558
+ ['2006-01-01', '2006-03-01'],
559
+ ['2006-01-01', '2007-03-01'],
560
+ ['2006-01-01', '2006-01-01 23:00:00'],
561
+ ['2006-01-01', '2006-01-01 00:59:00'],
562
+ ['2006-01-01', '2006-01-01 00:00:59'],
563
+ ['2006-01-01', '2006-01-01 00:00:00.999999'],
564
+ ['2006-01-01', '2006-01-01 23:59:59.999999'],
565
+ ['2006-01-01', '2005-12-31 23:00:00'],
566
+ ['2006-01-01', '2005-12-31 00:59:00'],
567
+ ['2006-01-01', '2005-12-31 00:00:59'],
568
+ ['2006-01-01', '2005-12-31 00:00:00.999999'],
569
+ ['2006-01-01', '2005-12-31 23:59:59.999999']
570
+ ].each do |date1, date2|
571
+ begin
572
+ OCI8::BindType::IntervalDS.unit = :day
573
+ cursor[:in1] = date1
574
+ cursor[:in2] = date2
575
+ cursor.exec
576
+ assert_equal(DateTime.parse(date1) - DateTime.parse(date2), cursor[:out])
577
+ ensure
578
+ OCI8::BindType::IntervalDS.unit = :second
579
+ end
580
+ end
581
+ cursor.close
582
+ end
583
+
584
+ def test_days_interval_ds_in_bind
585
+ return if $oracle_version < OCI8::ORAVER_9_0
586
+
587
+ cursor = @conn.parse(<<-EOS)
588
+ DECLARE
589
+ ts1 TIMESTAMP;
590
+ BEGIN
591
+ ts1 := TO_TIMESTAMP(:in1, 'YYYY-MM-DD');
592
+ :out := TO_CHAR(ts1 + :in2, 'YYYY-MM-DD HH24:MI:SS.FF');
593
+ END;
594
+ EOS
595
+ cursor.bind_param(:out, nil, String, 36)
596
+ cursor.bind_param(:in1, nil, String, 36)
597
+ cursor.bind_param(:in2, nil, :interval_ds)
598
+ [['2006-01-01', -22],
599
+ ['2006-01-01', -10],
600
+ ['2006-01-01', +2],
601
+ ['2006-01-01', +12],
602
+ ['2006-01-01', -1.to_r / 24], # one hour
603
+ ['2006-01-01', -1.to_r / (24*60)], # one minute
604
+ ['2006-01-01', -1.to_r / (24*60*60)], # one second
605
+ ['2006-01-01', -999999.to_r / (24*60*60*1000000)], # 0.999999 seconds
606
+ ['2006-01-01', +1.to_r / 24], # one hour
607
+ ['2006-01-01', +1.to_r / (24*60)], # one minute
608
+ ['2006-01-01', +1.to_r / (24*60*60)], # one second
609
+ ['2006-01-01', +999999.to_r / (24*60*60*1000000)] # 0.999999 seconds
610
+ ].each do |date, interval|
611
+ begin
612
+ OCI8::BindType::IntervalDS.unit = :day
613
+ cursor[:in1] = date
614
+ cursor[:in2] = interval
615
+ cursor.exec
616
+ assert_equal(DateTime.parse(date) + interval, DateTime.parse(cursor[:out]))
617
+ ensure
618
+ OCI8::BindType::IntervalDS.unit = :second
619
+ end
620
+ end
621
+ end
622
+ end # TestOCI8