Class: Sequel::Postgres::JSONBOp
- Inherits:
-
JSONBaseOp
- Object
- SQL::Expression
- SQL::GenericExpression
- SQL::Wrapper
- JSONBaseOp
- Sequel::Postgres::JSONBOp
- Defined in:
- lib/sequel/extensions/pg_json_ops.rb
Overview
Constant Summary collapse
- CONCAT =
["(".freeze, " || ".freeze, ")".freeze].freeze
- CONTAIN_ALL =
["(".freeze, " ?& ".freeze, ")".freeze].freeze
- CONTAIN_ANY =
["(".freeze, " ?| ".freeze, ")".freeze].freeze
- CONTAINS =
["(".freeze, " @> ".freeze, ")".freeze].freeze
- CONTAINED_BY =
["(".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
Constants inherited from JSONBaseOp
Sequel::Postgres::JSONBaseOp::EMPTY_STRING, Sequel::Postgres::JSONBaseOp::GET, Sequel::Postgres::JSONBaseOp::GET_PATH, Sequel::Postgres::JSONBaseOp::GET_PATH_TEXT, Sequel::Postgres::JSONBaseOp::GET_TEXT, Sequel::Postgres::JSONBaseOp::IS_JSON, Sequel::Postgres::JSONBaseOp::IS_JSON_MAP, Sequel::Postgres::JSONBaseOp::IS_NOT_JSON, Sequel::Postgres::JSONBaseOp::WITH_UNIQUE
Instance Attribute Summary
Attributes inherited from SQL::Wrapper
Instance Method Summary collapse
-
#-(other) ⇒ Object
jsonb expression for deletion of the given argument from the current jsonb.
-
#[](key) ⇒ Object
Support subscript syntax for JSONB.
-
#concat(other) ⇒ Object
jsonb expression for concatenation of the given jsonb into the current jsonb.
-
#contain_all(other) ⇒ Object
Check if the receiver contains all of the keys in the given array:.
-
#contain_any(other) ⇒ Object
Check if the receiver contains any of the keys in the given array:.
-
#contained_by(other) ⇒ Object
Check if the other jsonb contains all entries in the receiver:.
-
#contains(other) ⇒ Object
Check if the receiver contains all entries in the other jsonb:.
-
#delete_path(other) ⇒ Object
Removes the given path from the receiver.
-
#has_key?(key) ⇒ Boolean
(also: #include?)
Check if the receiver contains the given key:.
-
#insert(path, other, insert_after = false) ⇒ Object
Inserts the given jsonb value at the given path in the receiver.
-
#path_exists(path) ⇒ Object
Returns whether the JSON path returns any item for the json object.
-
#path_exists!(path, vars = nil, silent = nil) ⇒ Object
Returns whether the JSON path returns any item for the json object.
-
#path_exists_tz!(path, vars = nil, silent = nil) ⇒ Object
The same as #path_exists!, except that timezone-aware conversions are used for date/time values.
-
#path_match(path) ⇒ Object
Returns the first item of the result of JSON path predicate check for the json object.
-
#path_match!(path, vars = nil, silent = nil) ⇒ Object
Returns the first item of the result of JSON path predicate check for the json object.
-
#path_match_tz!(path, vars = nil, silent = nil) ⇒ Object
The same as #path_match!, except that timezone-aware conversions are used for date/time values.
-
#path_query(path, vars = nil, silent = nil) ⇒ Object
Returns a set of all jsonb values specified by the JSON path for the json object.
-
#path_query_array(path, vars = nil, silent = nil) ⇒ Object
Returns a jsonb array of all values specified by the JSON path for the json object.
-
#path_query_array_tz(path, vars = nil, silent = nil) ⇒ Object
The same as #path_query_array, except that timezone-aware conversions are used for date/time values.
-
#path_query_first(path, vars = nil, silent = nil) ⇒ Object
Returns the first item of the result specified by the JSON path for the json object.
-
#path_query_first_tz(path, vars = nil, silent = nil) ⇒ Object
The same as #path_query_first, except that timezone-aware conversions are used for date/time values.
-
#path_query_tz(path, vars = nil, silent = nil) ⇒ Object
The same as #path_query, except that timezone-aware conversions are used for date/time values.
-
#pg_jsonb ⇒ Object
Return the receiver, since it is already a JSONBOp.
-
#pretty ⇒ Object
Return a pretty printed version of the receiver as a string expression.
-
#set(path, other, create_missing = true) ⇒ Object
Set the given jsonb value at the given path in the receiver.
-
#set_lax(path, other, create_missing = true, null_value_treatment = 'use_json_null') ⇒ Object
The same as #set, except if
other
isnil
, then behaves according tonull_value_treatment
, which can be one of ‘raise_exception’, ‘use_json_null’ (default), ‘delete_key’, or ‘return_target’.
Methods inherited from JSONBaseOp
#array_elements, #array_elements_text, #array_length, #each, #each_text, #exists, #extract, #extract_text, #get_text, #is_json, #is_not_json, #keys, #populate, #populate_set, #query, #strip_nulls, #table, #to_record, #to_recordset, #typeof, #value
Methods inherited from SQL::Wrapper
Methods included from SQL::IsDistinctFrom::Methods
Methods included from SQLite::JSONOpMethods
#sqlite_json_op, #sqlite_jsonb_op
Methods included from HStoreOpMethods
Methods included from RangeOpMethods
Methods included from ArrayOpMethods
Methods included from JSONOpMethods
Methods included from InetOpMethods
Methods included from PGRowOp::ExpressionMethods
Methods included from SQL::SubscriptMethods
Methods included from SQL::StringMethods
#escaped_ilike, #escaped_like, #ilike, #like
Methods included from SQL::PatternMatchMethods
Methods included from SQL::OrderMethods
Methods included from SQL::NumericMethods
Methods included from SQL::ComplexExpressionMethods
#extract, #sql_boolean, #sql_number, #sql_string
Methods included from SQL::CastMethods
#cast, #cast_numeric, #cast_string
Methods included from SQL::BooleanMethods
Methods included from SQL::AliasMethods
Methods inherited from SQL::Expression
#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect
Constructor Details
This class inherits a constructor from Sequel::SQL::Wrapper
Instance Method Details
#-(other) ⇒ Object
jsonb expression for deletion of the given argument from the current jsonb.
jsonb_op - "a" # (jsonb - 'a')
613 614 615 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 613 def -(other) self.class.new(super) end |
#[](key) ⇒ Object
Support subscript syntax for JSONB.
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 592 def [](key) if is_array?(key) super else case @value when Symbol, SQL::Identifier, SQL::QualifiedIdentifier, JSONBSubscriptOp # Only use subscripts for identifiers. In other cases, switching from # the -> operator to [] for subscripts causes SQL syntax issues. You # only need the [] for subscripting when doing assignment, and # assignment is generally done on identifiers. self.class.new(JSONBSubscriptOp.new(self, key)) else super end end end |
#concat(other) ⇒ Object
jsonb expression for concatenation of the given jsonb into the current jsonb.
jsonb_op.concat(:h) # (jsonb || h)
621 622 623 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 621 def concat(other) json_op(CONCAT, wrap_input_jsonb(other)) end |
#contain_all(other) ⇒ Object
Check if the receiver contains all of the keys in the given array:
jsonb_op.contain_all(:a) # (jsonb ?& a)
628 629 630 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 628 def contain_all(other) bool_op(CONTAIN_ALL, wrap_input_array(other)) end |
#contain_any(other) ⇒ Object
Check if the receiver contains any of the keys in the given array:
jsonb_op.contain_any(:a) # (jsonb ?| a)
635 636 637 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 635 def contain_any(other) bool_op(CONTAIN_ANY, wrap_input_array(other)) end |
#contained_by(other) ⇒ Object
Check if the other jsonb contains all entries in the receiver:
jsonb_op.contained_by(:h) # (jsonb <@ h)
649 650 651 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 649 def contained_by(other) bool_op(CONTAINED_BY, wrap_input_jsonb(other)) end |
#contains(other) ⇒ Object
Check if the receiver contains all entries in the other jsonb:
jsonb_op.contains(:h) # (jsonb @> h)
642 643 644 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 642 def contains(other) bool_op(CONTAINS, wrap_input_jsonb(other)) end |
#delete_path(other) ⇒ Object
Removes the given path from the receiver.
jsonb_op.delete_path(:h) # (jsonb #- h)
656 657 658 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 656 def delete_path(other) json_op(DELETE_PATH, wrap_input_array(other)) end |
#has_key?(key) ⇒ Boolean Also known as: include?
Check if the receiver contains the given key:
jsonb_op.has_key?('a') # (jsonb ? 'a')
663 664 665 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 663 def has_key?(key) bool_op(HAS_KEY, key) end |
#insert(path, other, insert_after = false) ⇒ Object
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)
674 675 676 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 674 def insert(path, other, insert_after=false) self.class.new(function(:insert, wrap_input_array(path), wrap_input_jsonb(other), insert_after)) end |
#path_exists(path) ⇒ Object
Returns whether the JSON path returns any item for the json object.
json_op.path_exists("$.foo") # (json @? '$.foo')
681 682 683 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 681 def path_exists(path) bool_op(PATH_EXISTS, path) end |
#path_exists!(path, vars = nil, silent = nil) ⇒ Object
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)
695 696 697 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 695 def path_exists!(path, vars=nil, silent=nil) Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists, path, vars, silent)) end |
#path_exists_tz!(path, vars = nil, silent = nil) ⇒ Object
The same as #path_exists!, except that timezone-aware conversions are used for date/time values.
700 701 702 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 700 def path_exists_tz!(path, vars=nil, silent=nil) Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_exists_tz, path, vars, silent)) end |
#path_match(path) ⇒ Object
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')
708 709 710 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 708 def path_match(path) bool_op(PATH_MATCH, path) end |
#path_match!(path, vars = nil, silent = nil) ⇒ Object
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)
723 724 725 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 723 def path_match!(path, vars=nil, silent=nil) Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match, path, vars, silent)) end |
#path_match_tz!(path, vars = nil, silent = nil) ⇒ Object
The same as #path_match!, except that timezone-aware conversions are used for date/time values.
728 729 730 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 728 def path_match_tz!(path, vars=nil, silent=nil) Sequel::SQL::BooleanExpression.new(:NOOP, _path_function(:jsonb_path_match_tz, path, vars, silent)) end |
#path_query(path, vars = nil, silent = nil) ⇒ Object
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)
743 744 745 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 743 def path_query(path, vars=nil, silent=nil) _path_function(:jsonb_path_query, path, vars, silent) end |
#path_query_array(path, vars = nil, silent = nil) ⇒ Object
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)
763 764 765 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 763 def path_query_array(path, vars=nil, silent=nil) JSONBOp.new(_path_function(:jsonb_path_query_array, path, vars, silent)) end |
#path_query_array_tz(path, vars = nil, silent = nil) ⇒ Object
The same as #path_query_array, except that timezone-aware conversions are used for date/time values.
768 769 770 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 768 def path_query_array_tz(path, vars=nil, silent=nil) JSONBOp.new(_path_function(:jsonb_path_query_array_tz, path, vars, silent)) end |
#path_query_first(path, vars = nil, silent = nil) ⇒ Object
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)
783 784 785 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 783 def path_query_first(path, vars=nil, silent=nil) JSONBOp.new(_path_function(:jsonb_path_query_first, path, vars, silent)) end |
#path_query_first_tz(path, vars = nil, silent = nil) ⇒ Object
The same as #path_query_first, except that timezone-aware conversions are used for date/time values.
788 789 790 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 788 def path_query_first_tz(path, vars=nil, silent=nil) JSONBOp.new(_path_function(:jsonb_path_query_first_tz, path, vars, silent)) end |
#path_query_tz(path, vars = nil, silent = nil) ⇒ Object
The same as #path_query, except that timezone-aware conversions are used for date/time values.
748 749 750 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 748 def path_query_tz(path, vars=nil, silent=nil) _path_function(:jsonb_path_query_tz, path, vars, silent) end |
#pg_jsonb ⇒ Object
Return the receiver, since it is already a JSONBOp.
793 794 795 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 793 def pg_jsonb self end |
#pretty ⇒ Object
Return a pretty printed version of the receiver as a string expression.
jsonb_op.pretty # jsonb_pretty(jsonb)
800 801 802 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 800 def pretty Sequel::SQL::StringExpression.new(:NOOP, function(:pretty)) end |
#set(path, other, create_missing = true) ⇒ Object
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)
810 811 812 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 810 def set(path, other, create_missing=true) self.class.new(function(:set, wrap_input_array(path), wrap_input_jsonb(other), create_missing)) end |
#set_lax(path, other, create_missing = true, null_value_treatment = 'use_json_null') ⇒ Object
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’.
816 817 818 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 816 def set_lax(path, other, create_missing=true, null_value_treatment='use_json_null') self.class.new(function(:set_lax, wrap_input_array(path), wrap_input_jsonb(other), create_missing, null_value_treatment)) end |