Class: OCI8::BindType::IntervalDS

Inherits:
OCIIntervalDS
  • Object
show all
Defined in:
lib/oci8/datetime.rb

Overview

– OCI8::BindType::IntervalDS ++

This is a helper class to bind ruby’s Rational object as Oracle’s INTERVAL DAY TO SECOND datatype.

Select

The fetched value for a INTERVAL DAY TO SECOND column is a Rational or an Integer. The value is usable to apply to DateTime#+ and DateTime#-.

Bind

You cannot bind as INTERVAL YEAR TO MONTH implicitly. It must be bound explicitly with :interval_ds.

# output
ts1 = DateTime.parse('1969-11-19 06:54:35 00:00')
ts2 = DateTime.parse('1969-07-20 20:17:40 00:00')
cursor = conn.parse(<<-EOS)
  BEGIN
    :itv := (:ts1 - :ts2) DAY TO SECOND;
  END;
EOS
cursor.bind_param(:itv, nil, :interval_ds)
cursor.bind_param(:ts1, ts1)
cursor.bind_param(:ts2, ts2)
cursor.exec
cursor[:itv] # == ts1 - ts2
cursor.close

# input
ts2 = DateTime.parse('1969-07-20 20:17:40 00:00')
itv = 121 + 10.to_r/24 + 36.to_r/(24*60) + 55.to_r/(24*60*60)
# 121 days, 10 hours,    36 minutes,       55 seconds
cursor = conn.parse(<<-EOS)
  BEGIN
    :ts1 := :ts2 + :itv;
  END;
EOS
cursor.bind_param(:ts1, nil, DateTime)
cursor.bind_param(:ts2, ts2)
cursor.bind_param(:itv, itv, :interval_ds)
cursor.exec
cursor[:ts1].strftime('%Y-%m-%d %H:%M:%S') # => 1969-11-19 06:54:35
cursor.close

Constant Summary collapse

@@hour =
1 / 24.to_r
@@minute =
@@hour / 60
@@sec =
@@minute / 60
@@fsec =
@@sec / 1000000000

Instance Method Summary collapse

Instance Method Details

#getObject

:nodoc:



482
483
484
485
486
487
# File 'lib/oci8/datetime.rb', line 482

def get() # :nodoc:
  val = super()
  return nil if val.nil?
  day, hour, minute, sec, fsec = val
  day + (hour * @@hour) + (minute * @@minute) + (sec * @@sec) + (fsec * @@fsec)
end

#set(val) ⇒ Object

:nodoc:



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/oci8/datetime.rb', line 457

def set(val) # :nodoc:
  unless val.nil?
    if val < 0
      is_minus = true
      val = -val
    else
      is_minus = false
    end
    day, val = val.divmod 1
    hour, val = (val * 24).divmod 1
    minute, val = (val * 60).divmod 1
    sec, val = (val * 60).divmod 1
    fsec, val = (val * 1000000000).divmod 1
    if is_minus
      day = - day
      hour = - hour
      minute = - minute
      sec = - sec
      fsec = - fsec
    end
    val = [day, hour, minute, sec, fsec]
  end
  super(val)
end