class Sequel::SQLite::JSONOp
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)
Constants
- GET
- GET_JSON
Public Instance Methods
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]')
# File lib/sequel/extensions/sqlite_json_ops.rb, line 73 def [](key) json_op(GET, key) end
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]')
# File lib/sequel/extensions/sqlite_json_ops.rb, line 83 def array_length(*args) Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length, *args)) end
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')
# File lib/sequel/extensions/sqlite_json_ops.rb, line 93 def each(*args) function(:each, *args) end
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')
# File lib/sequel/extensions/sqlite_json_ops.rb, line 103 def extract(*a) function(:extract, *a) end
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]')
# File lib/sequel/extensions/sqlite_json_ops.rb, line 114 def get_json(key) self.class.new(json_op(GET_JSON, key)) end
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)
# File lib/sequel/extensions/sqlite_json_ops.rb, line 123 def insert(path, value, *args) wrapped_function(:insert, path, value, *args) end
Returns an expression for a minified version of the JSON.
json_op.json # json(json)
# File lib/sequel/extensions/sqlite_json_ops.rb, line 130 def json self.class.new(SQL::Function.new(:json, self)) end
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}')
# File lib/sequel/extensions/sqlite_json_ops.rb, line 138 def patch(json_patch) wrapped_function(:patch, json_patch) end
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')
# File lib/sequel/extensions/sqlite_json_ops.rb, line 146 def remove(path, *paths) wrapped_function(:remove, path, *paths) end
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)
# File lib/sequel/extensions/sqlite_json_ops.rb, line 155 def replace(path, value, *args) wrapped_function(:replace, path, value, *args) end
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)
# File lib/sequel/extensions/sqlite_json_ops.rb, line 164 def set(path, value, *args) wrapped_function(:set, path, value, *args) end
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')
# File lib/sequel/extensions/sqlite_json_ops.rb, line 173 def tree(*args) function(:tree, *args) end
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]')
# File lib/sequel/extensions/sqlite_json_ops.rb, line 181 def type(*args) Sequel::SQL::StringExpression.new(:NOOP, function(:type, *args)) end
Returns a boolean expression for whether the JSON is valid or not.
# File lib/sequel/extensions/sqlite_json_ops.rb, line 187 def valid Sequel::SQL::BooleanExpression.new(:NOOP, function(:valid)) end
Private Instance Methods
Internals of the methods that return functions prefixed with
json_.
# File lib/sequel/extensions/sqlite_json_ops.rb, line 199 def function(name, *args) SQL::Function.new("json_#{name}", self, *args) end
Internals of the [], get, #get_json methods, using a placeholder literal string.
# File lib/sequel/extensions/sqlite_json_ops.rb, line 194 def json_op(str, args) self.class.new(Sequel::SQL::PlaceholderLiteralString.new(str, [self, args])) end
Internals of the methods that return functions prefixed with
json_, that return JSON values.
# File lib/sequel/extensions/sqlite_json_ops.rb, line 205 def wrapped_function(*args) self.class.new(function(*args)) end