class Sequel::Postgres::JSONBaseOp

  1. lib/sequel/extensions/pg_json_ops.rb
Superclass: 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)

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

[](key)

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'])
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
159 def [](key)
160   if is_array?(key)
161     json_op(GET_PATH, wrap_array(key))
162   else
163     json_op(GET, key)
164   end
165 end
array_elements()

Returns a set of json values for the elements in the json array.

json_op.array_elements # json_array_elements(json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
171 def array_elements
172   function(:array_elements)
173 end
array_elements_text()

Returns a set of text values for the elements in the json array.

json_op.array_elements_text # json_array_elements_text(json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
178 def array_elements_text
179   function(:array_elements_text)
180 end
array_length()

Get the length of the outermost json array.

json_op.array_length # json_array_length(json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
185 def array_length
186   Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length))
187 end
each()

Returns a set of key and value pairs, where the keys are text and the values are JSON.

json_op.each # json_each(json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
193 def each
194   function(:each)
195 end
each_text()

Returns a set of key and value pairs, where the keys and values are both text.

json_op.each_text # json_each_text(json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
201 def each_text
202   function(:each_text)
203 end
extract(*a)

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')
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
209 def extract(*a)
210   self.class.new(function(:extract_path, *a))
211 end
extract_text(*a)

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')
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
217 def extract_text(*a)
218   Sequel::SQL::StringExpression.new(:NOOP, function(:extract_path_text, *a))
219 end
get_text(key)

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'])
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
227 def get_text(key)
228   if is_array?(key)
229     json_op(GET_PATH_TEXT, wrap_array(key))
230   else
231     json_op(GET_TEXT, key)
232   end
233 end
keys()

Returns a set of keys AS text in the json object.

json_op.keys # json_object_keys(json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
238 def keys
239   function(:object_keys)
240 end
populate(arg)

Expands the given argument using the columns in the json.

json_op.populate(arg) # json_populate_record(arg, json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
245 def populate(arg)
246   SQL::Function.new(function_name(:populate_record), arg, self)
247 end
populate_set(arg)

Expands the given argument using the columns in the json.

json_op.populate_set(arg) # json_populate_recordset(arg, json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
252 def populate_set(arg)
253   SQL::Function.new(function_name(:populate_recordset), arg, self)
254 end
strip_nulls()

Returns a json value stripped of all internal null values.

json_op.strip_nulls # json_strip_nulls(json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
259 def strip_nulls
260   self.class.new(function(:strip_nulls))
261 end
to_record()

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)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
267 def to_record
268   function(:to_record)
269 end
to_recordset()

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)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
275 def to_recordset
276   function(:to_recordset)
277 end
typeof()

Returns the type of the outermost json value as text.

json_op.typeof # json_typeof(json)
[show source]
    # File lib/sequel/extensions/pg_json_ops.rb
282 def typeof
283   function(:typeof)
284 end