Class: OCI8::BindType::IntervalDS

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

Overview

– OCI8::BindType::IntervalDS ++

(new in 2.0)

This is a helper class to select or bind Oracle data type INTERVAL DAY TO SECOND. The retrieved value is the number of seconds between two typestamps as a Float.

Note that it is the number days as a Rational if OCI8::BindType::IntervalDS.unit is :day or the ruby-oci8 version is prior to 2.0.3.

How to bind INTERVAL DAY TO SECOND

You cannot bind a bind variable as INTERVAL DAY TO SECOND implicitly. It must be bound explicitly by OCI8::Cursor#bind_param.

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

# input bind variable
cursor = conn.parse(<<-EOS)
  BEGIN
    :ts1 := :ts2 + :interval;
  END;
EOS
cursor.bind_param(:ts1, nil, DateTime)
cursor.bind_param(:ts2, DateTime.parse('1969-07-20 20:17:40 00:00'))
cursor.bind_param(:interval, 10492615.0, :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
@@unit =
:second

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unit:second or :day

Retrieves the unit of interval.

Returns:

  • (:second or :day)

Since:

  • 2.0.3



543
544
545
# File 'lib/oci8/datetime.rb', line 543

def self.unit
  @@unit
end

.unit=(val) ⇒ Object

Changes the unit of interval. :second is the default.

Parameters:

  • val (:second or :day)

Since:

  • 2.0.3



551
552
553
554
555
556
557
558
# File 'lib/oci8/datetime.rb', line 551

def self.unit=(val)
  case val
  when :second, :day
    @@unit = val
  else
    raise 'unit should be :second or :day'
  end
end

Instance Method Details

#getObject

:nodoc:



592
593
594
595
596
597
598
599
600
601
602
# File 'lib/oci8/datetime.rb', line 592

def get() # :nodoc:
  val = super()
  return nil if val.nil?
  day, hour, minute, sec, fsec = val
  if @@unit == :second
    fsec = fsec / 1000000000.0
    day * 86400 + hour * 3600 + minute * 60 + sec + fsec
  else
    day + (hour * @@hour) + (minute * @@minute) + (sec * @@sec) + (fsec * @@fsec)
  end
end

#set(val) ⇒ Object

:nodoc:



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/oci8/datetime.rb', line 560

def set(val) # :nodoc:
  unless val.nil?
    if val < 0
      is_minus = true
      val = -val
    else
      is_minus = false
    end
    if @@unit == :second
      day, val = val.divmod 86400
      hour, val = val.divmod 3600
      minute, val = val.divmod 60
      sec, val = val.divmod 1
    else
      day, val = val.divmod 1
      hour, val = (val * 24).divmod 1
      minute, val = (val * 60).divmod 1
      sec, val = (val * 60).divmod 1
    end
    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