class Sequel::Postgres::HStoreOp

  1. lib/sequel/extensions/pg_hstore_ops.rb
Parent: Postgres

The HStoreOp class is a simple container for a single object that defines methods that yield Sequel expression objects representing PostgreSQL hstore operators and functions.

In the method documentation examples, assume that:

hstore_op = :hstore.hstore

Constants

CONCAT = ["(".freeze, " || ".freeze, ")".freeze].freeze  
CONTAINED_BY = ["(".freeze, " <@ ".freeze, ")".freeze].freeze  
CONTAINS = ["(".freeze, " @> ".freeze, ")".freeze].freeze  
CONTAIN_ALL = ["(".freeze, " ?& ".freeze, ")".freeze].freeze  
CONTAIN_ANY = ["(".freeze, " ?| ".freeze, ")".freeze].freeze  
HAS_KEY = ["(".freeze, " ? ".freeze, ")".freeze].freeze  
LOOKUP = ["(".freeze, " -> ".freeze, ")".freeze].freeze  
RECORD_SET = ["(".freeze, " #= ".freeze, ")".freeze].freeze  

Public Instance Aliases

akeys -> keys
avals -> values
concat -> merge
exist? -> has_key?
include? -> has_key?
key? -> has_key?
member? -> has_key?

Public Instance methods

- (other)

Delete entries from an hstore using the subtraction operator:

hstore_op - 'a' # (hstore - 'a')
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 84
def -(other)
  other = if other.is_a?(String) && !other.is_a?(Sequel::LiteralString)
    Sequel.cast_string(other)
  else
    wrap_input_array(wrap_input_hash(other))
  end
  HStoreOp.new(super)
end
[] (key)

Lookup the value for the given key in an hstore:

hstore_op['a'] # (hstore -> 'a')
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 96
def [](key)
  v = Sequel::SQL::PlaceholderLiteralString.new(LOOKUP, [value, wrap_input_array(key)])
  if key.is_a?(Array) || (defined?(Sequel::Postgres::PGArray) && key.is_a?(Sequel::Postgres::PGArray)) || (defined?(Sequel::Postgres::ArrayOp) && key.is_a?(Sequel::Postgres::ArrayOp))
    wrap_output_array(v)
  else
    Sequel::SQL::StringExpression.new(:NOOP, v)
  end
end
contain_all (other)

Check if the receiver contains all of the keys in the given array:

hstore_op.contain_all(:a) # (hstore ?& a)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 108
def contain_all(other)
  bool_op(CONTAIN_ALL, wrap_input_array(other))
end
contain_any (other)

Check if the receiver contains any of the keys in the given array:

hstore_op.contain_any(:a) # (hstore ?| a)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 115
def contain_any(other)
  bool_op(CONTAIN_ANY, wrap_input_array(other))
end
contained_by (other)

Check if the other hstore contains all entries in the receiver:

hstore_op.contained_by(:h) # (hstore <@ h)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 129
def contained_by(other)
  bool_op(CONTAINED_BY, wrap_input_hash(other))
end
contains (other)

Check if the receiver contains all entries in the other hstore:

hstore_op.contains(:h) # (hstore @> h)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 122
def contains(other)
  bool_op(CONTAINS, wrap_input_hash(other))
end
defined (key)

Check if the receiver contains a non-NULL value for the given key:

hstore_op.defined('a') # defined(hstore, 'a')
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 136
def defined(key)
  Sequel::SQL::BooleanExpression.new(:NOOP, function(:defined, key))
end
delete (key)

Delete the matching entries from the receiver:

hstore_op.delete('a') # delete(hstore, 'a')
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 143
def delete(key)
  HStoreOp.new(function(:delete, wrap_input_array(wrap_input_hash(key))))
end
each ()

Transform the receiver into a set of keys and values:

hstore_op.each # each(hstore)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 150
def each
  function(:each)
end
has_key? (key)

Check if the receiver contains the given key:

hstore_op.has_key?('a') # (hstore ? 'a')
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 157
def has_key?(key)
  bool_op(HAS_KEY, key)
end
hstore ()

Return the receiver.

[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 166
def hstore
  self
end
keys ()

Return the keys as a PostgreSQL array:

hstore_op.keys # akeys(hstore)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 173
def keys
  wrap_output_array(function(:akeys))
end
merge (other)

Merge a given hstore into the receiver:

hstore_op.merge(:a) # (hstore || a)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 181
def merge(other)
  HStoreOp.new(Sequel::SQL::PlaceholderLiteralString.new(CONCAT, [self, wrap_input_hash(other)]))
end
populate (record)

Create a new record populated with entries from the receiver:

hstore_op.populate(:a) # populate_record(a, hstore)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 189
def populate(record)
  SQL::Function.new(:populate_record, record, self)
end
record_set (record)

Update the values in a record using entries in the receiver:

hstore_op.record_set(:a) # (a #= hstore)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 196
def record_set(record)
  Sequel::SQL::PlaceholderLiteralString.new(RECORD_SET, [record, value])
end
skeys ()

Return the keys as a PostgreSQL set:

hstore_op.skeys # skeys(hstore)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 203
def skeys
  function(:skeys)
end
slice (keys)

Return an hstore with only the keys in the given array:

hstore_op.slice(:a) # slice(hstore, a)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 210
def slice(keys)
  HStoreOp.new(function(:slice, wrap_input_array(keys)))
end
svals ()

Return the values as a PostgreSQL set:

hstore_op.svals # svals(hstore)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 217
def svals
  function(:svals)
end
to_array ()

Return a flattened array of the receiver with alternating keys and values:

hstore_op.to_array # hstore_to_array(hstore)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 225
def to_array
  wrap_output_array(function(:hstore_to_array))
end
to_matrix ()

Return a nested array of the receiver, with arrays of 2 element (key/value) arrays:

hstore_op.to_matrix # hstore_to_matrix(hstore)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 233
def to_matrix
  wrap_output_array(function(:hstore_to_matrix))
end
values ()

Return the values as a PostgreSQL array:

hstore_op.values # avals(hstore)
[show source]
# File lib/sequel/extensions/pg_hstore_ops.rb, line 240
def values
  wrap_output_array(function(:avals))
end