module Sequel::Postgres::IntervalDatabaseMethods

  1. lib/sequel/extensions/pg_interval.rb

Methods

Public Class

  1. extended
  2. literal_duration

Public Instance

  1. bound_variable_arg

Constants

DURATION_UNITS = [:years, :months, :weeks, :days, :hours, :minutes, :seconds].freeze  
PARSER = Parser.new  

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

Public Class methods

extended(db)

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

[show source]
    # File lib/sequel/extensions/pg_interval.rb
142 def self.extended(db)
143   db.instance_exec do
144     extend_datasets(IntervalDatasetMethods)
145     add_conversion_proc(1186, Postgres::IntervalDatabaseMethods::PARSER)
146     if respond_to?(:register_array_type)
147       register_array_type('interval', :oid=>1187, :scalar_oid=>1186)
148     end
149     @schema_type_classes[:interval] = ActiveSupport::Duration
150   end
151 end
literal_duration(duration)

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

[show source]
   # File lib/sequel/extensions/pg_interval.rb
51 def self.literal_duration(duration)
52   h = Hash.new(0)
53   duration.parts.each{|unit, value| h[unit] += value}
54   s = String.new
55 
56   DURATION_UNITS.each do |unit|
57     if (v = h[unit]) != 0
58       s << "#{v.is_a?(Integer) ? v : sprintf('%0.6f', v)} #{unit} "
59     end
60   end
61 
62   if s.empty?
63     '0'
64   else
65     s
66   end
67 end

Public Instance methods

bound_variable_arg(arg, conn)

Handle ActiveSupport::Duration values in bound variables.

[show source]
    # File lib/sequel/extensions/pg_interval.rb
154 def bound_variable_arg(arg, conn)
155   case arg
156   when ActiveSupport::Duration
157     IntervalDatabaseMethods.literal_duration(arg)
158   else
159     super
160   end
161 end