Class: Sequel::Postgres::JSONBaseOp

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

Overview

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)

Direct Known Subclasses

JSONBOp, JSONOp

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
IS_JSON =
["(".freeze, " IS JSON".freeze, "".freeze, ")".freeze].freeze
IS_NOT_JSON =
["(".freeze, " IS NOT JSON".freeze, "".freeze, ")".freeze].freeze
EMPTY_STRING =
Sequel::LiteralString.new('').freeze
WITH_UNIQUE =
Sequel::LiteralString.new(' WITH UNIQUE').freeze
IS_JSON_MAP =
{
  nil => EMPTY_STRING,
  :value => Sequel::LiteralString.new(' VALUE').freeze,
  :scalar => Sequel::LiteralString.new(' SCALAR').freeze,
  :object => Sequel::LiteralString.new(' OBJECT').freeze,
  :array => Sequel::LiteralString.new(' ARRAY').freeze
}.freeze

Instance Attribute Summary

Attributes inherited from SQL::Wrapper

#value

Instance Method Summary collapse

Methods inherited from SQL::Wrapper

#initialize

Methods included from SQL::IsDistinctFrom::Methods

#is_distinct_from

Methods included from SQLite::JSONOpMethods

#sqlite_json_op, #sqlite_jsonb_op

Methods included from HStoreOpMethods

#hstore

Methods included from RangeOpMethods

#pg_range

Methods included from ArrayOpMethods

#pg_array

Methods included from JSONOpMethods

#pg_json, #pg_jsonb

Methods included from InetOpMethods

#pg_inet

Methods included from PGRowOp::ExpressionMethods

#pg_row

Methods included from SQL::SubscriptMethods

#sql_subscript

Methods included from SQL::StringMethods

#escaped_ilike, #escaped_like, #ilike, #like

Methods included from SQL::PatternMatchMethods

#!~, #=~

Methods included from SQL::OrderMethods

#asc, #desc

Methods included from SQL::NumericMethods

#+, #coerce

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, #clone, #eql?, #hash, inherited, #inspect

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


181
182
183
184
185
186
187
# File 'lib/sequel/extensions/pg_json_ops.rb', line 181

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_array_elements(json)


193
194
195
# File 'lib/sequel/extensions/pg_json_ops.rb', line 193

def array_elements
  function(:array_elements)
end

#array_elements_textObject

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

json_op.array_elements_text # json_array_elements_text(json)


200
201
202
# File 'lib/sequel/extensions/pg_json_ops.rb', line 200

def array_elements_text
  function(:array_elements_text)
end

#array_lengthObject

Get the length of the outermost json array.

json_op.array_length # json_array_length(json)


207
208
209
# File 'lib/sequel/extensions/pg_json_ops.rb', line 207

def array_length
  Sequel::SQL::NumericExpression.new(:NOOP, function(: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)


215
216
217
# File 'lib/sequel/extensions/pg_json_ops.rb', line 215

def each
  function(: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)


223
224
225
# File 'lib/sequel/extensions/pg_json_ops.rb', line 223

def each_text
  function(: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')


231
232
233
# File 'lib/sequel/extensions/pg_json_ops.rb', line 231

def extract(*a)
  self.class.new(function(: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')


239
240
241
# File 'lib/sequel/extensions/pg_json_ops.rb', line 239

def extract_text(*a)
  Sequel::SQL::StringExpression.new(:NOOP, function(: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'])


249
250
251
252
253
254
255
# File 'lib/sequel/extensions/pg_json_ops.rb', line 249

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

#is_json(opts = OPTS) ⇒ Object

Return whether the json object can be parsed as JSON.

Options:

:type

Check whether the json object can be parsed as a specific type of JSON (:value, :scalar, :object, :array).

:unique

Check JSON objects for unique keys.

json_op.is_json                 # json IS JSON
json_op.is_json(type: :object)  # json IS JSON OBJECT
json_op.is_json(unique: true)   # json IS JSON WITH UNIQUE


267
268
269
# File 'lib/sequel/extensions/pg_json_ops.rb', line 267

def is_json(opts=OPTS)
  _is_json(IS_JSON, opts)
end

#is_not_json(opts = OPTS) ⇒ Object

Return whether the json object cannot be parsed as JSON. The opposite of #is_json. See #is_json for options.

json_op.is_not_json                 # json IS NOT JSON
json_op.is_not_json(type: :object)  # json IS NOT JSON OBJECT
json_op.is_not_json(unique: true)   # json IS NOT JSON WITH UNIQUE


277
278
279
# File 'lib/sequel/extensions/pg_json_ops.rb', line 277

def is_not_json(opts=OPTS)
  _is_json(IS_NOT_JSON, opts)
end

#keysObject

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

json_op.keys # json_object_keys(json)


284
285
286
# File 'lib/sequel/extensions/pg_json_ops.rb', line 284

def keys
  function(:object_keys)
end

#populate(arg) ⇒ Object

Expands the given argument using the columns in the json.

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


291
292
293
# File 'lib/sequel/extensions/pg_json_ops.rb', line 291

def populate(arg)
  SQL::Function.new(function_name(: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)


298
299
300
# File 'lib/sequel/extensions/pg_json_ops.rb', line 298

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

#strip_nullsObject

Returns a json value stripped of all internal null values.

json_op.strip_nulls # json_strip_nulls(json)


305
306
307
# File 'lib/sequel/extensions/pg_json_ops.rb', line 305

def strip_nulls
  self.class.new(function(:strip_nulls))
end

#to_recordObject

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)


313
314
315
# File 'lib/sequel/extensions/pg_json_ops.rb', line 313

def to_record
  function(:to_record)
end

#to_recordsetObject

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)


321
322
323
# File 'lib/sequel/extensions/pg_json_ops.rb', line 321

def to_recordset
  function(:to_recordset)
end

#typeofObject

Returns the type of the outermost json value as text.

json_op.typeof # json_typeof(json)


328
329
330
# File 'lib/sequel/extensions/pg_json_ops.rb', line 328

def typeof
  function(:typeof)
end