class Sequel::SQLite::JSONOp

  1. lib/sequel/extensions/sqlite_json_ops.rb
Superclass: Sequel::SQL::Wrapper

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)

Public Instance Aliases

get -> []
minify -> json
typeof -> type

Public Instance methods

[](key)

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]')
[show source]
   # File lib/sequel/extensions/sqlite_json_ops.rb
74 def [](key)
75   json_op(GET, key)
76 end
array_length(*args)

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]')
[show source]
   # File lib/sequel/extensions/sqlite_json_ops.rb
84 def array_length(*args)
85   Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length, *args))
86 end
each(*args)

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')
[show source]
   # File lib/sequel/extensions/sqlite_json_ops.rb
94 def each(*args)
95   function(:each, *args)
96 end
extract(*a)

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')
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
104 def extract(*a)
105   function(:extract, *a)
106 end
get_json(key)

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]')
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
115 def get_json(key)
116   self.class.new(json_op(GET_JSON, key))
117 end
insert(path, value, *args)

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)
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
124 def insert(path, value, *args)
125   wrapped_function(:insert, path, value, *args)
126 end
json()

Returns an expression for a minified version of the JSON.

json_op.json   # json(json)
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
131 def json
132   self.class.new(SQL::Function.new(:json, self))
133 end
patch(json_patch)

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}')
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
139 def patch(json_patch)
140   wrapped_function(:patch, json_patch)
141 end
remove(path, *paths)

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')
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
147 def remove(path, *paths)
148   wrapped_function(:remove, path, *paths)
149 end
replace(path, value, *args)

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)
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
156 def replace(path, value, *args)
157   wrapped_function(:replace, path, value, *args)
158 end
set(path, value, *args)

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)
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
165 def set(path, value, *args)
166   wrapped_function(:set, path, value, *args)
167 end
tree(*args)

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')
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
174 def tree(*args)
175   function(:tree, *args)
176 end
type(*args)

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]')
[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
182 def type(*args)
183   Sequel::SQL::StringExpression.new(:NOOP, function(:type, *args))
184 end
valid()

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

[show source]
    # File lib/sequel/extensions/sqlite_json_ops.rb
188 def valid
189   Sequel::SQL::BooleanExpression.new(:NOOP, function(:valid))
190 end