Class: OCI8::BindType::IntervalYM

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

Overview

– OCI8::BindType::IntervalYM ++

This is a helper class to select or bind Oracle data type INTERVAL YEAR TO MONTH. The retrieved value is the number of months between two timestamps.

The value can be applied to DateTime#>> to shift months. It can be applied to Time#months_since if activisupport has been loaded.

How to select INTERVAL YEAR TO MONTH

INTERVAL YEAR TO MONTH is selected as an Integer.

conn.exec("select (current_timestamp - hiredate) year to month from emp") do |hired_months|
  puts "hired_months = #{hired_months}"
end

How to bind INTERVAL YEAR TO MONTH

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

# output bind variable
cursor = conn.parse(<<-EOS)
  BEGIN
    :interval := (:ts1 - :ts2) YEAR TO MONTH;
  END;
EOS
cursor.bind_param(:interval, nil, :interval_ym)
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] # => 4 (months)
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, Date.parse('1969-11-19'))
cursor.bind_param(:interval, 4, :interval_ym)
cursor.exec
cursor[:ts1].strftime('%Y-%m-%d') # => 1970-03-19
cursor.close

Instance Method Summary collapse

Instance Method Details

#getObject

:nodoc:


479
480
481
482
483
484
# File 'lib/oci8/datetime.rb', line 479

def get() # :nodoc:
  val = super()
  return nil if val.nil?
  year, month = val
  year * 12 + month
end

#set(val) ⇒ Object

:nodoc:


473
474
475
476
477
478
# File 'lib/oci8/datetime.rb', line 473

def set(val) # :nodoc:
  unless val.nil?
    val = [val / 12, val % 12]
  end
  super(val)
end