Class | Sequel::Postgres::JSONOp |
In: |
lib/sequel/extensions/pg_json_ops.rb
|
Parent: | Sequel::SQL::Wrapper |
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 73 73: def [](key) 74: if is_array?(key) 75: json_op(GET_PATH, wrap_array(key)) 76: else 77: json_op(GET, key) 78: end 79: end
Returns a set of json values for the elements in the json array.
json_op.array_elements # json_oarray_elements(json)
# File lib/sequel/extensions/pg_json_ops.rb, line 85 85: def array_elements 86: function(:json_array_elements) 87: 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 92 92: def array_length 93: Sequel::SQL::NumericExpression.new(:NOOP, function(:json_array_length)) 94: 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 116 116: def extract(*a) 117: JSONOp.new(function(:json_extract_path, *a)) 118: 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 124 124: def extract_text(*a) 125: Sequel::SQL::StringExpression.new(:NOOP, function(:json_extract_path_text, *a)) 126: 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 134 134: def get_text(key) 135: if is_array?(key) 136: json_op(GET_PATH_TEXT, wrap_array(key)) 137: else 138: json_op(GET_TEXT, key) 139: end 140: 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 157 157: def populate(arg) 158: SQL::Function.new(:json_populate_record, arg, self) 159: end