Class: Sequel::Postgres::JSONBOp

Inherits:
JSONBaseOp show all
Defined in:
lib/sequel/extensions/pg_json_ops.rb

Overview

JSONBaseOp subclass for the jsonb type.

In the method documentation examples, assume that:

jsonb_op = Sequel.pg_jsonb(:jsonb)

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
DELETE_PATH =
["(".freeze, " #- ".freeze, ")".freeze].freeze
HAS_KEY =
["(".freeze, " ? ".freeze, ")".freeze].freeze

Constants inherited from JSONBaseOp

Sequel::Postgres::JSONBaseOp::GET, Sequel::Postgres::JSONBaseOp::GET_PATH, Sequel::Postgres::JSONBaseOp::GET_PATH_TEXT, Sequel::Postgres::JSONBaseOp::GET_TEXT

Instance Attribute Summary

Attributes inherited from SQL::Wrapper

#value

Instance Method Summary collapse

Methods inherited from JSONBaseOp

#[], #array_elements, #array_elements_text, #array_length, #each, #each_text, #extract, #extract_text, #get_text, #keys, #populate, #populate_set, #strip_nulls, #to_record, #to_recordset, #typeof

Methods inherited from SQL::Wrapper

#initialize

Methods included from HStoreOpMethods

#hstore

Methods included from RangeOpMethods

#pg_range

Methods included from ArrayOpMethods

#pg_array

Methods included from JSONOpMethods

#pg_json

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

#ilike, #like

Methods included from SQL::PatternMatchMethods

#=~

Methods included from SQL::OrderMethods

#asc, #desc

Methods included from SQL::NumericMethods

#+

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, #eql?, #hash, inherited, #inspect, #lit, #sql_literal

Constructor Details

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

Instance Method Details

#-(other) ⇒ Object

jsonb expression for deletion of the given argument from the current jsonb.

jsonb_op - "a" # (jsonb - 'a')


299
300
301
# File 'lib/sequel/extensions/pg_json_ops.rb', line 299

def -(other)
  self.class.new(super)
end

#concat(other) ⇒ Object

jsonb expression for concatenation of the given jsonb into the current jsonb.

jsonb_op.concat(:h) # (jsonb || h)


307
308
309
# File 'lib/sequel/extensions/pg_json_ops.rb', line 307

def concat(other)
  json_op(CONCAT, wrap_input_jsonb(other))
end

#contain_all(other) ⇒ Object

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

jsonb_op.contain_all(:a) # (jsonb ?& a)


314
315
316
# File 'lib/sequel/extensions/pg_json_ops.rb', line 314

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:

jsonb_op.contain_any(:a) # (jsonb ?| a)


321
322
323
# File 'lib/sequel/extensions/pg_json_ops.rb', line 321

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

#contained_by(other) ⇒ Object

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

jsonb_op.contained_by(:h) # (jsonb <@ h)


335
336
337
# File 'lib/sequel/extensions/pg_json_ops.rb', line 335

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

#contains(other) ⇒ Object

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

jsonb_op.contains(:h) # (jsonb @> h)


328
329
330
# File 'lib/sequel/extensions/pg_json_ops.rb', line 328

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

#delete_path(other) ⇒ Object

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

jsonb_op.delete_path(:h) # (jsonb #- h)


342
343
344
# File 'lib/sequel/extensions/pg_json_ops.rb', line 342

def delete_path(other)
  json_op(DELETE_PATH, wrap_input_array(other))
end

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

Check if the receiver contains the given key:

jsonb_op.has_key?('a') # (jsonb ? 'a')

Returns:

  • (Boolean)


349
350
351
# File 'lib/sequel/extensions/pg_json_ops.rb', line 349

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

#pg_jsonbObject

Return the receiver, since it is already a JSONBOp.



355
356
357
# File 'lib/sequel/extensions/pg_json_ops.rb', line 355

def pg_jsonb
  self
end

#prettyObject

Returns a json value for the object at the given path.

jsonb_op.pretty # jsonb_pretty(jsonb)


362
363
364
# File 'lib/sequel/extensions/pg_json_ops.rb', line 362

def pretty
  Sequel::SQL::StringExpression.new(:NOOP, function(:pretty))
end

#set(path, other, create_missing = true) ⇒ Object

Returns a json value for the object at the given path.

jsonb_op.set(['a', 'b'], h) # jsonb_set(jsonb, ARRAY['a', 'b'], h, true)
jsonb_op.set(['a', 'b'], h, false) # jsonb_set(jsonb, ARRAY['a', 'b'], h, false)


370
371
372
# File 'lib/sequel/extensions/pg_json_ops.rb', line 370

def set(path, other, create_missing=true)
  self.class.new(function(:set, wrap_input_array(path), wrap_input_jsonb(other), create_missing))
end