Class: Sequel::Postgres::JSONOp

Inherits:
SQL::Wrapper show all
Defined in:
lib/sequel/extensions/pg_json_ops.rb

Overview

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)

Constant Summary collapse

GET =
["(".freeze, " -> ".freeze, ")".freeze].freeze
GET_TEXT =
["(".freeze, " ->> ".freeze, ")".freeze].freeze
GET_PATH =
["(".freeze, " #> ".freeze, ")".freeze].freeze
GET_PATH_TEXT =
["(".freeze, " #>> ".freeze, ")".freeze].freeze

Instance Attribute Summary

Attributes inherited from SQL::Wrapper

#value

Instance Method Summary collapse

Methods inherited from SQL::Wrapper

#initialize

Methods included from HStoreOpMethods

#hstore

Methods included from RangeOpMethods

#pg_range

Methods included from ArrayOpMethods

#pg_array

Methods included from PGRowOp::ExpressionMethods

#pg_row

Methods included from SQL::SubscriptMethods

#sql_subscript

Methods included from SQL::StringMethods

#ilike, #like

Methods included from SQL::OrderMethods

#asc, #desc

Methods included from SQL::NumericMethods

#+

Methods included from SQL::ComplexExpressionMethods

#sql_boolean, #sql_number, #sql_string

Methods included from SQL::CastMethods

#cast, #cast_numeric, #cast_string

Methods included from SQL::BooleanMethods

#~

Methods included from SQL::AliasMethods

#as

Methods inherited from SQL::Expression

#==, attr_reader, #eql?, #hash, inherited, #inspect, #lit, #sql_literal

Constructor Details

This class inherits a constructor from Sequel::SQL::Wrapper

Instance Method Details

#[](key) ⇒ Object Also known as: get

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'])


78
79
80
81
82
83
84
# File 'lib/sequel/extensions/pg_json_ops.rb', line 78

def [](key)
  if is_array?(key)
    json_op(GET_PATH, wrap_array(key))
  else
    json_op(GET, key)
  end
end

#array_elementsObject

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

json_op.array_elements # json_oarray_elements(json)


90
91
92
# File 'lib/sequel/extensions/pg_json_ops.rb', line 90

def array_elements
  function(:json_array_elements)
end

#array_lengthObject

Get the length of the outermost json array.

json_op.array_length # json_array_length(json)


97
98
99
# File 'lib/sequel/extensions/pg_json_ops.rb', line 97

def array_length
  Sequel::SQL::NumericExpression.new(:NOOP, function(:json_array_length))
end

#eachObject

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

json_op.each # json_each(json)


105
106
107
# File 'lib/sequel/extensions/pg_json_ops.rb', line 105

def each
  function(:json_each)
end

#each_textObject

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

json_op.each_text # json_each_text(json)


113
114
115
# File 'lib/sequel/extensions/pg_json_ops.rb', line 113

def each_text
  function(:json_each_text)
end

#extract(*a) ⇒ Object

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')


121
122
123
# File 'lib/sequel/extensions/pg_json_ops.rb', line 121

def extract(*a)
  JSONOp.new(function(:json_extract_path, *a))
end

#extract_text(*a) ⇒ Object

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')


129
130
131
# File 'lib/sequel/extensions/pg_json_ops.rb', line 129

def extract_text(*a)
  Sequel::SQL::StringExpression.new(:NOOP, function(:json_extract_path_text, *a))
end

#get_text(key) ⇒ Object

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'])


139
140
141
142
143
144
145
# File 'lib/sequel/extensions/pg_json_ops.rb', line 139

def get_text(key)
  if is_array?(key)
    json_op(GET_PATH_TEXT, wrap_array(key))
  else
    json_op(GET_TEXT, key)
  end
end

#keysObject

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

json_op.keys # json_object_keys(json)


150
151
152
# File 'lib/sequel/extensions/pg_json_ops.rb', line 150

def keys
  function(:json_object_keys)
end

#pg_jsonObject

Return the receiver, since it is already a JSONOp.



155
156
157
# File 'lib/sequel/extensions/pg_json_ops.rb', line 155

def pg_json
  self
end

#populate(arg) ⇒ Object

Expands the given argument using the columns in the json.

json_op.populate(arg) # json_populate_record(arg, json)


162
163
164
# File 'lib/sequel/extensions/pg_json_ops.rb', line 162

def populate(arg)
  SQL::Function.new(:json_populate_record, arg, self)
end

#populate_set(arg) ⇒ Object

Expands the given argument using the columns in the json.

json_op.populate_set(arg) # json_populate_recordset(arg, json)


169
170
171
# File 'lib/sequel/extensions/pg_json_ops.rb', line 169

def populate_set(arg)
  SQL::Function.new(:json_populate_recordset, arg, self)
end