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 330 def -(other) 331 self.class.new(super) 332 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 338 def concat(other) 339 json_op(CONCAT, wrap_input_jsonb(other)) 340 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 345 def contain_all(other) 346 bool_op(CONTAIN_ALL, wrap_input_array(other)) 347 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 352 def contain_any(other) 353 bool_op(CONTAIN_ANY, wrap_input_array(other)) 354 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 366 def contained_by(other) 367 bool_op(CONTAINED_BY, wrap_input_jsonb(other)) 368 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 359 def contains(other) 360 bool_op(CONTAINS, wrap_input_jsonb(other)) 361 end
Removes the given path from the receiver.
jsonb_op.delete_path(:h) # (jsonb #- h)
# File lib/sequel/extensions/pg_json_ops.rb 373 def delete_path(other) 374 json_op(DELETE_PATH, wrap_input_array(other)) 375 end
Check if the receiver contains the given key:
jsonb_op.has_key?('a') # (jsonb ? 'a')
# File lib/sequel/extensions/pg_json_ops.rb 380 def has_key?(key) 381 bool_op(HAS_KEY, key) 382 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 391 def insert(path, other, insert_after=false) 392 self.class.new(function(:insert, wrap_input_array(path), wrap_input_jsonb(other), insert_after)) 393 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 398 def path_exists(path) 399 bool_op(PATH_EXISTS, path) 400 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 412 def path_exists!(path, vars=nil, silent=nil) 413 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists, path, vars, silent)) 414 end
The same as path_exists!
, except that timezone-aware conversions are used for date/time values.
# File lib/sequel/extensions/pg_json_ops.rb 417 def path_exists_tz!(path, vars=nil, silent=nil) 418 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists_tz, path, vars, silent)) 419 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 425 def path_match(path) 426 bool_op(PATH_MATCH, path) 427 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 440 def path_match!(path, vars=nil, silent=nil) 441 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match, path, vars, silent)) 442 end
The same as path_match!
, except that timezone-aware conversions are used for date/time values.
# File lib/sequel/extensions/pg_json_ops.rb 445 def path_match_tz!(path, vars=nil, silent=nil) 446 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match_tz, path, vars, silent)) 447 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 460 def path_query(path, vars=nil, silent=nil) 461 _path_function(:jsonb_path_query, path, vars, silent) 462 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 480 def path_query_array(path, vars=nil, silent=nil) 481 JSONBOp.new(_path_function(:jsonb_path_query_array, path, vars, silent)) 482 end
The same as path_query_array
, except that timezone-aware conversions are used for date/time values.
# File lib/sequel/extensions/pg_json_ops.rb 485 def path_query_array_tz(path, vars=nil, silent=nil) 486 JSONBOp.new(_path_function(:jsonb_path_query_array_tz, path, vars, silent)) 487 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 500 def path_query_first(path, vars=nil, silent=nil) 501 JSONBOp.new(_path_function(:jsonb_path_query_first, path, vars, silent)) 502 end
The same as path_query_first
, except that timezone-aware conversions are used for date/time values.
# File lib/sequel/extensions/pg_json_ops.rb 505 def path_query_first_tz(path, vars=nil, silent=nil) 506 JSONBOp.new(_path_function(:jsonb_path_query_first_tz, path, vars, silent)) 507 end
The same as path_query
, except that timezone-aware conversions are used for date/time values.
# File lib/sequel/extensions/pg_json_ops.rb 465 def path_query_tz(path, vars=nil, silent=nil) 466 _path_function(:jsonb_path_query_tz, path, vars, silent) 467 end
Return the receiver, since it is already a JSONBOp
.
# File lib/sequel/extensions/pg_json_ops.rb 510 def pg_jsonb 511 self 512 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 517 def pretty 518 Sequel::SQL::StringExpression.new(:NOOP, function(:pretty)) 519 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 527 def set(path, other, create_missing=true) 528 self.class.new(function(:set, wrap_input_array(path), wrap_input_jsonb(other), create_missing)) 529 end
The same as set
, except if other
is nil
, then behaves according to null_value_treatment
, which can be one of 'raise_exception', 'use_json_null' (default), 'delete_key', or 'return_target'.
# File lib/sequel/extensions/pg_json_ops.rb 533 def set_lax(path, other, create_missing=true, null_value_treatment='use_json_null') 534 self.class.new(function(:set_lax, wrap_input_array(path), wrap_input_jsonb(other), create_missing, null_value_treatment)) 535 end
Private Instance Methods
Internals of the jsonb SQL/JSON path functions.
# File lib/sequel/extensions/pg_json_ops.rb 540 def _path_function(func, path, vars, silent) 541 args = [] 542 if vars 543 if vars.is_a?(Hash) 544 vars = vars.to_json 545 end 546 args << vars 547 548 unless silent.nil? 549 args << silent 550 end 551 end 552 SQL::Function.new(func, self, path, *args) 553 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 557 def bool_op(str, other) 558 Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other])) 559 end
The jsonb type functions are prefixed with jsonb_
# File lib/sequel/extensions/pg_json_ops.rb 580 def function_name(name) 581 "jsonb_#{name}" 582 end
Wrap argument in a PGArray
if it is an array
# File lib/sequel/extensions/pg_json_ops.rb 562 def wrap_input_array(obj) 563 if obj.is_a?(Array) && Sequel.respond_to?(:pg_array) 564 Sequel.pg_array(obj) 565 else 566 obj 567 end 568 end
Wrap argument in a JSONBArray
or JSONBHash
if it is an array or hash.
# File lib/sequel/extensions/pg_json_ops.rb 571 def wrap_input_jsonb(obj) 572 if Sequel.respond_to?(:pg_jsonb) && (obj.is_a?(Array) || obj.is_a?(Hash)) 573 Sequel.pg_jsonb(obj) 574 else 575 obj 576 end 577 end