class Sequel::Postgres::JSONBaseOp

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
GET_PATH
GET_PATH_TEXT
GET_TEXT

Public Instance Methods

[](key) click to toggle source

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
127 def [](key)
128   if is_array?(key)
129     json_op(GET_PATH, wrap_array(key))
130   else
131     json_op(GET, key)
132   end
133 end
Also aliased as: get
array_elements() click to toggle source

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
139 def array_elements
140   function(:array_elements)
141 end
array_elements_text() click to toggle source

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
146 def array_elements_text
147   function(:array_elements_text)
148 end
array_length() click to toggle source

Get the length of the outermost json array.

json_op.array_length # json_array_length(json)
    # File lib/sequel/extensions/pg_json_ops.rb
153 def array_length
154   Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length))
155 end
each() click to toggle source

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
161 def each
162   function(:each)
163 end
each_text() click to toggle source

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
169 def each_text
170   function(:each_text)
171 end
extract(*a) click to toggle source

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
177 def extract(*a)
178   self.class.new(function(:extract_path, *a))
179 end
extract_text(*a) click to toggle source

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
185 def extract_text(*a)
186   Sequel::SQL::StringExpression.new(:NOOP, function(:extract_path_text, *a))
187 end
get(key)
Alias for: []
get_text(key) click to toggle source

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
195 def get_text(key)
196   if is_array?(key)
197     json_op(GET_PATH_TEXT, wrap_array(key))
198   else
199     json_op(GET_TEXT, key)
200   end
201 end
keys() click to toggle source

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
206 def keys
207   function(:object_keys)
208 end
populate(arg) click to toggle source

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
213 def populate(arg)
214   SQL::Function.new(function_name(:populate_record), arg, self)
215 end
populate_set(arg) click to toggle source

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
220 def populate_set(arg)
221   SQL::Function.new(function_name(:populate_recordset), arg, self)
222 end
strip_nulls() click to toggle source

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
227 def strip_nulls
228   self.class.new(function(:strip_nulls))
229 end
to_record() click to toggle source

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
235 def to_record
236   function(:to_record)
237 end
to_recordset() click to toggle source

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
243 def to_recordset
244   function(:to_recordset)
245 end
typeof() click to toggle source

Returns the type of the outermost json value as text.

json_op.typeof # json_typeof(json)
    # File lib/sequel/extensions/pg_json_ops.rb
250 def typeof
251   function(:typeof)
252 end

Private Instance Methods

function(name, *args) click to toggle source

Return a function with the given name, and the receiver as the first argument, with any additional arguments given.

    # File lib/sequel/extensions/pg_json_ops.rb
264 def function(name, *args)
265   SQL::Function.new(function_name(name), self, *args)
266 end
is_array?(a) click to toggle source

Whether the given object represents an array in PostgreSQL.

    # File lib/sequel/extensions/pg_json_ops.rb
269 def is_array?(a)
270   a.is_a?(Array) || (defined?(PGArray) && a.is_a?(PGArray)) || (defined?(ArrayOp) && a.is_a?(ArrayOp))
271 end
json_op(str, args) click to toggle source

Return a placeholder literal with the given str and args, wrapped in an JSONOp or JSONBOp, used by operators that return json or jsonb.

    # File lib/sequel/extensions/pg_json_ops.rb
258 def json_op(str, args)
259   self.class.new(Sequel::SQL::PlaceholderLiteralString.new(str, [self, args]))
260 end
wrap_array(arg) click to toggle source

Automatically wrap argument in a PGArray if it is a plain Array. Requires that the pg_array extension has been loaded to work.

    # File lib/sequel/extensions/pg_json_ops.rb
275 def wrap_array(arg)
276   if arg.instance_of?(Array) && Sequel.respond_to?(:pg_array)
277     Sequel.pg_array(arg)
278   else
279     arg
280   end
281 end