Class: Sequel::Postgres::JSONOp
- Inherits:
-
SQL::Wrapper
- Object
- SQL::Expression
- SQL::GenericExpression
- SQL::Wrapper
- Sequel::Postgres::JSONOp
- Defined in:
- lib/sequel/extensions/pg_json_ops.rb
Overview
Constant Summary collapse
- GET =
["(".freeze, " -> ".freeze, ")".freeze].freeze
- GET_TEXT =
["(".freeze, " ->> ".freeze, ")".freeze].freeze
- GET_PATH =
["(".freeze, " #> ".freeze, ")".freeze].freeze
- GET_PATH_TEXT =
["(".freeze, " #>> ".freeze, ")".freeze].freeze
Instance Attribute Summary
Attributes inherited from SQL::Wrapper
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #get)
Get JSON array element or object field as json.
-
#array_elements ⇒ Object
Returns a set of json values for the elements in the json array.
-
#array_length ⇒ Object
Get the length of the outermost json array.
-
#each ⇒ Object
Returns a set of key and value pairs, where the keys are text and the values are JSON.
-
#each_text ⇒ Object
Returns a set of key and value pairs, where the keys and values are both text.
-
#extract(*a) ⇒ Object
Returns a json value for the object at the given path.
-
#extract_text(*a) ⇒ Object
Returns a text value for the object at the given path.
-
#get_text(key) ⇒ Object
Get JSON array element or object field as text.
-
#keys ⇒ Object
Returns a set of keys AS text in the json object.
-
#pg_json ⇒ Object
Return the receiver, since it is already a JSONOp.
-
#populate(arg) ⇒ Object
Expands the given argument using the columns in the json.
-
#populate_set(arg) ⇒ Object
Expands the given argument using the columns in the json.
Methods inherited from SQL::Wrapper
Methods included from HStoreOpMethods
Methods included from RangeOpMethods
Methods included from ArrayOpMethods
Methods included from PGRowOp::ExpressionMethods
Methods included from SQL::SubscriptMethods
Methods included from SQL::StringMethods
Methods included from SQL::OrderMethods
Methods included from SQL::NumericMethods
Methods included from SQL::ComplexExpressionMethods
#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, #eql?, #hash, inherited, #inspect, #lit, #sql_literal
Constructor Details
This class inherits a constructor from Sequel::SQL::Wrapper
Instance Method Details
#[](key) ⇒ Object Also known as: get
Get JSON array element or object field as json. If an array is given, gets the object at the specified path.
json_op[1] # (json -> 1)
json_op['a'] # (json -> 'a')
json_op[%w'a b'] # (json #> ARRAY['a', 'b'])
73 74 75 76 77 78 79 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 73 def [](key) if is_array?(key) json_op(GET_PATH, wrap_array(key)) else json_op(GET, key) end end |
#array_elements ⇒ Object
Returns a set of json values for the elements in the json array.
json_op.array_elements # json_oarray_elements(json)
85 86 87 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 85 def array_elements function(:json_array_elements) end |
#array_length ⇒ Object
Get the length of the outermost json array.
json_op.array_length # json_array_length(json)
92 93 94 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 92 def array_length Sequel::SQL::NumericExpression.new(:NOOP, function(:json_array_length)) end |
#each ⇒ Object
Returns a set of key and value pairs, where the keys are text and the values are JSON.
json_op.each # json_each(json)
100 101 102 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 100 def each function(:json_each) end |
#each_text ⇒ Object
Returns a set of key and value pairs, where the keys and values are both text.
json_op.each_text # json_each_text(json)
108 109 110 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 108 def each_text function(:json_each_text) end |
#extract(*a) ⇒ Object
Returns a json value for the object at the given path.
json_op.extract('a') # json_extract_path(json, 'a')
json_op.extract('a', 'b') # json_extract_path(json, 'a', 'b')
116 117 118 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 116 def extract(*a) JSONOp.new(function(:json_extract_path, *a)) end |
#extract_text(*a) ⇒ Object
Returns a text value for the object at the given path.
json_op.extract_text('a') # json_extract_path_text(json, 'a')
json_op.extract_text('a', 'b') # json_extract_path_text(json, 'a', 'b')
124 125 126 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 124 def extract_text(*a) Sequel::SQL::StringExpression.new(:NOOP, function(:json_extract_path_text, *a)) end |
#get_text(key) ⇒ Object
Get JSON array element or object field as text. If an array is given, gets the object at the specified path.
json_op.get_text(1) # (json ->> 1)
json_op.get_text('a') # (json ->> 'a')
json_op.get_text(%w'a b') # (json #>> ARRAY['a', 'b'])
134 135 136 137 138 139 140 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 134 def get_text(key) if is_array?(key) json_op(GET_PATH_TEXT, wrap_array(key)) else json_op(GET_TEXT, key) end end |
#keys ⇒ Object
Returns a set of keys AS text in the json object.
json_op.keys # json_object_keys(json)
145 146 147 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 145 def keys function(:json_object_keys) end |
#pg_json ⇒ Object
Return the receiver, since it is already a JSONOp.
150 151 152 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 150 def pg_json self end |