class Sequel::Postgres::PGRange

Attributes

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.

Public Class Methods

empty(db_type=nil) click to toggle source

Create an empty PGRange with the given database type.

    # File lib/sequel/extensions/pg_range.rb
337 def self.empty(db_type=nil)
338   new(nil, nil, :empty=>true, :db_type=>db_type)
339 end
from_range(range, db_type=nil) click to toggle source

Create a new PGRange instance using the beginning and ending of the ruby Range, with the given db_type.

    # File lib/sequel/extensions/pg_range.rb
332 def self.from_range(range, db_type=nil)
333   new(range.begin, range.end, :exclude_end=>range.exclude_end?, :db_type=>db_type)
334 end
new(beg, en, opts=OPTS) click to toggle source

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
347 def initialize(beg, en, opts=OPTS)
348   @begin = beg
349   @end = en
350   @empty = !!opts[:empty]
351   @exclude_begin = !!opts[:exclude_begin]
352   @exclude_end = !!opts[:exclude_end]
353   @db_type = opts[:db_type]
354   if @empty
355     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?
356   end
357 end

Public Instance Methods

==(other)
Alias for: eql?
===(other) click to toggle source

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
408 def ===(other)
409   if eql?(other)
410     true
411   else
412     if valid_ruby_range?
413       to_range === other 
414     else
415       false
416     end
417   end
418 end
cover?(value) click to toggle source

Return whether the value is inside the range.

    # File lib/sequel/extensions/pg_range.rb
366 def cover?(value)
367   return false if empty?
368   b = self.begin
369   return false if b && b.public_send(exclude_begin? ? :>= : :>, value)
370   e = self.end
371   return false if e && e.public_send(exclude_end? ? :<= : :<, value)
372   true
373 end
empty?() click to toggle source

Whether this range is empty (has no points). Note that for manually created ranges (ones not retrieved from the database), this will only be true if the range was created using the :empty option.

    # File lib/sequel/extensions/pg_range.rb
423 def empty?
424   @empty
425 end
eql?(other) click to toggle source

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
379 def eql?(other)
380   case other
381   when PGRange
382     if db_type == other.db_type
383       if empty?
384         other.empty?
385       elsif other.empty?
386         false
387       else
388         [:@begin, :@end, :@exclude_begin, :@exclude_end].all?{|v| instance_variable_get(v) == other.instance_variable_get(v)}
389       end
390     else
391       false
392     end
393   when Range
394     if valid_ruby_range?
395       to_range.eql?(other)
396     else
397       false
398     end
399   else
400     false
401   end
402 end
Also aliased as: ==
exclude_begin?() click to toggle source

Whether the beginning element is excluded from the range.

    # File lib/sequel/extensions/pg_range.rb
428 def exclude_begin?
429   @exclude_begin
430 end
exclude_end?() click to toggle source

Whether the ending element is excluded from the range.

    # File lib/sequel/extensions/pg_range.rb
433 def exclude_end?
434   @exclude_end
435 end
op() click to toggle source

Wrap the PGRange instance in an RangeOp, allowing you to easily use the PostgreSQL range functions and operators with literal ranges.

    # File lib/sequel/extensions/pg_range_ops.rb
123 def op
124   RangeOp.new(self)
125 end
sql_literal_append(ds, sql) click to toggle source

Append a literalize version of the receiver to the sql.

    # File lib/sequel/extensions/pg_range.rb
438 def sql_literal_append(ds, sql)
439   if (s = @db_type) && !empty?
440     sql << s.to_s << "("
441     ds.literal_append(sql, self.begin)
442     sql << ','
443     ds.literal_append(sql, self.end)
444     sql << ','
445     ds.literal_append(sql, "#{exclude_begin? ? "(" : "["}#{exclude_end? ? ")" : "]"}")
446     sql << ")"
447   else
448     ds.literal_append(sql, unquoted_literal(ds))
449     if s
450       sql << '::' << s.to_s
451     end
452   end
453 end
to_range() click to toggle source

Return a ruby Range object for this instance, if one can be created.

    # File lib/sequel/extensions/pg_range.rb
456 def to_range
457   return @range if @range
458   raise(Error, "cannot create ruby range for an empty PostgreSQL range") if empty?
459   raise(Error, "cannot create ruby range when PostgreSQL range excludes beginning element") if exclude_begin?
460   raise(Error, "cannot create ruby range when PostgreSQL range has unbounded beginning") unless self.begin
461   raise(Error, "cannot create ruby range when PostgreSQL range has unbounded ending") unless self.end
462   @range = Range.new(self.begin, self.end, exclude_end?)
463 end
unbounded_begin?() click to toggle source

Whether the beginning of the range is unbounded.

    # File lib/sequel/extensions/pg_range.rb
473 def unbounded_begin?
474   self.begin.nil? && !empty?
475 end
unbounded_end?() click to toggle source

Whether the end of the range is unbounded.

    # File lib/sequel/extensions/pg_range.rb
478 def unbounded_end?
479   self.end.nil? && !empty?
480 end
unquoted_literal(ds) click to toggle source

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
484 def unquoted_literal(ds)
485   if empty?
486     'empty'
487   else
488     "#{exclude_begin? ? "(" : "["}#{escape_value(self.begin, ds)},#{escape_value(self.end, ds)}#{exclude_end? ? ")" : "]"}"
489   end
490 end
valid_ruby_range?() click to toggle source

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
468 def valid_ruby_range?
469   !(empty? || exclude_begin? || !self.begin || !self.end)
470 end

Private Instance Methods

escape_value(k, ds) click to toggle source

Escape common range types. Instead of quoting, just backslash escape all special characters.

    # File lib/sequel/extensions/pg_range.rb
496 def escape_value(k, ds)
497   case k
498   when nil
499     ''
500   when Date, Time
501     ds.literal(k)[1...-1]
502   when Integer, Float
503     k.to_s
504   when BigDecimal
505     k.to_s('F')
506   when LiteralString
507     k
508   when String
509     if k.empty?
510       '""'
511     else
512       k.gsub(/("|,|\\|\[|\]|\(|\))/, '\\\\\1')
513     end
514   else
515     ds.literal(k).gsub(/("|,|\\|\[|\]|\(|\))/, '\\\\\1')
516   end
517 end