Module: Sequel::Postgres::IntervalDatabaseMethods

Defined in:
lib/sequel/extensions/pg_interval.rb

Defined Under Namespace

Classes: Parser

Constant Summary collapse

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

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

Parser.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(db) ⇒ Object

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



143
144
145
146
147
148
149
150
151
152
# File 'lib/sequel/extensions/pg_interval.rb', line 143

def self.extended(db)
  db.instance_exec do
    extend_datasets(IntervalDatasetMethods)
    add_conversion_proc(1186, Postgres::IntervalDatabaseMethods::PARSER)
    if respond_to?(:register_array_type)
      register_array_type('interval', :oid=>1187, :scalar_oid=>1186)
    end
    @schema_type_classes[:interval] = ActiveSupport::Duration
  end
end

.literal_duration(duration) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sequel/extensions/pg_interval.rb', line 52

def self.literal_duration(duration)
  h = Hash.new(0)
  duration.parts.each{|unit, value| h[unit] += value}
  s = String.new

  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?
    '0'
  else
    s
  end
end

Instance Method Details

#bound_variable_arg(arg, conn) ⇒ Object

Handle ActiveSupport::Duration values in bound variables.



155
156
157
158
159
160
161
162
# File 'lib/sequel/extensions/pg_interval.rb', line 155

def bound_variable_arg(arg, conn)
  case arg
  when ActiveSupport::Duration
    IntervalDatabaseMethods.literal_duration(arg)
  else
    super
  end
end