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
143 def self.extended(db)
144   db.instance_exec do
145     extend_datasets(IntervalDatasetMethods)
146     add_conversion_proc(1186, Postgres::IntervalDatabaseMethods::PARSER)
147     if respond_to?(:register_array_type)
148       register_array_type('interval', :oid=>1187, :scalar_oid=>1186)
149     end
150     @schema_type_classes[:interval] = ActiveSupport::Duration
151   end
152 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
52 def self.literal_duration(duration)
53   h = Hash.new(0)
54   duration.parts.each{|unit, value| h[unit] += value}
55   s = String.new
56 
57   DURATION_UNITS.each do |unit|
58     if (v = h[unit]) != 0
59       s << "#{v.is_a?(Integer) ? v : sprintf('%0.6f', v)} #{unit} "
60     end
61   end
62 
63   if s.empty?
64     '0'
65   else
66     s
67   end
68 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
155 def bound_variable_arg(arg, conn)
156   case arg
157   when ActiveSupport::Duration
158     IntervalDatabaseMethods.literal_duration(arg)
159   else
160     super
161   end
162 end

Private Instance Methods

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
167 def schema_post_process(_)
168   super.each do |a|
169     h = a[1]
170     if h[:type] == :interval && h[:default] =~ /\A'([\w ]+)'::interval\z/
171       h[:ruby_default] = PARSER.call($1)
172     end
173   end
174 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
183 def typecast_value_interval(value)
184   case value
185   when ActiveSupport::Duration
186     value
187   when Numeric
188     ActiveSupport::Duration.new(value, [[:seconds, value]])
189   when String
190     PARSER.call(typecast_check_string_length(value, 1000))
191   else
192     raise Sequel::InvalidValue, "invalid value for interval type: #{value.inspect}"
193   end
194 end