Class: Sequel::SQLite::JSONOp

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

Overview

The JSONOp class is a simple container for a single object that defines methods that yield Sequel expression objects representing SQLite json operators and functions.

In the method documentation examples, assume that:

json_op = Sequel.sqlite_json_op(:json)

Instance Attribute Summary

Attributes inherited from Sequel::SQL::Wrapper

#value

Instance Method Summary collapse

Methods inherited from Sequel::SQL::Wrapper

#initialize

Methods included from Sequel::SQL::IsDistinctFrom::Methods

#is_distinct_from

Methods included from JSONOpMethods

#sqlite_json_op

Methods included from Postgres::HStoreOpMethods

#hstore

Methods included from Postgres::RangeOpMethods

#pg_range

Methods included from Postgres::ArrayOpMethods

#pg_array

Methods included from Postgres::JSONOpMethods

#pg_json, #pg_jsonb

Methods included from Postgres::InetOpMethods

#pg_inet

Methods included from Postgres::PGRowOp::ExpressionMethods

#pg_row

Methods included from Sequel::SQL::SubscriptMethods

#sql_subscript

Methods included from Sequel::SQL::StringMethods

#escaped_ilike, #escaped_like, #ilike, #like

Methods included from Sequel::SQL::PatternMatchMethods

#!~, #=~

Methods included from Sequel::SQL::OrderMethods

#asc, #desc

Methods included from Sequel::SQL::NumericMethods

#+, #coerce

Methods included from Sequel::SQL::ComplexExpressionMethods

#sql_boolean, #sql_number, #sql_string

Methods included from Sequel::SQL::CastMethods

#cast, #cast_numeric, #cast_string

Methods included from Sequel::SQL::BooleanMethods

#~

Methods included from Sequel::SQL::AliasMethods

#as

Methods inherited from Sequel::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

Returns an expression for getting the JSON array element or object field at the specified path as a SQLite value.

json_op[1]         # (json ->> 1)
json_op['a']       # (json ->> 'a')
json_op['$.a.b']   # (json ->> '$.a.b')
json_op['$[1][2]'] # (json ->> '$[1][2]')


74
75
76
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 74

def [](key)
  json_op(GET, key)
end

#array_length(*args) ⇒ Object

Returns an expression for the length of the JSON array, or the JSON array at the given path.

json_op.array_length         # json_array_length(json)
json_op.array_length('$[1]') # json_array_length(json, '$[1]')


84
85
86
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 84

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

#each(*args) ⇒ Object

Returns an expression for a set of information extracted from the top-level members of the JSON array or object, or the top-level members of the JSON array or object at the given path.

json_op.each        # json_each(json)
json_op.each('$.a') # json_each(json, '$.a')


94
95
96
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 94

def each(*args)
  function(:each, *args)
end

#extract(*a) ⇒ Object

Returns an expression for the JSON array element or object field at the specified path as a SQLite value, but only accept paths as arguments, and allow the use of multiple paths.

json_op.extract('$.a')        # json_extract(json, '$.a')
json_op.extract('$.a', '$.b') # json_extract(json, '$.a', '$.b')


104
105
106
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 104

def extract(*a)
  function(:extract, *a)
end

#get_json(key) ⇒ Object

Returns an expression for getting the JSON array element or object field at the specified path as a JSON value.

json_op.get_json(1)         # (json -> 1)
json_op.get_json('a')       # (json -> 'a')
json_op.get_json('$.a.b')   # (json -> '$.a.b')
json_op.get_json('$[1][2]') # (json -> '$[1][2]')


115
116
117
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 115

def get_json(key)
  self.class.new(json_op(GET_JSON, key))
end

#insert(path, value, *args) ⇒ Object

Returns an expression for creating new entries at the given paths in the JSON array or object, but not overwriting existing entries.

json_op.insert('$.a', 1)           # json_insert(json, '$.a', 1)
json_op.insert('$.a', 1, '$.b', 2) # json_insert(json, '$.a', 1, '$.b', 2)


124
125
126
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 124

def insert(path, value, *args)
  wrapped_function(:insert, path, value, *args)
end

#jsonObject Also known as: minify

Returns an expression for a minified version of the JSON.

json_op.json   # json(json)


131
132
133
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 131

def json
  self.class.new(SQL::Function.new(:json, self))
end

#patch(json_patch) ⇒ Object

Returns an expression for updating the JSON object using the RFC 7396 MergePatch algorithm

json_op.patch('{"a": 1, "b": null}') # json_patch(json, '{"a": 1, "b": null}')


139
140
141
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 139

def patch(json_patch)
  wrapped_function(:patch, json_patch)
end

#remove(path, *paths) ⇒ Object

Returns an expression for removing entries at the given paths from the JSON array or object.

json_op.remove('$.a')        # json_remove(json, '$.a')
json_op.remove('$.a', '$.b') # json_remove(json, '$.a', '$.b')


147
148
149
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 147

def remove(path, *paths)
  wrapped_function(:remove, path, *paths)
end

#replace(path, value, *args) ⇒ Object

Returns an expression for replacing entries at the given paths in the JSON array or object, but not creating new entries.

json_op.replace('$.a', 1)           # json_replace(json, '$.a', 1)
json_op.replace('$.a', 1, '$.b', 2) # json_replace(json, '$.a', 1, '$.b', 2)


156
157
158
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 156

def replace(path, value, *args)
  wrapped_function(:replace, path, value, *args)
end

#set(path, value, *args) ⇒ Object

Returns an expression for creating or replacing entries at the given paths in the JSON array or object.

json_op.set('$.a', 1)           # json_set(json, '$.a', 1)
json_op.set('$.a', 1, '$.b', 2) # json_set(json, '$.a', 1, '$.b', 2)


165
166
167
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 165

def set(path, value, *args)
  wrapped_function(:set, path, value, *args)
end

#tree(*args) ⇒ Object

Returns an expression for a set of information extracted from the JSON array or object, or the JSON array or object at the given path.

json_op.tree        # json_tree(json)
json_op.tree('$.a') # json_tree(json, '$.a')


174
175
176
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 174

def tree(*args)
  function(:tree, *args)
end

#type(*args) ⇒ Object Also known as: typeof

Returns an expression for the type of the JSON value or the JSON value at the given path.

json_op.type         # json_type(json)
json_op.type('$[1]') # json_type(json, '$[1]')


182
183
184
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 182

def type(*args)
  Sequel::SQL::StringExpression.new(:NOOP, function(:type, *args))
end

#validObject

Returns a boolean expression for whether the JSON is valid or not.



188
189
190
# File 'lib/sequel/extensions/sqlite_json_ops.rb', line 188

def valid
  Sequel::SQL::BooleanExpression.new(:NOOP, function(:valid))
end