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
Delete entries from an hstore using the subtraction operator:
hstore_op - 'a' # (hstore - 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb 117 def -(other) 118 other = if other.is_a?(String) && !other.is_a?(Sequel::LiteralString) 119 Sequel.cast_string(other) 120 else 121 wrap_input_array(wrap_input_hash(other)) 122 end 123 HStoreOp.new(super) 124 end
Lookup the value for the given key in an hstore:
hstore_op['a'] # (hstore -> 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb 129 def [](key) 130 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)) 131 wrap_output_array(Sequel::SQL::PlaceholderLiteralString.new(LOOKUP, [value, wrap_input_array(key)])) 132 else 133 v = case @value 134 when Symbol, SQL::Identifier, SQL::QualifiedIdentifier 135 HStoreSubscriptOp.new(self, key) 136 else 137 Sequel::SQL::PlaceholderLiteralString.new(LOOKUP, [value, key]) 138 end 139 Sequel::SQL::StringExpression.new(:NOOP, v) 140 end 141 end
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 146 def contain_all(other) 147 bool_op(CONTAIN_ALL, wrap_input_array(other)) 148 end
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 153 def contain_any(other) 154 bool_op(CONTAIN_ANY, wrap_input_array(other)) 155 end
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 167 def contained_by(other) 168 bool_op(CONTAINED_BY, wrap_input_hash(other)) 169 end
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 160 def contains(other) 161 bool_op(CONTAINS, wrap_input_hash(other)) 162 end
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 174 def defined(key) 175 Sequel::SQL::BooleanExpression.new(:NOOP, function(:defined, key)) 176 end
Delete the matching entries from the receiver:
hstore_op.delete('a') # delete(hstore, 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb 181 def delete(key) 182 HStoreOp.new(function(:delete, wrap_input_array(wrap_input_hash(key)))) 183 end
Transform the receiver into a set of keys and values:
hstore_op.each # each(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 188 def each 189 function(:each) 190 end
Check if the receiver contains the given key:
hstore_op.has_key?('a') # (hstore ? 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb 195 def has_key?(key) 196 bool_op(HAS_KEY, key) 197 end
Return the receiver.
# File lib/sequel/extensions/pg_hstore_ops.rb 204 def hstore 205 self 206 end
Return the keys as a PostgreSQL array:
hstore_op.keys # akeys(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 211 def keys 212 wrap_output_array(function(:akeys)) 213 end
Merge a given hstore into the receiver:
hstore_op.merge(:a) # (hstore || a)
# File lib/sequel/extensions/pg_hstore_ops.rb 219 def merge(other) 220 HStoreOp.new(Sequel::SQL::PlaceholderLiteralString.new(CONCAT, [self, wrap_input_hash(other)])) 221 end
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 227 def populate(record) 228 SQL::Function.new(:populate_record, record, self) 229 end
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 234 def record_set(record) 235 Sequel::SQL::PlaceholderLiteralString.new(RECORD_SET, [record, value]) 236 end
Return the keys as a PostgreSQL set:
hstore_op.skeys # skeys(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 241 def skeys 242 function(:skeys) 243 end
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 248 def slice(keys) 249 HStoreOp.new(function(:slice, wrap_input_array(keys))) 250 end
Return the values as a PostgreSQL set:
hstore_op.svals # svals(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 255 def svals 256 function(:svals) 257 end
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 263 def to_array 264 wrap_output_array(function(:hstore_to_array)) 265 end
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 271 def to_matrix 272 wrap_output_array(function(:hstore_to_matrix)) 273 end
Return the values as a PostgreSQL array:
hstore_op.values # avals(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 278 def values 279 wrap_output_array(function(:avals)) 280 end
Private Instance Methods
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 287 def bool_op(str, other) 288 Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other])) 289 end
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 293 def function(name, *args) 294 SQL::Function.new(name, self, *args) 295 end
Wrap argument in a PGArray
if it is an array
# File lib/sequel/extensions/pg_hstore_ops.rb 298 def wrap_input_array(obj) 299 if obj.is_a?(Array) && Sequel.respond_to?(:pg_array) 300 Sequel.pg_array(obj) 301 else 302 obj 303 end 304 end
Wrap argument in an Hstore if it is a hash
# File lib/sequel/extensions/pg_hstore_ops.rb 307 def wrap_input_hash(obj) 308 if obj.is_a?(Hash) && Sequel.respond_to?(:hstore) 309 Sequel.hstore(obj) 310 else 311 obj 312 end 313 end
Wrap argument in a PGArrayOp if supported
# File lib/sequel/extensions/pg_hstore_ops.rb 316 def wrap_output_array(obj) 317 if Sequel.respond_to?(:pg_array_op) 318 Sequel.pg_array_op(obj) 319 else 320 obj 321 end 322 end