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 90 90: def [](key) 91: if is_array?(key) 92: json_op(GET_PATH, wrap_array(key)) 93: else 94: json_op(GET, key) 95: end 96: 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 102 102: def array_elements 103: function(:array_elements) 104: 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 109 109: def array_elements_text 110: function(:array_elements_text) 111: 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 116 116: def array_length 117: Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length)) 118: 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 140 140: def extract(*a) 141: self.class.new(function(:extract_path, *a)) 142: 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 148 148: def extract_text(*a) 149: Sequel::SQL::StringExpression.new(:NOOP, function(:extract_path_text, *a)) 150: 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 158 158: def get_text(key) 159: if is_array?(key) 160: json_op(GET_PATH_TEXT, wrap_array(key)) 161: else 162: json_op(GET_TEXT, key) 163: end 164: 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 176 176: def populate(arg) 177: SQL::Function.new(function_name(:populate_record), arg, self) 178: 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 183 183: def populate_set(arg) 184: SQL::Function.new(function_name(:populate_recordset), arg, self) 185: 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 191 191: def to_record 192: function(:to_record) 193: 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 199 199: def to_recordset 200: function(:to_recordset) 201: end