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
Source
# File lib/sequel/extensions/pg_json_ops.rb 613 def -(other) 614 self.class.new(super) 615 end
jsonb expression for deletion of the given argument from the current jsonb.
jsonb_op - "a" # (jsonb - 'a')
Source
# File lib/sequel/extensions/pg_json_ops.rb 592 def [](key) 593 if is_array?(key) 594 super 595 else 596 case @value 597 when Symbol, SQL::Identifier, SQL::QualifiedIdentifier, JSONBSubscriptOp 598 # Only use subscripts for identifiers. In other cases, switching from 599 # the -> operator to [] for subscripts causes SQL syntax issues. You 600 # only need the [] for subscripting when doing assignment, and 601 # assignment is generally done on identifiers. 602 self.class.new(JSONBSubscriptOp.new(self, key)) 603 else 604 super 605 end 606 end 607 end
Support subscript syntax for JSONB.
Sequel::Postgres::JSONBaseOp#[]
Source
# File lib/sequel/extensions/pg_json_ops.rb 621 def concat(other) 622 json_op(CONCAT, wrap_input_jsonb(other)) 623 end
jsonb expression for concatenation of the given jsonb into the current jsonb.
jsonb_op.concat(:h) # (jsonb || h)
Source
# File lib/sequel/extensions/pg_json_ops.rb 628 def contain_all(other) 629 bool_op(CONTAIN_ALL, wrap_input_array(other)) 630 end
Check if the receiver contains all of the keys in the given array:
jsonb_op.contain_all(:a) # (jsonb ?& a)
Source
# File lib/sequel/extensions/pg_json_ops.rb 635 def contain_any(other) 636 bool_op(CONTAIN_ANY, wrap_input_array(other)) 637 end
Check if the receiver contains any of the keys in the given array:
jsonb_op.contain_any(:a) # (jsonb ?| a)
Source
# File lib/sequel/extensions/pg_json_ops.rb 649 def contained_by(other) 650 bool_op(CONTAINED_BY, wrap_input_jsonb(other)) 651 end
Check if the other jsonb contains all entries in the receiver:
jsonb_op.contained_by(:h) # (jsonb <@ h)
Source
# File lib/sequel/extensions/pg_json_ops.rb 642 def contains(other) 643 bool_op(CONTAINS, wrap_input_jsonb(other)) 644 end
Check if the receiver contains all entries in the other jsonb:
jsonb_op.contains(:h) # (jsonb @> h)
Source
# File lib/sequel/extensions/pg_json_ops.rb 656 def delete_path(other) 657 json_op(DELETE_PATH, wrap_input_array(other)) 658 end
Removes the given path from the receiver.
jsonb_op.delete_path(:h) # (jsonb #- h)
Source
# File lib/sequel/extensions/pg_json_ops.rb 663 def has_key?(key) 664 bool_op(HAS_KEY, key) 665 end
Check if the receiver contains the given key:
jsonb_op.has_key?('a') # (jsonb ? 'a')
Source
# File lib/sequel/extensions/pg_json_ops.rb 674 def insert(path, other, insert_after=false) 675 self.class.new(function(:insert, wrap_input_array(path), wrap_input_jsonb(other), insert_after)) 676 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)
Source
# File lib/sequel/extensions/pg_json_ops.rb 681 def path_exists(path) 682 bool_op(PATH_EXISTS, path) 683 end
Returns whether the JSON path returns any item for the json object.
json_op.path_exists("$.foo") # (json @? '$.foo')
Source
# File lib/sequel/extensions/pg_json_ops.rb 695 def path_exists!(path, vars=nil, silent=nil) 696 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists, path, vars, silent)) 697 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)
Source
# File lib/sequel/extensions/pg_json_ops.rb 700 def path_exists_tz!(path, vars=nil, silent=nil) 701 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists_tz, path, vars, silent)) 702 end
The same as path_exists!, except that timezone-aware conversions are used for date/time values.
Source
# File lib/sequel/extensions/pg_json_ops.rb 708 def path_match(path) 709 bool_op(PATH_MATCH, path) 710 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')
Source
# File lib/sequel/extensions/pg_json_ops.rb 723 def path_match!(path, vars=nil, silent=nil) 724 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match, path, vars, silent)) 725 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)
Source
# File lib/sequel/extensions/pg_json_ops.rb 728 def path_match_tz!(path, vars=nil, silent=nil) 729 Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match_tz, path, vars, silent)) 730 end
The same as path_match!, except that timezone-aware conversions are used for date/time values.
Source
# File lib/sequel/extensions/pg_json_ops.rb 743 def path_query(path, vars=nil, silent=nil) 744 _path_function(:jsonb_path_query, path, vars, silent) 745 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)
Source
# File lib/sequel/extensions/pg_json_ops.rb 763 def path_query_array(path, vars=nil, silent=nil) 764 JSONBOp.new(_path_function(:jsonb_path_query_array, path, vars, silent)) 765 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)
Source
# File lib/sequel/extensions/pg_json_ops.rb 768 def path_query_array_tz(path, vars=nil, silent=nil) 769 JSONBOp.new(_path_function(:jsonb_path_query_array_tz, path, vars, silent)) 770 end
The same as path_query_array, except that timezone-aware conversions are used for date/time values.
Source
# File lib/sequel/extensions/pg_json_ops.rb 783 def path_query_first(path, vars=nil, silent=nil) 784 JSONBOp.new(_path_function(:jsonb_path_query_first, path, vars, silent)) 785 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)
Source
# File lib/sequel/extensions/pg_json_ops.rb 788 def path_query_first_tz(path, vars=nil, silent=nil) 789 JSONBOp.new(_path_function(:jsonb_path_query_first_tz, path, vars, silent)) 790 end
The same as path_query_first, except that timezone-aware conversions are used for date/time values.
Source
# File lib/sequel/extensions/pg_json_ops.rb 748 def path_query_tz(path, vars=nil, silent=nil) 749 _path_function(:jsonb_path_query_tz, path, vars, silent) 750 end
The same as path_query, except that timezone-aware conversions are used for date/time values.
Source
# File lib/sequel/extensions/pg_json_ops.rb 793 def pg_jsonb 794 self 795 end
Return the receiver, since it is already a JSONBOp.
Source
# File lib/sequel/extensions/pg_json_ops.rb 800 def pretty 801 Sequel::SQL::StringExpression.new(:NOOP, function(:pretty)) 802 end
Return a pretty printed version of the receiver as a string expression.
jsonb_op.pretty # jsonb_pretty(jsonb)
Source
# File lib/sequel/extensions/pg_json_ops.rb 810 def set(path, other, create_missing=true) 811 self.class.new(function(:set, wrap_input_array(path), wrap_input_jsonb(other), create_missing)) 812 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)
Source
# File lib/sequel/extensions/pg_json_ops.rb 816 def set_lax(path, other, create_missing=true, null_value_treatment='use_json_null') 817 self.class.new(function(:set_lax, wrap_input_array(path), wrap_input_jsonb(other), create_missing, null_value_treatment)) 818 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’.
Private Instance Methods
Source
# File lib/sequel/extensions/pg_json_ops.rb 823 def _path_function(func, path, vars, silent) 824 args = [] 825 if vars 826 if vars.is_a?(Hash) 827 vars = vars.to_json 828 end 829 args << vars 830 831 unless silent.nil? 832 args << silent 833 end 834 end 835 SQL::Function.new(func, self, path, *args) 836 end
Internals of the jsonb SQL/JSON path functions.
Source
# File lib/sequel/extensions/pg_json_ops.rb 840 def bool_op(str, other) 841 Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other])) 842 end
Return a placeholder literal with the given str and args, wrapped in a boolean expression, used by operators that return booleans.
Source
# File lib/sequel/extensions/pg_json_ops.rb 870 def function_name(name) 871 "jsonb_#{name}" 872 end
The jsonb type functions are prefixed with jsonb_
Source
# File lib/sequel/extensions/pg_json_ops.rb 845 def wrap_input_array(obj) 846 # :nocov: 847 if Sequel.respond_to?(:pg_array) 848 # :nocov: 849 case obj 850 when Array 851 return Sequel.pg_array(obj) 852 when Set 853 return Sequel.pg_array(obj.to_a) 854 end 855 end 856 857 obj 858 end
Wrap argument in a PGArray if it is an array or a set
Source
# File lib/sequel/extensions/pg_json_ops.rb 861 def wrap_input_jsonb(obj) 862 if Sequel.respond_to?(:pg_jsonb) && (obj.is_a?(Array) || obj.is_a?(Hash)) 863 Sequel.pg_jsonb(obj) 864 else 865 obj 866 end 867 end
Wrap argument in a JSONBArray or JSONBHash if it is an array or hash.