Last Update: 2021-12-18 12:49:34 -0800

The pg_range_ops extension adds support to Sequel’s DSL to make it easier to call PostgreSQL range and multirange functions and operators.

To load the extension:

Sequel.extension :pg_range_ops

The most common usage is passing an expression to Sequel.pg_range_op:

r = Sequel.pg_range_op(:range)

If you have also loaded the pg_range or pg_multirange extensions, you can use Sequel.pg_range or Sequel.pg_multirange as well:

r = Sequel.pg_range(:range)
r = Sequel.pg_multirange(:range)

Also, on most Sequel expression objects, you can call the pg_range method:

r = Sequel[:range].pg_range

If you have loaded the core_extensions extension, or you have loaded the core_refinements extension and have activated refinements for the file, you can also use Symbol#pg_range:

r = :range.pg_range

This creates a Sequel::Postgres::RangeOp object that can be used for easier querying:

r.contains(:other)      # range @> other
r.contained_by(:other)  # range <@ other
r.overlaps(:other)      # range && other
r.left_of(:other)       # range << other
r.right_of(:other)      # range >> other
r.starts_after(:other)  # range &> other
r.ends_before(:other)   # range &< other
r.adjacent_to(:other)   # range -|- other

r.lower            # lower(range)
r.upper            # upper(range)
r.isempty          # isempty(range)
r.lower_inc        # lower_inc(range)
r.upper_inc        # upper_inc(range)
r.lower_inf        # lower_inf(range)
r.upper_inf        # upper_inf(range)

All of the above methods work for both ranges and multiranges, as long as PostgreSQL supports the operation. The following methods are also supported:

r.range_merge      # range_merge(range)
r.unnest           # unnest(range)
r.multirange       # multirange(range)

range_merge and unnest expect the receiver to represent a multirange value, while multi_range expects the receiver to represent a range value.

See the PostgreSQL range and multirange function and operator documentation for more details on what these functions and operators do.

If you are also using the pg_range or pg_multirange extension, you should load them before loading this extension. Doing so will allow you to use PGRange#op and PGMultiRange#op to get a RangeOp, allowing you to perform range operations on range literals.

Related module: Sequel::Postgres::RangeOp