class Sequel::Postgres::HStoreOp

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
CONTAINED_BY
CONTAINS
CONTAIN_ALL
CONTAIN_ANY
HAS_KEY
LOOKUP
RECORD_SET

Public Instance Methods

-(other) click to toggle source

Delete entries from an hstore using the subtraction operator:

hstore_op - 'a' # (hstore - 'a')
Calls superclass method
# File lib/sequel/extensions/pg_hstore_ops.rb, line 92
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) click to toggle source

Lookup the value for the given key in an hstore:

hstore_op['a'] # (hstore -> 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb, line 104
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
akeys()
Alias for: keys
avals()
Alias for: values
concat(other)
Alias for: merge
contain_all(other) click to toggle source

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

hstore_op.contain_all(:a) # (hstore ?& a)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 116
def contain_all(other)
  bool_op(CONTAIN_ALL, wrap_input_array(other))
end
contain_any(other) click to toggle source

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

hstore_op.contain_any(:a) # (hstore ?| a)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 123
def contain_any(other)
  bool_op(CONTAIN_ANY, wrap_input_array(other))
end
contained_by(other) click to toggle source

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

hstore_op.contained_by(:h) # (hstore <@ h)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 137
def contained_by(other)
  bool_op(CONTAINED_BY, wrap_input_hash(other))
end
contains(other) click to toggle source

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

hstore_op.contains(:h) # (hstore @> h)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 130
def contains(other)
  bool_op(CONTAINS, wrap_input_hash(other))
end
defined(key) click to toggle source

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

hstore_op.defined('a') # defined(hstore, 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb, line 144
def defined(key)
  Sequel::SQL::BooleanExpression.new(:NOOP, function(:defined, key))
end
delete(key) click to toggle source

Delete the matching entries from the receiver:

hstore_op.delete('a') # delete(hstore, 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb, line 151
def delete(key)
  HStoreOp.new(function(:delete, wrap_input_array(wrap_input_hash(key))))
end
each() click to toggle source

Transform the receiver into a set of keys and values:

hstore_op.each # each(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 158
def each
  function(:each)
end
exist?(key)
Alias for: has_key?
has_key?(key) click to toggle source

Check if the receiver contains the given key:

hstore_op.has_key?('a') # (hstore ? 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb, line 165
def has_key?(key)
  bool_op(HAS_KEY, key)
end
Also aliased as: include?, key?, member?, exist?
hstore() click to toggle source

Return the receiver.

# File lib/sequel/extensions/pg_hstore_ops.rb, line 174
def hstore
  self
end
include?(key)
Alias for: has_key?
key?(key)
Alias for: has_key?
keys() click to toggle source

Return the keys as a PostgreSQL array:

hstore_op.keys # akeys(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 181
def keys
  wrap_output_array(function(:akeys))
end
Also aliased as: akeys
member?(key)
Alias for: has_key?
merge(other) click to toggle source

Merge a given hstore into the receiver:

hstore_op.merge(:a) # (hstore || a)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 189
def merge(other)
  HStoreOp.new(Sequel::SQL::PlaceholderLiteralString.new(CONCAT, [self, wrap_input_hash(other)]))
end
Also aliased as: concat
populate(record) click to toggle source

Create a new record populated with entries from the receiver:

hstore_op.populate(:a) # populate_record(a, hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 197
def populate(record)
  SQL::Function.new(:populate_record, record, self)
end
record_set(record) click to toggle source

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

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

Return the keys as a PostgreSQL set:

hstore_op.skeys # skeys(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 211
def skeys
  function(:skeys)
end
slice(keys) click to toggle source

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

hstore_op.slice(:a) # slice(hstore, a)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 218
def slice(keys)
  HStoreOp.new(function(:slice, wrap_input_array(keys)))
end
svals() click to toggle source

Return the values as a PostgreSQL set:

hstore_op.svals # svals(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 225
def svals
  function(:svals)
end
to_array() click to toggle source

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

hstore_op.to_array # hstore_to_array(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 233
def to_array
  wrap_output_array(function(:hstore_to_array))
end
to_matrix() click to toggle source

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

hstore_op.to_matrix # hstore_to_matrix(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 241
def to_matrix
  wrap_output_array(function(:hstore_to_matrix))
end
values() click to toggle source

Return the values as a PostgreSQL array:

hstore_op.values # avals(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb, line 248
def values
  wrap_output_array(function(:avals))
end
Also aliased as: avals

Private Instance Methods

bool_op(str, other) click to toggle source

Return a placeholder literal with the given str and args, wrapped in a boolean expression, used by operators that return booleans.

# File lib/sequel/extensions/pg_hstore_ops.rb, line 257
def bool_op(str, other)
  Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other]))
end
function(name, *args) click to toggle source

Return a function with the given name, and the receiver as the first argument, with any additional arguments given.

# File lib/sequel/extensions/pg_hstore_ops.rb, line 263
def function(name, *args)
  SQL::Function.new(name, self, *args)
end
wrap_input_array(obj) click to toggle source

Wrap argument in a PGArray if it is an array

# File lib/sequel/extensions/pg_hstore_ops.rb, line 268
def wrap_input_array(obj)
  if obj.is_a?(Array) && Sequel.respond_to?(:pg_array) 
    Sequel.pg_array(obj)
  else
    obj
  end
end
wrap_input_hash(obj) click to toggle source

Wrap argument in an Hstore if it is a hash

# File lib/sequel/extensions/pg_hstore_ops.rb, line 277
def wrap_input_hash(obj)
  if obj.is_a?(Hash) && Sequel.respond_to?(:hstore) 
    Sequel.hstore(obj)
  else
    obj
  end
end
wrap_output_array(obj) click to toggle source

Wrap argument in a PGArrayOp if supported

# File lib/sequel/extensions/pg_hstore_ops.rb, line 286
def wrap_output_array(obj)
  if Sequel.respond_to?(:pg_array_op) 
    Sequel.pg_array_op(obj)
  else
    obj
  end
end