module Sequel::Postgres::IntervalDatabaseMethods

Constants

DURATION_UNITS
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
116 def self.extended(db)
117   db.instance_exec do
118     extend_datasets(IntervalDatasetMethods)
119     add_conversion_proc(1186, Postgres::IntervalDatabaseMethods::PARSER)
120     if respond_to?(:register_array_type)
121       register_array_type('interval', :oid=>1187, :scalar_oid=>1186)
122     end
123     @schema_type_classes[:interval] = ActiveSupport::Duration
124   end
125 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
44 def self.literal_duration(duration)
45   h = Hash.new(0)
46   duration.parts.each{|unit, value| h[unit] += value}
47   s = String.new
48 
49   DURATION_UNITS.each do |unit|
50     if (v = h[unit]) != 0
51       s << "#{v.is_a?(Integer) ? v : sprintf('%0.6f', v)} #{unit} "
52     end
53   end
54 
55   if s.empty?
56     '0'
57   else
58     s
59   end
60 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
128 def bound_variable_arg(arg, conn)
129   case arg
130   when ActiveSupport::Duration
131     IntervalDatabaseMethods.literal_duration(arg)
132   else
133     super
134   end
135 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
140 def bound_variable_array(a)
141   case a
142   when ActiveSupport::Duration
143     "\"#{IntervalDatabaseMethods.literal_duration(a)}\""
144   else
145     super
146   end
147 end
schema_post_process(_) click to toggle source

Set the :ruby_default value if the default value is recognized as an interval.

Calls superclass method
    # File lib/sequel/extensions/pg_interval.rb
150 def schema_post_process(_)
151   super.each do |a|
152     h = a[1]
153     if h[:type] == :interval && h[:default] =~ /\A'([\w ]+)'::interval\z/
154       h[:ruby_default] = PARSER.call($1)
155     end
156   end
157 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
166 def typecast_value_interval(value)
167   case value
168   when ActiveSupport::Duration
169     value
170   when Numeric
171     ActiveSupport::Duration.new(value, [[:seconds, value]])
172   when String
173     PARSER.call(value)
174   else
175     raise Sequel::InvalidValue, "invalid value for interval type: #{value.inspect}"
176   end
177 end