Class: Sequel::Postgres::HStoreOp

Inherits:
SQL::Wrapper show all
Defined in:
lib/sequel/extensions/pg_hstore_ops.rb

Overview

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

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from SQL::Wrapper

#value

Instance Method Summary collapse

Methods inherited from SQL::Wrapper

#initialize

Methods included from SQL::IsDistinctFrom::Methods

#is_distinct_from

Methods included from SQLite::JSONOpMethods

#sqlite_json_op, #sqlite_jsonb_op

Methods included from RangeOpMethods

#pg_range

Methods included from ArrayOpMethods

#pg_array

Methods included from JSONOpMethods

#pg_json, #pg_jsonb

Methods included from InetOpMethods

#pg_inet

Methods included from PGRowOp::ExpressionMethods

#pg_row

Methods included from SQL::SubscriptMethods

#sql_subscript

Methods included from SQL::StringMethods

#escaped_ilike, #escaped_like, #ilike, #like

Methods included from SQL::PatternMatchMethods

#!~, #=~

Methods included from SQL::OrderMethods

#asc, #desc

Methods included from SQL::NumericMethods

#+, #coerce

Methods included from SQL::ComplexExpressionMethods

#extract, #sql_boolean, #sql_number, #sql_string

Methods included from SQL::CastMethods

#cast, #cast_numeric, #cast_string

Methods included from SQL::BooleanMethods

#~

Methods included from SQL::AliasMethods

#as

Methods inherited from SQL::Expression

#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect

Constructor Details

This class inherits a constructor from Sequel::SQL::Wrapper

Instance Method Details

#-(other) ⇒ Object

Delete entries from an hstore using the subtraction operator:

hstore_op - 'a' # (hstore - 'a')


117
118
119
120
121
122
123
124
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 117

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) ⇒ Object

Lookup the value for the given key in an hstore:

hstore_op['a'] # (hstore -> 'a')


129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 129

def [](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(Sequel::SQL::PlaceholderLiteralString.new(LOOKUP, [value, wrap_input_array(key)]))
  else
    v = case @value
    when Symbol, SQL::Identifier, SQL::QualifiedIdentifier
      HStoreSubscriptOp.new(self, key)
    else
      Sequel::SQL::PlaceholderLiteralString.new(LOOKUP, [value, key])
    end
    Sequel::SQL::StringExpression.new(:NOOP, v)
  end
end

#contain_all(other) ⇒ Object

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

hstore_op.contain_all(:a) # (hstore ?& a)


146
147
148
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 146

def contain_all(other)
  bool_op(CONTAIN_ALL, wrap_input_array(other))
end

#contain_any(other) ⇒ Object

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

hstore_op.contain_any(:a) # (hstore ?| a)


153
154
155
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 153

def contain_any(other)
  bool_op(CONTAIN_ANY, wrap_input_array(other))
end

#contained_by(other) ⇒ Object

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

hstore_op.contained_by(:h) # (hstore <@ h)


167
168
169
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 167

def contained_by(other)
  bool_op(CONTAINED_BY, wrap_input_hash(other))
end

#contains(other) ⇒ Object

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

hstore_op.contains(:h) # (hstore @> h)


160
161
162
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 160

def contains(other)
  bool_op(CONTAINS, wrap_input_hash(other))
end

#defined(key) ⇒ Object

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

hstore_op.defined('a') # defined(hstore, 'a')


174
175
176
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 174

def defined(key)
  Sequel::SQL::BooleanExpression.new(:NOOP, function(:defined, key))
end

#delete(key) ⇒ Object

Delete the matching entries from the receiver:

hstore_op.delete('a') # delete(hstore, 'a')


181
182
183
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 181

def delete(key)
  HStoreOp.new(function(:delete, wrap_input_array(wrap_input_hash(key))))
end

#eachObject

Transform the receiver into a set of keys and values:

hstore_op.each # each(hstore)


188
189
190
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 188

def each
  function(:each)
end

#has_key?(key) ⇒ Boolean Also known as: include?, key?, member?, exist?

Check if the receiver contains the given key:

hstore_op.has_key?('a') # (hstore ? 'a')

Returns:

  • (Boolean)


195
196
197
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 195

def has_key?(key)
  bool_op(HAS_KEY, key)
end

#hstoreObject

Return the receiver.



204
205
206
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 204

def hstore
  self
end

#keysObject Also known as: akeys

Return the keys as a PostgreSQL array:

hstore_op.keys # akeys(hstore)


211
212
213
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 211

def keys
  wrap_output_array(function(:akeys))
end

#merge(other) ⇒ Object Also known as: concat

Merge a given hstore into the receiver:

hstore_op.merge(:a) # (hstore || a)


219
220
221
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 219

def merge(other)
  HStoreOp.new(Sequel::SQL::PlaceholderLiteralString.new(CONCAT, [self, wrap_input_hash(other)]))
end

#populate(record) ⇒ Object

Create a new record populated with entries from the receiver:

hstore_op.populate(:a) # populate_record(a, hstore)


227
228
229
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 227

def populate(record)
  SQL::Function.new(:populate_record, record, self)
end

#record_set(record) ⇒ Object

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

hstore_op.record_set(:a) # (a #= hstore)


234
235
236
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 234

def record_set(record)
  Sequel::SQL::PlaceholderLiteralString.new(RECORD_SET, [record, value])
end

#skeysObject

Return the keys as a PostgreSQL set:

hstore_op.skeys # skeys(hstore)


241
242
243
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 241

def skeys
  function(:skeys)
end

#slice(keys) ⇒ Object

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

hstore_op.slice(:a) # slice(hstore, a)


248
249
250
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 248

def slice(keys)
  HStoreOp.new(function(:slice, wrap_input_array(keys)))
end

#svalsObject

Return the values as a PostgreSQL set:

hstore_op.svals # svals(hstore)


255
256
257
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 255

def svals
  function(:svals)
end

#to_arrayObject

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

hstore_op.to_array # hstore_to_array(hstore)


263
264
265
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 263

def to_array
  wrap_output_array(function(:hstore_to_array))
end

#to_matrixObject

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

hstore_op.to_matrix # hstore_to_matrix(hstore)


271
272
273
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 271

def to_matrix
  wrap_output_array(function(:hstore_to_matrix))
end

#valuesObject Also known as: avals

Return the values as a PostgreSQL array:

hstore_op.values # avals(hstore)


278
279
280
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 278

def values
  wrap_output_array(function(:avals))
end