Class | Sequel::Postgres::PGRange |
In: |
lib/sequel/extensions/pg_range_ops.rb
lib/sequel/extensions/pg_range.rb |
Parent: | Object |
RANGE_TYPES | = | {} | Map of string database type names to type symbols (e.g. ‘int4range’ => :int4range), used in the schema parsing. | |
EMPTY | = | 'empty'.freeze | ||
EMPTY_STRING | = | ''.freeze | ||
QUOTED_EMPTY_STRING | = | '""'.freeze | ||
OPEN_PAREN | = | "(".freeze | ||
CLOSE_PAREN | = | ")".freeze | ||
OPEN_BRACKET | = | "[".freeze | ||
CLOSE_BRACKET | = | "]".freeze | ||
ESCAPE_RE | = | /("|,|\\|\[|\]|\(|\))/.freeze | ||
ESCAPE_REPLACE | = | '\\\\\1'.freeze | ||
CAST | = | '::'.freeze |
begin | [R] | The beginning of the range. If nil, the range has an unbounded beginning. |
db_type | [R] | The PostgreSQL database type for the range (e.g. ‘int4range’). |
end | [R] | The end of the range. If nil, the range has an unbounded ending. |
Initialize a new PGRange instance. Accepts the following options:
:db_type : | The PostgreSQL database type for the range. |
:empty : | Whether the range is empty (has no points) |
:exclude_begin : | Whether the beginning element is excluded from the range. |
:exclude_end : | Whether the ending element is excluded from the range. |
# File lib/sequel/extensions/pg_range.rb, line 331 331: def initialize(beg, en, opts=OPTS) 332: @begin = beg 333: @end = en 334: @empty = !!opts[:empty] 335: @exclude_begin = !!opts[:exclude_begin] 336: @exclude_end = !!opts[:exclude_end] 337: @db_type = opts[:db_type] 338: if @empty 339: raise(Error, 'cannot have an empty range with either a beginning or ending') unless @begin.nil? && @end.nil? && opts[:exclude_begin].nil? && opts[:exclude_end].nil? 340: end 341: end
Registers a range type that the extension should handle. Makes a Database instance that has been extended with DatabaseMethods recognize the range type given and set up the appropriate typecasting. Also sets up automatic typecasting for the native postgres adapter, so that on retrieval, the values are automatically converted to PGRange instances. The db_type argument should be the name of the range type. Accepts the following 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 Sequel postgres adapter to set up automatic type conversion on retrieval from the database. |
: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, line 95 95: def self.register(db_type, opts=OPTS, &block) 96: db_type = db_type.to_s.dup.freeze 97: 98: if converter = opts[:converter] 99: raise Error, "can't provide both a block and :converter option to register" if block 100: else 101: converter = block 102: end 103: 104: if soid = opts[:subtype_oid] 105: raise Error, "can't provide both a converter and :scalar_oid option to register" if converter 106: raise Error, "no conversion proc for :scalar_oid=>#{soid.inspect} in PG_TYPES" unless converter = PG_TYPES[soid] 107: end 108: 109: parser = Parser.new(db_type, converter) 110: 111: RANGE_TYPES[db_type] = db_type.to_sym 112: 113: DatabaseMethods.define_range_typecast_method(db_type, parser) 114: 115: if oid = opts[:oid] 116: Sequel::Postgres::PG_TYPES[oid] = parser 117: end 118: 119: nil 120: end
Allow PGRange values in case statements, where they return true if they are equal to each other using eql?, or if this PGRange can be converted to a Range, delegating to that range.
# File lib/sequel/extensions/pg_range.rb, line 383 383: def ===(other) 384: if eql?(other) 385: true 386: else 387: if valid_ruby_range? 388: to_range === other 389: else 390: false 391: end 392: end 393: end
Consider the receiver equal to other PGRange instances with the same beginning, ending, exclusions, and database type. Also consider it equal to Range instances if this PGRange can be converted to a a Range and those ranges are equal.
# File lib/sequel/extensions/pg_range.rb, line 354 354: def eql?(other) 355: case other 356: when PGRange 357: if db_type == other.db_type 358: if empty? 359: other.empty? 360: elsif other.empty? 361: false 362: else 363: [:@begin, :@end, :@exclude_begin, :@exclude_end].all?{|v| instance_variable_get(v) == other.instance_variable_get(v)} 364: end 365: else 366: false 367: end 368: when Range 369: if valid_ruby_range? 370: to_range.eql?(other) 371: else 372: false 373: end 374: else 375: false 376: end 377: end
Whether the beginning element is excluded from the range.
# File lib/sequel/extensions/pg_range.rb, line 401 401: def exclude_begin? 402: @exclude_begin 403: end
Whether the ending element is excluded from the range.
# File lib/sequel/extensions/pg_range.rb, line 406 406: def exclude_end? 407: @exclude_end 408: end
Append a literalize version of the receiver to the sql.
# File lib/sequel/extensions/pg_range.rb, line 411 411: def sql_literal_append(ds, sql) 412: ds.literal_append(sql, unquoted_literal(ds)) 413: if s = @db_type 414: sql << CAST << s.to_s 415: end 416: end
Return a ruby Range object for this instance, if one can be created.
# File lib/sequel/extensions/pg_range.rb, line 419 419: def to_range 420: return @range if @range 421: raise(Error, "cannot create ruby range for an empty PostgreSQL range") if empty? 422: raise(Error, "cannot create ruby range when PostgreSQL range excludes beginning element") if exclude_begin? 423: raise(Error, "cannot create ruby range when PostgreSQL range has unbounded beginning") unless self.begin 424: raise(Error, "cannot create ruby range when PostgreSQL range has unbounded ending") unless self.end 425: @range = Range.new(self.begin, self.end, exclude_end?) 426: end
Whether the beginning of the range is unbounded.
# File lib/sequel/extensions/pg_range.rb, line 436 436: def unbounded_begin? 437: self.begin.nil? && !empty? 438: end
Whether the end of the range is unbounded.
# File lib/sequel/extensions/pg_range.rb, line 441 441: def unbounded_end? 442: self.end.nil? && !empty? 443: end
Return a string containing the unescaped version of the range. Separated out for use by the bound argument code.
# File lib/sequel/extensions/pg_range.rb, line 447 447: def unquoted_literal(ds) 448: if empty? 449: EMPTY 450: else 451: "#{exclude_begin? ? OPEN_PAREN : OPEN_BRACKET}#{escape_value(self.begin, ds)},#{escape_value(self.end, ds)}#{exclude_end? ? CLOSE_PAREN : CLOSE_BRACKET}" 452: end 453: end
Whether or not this PGRange is a valid ruby range. In order to be a valid ruby range, it must have a beginning and an ending (no unbounded ranges), and it cannot exclude the beginning element.
# File lib/sequel/extensions/pg_range.rb, line 431 431: def valid_ruby_range? 432: !(empty? || exclude_begin? || !self.begin || !self.end) 433: end