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 300 def -(other) 301 self.class.new(super) 302 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 308 def concat(other) 309 json_op(CONCAT, wrap_input_jsonb(other)) 310 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 315 def contain_all(other) 316 bool_op(CONTAIN_ALL, wrap_input_array(other)) 317 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 322 def contain_any(other) 323 bool_op(CONTAIN_ANY, wrap_input_array(other)) 324 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 336 def contained_by(other) 337 bool_op(CONTAINED_BY, wrap_input_jsonb(other)) 338 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 329 def contains(other) 330 bool_op(CONTAINS, wrap_input_jsonb(other)) 331 end
Removes the given path from the receiver.
jsonb_op.delete_path(:h) # (jsonb #- h)
# File lib/sequel/extensions/pg_json_ops.rb 343 def delete_path(other) 344 json_op(DELETE_PATH, wrap_input_array(other)) 345 end
Check if the receiver contains the given key:
jsonb_op.has_key?('a') # (jsonb ? 'a')
# File lib/sequel/extensions/pg_json_ops.rb 350 def has_key?(key) 351 bool_op(HAS_KEY, key) 352 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 361 def insert(path, other, insert_after=false) 362 self.class.new(function(:insert, wrap_input_array(path), wrap_input_jsonb(other), insert_after)) 363 end
Return the receiver, since it is already a JSONBOp
.
# File lib/sequel/extensions/pg_json_ops.rb 366 def pg_jsonb 367 self 368 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 373 def pretty 374 Sequel::SQL::StringExpression.new(:NOOP, function(:pretty)) 375 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 383 def set(path, other, create_missing=true) 384 self.class.new(function(:set, wrap_input_array(path), wrap_input_jsonb(other), create_missing)) 385 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 391 def bool_op(str, other) 392 Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other])) 393 end
The jsonb type functions are prefixed with jsonb_
# File lib/sequel/extensions/pg_json_ops.rb 414 def function_name(name) 415 "jsonb_#{name}" 416 end
Wrap argument in a PGArray
if it is an array
# File lib/sequel/extensions/pg_json_ops.rb 396 def wrap_input_array(obj) 397 if obj.is_a?(Array) && Sequel.respond_to?(:pg_array) 398 Sequel.pg_array(obj) 399 else 400 obj 401 end 402 end
Wrap argument in a JSONBArray
or JSONBHash
if it is an array or hash.
# File lib/sequel/extensions/pg_json_ops.rb 405 def wrap_input_jsonb(obj) 406 if Sequel.respond_to?(:pg_jsonb) && (obj.is_a?(Array) || obj.is_a?(Hash)) 407 Sequel.pg_jsonb(obj) 408 else 409 obj 410 end 411 end