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)
Methods
Public Instance
Constants
EMPTY_STRING | = | Sequel::LiteralString.new('').freeze | ||
GET | = | ["(".freeze, " -> ".freeze, ")".freeze].freeze | ||
GET_PATH | = | ["(".freeze, " #> ".freeze, ")".freeze].freeze | ||
GET_PATH_TEXT | = | ["(".freeze, " #>> ".freeze, ")".freeze].freeze | ||
GET_TEXT | = | ["(".freeze, " ->> ".freeze, ")".freeze].freeze | ||
IS_JSON | = | ["(".freeze, " IS JSON".freeze, "".freeze, ")".freeze].freeze | ||
IS_JSON_MAP | = | { nil => EMPTY_STRING, :value => Sequel::LiteralString.new(' VALUE').freeze, :scalar => Sequel::LiteralString.new(' SCALAR').freeze, :object => Sequel::LiteralString.new(' OBJECT').freeze, :array => Sequel::LiteralString.new(' ARRAY').freeze }.freeze | ||
IS_NOT_JSON | = | ["(".freeze, " IS NOT JSON".freeze, "".freeze, ")".freeze].freeze | ||
WITH_UNIQUE | = | Sequel::LiteralString.new(' WITH UNIQUE').freeze |
Public Instance Aliases
get | -> | [] |
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 181 def [](key) 182 if is_array?(key) 183 json_op(GET_PATH, wrap_array(key)) 184 else 185 json_op(GET, key) 186 end 187 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 193 def array_elements 194 function(:array_elements) 195 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 200 def array_elements_text 201 function(:array_elements_text) 202 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 207 def array_length 208 Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length)) 209 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 215 def each 216 function(:each) 217 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 223 def each_text 224 function(:each_text) 225 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 231 def extract(*a) 232 self.class.new(function(:extract_path, *a)) 233 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 239 def extract_text(*a) 240 Sequel::SQL::StringExpression.new(:NOOP, function(:extract_path_text, *a)) 241 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 249 def get_text(key) 250 if is_array?(key) 251 json_op(GET_PATH_TEXT, wrap_array(key)) 252 else 253 json_op(GET_TEXT, key) 254 end 255 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 267 def is_json(opts=OPTS) 268 _is_json(IS_JSON, opts) 269 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 277 def is_not_json(opts=OPTS) 278 _is_json(IS_NOT_JSON, opts) 279 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 284 def keys 285 function(:object_keys) 286 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 291 def populate(arg) 292 SQL::Function.new(function_name(:populate_record), arg, self) 293 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 298 def populate_set(arg) 299 SQL::Function.new(function_name(:populate_recordset), arg, self) 300 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 305 def strip_nulls 306 self.class.new(function(:strip_nulls)) 307 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 313 def to_record 314 function(:to_record) 315 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 321 def to_recordset 322 function(:to_recordset) 323 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 328 def typeof 329 function(:typeof) 330 end