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
jsonb expression for deletion of the given argument from the current jsonb.
jsonb_op - "a" # (jsonb - 'a')
# File lib/sequel/extensions/pg_json_ops.rb, line 300 def -(other) self.class.new(super) end
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 308 def concat(other) json_op(CONCAT, wrap_input_jsonb(other)) end
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 315 def contain_all(other) bool_op(CONTAIN_ALL, wrap_input_array(other)) end
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 322 def contain_any(other) bool_op(CONTAIN_ANY, wrap_input_array(other)) end
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 336 def contained_by(other) bool_op(CONTAINED_BY, wrap_input_jsonb(other)) end
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 329 def contains(other) bool_op(CONTAINS, wrap_input_jsonb(other)) end
Removes the given path from the receiver.
jsonb_op.delete_path(:h) # (jsonb #- h)
# File lib/sequel/extensions/pg_json_ops.rb, line 343 def delete_path(other) json_op(DELETE_PATH, wrap_input_array(other)) end
Check if the receiver contains the given key:
jsonb_op.has_key?('a') # (jsonb ? 'a')
# File lib/sequel/extensions/pg_json_ops.rb, line 350 def has_key?(key) bool_op(HAS_KEY, key) end
Inserts the given jsonb value at the given path in the receiver. The default is to insert the value before the given path, but insert_after can be set to true to insert it after the given path.
jsonb_op.insert(['a', 'b'], h) # jsonb_insert(jsonb, ARRAY['a', 'b'], h, false) jsonb_op.insert(['a', 'b'], h, true) # jsonb_insert(jsonb, ARRAY['a', 'b'], h, true)
# File lib/sequel/extensions/pg_json_ops.rb, line 361 def insert(path, other, insert_after=false) self.class.new(function(:insert, wrap_input_array(path), wrap_input_jsonb(other), insert_after)) end
Return the receiver, since it is already a JSONBOp.
# File lib/sequel/extensions/pg_json_ops.rb, line 366 def pg_jsonb self end
Return a pretty printed version of the receiver as a string expression.
jsonb_op.pretty # jsonb_pretty(jsonb)
# File lib/sequel/extensions/pg_json_ops.rb, line 373 def pretty Sequel::SQL::StringExpression.new(:NOOP, function(:pretty)) end
Set the given jsonb value at the given path in the receiver. By default, this will create the value if it does not exist, but create_missing can be set to false to not create a new value.
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 383 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
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 391 def bool_op(str, other) Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other])) end
The jsonb type functions are prefixed with jsonb_
# File lib/sequel/extensions/pg_json_ops.rb, line 414 def function_name(name) "jsonb_#{name}" end
Wrap argument in a PGArray if it is an array
# File lib/sequel/extensions/pg_json_ops.rb, line 396 def wrap_input_array(obj) if obj.is_a?(Array) && Sequel.respond_to?(:pg_array) Sequel.pg_array(obj) else obj end end
Wrap argument in a JSONBArray or JSONBHash if it is an array or hash.
# File lib/sequel/extensions/pg_json_ops.rb, line 405 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