module Sequel::Postgres::IntervalDatabaseMethods

Constants

DURATION_UNITS
EMPTY_INTERVAL
PARSER

Single instance of Parser used for parsing, to save on memory (since the parser has no state).

Public Class Methods

extended(db) click to toggle source

Reset the conversion procs if using the native postgres adapter, and extend the datasets to correctly literalize ActiveSupport::Duration values.

# File lib/sequel/extensions/pg_interval.rb, line 124
def self.extended(db)
  db.instance_eval do
    extend_datasets(IntervalDatasetMethods)
    copy_conversion_procs([1186, 1187])
    @schema_type_classes[:interval] = ActiveSupport::Duration
  end
end
literal_duration(duration) click to toggle source

Return an unquoted string version of the duration object suitable for use as a bound variable.

# File lib/sequel/extensions/pg_interval.rb, line 49
def self.literal_duration(duration)
  h = Hash.new(0)
  duration.parts.each{|unit, value| h[unit] += value}
  s = ''

  DURATION_UNITS.each do |unit|
    if (v = h[unit]) != 0
      s << "#{v.is_a?(Integer) ? v : sprintf('%0.6f', v)} #{unit} "
    end
  end

  if s.empty?
    EMPTY_INTERVAL
  else
    s
  end
end

Public Instance Methods

bound_variable_arg(arg, conn) click to toggle source

Handle ActiveSupport::Duration values in bound variables.

Calls superclass method
# File lib/sequel/extensions/pg_interval.rb, line 133
def bound_variable_arg(arg, conn)
  case arg
  when ActiveSupport::Duration
    IntervalDatabaseMethods.literal_duration(arg)
  else
    super
  end
end

Private Instance Methods

bound_variable_array(a) click to toggle source

Handle arrays of interval types in bound variables.

Calls superclass method
# File lib/sequel/extensions/pg_interval.rb, line 145
def bound_variable_array(a)
  case a
  when ActiveSupport::Duration
    "\"#{IntervalDatabaseMethods.literal_duration(a)}\""
  else
    super
  end
end
typecast_value_interval(value) click to toggle source

Typecast value correctly to an ActiveSupport::Duration instance. If already an ActiveSupport::Duration, return it. If a numeric argument is given, assume it represents a number of seconds, and create a new ActiveSupport::Duration instance representing that number of seconds. If a String, assume it is in PostgreSQL interval output format and attempt to parse it.

# File lib/sequel/extensions/pg_interval.rb, line 161
def typecast_value_interval(value)
  case value
  when ActiveSupport::Duration
    value
  when Numeric
    ActiveSupport::Duration.new(value, [[:seconds, value]])
  when String
    PARSER.call(value)
  else
    raise Sequel::InvalidValue, "invalid value for interval type: #{value.inspect}"
  end
end