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
GET | = | ["(".freeze, " -> ".freeze, ")".freeze].freeze | ||
GET_PATH | = | ["(".freeze, " #> ".freeze, ")".freeze].freeze | ||
GET_PATH_TEXT | = | ["(".freeze, " #>> ".freeze, ")".freeze].freeze | ||
GET_TEXT | = | ["(".freeze, " ->> ".freeze, ")".freeze].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 160 def [](key) 161 if is_array?(key) 162 json_op(GET_PATH, wrap_array(key)) 163 else 164 json_op(GET, key) 165 end 166 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 172 def array_elements 173 function(:array_elements) 174 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 179 def array_elements_text 180 function(:array_elements_text) 181 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 186 def array_length 187 Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length)) 188 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 194 def each 195 function(:each) 196 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 202 def each_text 203 function(:each_text) 204 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 210 def extract(*a) 211 self.class.new(function(:extract_path, *a)) 212 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 218 def extract_text(*a) 219 Sequel::SQL::StringExpression.new(:NOOP, function(:extract_path_text, *a)) 220 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 228 def get_text(key) 229 if is_array?(key) 230 json_op(GET_PATH_TEXT, wrap_array(key)) 231 else 232 json_op(GET_TEXT, key) 233 end 234 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 239 def keys 240 function(:object_keys) 241 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 246 def populate(arg) 247 SQL::Function.new(function_name(:populate_record), arg, self) 248 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 253 def populate_set(arg) 254 SQL::Function.new(function_name(:populate_recordset), arg, self) 255 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 260 def strip_nulls 261 self.class.new(function(:strip_nulls)) 262 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 268 def to_record 269 function(:to_record) 270 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 276 def to_recordset 277 function(:to_recordset) 278 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 283 def typeof 284 function(:typeof) 285 end