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 104 def -(other) 105 other = if other.is_a?(String) && !other.is_a?(Sequel::LiteralString) 106 Sequel.cast_string(other) 107 else 108 wrap_input_array(wrap_input_hash(other)) 109 end 110 HStoreOp.new(super) 111 end
Lookup the value for the given key in an hstore:
hstore_op['a'] # (hstore -> 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb 116 def [](key) 117 v = Sequel::SQL::PlaceholderLiteralString.new(LOOKUP, [value, wrap_input_array(key)]) 118 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)) 119 wrap_output_array(v) 120 else 121 Sequel::SQL::StringExpression.new(:NOOP, v) 122 end 123 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 128 def contain_all(other) 129 bool_op(CONTAIN_ALL, wrap_input_array(other)) 130 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 135 def contain_any(other) 136 bool_op(CONTAIN_ANY, wrap_input_array(other)) 137 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 149 def contained_by(other) 150 bool_op(CONTAINED_BY, wrap_input_hash(other)) 151 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 142 def contains(other) 143 bool_op(CONTAINS, wrap_input_hash(other)) 144 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 156 def defined(key) 157 Sequel::SQL::BooleanExpression.new(:NOOP, function(:defined, key)) 158 end
Delete the matching entries from the receiver:
hstore_op.delete('a') # delete(hstore, 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb 163 def delete(key) 164 HStoreOp.new(function(:delete, wrap_input_array(wrap_input_hash(key)))) 165 end
Transform the receiver into a set of keys and values:
hstore_op.each # each(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 170 def each 171 function(:each) 172 end
Check if the receiver contains the given key:
hstore_op.has_key?('a') # (hstore ? 'a')
# File lib/sequel/extensions/pg_hstore_ops.rb 177 def has_key?(key) 178 bool_op(HAS_KEY, key) 179 end
Return the receiver.
# File lib/sequel/extensions/pg_hstore_ops.rb 186 def hstore 187 self 188 end
Return the keys as a PostgreSQL array:
hstore_op.keys # akeys(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 193 def keys 194 wrap_output_array(function(:akeys)) 195 end
Merge a given hstore into the receiver:
hstore_op.merge(:a) # (hstore || a)
# File lib/sequel/extensions/pg_hstore_ops.rb 201 def merge(other) 202 HStoreOp.new(Sequel::SQL::PlaceholderLiteralString.new(CONCAT, [self, wrap_input_hash(other)])) 203 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 209 def populate(record) 210 SQL::Function.new(:populate_record, record, self) 211 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 216 def record_set(record) 217 Sequel::SQL::PlaceholderLiteralString.new(RECORD_SET, [record, value]) 218 end
Return the keys as a PostgreSQL set:
hstore_op.skeys # skeys(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 223 def skeys 224 function(:skeys) 225 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 230 def slice(keys) 231 HStoreOp.new(function(:slice, wrap_input_array(keys))) 232 end
Return the values as a PostgreSQL set:
hstore_op.svals # svals(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 237 def svals 238 function(:svals) 239 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 245 def to_array 246 wrap_output_array(function(:hstore_to_array)) 247 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 253 def to_matrix 254 wrap_output_array(function(:hstore_to_matrix)) 255 end
Return the values as a PostgreSQL array:
hstore_op.values # avals(hstore)
# File lib/sequel/extensions/pg_hstore_ops.rb 260 def values 261 wrap_output_array(function(:avals)) 262 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 269 def bool_op(str, other) 270 Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other])) 271 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 275 def function(name, *args) 276 SQL::Function.new(name, self, *args) 277 end
Wrap argument in a PGArray
if it is an array
# File lib/sequel/extensions/pg_hstore_ops.rb 280 def wrap_input_array(obj) 281 if obj.is_a?(Array) && Sequel.respond_to?(:pg_array) 282 Sequel.pg_array(obj) 283 else 284 obj 285 end 286 end
Wrap argument in an Hstore if it is a hash
# File lib/sequel/extensions/pg_hstore_ops.rb 289 def wrap_input_hash(obj) 290 if obj.is_a?(Hash) && Sequel.respond_to?(:hstore) 291 Sequel.hstore(obj) 292 else 293 obj 294 end 295 end
Wrap argument in a PGArrayOp if supported
# File lib/sequel/extensions/pg_hstore_ops.rb 298 def wrap_output_array(obj) 299 if Sequel.respond_to?(:pg_array_op) 300 Sequel.pg_array_op(obj) 301 else 302 obj 303 end 304 end