Class | Sequel::Postgres::JSONBaseOp |
In: |
lib/sequel/extensions/pg_json_ops.rb
|
Parent: | Sequel::SQL::Wrapper |
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)
GET | = | ["(".freeze, " -> ".freeze, ")".freeze].freeze |
GET_TEXT | = | ["(".freeze, " ->> ".freeze, ")".freeze].freeze |
GET_PATH | = | ["(".freeze, " #> ".freeze, ")".freeze].freeze |
GET_PATH_TEXT | = | ["(".freeze, " #>> ".freeze, ")".freeze].freeze |
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 110 110: def [](key) 111: if is_array?(key) 112: json_op(GET_PATH, wrap_array(key)) 113: else 114: json_op(GET, key) 115: end 116: 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 122 122: def array_elements 123: function(:array_elements) 124: 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 129 129: def array_elements_text 130: function(:array_elements_text) 131: 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 136 136: def array_length 137: Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length)) 138: 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 160 160: def extract(*a) 161: self.class.new(function(:extract_path, *a)) 162: 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 168 168: def extract_text(*a) 169: Sequel::SQL::StringExpression.new(:NOOP, function(:extract_path_text, *a)) 170: 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 178 178: def get_text(key) 179: if is_array?(key) 180: json_op(GET_PATH_TEXT, wrap_array(key)) 181: else 182: json_op(GET_TEXT, key) 183: end 184: 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 196 196: def populate(arg) 197: SQL::Function.new(function_name(:populate_record), arg, self) 198: 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 203 203: def populate_set(arg) 204: SQL::Function.new(function_name(:populate_recordset), arg, self) 205: 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 210 210: def strip_nulls 211: self.class.new(function(:strip_nulls)) 212: 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 218 218: def to_record 219: function(:to_record) 220: 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 226 226: def to_recordset 227: function(:to_recordset) 228: end