Method: Sequel::Postgres::JSONBaseOp#value
- Defined in:
- lib/sequel/extensions/pg_json_ops.rb
#value(path = (no_args_given = true), opts = OPTS) ⇒ Object
If called without arguments, operates as SQL::Wrapper#value. Otherwise, return the result of applying the JSON path expression to the receiver, by default returning results as text. Options:
- :on_empty
-
How to handle case where path expression yields an empty set. Uses same values as :on_error option.
- :on_error
-
How to handle errors when evaluating the JSON path expression.
- :null
-
Return nil (default)
- :error
-
raise a DatabaseError
- any other value
-
used as default value
- :passing
-
Variables to pass to the JSON path expression. Keys are variable names, values are the values of the variable.
- :returning
-
The data type to return (text by default)
json_op.value("$.a") # json_value(json, '$.a')
json_op.value("$.a", passing: {a: 1}) # json_value(json, '$.a' PASSING 1 AS a)
json_op.value("$.a", on_error: :error) # json_value(json, '$.a' ERROR ON ERROR)
json_op.value("$.a", returning: Time) # json_value(json, '$.a' RETURNING timestamp)
json_op.value("$.a", on_empty: 2) # json_value(json, '$.a' DEFAULT 2 ON EMPTY)
514 515 516 517 518 519 520 521 |
# File 'lib/sequel/extensions/pg_json_ops.rb', line 514 def value(path=(no_args_given = true), opts=OPTS) if no_args_given # Act as SQL::Wrapper#value super() else Sequel::SQL::StringExpression.new(:NOOP, JSONValueOp.new(self, path, opts)) end end |