class Sequel::Postgres::JSONBOp

  1. lib/sequel/extensions/pg_json_ops.rb
Superclass: JSONBaseOp

JSONBaseOp subclass for the jsonb type.

In the method documentation examples, assume that:

jsonb_op = Sequel.pg_jsonb(:jsonb)

Constants

CONCAT = ["(".freeze, " || ".freeze, ")".freeze].freeze  
CONTAINED_BY = ["(".freeze, " <@ ".freeze, ")".freeze].freeze  
CONTAINS = ["(".freeze, " @> ".freeze, ")".freeze].freeze  
CONTAIN_ALL = ["(".freeze, " ?& ".freeze, ")".freeze].freeze  
CONTAIN_ANY = ["(".freeze, " ?| ".freeze, ")".freeze].freeze  
DELETE_PATH = ["(".freeze, " #- ".freeze, ")".freeze].freeze  
HAS_KEY = ["(".freeze, " ? ".freeze, ")".freeze].freeze  
PATH_EXISTS = ["(".freeze, " @? ".freeze, ")".freeze].freeze  
PATH_MATCH = ["(".freeze, " @@ ".freeze, ")".freeze].freeze  

Public Instance Aliases

include? -> has_key?

Public Instance methods

-(other)

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

jsonb_op - "a" # (jsonb - 'a')
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
369 def -(other)
370   self.class.new(super)
371 end
[](key)

Support subscript syntax for JSONB.

[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
348 def [](key)
349   if is_array?(key)
350     super
351   else
352     case @value
353     when Symbol, SQL::Identifier, SQL::QualifiedIdentifier, JSONBSubscriptOp
354       # Only use subscripts for identifiers.  In other cases, switching from
355       # the -> operator to [] for subscripts causes SQL syntax issues.  You
356       # only need the [] for subscripting when doing assignment, and
357       # assignment is generally done on identifiers.
358       self.class.new(JSONBSubscriptOp.new(self, key))
359     else
360       super
361     end
362   end
363 end
concat(other)

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

jsonb_op.concat(:h) # (jsonb || h)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
377 def concat(other)
378   json_op(CONCAT, wrap_input_jsonb(other))
379 end
contain_all(other)

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

jsonb_op.contain_all(:a) # (jsonb ?& a)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
384 def contain_all(other)
385   bool_op(CONTAIN_ALL, wrap_input_array(other))
386 end
contain_any(other)

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

jsonb_op.contain_any(:a) # (jsonb ?| a)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
391 def contain_any(other)
392   bool_op(CONTAIN_ANY, wrap_input_array(other))
393 end
contained_by(other)

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

jsonb_op.contained_by(:h) # (jsonb <@ h)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
405 def contained_by(other)
406   bool_op(CONTAINED_BY, wrap_input_jsonb(other))
407 end
contains(other)

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

jsonb_op.contains(:h) # (jsonb @> h)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
398 def contains(other)
399   bool_op(CONTAINS, wrap_input_jsonb(other))
400 end
delete_path(other)

Removes the given path from the receiver.

jsonb_op.delete_path(:h) # (jsonb #- h)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
412 def delete_path(other)
413   json_op(DELETE_PATH, wrap_input_array(other))
414 end
has_key?(key)

Check if the receiver contains the given key:

jsonb_op.has_key?('a') # (jsonb ? 'a')
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
419 def has_key?(key)
420   bool_op(HAS_KEY, key)
421 end
insert(path, other, insert_after=false)

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)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
430 def insert(path, other, insert_after=false)
431   self.class.new(function(:insert, wrap_input_array(path), wrap_input_jsonb(other), insert_after))
432 end
path_exists(path)

Returns whether the JSON path returns any item for the json object.

json_op.path_exists("$.foo") # (json @? '$.foo')
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
437 def path_exists(path)
438   bool_op(PATH_EXISTS, path)
439 end
path_exists!(path, vars=nil, silent=nil)

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)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
451 def path_exists!(path, vars=nil, silent=nil)
452   Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists, path, vars, silent))
453 end
path_exists_tz!(path, vars=nil, silent=nil)

The same as path_exists!, except that timezone-aware conversions are used for date/time values.

[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
456 def path_exists_tz!(path, vars=nil, silent=nil)
457   Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists_tz, path, vars, silent))
458 end
path_match(path)

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')
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
464 def path_match(path)
465   bool_op(PATH_MATCH, path)
466 end
path_match!(path, vars=nil, silent=nil)

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)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
479 def path_match!(path, vars=nil, silent=nil)
480   Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match, path, vars, silent))
481 end
path_match_tz!(path, vars=nil, silent=nil)

The same as path_match!, except that timezone-aware conversions are used for date/time values.

[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
484 def path_match_tz!(path, vars=nil, silent=nil)
485   Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match_tz, path, vars, silent))
486 end
path_query(path, vars=nil, silent=nil)

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)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
499 def path_query(path, vars=nil, silent=nil)
500   _path_function(:jsonb_path_query, path, vars, silent)
501 end
path_query_array(path, vars=nil, silent=nil)

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)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
519 def path_query_array(path, vars=nil, silent=nil)
520   JSONBOp.new(_path_function(:jsonb_path_query_array, path, vars, silent))
521 end
path_query_array_tz(path, vars=nil, silent=nil)

The same as path_query_array, except that timezone-aware conversions are used for date/time values.

[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
524 def path_query_array_tz(path, vars=nil, silent=nil)
525   JSONBOp.new(_path_function(:jsonb_path_query_array_tz, path, vars, silent))
526 end
path_query_first(path, vars=nil, silent=nil)

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)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
539 def path_query_first(path, vars=nil, silent=nil)
540   JSONBOp.new(_path_function(:jsonb_path_query_first, path, vars, silent))
541 end
path_query_first_tz(path, vars=nil, silent=nil)

The same as path_query_first, except that timezone-aware conversions are used for date/time values.

[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
544 def path_query_first_tz(path, vars=nil, silent=nil)
545   JSONBOp.new(_path_function(:jsonb_path_query_first_tz, path, vars, silent))
546 end
path_query_tz(path, vars=nil, silent=nil)

The same as path_query, except that timezone-aware conversions are used for date/time values.

[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
504 def path_query_tz(path, vars=nil, silent=nil)
505   _path_function(:jsonb_path_query_tz, path, vars, silent)
506 end
pg_jsonb()

Return the receiver, since it is already a JSONBOp.

[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
549 def pg_jsonb
550   self
551 end
pretty()

Return a pretty printed version of the receiver as a string expression.

jsonb_op.pretty # jsonb_pretty(jsonb)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
556 def pretty
557   Sequel::SQL::StringExpression.new(:NOOP, function(:pretty))
558 end
set(path, other, create_missing=true)

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)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
566 def set(path, other, create_missing=true)
567   self.class.new(function(:set, wrap_input_array(path), wrap_input_jsonb(other), create_missing))
568 end
set_lax(path, other, create_missing=true, null_value_treatment='use_json_null')

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’.

[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
572 def set_lax(path, other, create_missing=true, null_value_treatment='use_json_null')
573   self.class.new(function(:set_lax, wrap_input_array(path), wrap_input_jsonb(other), create_missing, null_value_treatment))
574 end