module Sequel::Postgres::PGRange::DatabaseMethods
Public Class Methods
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 240 def self.extended(db) db.instance_eval do @pg_range_schema_types ||= {} 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 261 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
Freeze the pg range schema types to prevent adding new ones.
# File lib/sequel/extensions/pg_range.rb, line 273 def freeze @pg_range_schema_types.freeze super end
Register a database specific range type. This can be used to support different range types per Database. Use of this method does not affect global state, unlike Sequel::Postgres::PGRange.register. See Sequel::Postgres::PGRange.register for possible options.
# File lib/sequel/extensions/pg_range.rb, line 282 def register_range_type(db_type, opts=OPTS, &block) opts = {:type_procs=>conversion_procs, :typecast_method_map=>@pg_range_schema_types, :typecast_methods_module=>(class << self; self; end)}.merge!(opts) unless (opts.has_key?(:subtype_oid) || block) && opts.has_key?(:oid) range_oid, subtype_oid = from(:pg_range).join(:pg_type, :oid=>:rngtypid).where(:typname=>db_type.to_s).get([:rngtypid, :rngsubtype]) opts[:subtype_oid] = subtype_oid unless opts.has_key?(:subtype_oid) || block opts[:oid] = range_oid unless opts.has_key?(:oid) end PGRange.register(db_type, opts, &block) @schema_type_classes[:"#{opts[:type_symbol] || db_type}"] = PGRange conversion_procs_updated end
Private Instance Methods
Handle arrays of range types in bound variables.
# File lib/sequel/extensions/pg_range.rb, line 298 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 310 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 324 def schema_column_type(db_type) if type = @pg_range_schema_types[db_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 339 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