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
- GET
- GET_PATH
- GET_PATH_TEXT
- GET_TEXT
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 138 def [](key) 139 if is_array?(key) 140 json_op(GET_PATH, wrap_array(key)) 141 else 142 json_op(GET, key) 143 end 144 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 150 def array_elements 151 function(:array_elements) 152 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 157 def array_elements_text 158 function(:array_elements_text) 159 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 164 def array_length 165 Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length)) 166 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 172 def each 173 function(:each) 174 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 180 def each_text 181 function(:each_text) 182 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 188 def extract(*a) 189 self.class.new(function(:extract_path, *a)) 190 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 196 def extract_text(*a) 197 Sequel::SQL::StringExpression.new(:NOOP, function(:extract_path_text, *a)) 198 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 206 def get_text(key) 207 if is_array?(key) 208 json_op(GET_PATH_TEXT, wrap_array(key)) 209 else 210 json_op(GET_TEXT, key) 211 end 212 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 217 def keys 218 function(:object_keys) 219 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 224 def populate(arg) 225 SQL::Function.new(function_name(:populate_record), arg, self) 226 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 231 def populate_set(arg) 232 SQL::Function.new(function_name(:populate_recordset), arg, self) 233 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 238 def strip_nulls 239 self.class.new(function(:strip_nulls)) 240 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 246 def to_record 247 function(:to_record) 248 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 254 def to_recordset 255 function(:to_recordset) 256 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 261 def typeof 262 function(:typeof) 263 end
Private Instance Methods
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 275 def function(name, *args) 276 SQL::Function.new(function_name(name), self, *args) 277 end
Whether the given object represents an array in PostgreSQL.
# File lib/sequel/extensions/pg_json_ops.rb 280 def is_array?(a) 281 a.is_a?(Array) || (defined?(PGArray) && a.is_a?(PGArray)) || (defined?(ArrayOp) && a.is_a?(ArrayOp)) 282 end
Automatically wrap argument in a PGArray
if it is a plain Array
. Requires that the pg_array extension has been loaded to work.
# File lib/sequel/extensions/pg_json_ops.rb 286 def wrap_array(arg) 287 if arg.instance_of?(Array) && Sequel.respond_to?(:pg_array) 288 Sequel.pg_array(arg) 289 else 290 arg 291 end 292 end