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
- PATH_EXISTS
- PATH_MATCH
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 319 def -(other) 320 self.class.new(super) 321 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 327 def concat(other) 328 json_op(CONCAT, wrap_input_jsonb(other)) 329 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 334 def contain_all(other) 335 bool_op(CONTAIN_ALL, wrap_input_array(other)) 336 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 341 def contain_any(other) 342 bool_op(CONTAIN_ANY, wrap_input_array(other)) 343 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 355 def contained_by(other) 356 bool_op(CONTAINED_BY, wrap_input_jsonb(other)) 357 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 348 def contains(other) 349 bool_op(CONTAINS, wrap_input_jsonb(other)) 350 end
Removes the given path from the receiver.
jsonb_op.delete_path(:h) # (jsonb #- h)
# File lib/sequel/extensions/pg_json_ops.rb 362 def delete_path(other) 363 json_op(DELETE_PATH, wrap_input_array(other)) 364 end
Check if the receiver contains the given key:
jsonb_op.has_key?('a') # (jsonb ? 'a')
# File lib/sequel/extensions/pg_json_ops.rb 369 def has_key?(key) 370 bool_op(HAS_KEY, key) 371 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 380 def insert(path, other, insert_after=false) 381 self.class.new(function(:insert, wrap_input_array(path), wrap_input_jsonb(other), insert_after)) 382 end
Returns whether the JSON path returns any item for the json object.
json_op.path_exists("$.foo") # (json @? '$.foo')
# File lib/sequel/extensions/pg_json_ops.rb 387 def path_exists(path) 388 bool_op(PATH_EXISTS, path) 389 end
Returns whether the JSON path returns any item for the json object.
json_op.path_exists!("$.foo") # jsonb_path_exists(json, '$.foo') json_op.path_exists!("$.foo ? ($ > $x)", x: 2) # jsonb_path_exists(json, '$.foo ? ($ > $x)', '{"x":2}') json_op.path_exists!("$.foo ? ($ > $x)", {x: 2}, true) # jsonb_path_exists(json, '$.foo ? ($ > $x)', '{"x":2}', true)
# File lib/sequel/extensions/pg_json_ops.rb 401 def path_exists!(path, vars=nil, silent=nil) 402 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists, path, vars, silent)) 403 end
Returns the first item of the result of JSON path predicate check for the json object. Returns nil if the first item is not true or false.
json_op.path_match("$.foo") # (json @@ '$.foo')
# File lib/sequel/extensions/pg_json_ops.rb 409 def path_match(path) 410 bool_op(PATH_MATCH, path) 411 end
Returns the first item of the result of JSON path predicate check for the json object. Returns nil if the first item is not true or false and silent is true.
json_op.path_match!("$.foo") # jsonb_path_match(json, '$.foo') json_op.path_match!("$.foo ? ($ > $x)", x: 2) # jsonb_path_match(json, '$.foo ? ($ > $x)', '{"x":2}') json_op.path_match!("$.foo ? ($ > $x)", {x: 2}, true) # jsonb_path_match(json, '$.foo ? ($ > $x)', '{"x":2}', true)
# File lib/sequel/extensions/pg_json_ops.rb 424 def path_match!(path, vars=nil, silent=nil) 425 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match, path, vars, silent)) 426 end
Returns a set of all jsonb values specified by the JSON path for the json object.
json_op.path_query("$.foo") # jsonb_path_query(json, '$.foo') json_op.path_query("$.foo ? ($ > $x)", x: 2) # jsonb_path_query(json, '$.foo ? ($ > $x)', '{"x":2}') json_op.path_query("$.foo ? ($ > $x)", {x: 2}, true) # jsonb_path_query(json, '$.foo ? ($ > $x)', '{"x":2}', true)
# File lib/sequel/extensions/pg_json_ops.rb 439 def path_query(path, vars=nil, silent=nil) 440 _path_function(:jsonb_path_query, path, vars, silent) 441 end
Returns a jsonb array of all values specified by the JSON path for the json object.
json_op.path_query_array("$.foo") # jsonb_path_query_array(json, '$.foo') json_op.path_query_array("$.foo ? ($ > $x)", x: 2) # jsonb_path_query_array(json, '$.foo ? ($ > $x)', '{"x":2}') json_op.path_query_array("$.foo ? ($ > $x)", {x: 2}, true) # jsonb_path_query_array(json, '$.foo ? ($ > $x)', '{"x":2}', true)
# File lib/sequel/extensions/pg_json_ops.rb 454 def path_query_array(path, vars=nil, silent=nil) 455 JSONBOp.new(_path_function(:jsonb_path_query_array, path, vars, silent)) 456 end
Returns the first item of the result specified by the JSON path for the json object.
json_op.path_query_first("$.foo") # jsonb_path_query_first(json, '$.foo') json_op.path_query_first("$.foo ? ($ > $x)", x: 2) # jsonb_path_query_first(json, '$.foo ? ($ > $x)', '{"x":2}') json_op.path_query_first("$.foo ? ($ > $x)", {x: 2}, true) # jsonb_path_query_first(json, '$.foo ? ($ > $x)', '{"x":2}', true)
# File lib/sequel/extensions/pg_json_ops.rb 469 def path_query_first(path, vars=nil, silent=nil) 470 JSONBOp.new(_path_function(:jsonb_path_query_first, path, vars, silent)) 471 end
Return the receiver, since it is already a JSONBOp
.
# File lib/sequel/extensions/pg_json_ops.rb 474 def pg_jsonb 475 self 476 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 481 def pretty 482 Sequel::SQL::StringExpression.new(:NOOP, function(:pretty)) 483 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 491 def set(path, other, create_missing=true) 492 self.class.new(function(:set, wrap_input_array(path), wrap_input_jsonb(other), create_missing)) 493 end
Private Instance Methods
Internals of the jsonb SQL/JSON path functions.
# File lib/sequel/extensions/pg_json_ops.rb 498 def _path_function(func, path, vars, silent) 499 args = [] 500 if vars 501 if vars.is_a?(Hash) 502 vars = vars.to_json 503 end 504 args << vars 505 506 unless silent.nil? 507 args << silent 508 end 509 end 510 SQL::Function.new(func, self, path, *args) 511 end
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 515 def bool_op(str, other) 516 Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other])) 517 end
The jsonb type functions are prefixed with jsonb_
# File lib/sequel/extensions/pg_json_ops.rb 538 def function_name(name) 539 "jsonb_#{name}" 540 end
Wrap argument in a PGArray
if it is an array
# File lib/sequel/extensions/pg_json_ops.rb 520 def wrap_input_array(obj) 521 if obj.is_a?(Array) && Sequel.respond_to?(:pg_array) 522 Sequel.pg_array(obj) 523 else 524 obj 525 end 526 end
Wrap argument in a JSONBArray
or JSONBHash
if it is an array or hash.
# File lib/sequel/extensions/pg_json_ops.rb 529 def wrap_input_jsonb(obj) 530 if Sequel.respond_to?(:pg_jsonb) && (obj.is_a?(Array) || obj.is_a?(Hash)) 531 Sequel.pg_jsonb(obj) 532 else 533 obj 534 end 535 end