module Sequel::Postgres::PGRange::DatabaseMethods

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 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

bound_variable_arg(arg, conn) click to toggle source

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() click to toggle source

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_range_type(db_type, opts=OPTS, &block) click to toggle source

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