pg_multirange.rb

lib/sequel/extensions/pg_multirange.rb
Last Update: 2023-04-13 12:51:52 -0700

The pg_multirange extension adds support for the PostgreSQL 14+ multirange types to Sequel. PostgreSQL multirange types are similar to an array of ranges, where a match against the multirange is a match against any of the ranges in the multirange.

When PostgreSQL multirange values are retrieved, they are parsed and returned as instances of Sequel::Postgres::PGMultiRange. PGMultiRange mostly acts like an array of Sequel::Postgres::PGRange (see the pg_range extension).

In addition to the parser, this extension comes with literalizers for PGMultiRanges, so they can be used in queries and as bound variables.

To turn an existing array of Ranges into a PGMultiRange, use Sequel.pg_multirange. You must provide the type of multirange when creating the multirange:

Sequel.pg_multirange(array_of_date_ranges, :datemultirange)

To use this extension, load it into the Database instance:

DB.extension :pg_multirange

See the schema modification guide for details on using multirange type columns in CREATE/ALTER TABLE statements.

This extension makes it easy to add support for other multirange types. In general, you just need to make sure that the subtype is handled and has the appropriate converter installed. For user defined types, you can do this via:

DB.add_conversion_proc(subtype_oid){|string| }

Then you can call Sequel::Postgres::PGMultiRange::DatabaseMethods#register_multirange_type to automatically set up a handler for the range type. So if you want to support the timemultirange type (assuming the time type is already supported):

DB.register_multirange_type('timerange')

This extension integrates with the pg_array extension. If you plan to use arrays of multirange types, load the pg_array extension before the pg_multirange extension:

DB.extension :pg_array, :pg_multirange

The pg_multirange extension will automatically load the pg_range extension.

Related module: Sequel::Postgres::PGMultiRange

Required files

  1. delegate
  2. strscan