pg_hstore_ops.rb

Path: lib/sequel/extensions/pg_hstore_ops.rb
Last Update: Fri Jul 13 14:52:09 +0000 2012

The pg_hstore_ops extension adds support to Sequel‘s DSL to make it easier to call PostgreSQL hstore functions and operators. The most common usage is taking an object that represents an SQL expression (such as a :symbol), and calling hstore on it:

  h = :hstore_column.hstore

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

  h - 'a'    # hstore_column - 'a'
  h['a']     # hstore_column -> 'a'

  h.concat(:other_hstore_column)       # ||
  h.has_key?('a')                      # ?
  h.contain_all(:array_column)         # ?&
  h.contain_any(:array_column)         # ?|
  h.contains(:other_hstore_column)     # @>
  h.contained_by(:other_hstore_column) # <@

  h.defined        # defined(hstore_column)
  h.delete('a')    # delete(hstore_column, 'a')
  h.each           # each(hstore_column)
  h.keys           # akeys(hstore_column)
  h.populate(:a)   # populate_record(a, hstore_column)
  h.record_set(:a) # (a #= hstore_column)
  h.skeys          # skeys(hstore_column)
  h.slice(:a)      # slice(hstore_column, a)
  h.svals          # svals(hstore_column)
  h.to_array       # hstore_to_array(hstore_column)
  h.to_matrix      # hstore_to_matrix(hstore_column)
  h.values         # avals(hstore_column)

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

If you are also using the pg_hstore extension, you should load it before loading this extension. Doing so will allow you to use HStore#op to get an HStoreOp, allowing you to perform hstore operations on hstore literals.

[Validate]