Public Class methods
Add the conversion procs to the database and extend the datasets to correctly literalize ruby Range
values.
# File lib/sequel/extensions/pg_range.rb 132 def self.extended(db) 133 db.instance_exec do 134 @pg_range_schema_types ||= {} 135 extend_datasets(DatasetMethods) 136 register_range_type('int4range', :oid=>3904, :subtype_oid=>23) 137 register_range_type('numrange', :oid=>3906, :subtype_oid=>1700) 138 register_range_type('tsrange', :oid=>3908, :subtype_oid=>1114) 139 register_range_type('tstzrange', :oid=>3910, :subtype_oid=>1184) 140 register_range_type('daterange', :oid=>3912, :subtype_oid=>1082) 141 register_range_type('int8range', :oid=>3926, :subtype_oid=>20) 142 if respond_to?(:register_array_type) 143 register_array_type('int4range', :oid=>3905, :scalar_oid=>3904, :scalar_typecast=>:int4range) 144 register_array_type('numrange', :oid=>3907, :scalar_oid=>3906, :scalar_typecast=>:numrange) 145 register_array_type('tsrange', :oid=>3909, :scalar_oid=>3908, :scalar_typecast=>:tsrange) 146 register_array_type('tstzrange', :oid=>3911, :scalar_oid=>3910, :scalar_typecast=>:tstzrange) 147 register_array_type('daterange', :oid=>3913, :scalar_oid=>3912, :scalar_typecast=>:daterange) 148 register_array_type('int8range', :oid=>3927, :scalar_oid=>3926, :scalar_typecast=>:int8range) 149 end 150 [:int4range, :numrange, :tsrange, :tstzrange, :daterange, :int8range].each do |v| 151 @schema_type_classes[v] = PGRange 152 end 153 154 procs = conversion_procs 155 add_conversion_proc(3908, Parser.new("tsrange", procs[1114])) 156 add_conversion_proc(3910, Parser.new("tstzrange", procs[1184])) 157 if respond_to?(:register_array_type) && defined?(PGArray::Creator) 158 add_conversion_proc(3909, PGArray::Creator.new("tsrange", procs[3908])) 159 add_conversion_proc(3911, PGArray::Creator.new("tstzrange", procs[3910])) 160 end 161 end 162 end
Public Instance methods
# File lib/sequel/extensions/pg_range.rb 165 def bound_variable_arg(arg, conn) 166 case arg 167 when PGRange 168 arg.unquoted_literal(schema_utility_dataset) 169 when Range 170 PGRange.from_range(arg).unquoted_literal(schema_utility_dataset) 171 else 172 super 173 end 174 end
Freeze the pg range schema types to prevent adding new ones.
# File lib/sequel/extensions/pg_range.rb 177 def freeze 178 @pg_range_schema_types.freeze 179 super 180 end
Register a database specific range type. This can be used to support different range types per Database
. Options:
:converter |
A callable object (e.g. Proc), that is called with the start or end of the range (usually a string), and should return the appropriate typecasted object. |
:oid |
The PostgreSQL OID for the range type. This is used by the |
:subtype_oid |
Should be the PostgreSQL OID for the range’s subtype. 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.
# File lib/sequel/extensions/pg_range.rb 194 def register_range_type(db_type, opts=OPTS, &block) 195 oid = opts[:oid] 196 soid = opts[:subtype_oid] 197 198 if has_converter = opts.has_key?(:converter) 199 raise Error, "can't provide both a block and :converter option to register_range_type" if block 200 converter = opts[:converter] 201 else 202 has_converter = true if block 203 converter = block 204 end 205 206 unless (soid || has_converter) && oid 207 range_oid, subtype_oid = from(:pg_range).join(:pg_type, :oid=>:rngtypid).where(:typname=>db_type.to_s).get([:rngtypid, :rngsubtype]) 208 soid ||= subtype_oid unless has_converter 209 oid ||= range_oid 210 end 211 212 db_type = db_type.to_s.dup.freeze 213 214 if soid 215 raise Error, "can't provide both a converter and :subtype_oid option to register" if has_converter 216 raise Error, "no conversion proc for :subtype_oid=>#{soid.inspect} in conversion_procs" unless converter = conversion_procs[soid] 217 end 218 219 parser = Parser.new(db_type, converter) 220 add_conversion_proc(oid, parser) 221 222 @pg_range_schema_types[db_type] = db_type.to_sym 223 224 singleton_class.class_eval do 225 meth = :"typecast_value_#{db_type}" 226 define_method(meth){|v| typecast_value_pg_range(v, parser)} 227 private meth 228 end 229 230 @schema_type_classes[:"#{opts[:type_symbol] || db_type}"] = PGRange 231 nil 232 end