module Sequel::Postgres::PGRange::DatabaseMethods
Public Class Methods
Define a private range typecasting method for the given type that uses the parser argument to do the type conversion.
# File lib/sequel/extensions/pg_range.rb, line 211 def self.define_range_typecast_method(type, parser) meth = :"typecast_value_#{type}" define_method(meth){|v| typecast_value_pg_range(v, parser)} private meth end
Reset the conversion procs if using the native postgres adapter, and extend the datasets to correctly literalize ruby Range values.
# File lib/sequel/extensions/pg_range.rb, line 190 def self.extended(db) db.instance_eval do extend_datasets(DatasetMethods) copy_conversion_procs([3904, 3906, 3912, 3926, 3905, 3907, 3913, 3927]) [:int4range, :numrange, :tsrange, :tstzrange, :daterange, :int8range].each do |v| @schema_type_classes[v] = PGRange end end procs = db.conversion_procs procs[3908] = Parser.new("tsrange", procs[1114]) procs[3910] = Parser.new("tstzrange", procs[1184]) if defined?(PGArray::Creator) procs[3909] = PGArray::Creator.new("tsrange", procs[3908]) procs[3911] = PGArray::Creator.new("tstzrange", procs[3910]) end end
Public Instance Methods
Handle Range and PGRange values in bound variables
# File lib/sequel/extensions/pg_range.rb, line 218 def bound_variable_arg(arg, conn) case arg when PGRange arg.unquoted_literal(schema_utility_dataset) when Range PGRange.from_range(arg).unquoted_literal(schema_utility_dataset) else super end end
Private Instance Methods
Handle arrays of range types in bound variables.
# File lib/sequel/extensions/pg_range.rb, line 232 def bound_variable_array(a) case a when PGRange, Range "\"#{bound_variable_arg(a, nil)}\"" else super end end
Manually override the typecasting for tsrange and tstzrange types so that they use the database's timezone instead of the global Sequel timezone.
# File lib/sequel/extensions/pg_range.rb, line 244 def get_conversion_procs procs = super procs[3908] = Parser.new("tsrange", procs[1114]) procs[3910] = Parser.new("tstzrange", procs[1184]) if defined?(PGArray::Creator) procs[3909] = PGArray::Creator.new("tsrange", procs[3908]) procs[3911] = PGArray::Creator.new("tstzrange", procs[3910]) end procs end
Recognize the registered database range types.
# File lib/sequel/extensions/pg_range.rb, line 258 def schema_column_type(db_type) if type = RANGE_TYPES[db_type] type else super end end
Typecast value correctly to a PGRange. If already an PGRange instance with the same db_type, return as is. If a PGRange with a different subtype, return a new PGRange with the same values and the expected subtype. If a Range object, create a PGRange with the given db_type. If a string, assume it is in PostgreSQL output format and parse it using the parser.
# File lib/sequel/extensions/pg_range.rb, line 273 def typecast_value_pg_range(value, parser) case value when PGRange if value.db_type.to_s == parser.db_type value elsif value.empty? PGRange.empty(parser.db_type) else PGRange.new(value.begin, value.end, :exclude_begin=>value.exclude_begin?, :exclude_end=>value.exclude_end?, :db_type=>parser.db_type) end when Range PGRange.from_range(value, parser.db_type) when String parser.call(value) else raise Sequel::InvalidValue, "invalid value for range type: #{value.inspect}" end end