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
Source
# 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
Reset the conversion procs if using the native postgres adapter, and extend the datasets to correctly literalize ActiveSupport::Duration values.
Source
# 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
Return an unquoted string version of the duration object suitable for use as a bound variable.
Public Instance Methods
Source
# 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
Handle ActiveSupport::Duration values in bound variables.
Private Instance Methods
Source
# 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
Set the :ruby_default value if the default value is recognized as an interval.
Source
# 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
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.