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
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) 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
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) click to toggle source

Handle ActiveSupport::Duration values in bound variables.

Calls superclass method
    # 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

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
166 def bound_variable_array(a)
167   case a
168   when ActiveSupport::Duration
169     "\"#{IntervalDatabaseMethods.literal_duration(a)}\""
170   else
171     super
172   end
173 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
176 def schema_post_process(_)
177   super.each do |a|
178     h = a[1]
179     if h[:type] == :interval && h[:default] =~ /\A'([\w ]+)'::interval\z/
180       h[:ruby_default] = PARSER.call($1)
181     end
182   end
183 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
192 def typecast_value_interval(value)
193   case value
194   when ActiveSupport::Duration
195     value
196   when Numeric
197     ActiveSupport::Duration.new(value, [[:seconds, value]])
198   when String
199     PARSER.call(value)
200   else
201     raise Sequel::InvalidValue, "invalid value for interval type: #{value.inspect}"
202   end
203 end