class Sequel::Postgres::JSONBOp

JSONBaseOp subclass for the jsonb type.

In the method documentation examples, assume that:

jsonb_op = Sequel.pg_jsonb(:jsonb)

Constants

CONCAT
CONTAINED_BY
CONTAINS
CONTAIN_ALL
CONTAIN_ANY
DELETE_PATH
HAS_KEY

Public Instance Methods

-(other) click to toggle source

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

jsonb_op - "a" # (jsonb - 'a')
Calls superclass method
# File lib/sequel/extensions/pg_json_ops.rb, line 294
def -(other)
  self.class.new(super)
end
concat(other) click to toggle source

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

jsonb_op.concat(:h) # (jsonb || h)
# File lib/sequel/extensions/pg_json_ops.rb, line 302
def concat(other)
  json_op(CONCAT, wrap_input_jsonb(other))
end
contain_all(other) click to toggle source

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

jsonb_op.contain_all(:a) # (jsonb ?& a)
# File lib/sequel/extensions/pg_json_ops.rb, line 309
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:

jsonb_op.contain_any(:a) # (jsonb ?| a)
# File lib/sequel/extensions/pg_json_ops.rb, line 316
def contain_any(other)
  bool_op(CONTAIN_ANY, wrap_input_array(other))
end
contained_by(other) click to toggle source

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

jsonb_op.contained_by(:h) # (jsonb <@ h)
# File lib/sequel/extensions/pg_json_ops.rb, line 330
def contained_by(other)
  bool_op(CONTAINED_BY, wrap_input_jsonb(other))
end
contains(other) click to toggle source

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

jsonb_op.contains(:h) # (jsonb @> h)
# File lib/sequel/extensions/pg_json_ops.rb, line 323
def contains(other)
  bool_op(CONTAINS, wrap_input_jsonb(other))
end
delete_path(other) click to toggle source

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

jsonb_op.delete_path(:h) # (jsonb #- h)
# File lib/sequel/extensions/pg_json_ops.rb, line 337
def delete_path(other)
  json_op(DELETE_PATH, wrap_input_array(other))
end
has_key?(key) click to toggle source

Check if the receiver contains the given key:

jsonb_op.has_key?('a') # (jsonb ? 'a')
# File lib/sequel/extensions/pg_json_ops.rb, line 344
def has_key?(key)
  bool_op(HAS_KEY, key)
end
Also aliased as: include?
include?(key)
Alias for: has_key?
pg_jsonb() click to toggle source

Return the receiver, since it is already a JSONBOp.

# File lib/sequel/extensions/pg_json_ops.rb, line 350
def pg_jsonb
  self
end
pretty() click to toggle source

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

jsonb_op.pretty # jsonb_pretty(jsonb)
# File lib/sequel/extensions/pg_json_ops.rb, line 357
def pretty
  Sequel::SQL::StringExpression.new(:NOOP, function(:pretty))
end
set(path, other, create_missing=true) click to toggle source

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)
# File lib/sequel/extensions/pg_json_ops.rb, line 365
def set(path, other, create_missing=true)
  self.class.new(function(:set, wrap_input_array(path), wrap_input_jsonb(other), create_missing))
end

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_json_ops.rb, line 373
def bool_op(str, other)
  Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other]))
end
function_name(name) click to toggle source

The jsonb type functions are prefixed with jsonb_

# File lib/sequel/extensions/pg_json_ops.rb, line 396
def function_name(name)
  "jsonb_#{name}"
end
wrap_input_array(obj) click to toggle source

Wrap argument in a PGArray if it is an array

# File lib/sequel/extensions/pg_json_ops.rb, line 378
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_jsonb(obj) click to toggle source

Wrap argument in a JSONBArray or JSONBHash if it is an array or hash.

# File lib/sequel/extensions/pg_json_ops.rb, line 387
def wrap_input_jsonb(obj)
  if Sequel.respond_to?(:pg_jsonb) && (obj.is_a?(Array) || obj.is_a?(Hash))
    Sequel.pg_jsonb(obj)
  else
    obj
  end
end