class Sequel::Postgres::JSONOp

  1. lib/sequel/extensions/pg_json_ops.rb
Parent: Postgres

The JSONOp 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, line 73
def [](key)
  if is_array?(key)
    json_op(GET_PATH, wrap_array(key))
  else
    json_op(GET, key)
  end
end
array_elements ()

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

json_op.array_elements # json_oarray_elements(json)
[show source]
# File lib/sequel/extensions/pg_json_ops.rb, line 85
def array_elements
  function(:json_array_elements)
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, line 92
def array_length
  Sequel::SQL::NumericExpression.new(:NOOP, function(:json_array_length))
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, line 100
def each
  function(:json_each)
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, line 108
def each_text
  function(:json_each_text)
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, line 116
def extract(*a)
  JSONOp.new(function(:json_extract_path, *a))
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, line 124
def extract_text(*a)
  Sequel::SQL::StringExpression.new(:NOOP, function(:json_extract_path_text, *a))
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, line 134
def get_text(key)
  if is_array?(key)
    json_op(GET_PATH_TEXT, wrap_array(key))
  else
    json_op(GET_TEXT, key)
  end
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, line 145
def keys
  function(:json_object_keys)
end
pg_json ()

Return the receiver, since it is already a JSONOp.

[show source]
# File lib/sequel/extensions/pg_json_ops.rb, line 150
def pg_json
  self
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, line 157
def populate(arg)
  SQL::Function.new(:json_populate_record, arg, self)
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, line 164
def populate_set(arg)
  SQL::Function.new(:json_populate_recordset, arg, self)
end