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

-(other) click to toggle source

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

jsonb_op - "a" # (jsonb - 'a')
Calls superclass method
    # File lib/sequel/extensions/pg_json_ops.rb
422 def -(other)
423   self.class.new(super)
424 end
[](key) click to toggle source

Support subscript syntax for JSONB.

Calls superclass method Sequel::Postgres::JSONBaseOp#[]
    # File lib/sequel/extensions/pg_json_ops.rb
401 def [](key)
402   if is_array?(key)
403     super
404   else
405     case @value
406     when Symbol, SQL::Identifier, SQL::QualifiedIdentifier, JSONBSubscriptOp
407       # Only use subscripts for identifiers.  In other cases, switching from
408       # the -> operator to [] for subscripts causes SQL syntax issues.  You
409       # only need the [] for subscripting when doing assignment, and
410       # assignment is generally done on identifiers.
411       self.class.new(JSONBSubscriptOp.new(self, key))
412     else
413       super
414     end
415   end
416 end
concat(other) click to toggle source

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
430 def concat(other)
431   json_op(CONCAT, wrap_input_jsonb(other))
432 end
contain_all(other) click to toggle source

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
437 def contain_all(other)
438   bool_op(CONTAIN_ALL, wrap_input_array(other))
439 end
contain_any(other) click to toggle source

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
444 def contain_any(other)
445   bool_op(CONTAIN_ANY, wrap_input_array(other))
446 end
contained_by(other) click to toggle source

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
458 def contained_by(other)
459   bool_op(CONTAINED_BY, wrap_input_jsonb(other))
460 end
contains(other) click to toggle source

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
451 def contains(other)
452   bool_op(CONTAINS, wrap_input_jsonb(other))
453 end
delete_path(other) click to toggle source

Removes the given path from the receiver.

jsonb_op.delete_path(:h) # (jsonb #- h)
    # File lib/sequel/extensions/pg_json_ops.rb
465 def delete_path(other)
466   json_op(DELETE_PATH, wrap_input_array(other))
467 end
has_key?(key) click to toggle source

Check if the receiver contains the given key:

jsonb_op.has_key?('a') # (jsonb ? 'a')
    # File lib/sequel/extensions/pg_json_ops.rb
472 def has_key?(key)
473   bool_op(HAS_KEY, key)
474 end
Also aliased as: include?
include?(key)
Alias for: has_key?
insert(path, other, insert_after=false) click to toggle source

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
483 def insert(path, other, insert_after=false)
484   self.class.new(function(:insert, wrap_input_array(path), wrap_input_jsonb(other), insert_after))
485 end
path_exists(path) click to toggle source

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
490 def path_exists(path)
491   bool_op(PATH_EXISTS, path)
492 end
path_exists!(path, vars=nil, silent=nil) click to toggle source

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
504 def path_exists!(path, vars=nil, silent=nil)
505   Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists, path, vars, silent))
506 end
path_exists_tz!(path, vars=nil, silent=nil) click to toggle source

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

    # File lib/sequel/extensions/pg_json_ops.rb
509 def path_exists_tz!(path, vars=nil, silent=nil)
510   Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists_tz, path, vars, silent))
511 end
path_match(path) click to toggle source

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
517 def path_match(path)
518   bool_op(PATH_MATCH, path)
519 end
path_match!(path, vars=nil, silent=nil) click to toggle source

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
532 def path_match!(path, vars=nil, silent=nil)
533   Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match, path, vars, silent))
534 end
path_match_tz!(path, vars=nil, silent=nil) click to toggle source

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

    # File lib/sequel/extensions/pg_json_ops.rb
537 def path_match_tz!(path, vars=nil, silent=nil)
538   Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match_tz, path, vars, silent))
539 end
path_query(path, vars=nil, silent=nil) click to toggle source

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
552 def path_query(path, vars=nil, silent=nil)
553   _path_function(:jsonb_path_query, path, vars, silent)
554 end
path_query_array(path, vars=nil, silent=nil) click to toggle source

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
572 def path_query_array(path, vars=nil, silent=nil)
573   JSONBOp.new(_path_function(:jsonb_path_query_array, path, vars, silent))
574 end
path_query_array_tz(path, vars=nil, silent=nil) click to toggle source

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
577 def path_query_array_tz(path, vars=nil, silent=nil)
578   JSONBOp.new(_path_function(:jsonb_path_query_array_tz, path, vars, silent))
579 end
path_query_first(path, vars=nil, silent=nil) click to toggle source

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
592 def path_query_first(path, vars=nil, silent=nil)
593   JSONBOp.new(_path_function(:jsonb_path_query_first, path, vars, silent))
594 end
path_query_first_tz(path, vars=nil, silent=nil) click to toggle source

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
597 def path_query_first_tz(path, vars=nil, silent=nil)
598   JSONBOp.new(_path_function(:jsonb_path_query_first_tz, path, vars, silent))
599 end
path_query_tz(path, vars=nil, silent=nil) click to toggle source

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

    # File lib/sequel/extensions/pg_json_ops.rb
557 def path_query_tz(path, vars=nil, silent=nil)
558   _path_function(:jsonb_path_query_tz, path, vars, silent)
559 end
pg_jsonb() click to toggle source

Return the receiver, since it is already a JSONBOp.

    # File lib/sequel/extensions/pg_json_ops.rb
602 def pg_jsonb
603   self
604 end
pretty() click to toggle source

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
609 def pretty
610   Sequel::SQL::StringExpression.new(:NOOP, function(:pretty))
611 end
set(path, other, create_missing=true) click to toggle source

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
619 def set(path, other, create_missing=true)
620   self.class.new(function(:set, wrap_input_array(path), wrap_input_jsonb(other), create_missing))
621 end
set_lax(path, other, create_missing=true, null_value_treatment='use_json_null') click to toggle source

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
625 def set_lax(path, other, create_missing=true, null_value_treatment='use_json_null')
626   self.class.new(function(:set_lax, wrap_input_array(path), wrap_input_jsonb(other), create_missing, null_value_treatment))
627 end

Private Instance Methods

_path_function(func, path, vars, silent) click to toggle source

Internals of the jsonb SQL/JSON path functions.

    # File lib/sequel/extensions/pg_json_ops.rb
632 def _path_function(func, path, vars, silent)
633   args = []
634   if vars
635     if vars.is_a?(Hash)
636       vars = vars.to_json
637     end
638     args << vars
639 
640     unless silent.nil?
641       args << silent
642     end
643   end
644   SQL::Function.new(func, self, path, *args)
645 end
bool_op(str, other) click to toggle source

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
649 def bool_op(str, other)
650   Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other]))
651 end
function_name(name) click to toggle source

The jsonb type functions are prefixed with jsonb_

    # File lib/sequel/extensions/pg_json_ops.rb
672 def function_name(name)
673   "jsonb_#{name}"
674 end
wrap_input_array(obj) click to toggle source

Wrap argument in a PGArray if it is an array

    # File lib/sequel/extensions/pg_json_ops.rb
654 def wrap_input_array(obj)
655   if obj.is_a?(Array) && Sequel.respond_to?(:pg_array) 
656     Sequel.pg_array(obj)
657   else
658     obj
659   end
660 end
wrap_input_jsonb(obj) click to toggle source

Wrap argument in a JSONBArray or JSONBHash if it is an array or hash.

    # File lib/sequel/extensions/pg_json_ops.rb
663 def wrap_input_jsonb(obj)
664   if Sequel.respond_to?(:pg_jsonb) && (obj.is_a?(Array) || obj.is_a?(Hash))
665     Sequel.pg_jsonb(obj)
666   else
667     obj
668   end
669 end