Class: Sequel::Postgres::JSONBaseOp
- Inherits:
-
SQL::Wrapper
- Object
- SQL::Expression
- SQL::GenericExpression
- SQL::Wrapper
- Sequel::Postgres::JSONBaseOp
- 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_elements_text ⇒ Object
Returns a set of text 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.
-
#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.
-
#strip_nulls ⇒ Object
Returns a json value stripped of all internal null values.
-
#to_record ⇒ Object
Builds arbitrary record from json object.
-
#to_recordset ⇒ Object
Builds arbitrary set of records from json array of objects.
-
#typeof ⇒ Object
Returns the type of the outermost json value as text.
Methods inherited from SQL::Wrapper
Methods included from SQL::IsDistinctFrom::Methods
Methods included from SQLite::JSONOpMethods
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
#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
#[](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'])
160 161 162 163 164 165 166 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 160 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_array_elements(json)
172 173 174 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 172 def array_elements function(:array_elements) end |
#array_elements_text ⇒ Object
Returns a set of text values for the elements in the json array.
json_op.array_elements_text # json_array_elements_text(json)
179 180 181 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 179 def array_elements_text function(:array_elements_text) end |
#array_length ⇒ Object
Get the length of the outermost json array.
json_op.array_length # json_array_length(json)
186 187 188 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 186 def array_length Sequel::SQL::NumericExpression.new(:NOOP, function(: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)
194 195 196 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 194 def each function(: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)
202 203 204 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 202 def each_text function(: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')
210 211 212 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 210 def extract(*a) self.class.new(function(: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')
218 219 220 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 218 def extract_text(*a) Sequel::SQL::StringExpression.new(:NOOP, function(: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'])
228 229 230 231 232 233 234 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 228 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)
239 240 241 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 239 def keys function(:object_keys) end |
#populate(arg) ⇒ Object
Expands the given argument using the columns in the json.
json_op.populate(arg) # json_populate_record(arg, json)
246 247 248 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 246 def populate(arg) SQL::Function.new(function_name(:populate_record), arg, self) end |
#populate_set(arg) ⇒ Object
Expands the given argument using the columns in the json.
json_op.populate_set(arg) # json_populate_recordset(arg, json)
253 254 255 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 253 def populate_set(arg) SQL::Function.new(function_name(:populate_recordset), arg, self) end |
#strip_nulls ⇒ Object
Returns a json value stripped of all internal null values.
json_op.strip_nulls # json_strip_nulls(json)
260 261 262 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 260 def strip_nulls self.class.new(function(:strip_nulls)) end |
#to_record ⇒ Object
268 269 270 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 268 def to_record function(:to_record) end |
#to_recordset ⇒ Object
276 277 278 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 276 def to_recordset function(:to_recordset) end |
#typeof ⇒ Object
Returns the type of the outermost json value as text.
json_op.typeof # json_typeof(json)
283 284 285 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 283 def typeof function(:typeof) end |