module Sequel::Postgres::PGMultiRange::DatabaseMethods

  1. lib/sequel/extensions/pg_multirange.rb

Methods

Public Class

  1. extended

Public Instance

  1. bound_variable_arg
  2. freeze
  3. register_multirange_type

Public Class methods

extended(db)

Add the default multirange conversion procs to the database

[show source]
    # File lib/sequel/extensions/pg_multirange.rb
113 def self.extended(db)
114   db.instance_exec do
115     raise Error, "multiranges not supported on this database" unless server_version >= 140000
116 
117     extension :pg_range
118     @pg_multirange_schema_types ||= {}
119 
120     register_multirange_type('int4multirange', :range_oid=>3904, :oid=>4451)
121     register_multirange_type('nummultirange', :range_oid=>3906, :oid=>4532)
122     register_multirange_type('tsmultirange', :range_oid=>3908, :oid=>4533)
123     register_multirange_type('tstzmultirange', :range_oid=>3910, :oid=>4534)
124     register_multirange_type('datemultirange', :range_oid=>3912, :oid=>4535)
125     register_multirange_type('int8multirange', :range_oid=>3926, :oid=>4536)
126 
127     if respond_to?(:register_array_type)
128       register_array_type('int4multirange', :oid=>6150, :scalar_oid=>4451, :scalar_typecast=>:int4multirange)
129       register_array_type('nummultirange', :oid=>6151, :scalar_oid=>4532, :scalar_typecast=>:nummultirange)
130       register_array_type('tsmultirange', :oid=>6152, :scalar_oid=>4533, :scalar_typecast=>:tsmultirange)
131       register_array_type('tstzmultirange', :oid=>6153, :scalar_oid=>4534, :scalar_typecast=>:tstzmultirange)
132       register_array_type('datemultirange', :oid=>6155, :scalar_oid=>4535, :scalar_typecast=>:datemultirange)
133       register_array_type('int8multirange', :oid=>6157, :scalar_oid=>4536, :scalar_typecast=>:int8multirange)
134     end
135 
136     [:int4multirange, :nummultirange, :tsmultirange, :tstzmultirange, :datemultirange, :int8multirange].each do |v|
137       @schema_type_classes[v] = PGMultiRange
138     end
139 
140     procs = conversion_procs
141     add_conversion_proc(4533, PGMultiRange::Creator.new("tsmultirange", procs[3908]))
142     add_conversion_proc(4534, PGMultiRange::Creator.new("tstzmultirange", procs[3910]))
143 
144     if respond_to?(:register_array_type) && defined?(PGArray::Creator)
145       add_conversion_proc(6152, PGArray::Creator.new("tsmultirange", procs[4533]))
146       add_conversion_proc(6153, PGArray::Creator.new("tstzmultirange", procs[4534]))
147     end
148   end
149 end

Public Instance methods

bound_variable_arg(arg, conn)

Handle PGMultiRange values in bound variables

[show source]
    # File lib/sequel/extensions/pg_multirange.rb
152 def bound_variable_arg(arg, conn)
153   case arg
154   when PGMultiRange 
155     arg.unquoted_literal(schema_utility_dataset)
156   else
157     super
158   end
159 end
freeze()

Freeze the pg multirange schema types to prevent adding new ones.

[show source]
    # File lib/sequel/extensions/pg_multirange.rb
162 def freeze
163   @pg_multirange_schema_types.freeze
164   super
165 end
register_multirange_type(db_type, opts=OPTS, &block)

Register a database specific multirange type. This can be used to support different multirange types per Database. Options:

:converter

A callable object (e.g. Proc), that is called with the PostgreSQL range string, and should return a PGRange instance.

:oid

The PostgreSQL OID for the multirange type. This is used by Sequel to set up automatic type conversion on retrieval from the database.

:range_oid

Should be the PostgreSQL OID for the multirange subtype (the range type). If given, automatically sets the :converter option by looking for scalar conversion proc.

If a block is given, it is treated as the :converter option.

[show source]
    # File lib/sequel/extensions/pg_multirange.rb
179 def register_multirange_type(db_type, opts=OPTS, &block)
180   oid = opts[:oid]
181   soid = opts[:range_oid]
182 
183   if has_converter = opts.has_key?(:converter)
184     raise Error, "can't provide both a block and :converter option to register_multirange_type" if block
185     converter = opts[:converter]
186   else
187     has_converter = true if block
188     converter = block
189   end
190 
191   unless (soid || has_converter) && oid
192     range_oid, subtype_oid = from(:pg_range).join(:pg_type, :oid=>:rngmultitypid).where(:typname=>db_type.to_s).get([:rngmultitypid, :rngtypid])
193     soid ||= subtype_oid unless has_converter
194     oid ||= range_oid
195   end
196 
197   db_type = db_type.to_s.dup.freeze
198 
199   if soid
200     raise Error, "can't provide both a converter and :range_oid option to register" if has_converter 
201     raise Error, "no conversion proc for :range_oid=>#{soid.inspect} in conversion_procs" unless converter = conversion_procs[soid]
202   end
203 
204   raise Error, "cannot add a multirange type without a convertor (use :converter or :range_oid option or pass block)" unless converter
205   creator = Creator.new(db_type, converter)
206   add_conversion_proc(oid, creator)
207 
208   @pg_multirange_schema_types[db_type] = db_type.to_sym
209 
210   singleton_class.class_eval do
211     meth = :"typecast_value_#{db_type}"
212     scalar_typecast_method = :"typecast_value_#{opts.fetch(:scalar_typecast, db_type.sub('multirange', 'range'))}"
213     define_method(meth){|v| typecast_value_pg_multirange(v, creator, scalar_typecast_method)}
214     private meth
215   end
216 
217   @schema_type_classes[db_type] = PGMultiRange
218   nil
219 end