class Sequel::Postgres::JSONBaseOp
The JSONBaseOp class is a simple container for a single object that defines methods that yield Sequel expression objects representing PostgreSQL json operators and functions.
In the method documentation examples, assume that:
json_op = Sequel.pg_json(:json)
Constants
- EMPTY_STRING
- GET
- GET_PATH
- GET_PATH_TEXT
- GET_TEXT
- IS_JSON
- IS_JSON_MAP
- IS_NOT_JSON
- WITH_UNIQUE
Public Instance Methods
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'])
# File lib/sequel/extensions/pg_json_ops.rb, line 180 def [](key) if is_array?(key) json_op(GET_PATH, wrap_array(key)) else json_op(GET, key) end end
Returns a set of json values for the elements in the json array.
json_op.array_elements # json_array_elements(json)
# File lib/sequel/extensions/pg_json_ops.rb, line 192 def array_elements function(:array_elements) end
Returns a set of text values for the elements in the json array.
json_op.array_elements_text # json_array_elements_text(json)
# File lib/sequel/extensions/pg_json_ops.rb, line 199 def array_elements_text function(:array_elements_text) end
Get the length of the outermost json array.
json_op.array_length # json_array_length(json)
# File lib/sequel/extensions/pg_json_ops.rb, line 206 def array_length Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length)) end
Returns a set of key and value pairs, where the keys are text and the values are JSON.
json_op.each # json_each(json)
# File lib/sequel/extensions/pg_json_ops.rb, line 214 def each function(:each) end
Returns a set of key and value pairs, where the keys and values are both text.
json_op.each_text # json_each_text(json)
# File lib/sequel/extensions/pg_json_ops.rb, line 222 def each_text function(:each_text) end
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')
# File lib/sequel/extensions/pg_json_ops.rb, line 230 def extract(*a) self.class.new(function(:extract_path, *a)) end
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')
# File lib/sequel/extensions/pg_json_ops.rb, line 238 def extract_text(*a) Sequel::SQL::StringExpression.new(:NOOP, function(:extract_path_text, *a)) end
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'])
# File lib/sequel/extensions/pg_json_ops.rb, line 248 def get_text(key) if is_array?(key) json_op(GET_PATH_TEXT, wrap_array(key)) else json_op(GET_TEXT, key) end end
Return whether the json object can be parsed as JSON.
Options:
- :type
-
Check whether the json object can be parsed as a specific type of JSON (:value, :scalar, :object, :array).
- :unique
-
Check JSON objects for unique keys.
json_op.is_json # json IS JSON json_op.is_json(type: :object) # json IS JSON OBJECT json_op.is_json(unique: true) # json IS JSON WITH UNIQUE
# File lib/sequel/extensions/pg_json_ops.rb, line 266 def is_json(opts=OPTS) _is_json(IS_JSON, opts) end
Return whether the json object cannot be parsed as JSON. The opposite of is_json. See is_json for options.
json_op.is_not_json # json IS NOT JSON json_op.is_not_json(type: :object) # json IS NOT JSON OBJECT json_op.is_not_json(unique: true) # json IS NOT JSON WITH UNIQUE
# File lib/sequel/extensions/pg_json_ops.rb, line 276 def is_not_json(opts=OPTS) _is_json(IS_NOT_JSON, opts) end
Returns a set of keys AS text in the json object.
json_op.keys # json_object_keys(json)
# File lib/sequel/extensions/pg_json_ops.rb, line 283 def keys function(:object_keys) end
Expands the given argument using the columns in the json.
json_op.populate(arg) # json_populate_record(arg, json)
# File lib/sequel/extensions/pg_json_ops.rb, line 290 def populate(arg) SQL::Function.new(function_name(:populate_record), arg, self) end
Expands the given argument using the columns in the json.
json_op.populate_set(arg) # json_populate_recordset(arg, json)
# File lib/sequel/extensions/pg_json_ops.rb, line 297 def populate_set(arg) SQL::Function.new(function_name(:populate_recordset), arg, self) end
Returns a json value stripped of all internal null values.
json_op.strip_nulls # json_strip_nulls(json)
# File lib/sequel/extensions/pg_json_ops.rb, line 304 def strip_nulls self.class.new(function(:strip_nulls)) end
Builds arbitrary record from json object. You need to define the structure of the record using as on the resulting object:
json_op.to_record.as(:x, [Sequel.lit('a integer'), Sequel.lit('b text')]) # json_to_record(json) AS x(a integer, b text)
# File lib/sequel/extensions/pg_json_ops.rb, line 312 def to_record function(:to_record) end
Builds arbitrary set of records from json array of objects. You need to define the structure of the records using as on the resulting object:
json_op.to_recordset.as(:x, [Sequel.lit('a integer'), Sequel.lit('b text')]) # json_to_recordset(json) AS x(a integer, b text)
# File lib/sequel/extensions/pg_json_ops.rb, line 320 def to_recordset function(:to_recordset) end
Returns the type of the outermost json value as text.
json_op.typeof # json_typeof(json)
# File lib/sequel/extensions/pg_json_ops.rb, line 327 def typeof function(:typeof) end
Private Instance Methods
Internals of IS [NOT] JSON support
# File lib/sequel/extensions/pg_json_ops.rb, line 334 def _is_json(lit_array, opts) raise Error, "invalid is_json :type option: #{opts[:type].inspect}" unless type = IS_JSON_MAP[opts[:type]] unique = opts[:unique] ? WITH_UNIQUE : EMPTY_STRING Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(lit_array, [self, type, unique])) end
Return a function with the given name, and the receiver as the first argument, with any additional arguments given.
# File lib/sequel/extensions/pg_json_ops.rb, line 348 def function(name, *args) SQL::Function.new(function_name(name), self, *args) end
Whether the given object represents an array in PostgreSQL.
# File lib/sequel/extensions/pg_json_ops.rb, line 353 def is_array?(a) a.is_a?(Array) || (defined?(PGArray) && a.is_a?(PGArray)) || (defined?(ArrayOp) && a.is_a?(ArrayOp)) end