1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
|
# frozen-string-literal: true
#
# The pg_json extension adds support for Sequel to handle
# PostgreSQL's json and jsonb types. By default, it wraps
# JSON arrays and JSON objects with ruby array-like and
# hash-like objects. If you would like to wrap JSON primitives
# (numbers, strings, +null+, +true+, and +false+), you need to
# use the +wrap_json_primitives+ setter:
#
# DB.extension :pg_json
# DB.wrap_json_primitives = true
#
# Note that wrapping JSON primitives changes the behavior for
# JSON false and null values. Because only +false+ and +nil+
# in Ruby are considered falsey, wrapping these objects results
# in unexpected behavior if you use the values directly in
# conditionals:
#
# if DB[:table].get(:json_column)
# # called if the value of json_column is null/false
# # if you are wrapping primitives
# end
#
# To extract the Ruby primitive object from the wrapper object,
# you can use +__getobj__+ (this comes from Ruby's delegate library).
#
# To wrap an existing Ruby array, hash, string, integer, float,
# +nil+, +true+, or +false+, use +Sequel.pg_json_wrap+ or +Sequel.pg_jsonb_wrap+:
#
# Sequel.pg_json_wrap(object) # json type
# Sequel.pg_jsonb_wrap(object) # jsonb type
#
# So if you want to insert an array or hash into an json database column:
#
# DB[:table].insert(column: Sequel.pg_json_wrap([1, 2, 3]))
# DB[:table].insert(column: Sequel.pg_json_wrap({'a'=>1, 'b'=>2}))
#
# Note that the +pg_json_wrap+ and +pg_jsonb_wrap+ methods only handle Ruby primitives,
# they do not handle already wrapped objects.
#
# If you have loaded the {core_extensions extension}[rdoc-ref:doc/core_extensions.rdoc],
# or you have loaded the core_refinements extension
# and have activated refinements for the file, you can also use the
# +pg_json+ and +pg_jsonb+ methods directly on Array or Hash:
#
# array.pg_json # json type
# array.pg_jsonb # jsonb type
#
# hash.pg_json # json type
# hash.pg_jsonb # jsonb type
#
# Model classes that use json or jsonb columns will have typecasting automatically
# setup, so you can assign Ruby primitives to model columns and have the wrapped
# objects automatically created. However, for backwards compatibility, passing
# a string object will parse the string as JSON, not create a JSON string object.
#
# obj = Model.new
# obj.json_column = {'a'=>'b'}
# obj.json_column.class
# # => Sequel::Postgres::JSONHash
# obj.json_column['a']
# # => 'b'
#
# obj.json_column = '{"a": "b"}'
# obj.json_column.class
# # => Sequel::Postgres::JSONHash
# obj.json_column['a']
# # => 'b'
#
# You can change the handling of string typecasting by using +typecast_json_strings+:
#
# DB.typecast_json_strings = true
# obj.json_column = '{"a": "b"}'
# obj.json_column.class
# # => Sequel::Postgres::JSONString
# obj.json_column
# # => '{"a": "b"}'
#
# Note that +nil+ values are never automatically wrapped:
#
# obj.json_column = nil
# obj.json_column.class
# # => NilClass
# obj.json_column
# # => nil
#
# If you want to set a JSON null value when using a model, you must wrap it
# explicitly:
#
# obj.json_column = Sequel.pg_json_wrap(nil)
# obj.json_column.class
# # => Sequel::Postgres::JSONNull
# obj.json_column
# # => nil
#
# To use this extension, load it into the Database instance:
#
# DB.extension :pg_json
#
# See the {schema modification guide}[rdoc-ref:doc/schema_modification.rdoc]
# for details on using json columns in CREATE/ALTER TABLE statements.
#
# This extension integrates with the pg_array extension. If you plan
# to use the json[] or jsonb[] types, load the pg_array extension before the
# pg_json extension:
#
# DB.extension :pg_array, :pg_json
#
# Note that when accessing json hashes, you should always use strings for keys.
# Attempting to use other values (such as symbols) will not work correctly.
#
# This extension requires both the json and delegate libraries. However, you
# can override +Sequel.parse_json+, +Sequel.object_to_json+, and
# +Sequel.json_parser_error_class+ to use an alternative JSON implementation.
#
# Related modules: Sequel::Postgres::JSONDatabaseMethods
require 'delegate'
require 'json'
module Sequel
module Postgres
# A module included in all of the JSON wrapper classes.
module JSONObject
end
# A module included in all of the JSONB wrapper classes.
module JSONBObject
end
create_delegate_class = lambda do |name, delegate_class|
base_class = DelegateClass(delegate_class)
base_class.class_eval do
include Sequel::SQL::AliasMethods
include Sequel::SQL::CastMethods
end
json_class = Class.new(base_class) do
include JSONObject
def sql_literal_append(ds, sql)
ds.literal_append(sql, Sequel.object_to_json(self))
sql << '::json'
end
# Allow automatic parameterization.
def sequel_auto_param_type(ds)
"::json"
end
end
jsonb_class = Class.new(base_class) do
include JSONBObject
def sql_literal_append(ds, sql)
ds.literal_append(sql, Sequel.object_to_json(self))
sql << '::jsonb'
end
# Allow automatic parameterization.
def sequel_auto_param_type(ds)
"::jsonb"
end
end
const_set(:"JSON#{name}Base", base_class)
const_set(:"JSON#{name}", json_class)
const_set(:"JSONB#{name}", jsonb_class)
end
create_delegate_class.call(:Array, Array)
create_delegate_class.call(:Hash, Hash)
create_delegate_class.call(:String, String)
create_delegate_class.call(:Integer, Integer)
create_delegate_class.call(:Float, Float)
create_delegate_class.call(:Null, NilClass)
create_delegate_class.call(:True, TrueClass)
create_delegate_class.call(:False, FalseClass)
JSON_WRAPPER_MAPPING = {
::Array => JSONArray,
::Hash => JSONHash,
}.freeze
JSONB_WRAPPER_MAPPING = {
::Array => JSONBArray,
::Hash => JSONBHash,
}.freeze
JSON_PRIMITIVE_WRAPPER_MAPPING = {
::String => JSONString,
::Integer => JSONInteger,
::Float => JSONFloat,
::NilClass => JSONNull,
::TrueClass => JSONTrue,
::FalseClass => JSONFalse,
}
JSONB_PRIMITIVE_WRAPPER_MAPPING = {
::String => JSONBString,
::Integer => JSONBInteger,
::Float => JSONBFloat,
::NilClass => JSONBNull,
::TrueClass => JSONBTrue,
::FalseClass => JSONBFalse,
}
if RUBY_VERSION < '2.4'
# :nocov:
JSON_PRIMITIVE_WRAPPER_MAPPING[Fixnum] = JSONInteger
JSON_PRIMITIVE_WRAPPER_MAPPING[Bignum] = JSONInteger
JSONB_PRIMITIVE_WRAPPER_MAPPING[Fixnum] = JSONBInteger
JSONB_PRIMITIVE_WRAPPER_MAPPING[Bignum] = JSONBInteger
# :nocov:
end
JSON_PRIMITIVE_WRAPPER_MAPPING.freeze
JSONB_PRIMITIVE_WRAPPER_MAPPING.freeze
JSON_COMBINED_WRAPPER_MAPPING =JSON_WRAPPER_MAPPING.merge(JSON_PRIMITIVE_WRAPPER_MAPPING).freeze
JSONB_COMBINED_WRAPPER_MAPPING =JSONB_WRAPPER_MAPPING.merge(JSONB_PRIMITIVE_WRAPPER_MAPPING).freeze
JSONB_WRAP_CLASSES = JSONB_COMBINED_WRAPPER_MAPPING.keys.freeze
Sequel::Deprecation.deprecate_constant(self, :JSON_WRAPPER_MAPPING)
Sequel::Deprecation.deprecate_constant(self, :JSONB_WRAPPER_MAPPING)
Sequel::Deprecation.deprecate_constant(self, :JSON_PRIMITIVE_WRAPPER_MAPPING)
Sequel::Deprecation.deprecate_constant(self, :JSONB_PRIMITIVE_WRAPPER_MAPPING)
Sequel::Deprecation.deprecate_constant(self, :JSON_COMBINED_WRAPPER_MAPPING)
Sequel::Deprecation.deprecate_constant(self, :JSONB_COMBINED_WRAPPER_MAPPING)
Sequel::Deprecation.deprecate_constant(self, :JSONB_WRAP_CLASSES)
JSON_WRAP_CLASSES = [Hash, Array, String, Integer, Float, NilClass, TrueClass, FalseClass].freeze
# Methods enabling Database object integration with the json type.
module JSONDatabaseMethods
def self.extended(db)
db.instance_exec do
add_conversion_proc(114, method(:_db_parse_json))
add_conversion_proc(3802, method(:_db_parse_jsonb))
if respond_to?(:register_array_type)
register_array_type('json', :oid=>199, :scalar_oid=>114)
register_array_type('jsonb', :oid=>3807, :scalar_oid=>3802)
end
@schema_type_classes[:json] = [JSONObject]
@schema_type_classes[:jsonb] = [JSONBObject]
end
end
# Return the wrapper class for the json type if value is Hash or Array.
def self.json_wrapper(value)
case value
when ::Hash
JSONHash
when ::Array
JSONArray
end
end
# Return the wrapper class for the jsonb type if value is Hash or Array.
def self.jsonb_wrapper(value)
case value
when ::Hash
JSONBHash
when ::Array
JSONBArray
end
end
# Return the wrapper class for the json type if value is a supported type.
def self.json_primitive_wrapper(value)
case value
when ::Hash
JSONHash
when ::Array
JSONArray
when ::String
JSONString
when ::Integer
JSONInteger
when ::Float
JSONFloat
when ::NilClass
JSONNull
when ::TrueClass
JSONTrue
when ::FalseClass
JSONFalse
end
end
# Return the wrapper class for the jsonb type if value is a supported type.
def self.jsonb_primitive_wrapper(value)
case value
when ::Hash
JSONBHash
when ::Array
JSONBArray
when ::String
JSONBString
when ::Integer
JSONBInteger
when ::Float
JSONBFloat
when ::NilClass
JSONBNull
when ::TrueClass
JSONBTrue
when ::FalseClass
JSONBFalse
end
end
# Deprecated
def self.db_parse_json(s)
# SEQUEL6: Remove
parse_json(s)
rescue Sequel::InvalidValue
raise unless s.is_a?(String)
parse_json("[#{s}]").first
end
# Deprecated
def self.db_parse_jsonb(s)
# SEQUEL6: Remove
parse_json(s, true)
rescue Sequel::InvalidValue
raise unless s.is_a?(String)
parse_json("[#{s}]").first
end
# Deprecated
def self.parse_json(s, jsonb=false)
# SEQUEL6: Remove
Sequel::Deprecation.deprecate("Sequel::Postgres::JSONDatabaseMethods.{parse_json,db_parse_json,db_parse_jsonb} are deprecated and will be removed in Sequel 6.")
begin
value = Sequel.parse_json(s)
rescue Sequel.json_parser_error_class => e
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
end
case value
when Array
(jsonb ? JSONBArray : JSONArray).new(value)
when Hash
(jsonb ? JSONBHash : JSONHash).new(value)
when String, Numeric, true, false, nil
value
else
raise Sequel::InvalidValue, "unhandled json value: #{value.inspect} (from #{s.inspect})"
end
end
# Whether to wrap JSON primitives instead of using Ruby objects.
# Wrapping the primitives allows the primitive values to roundtrip,
# but it can cause problems, especially as false/null JSON values
# will be treated as truthy in Ruby due to the wrapping. False by
# default.
attr_accessor :wrap_json_primitives
# Whether to typecast strings for json/jsonb types as JSON
# strings, instead of trying to parse the string as JSON.
# False by default.
attr_accessor :typecast_json_strings
# Handle json and jsonb types in bound variables
def bound_variable_arg(arg, conn)
case arg
when JSONObject, JSONBObject
Sequel.object_to_json(arg)
else
super
end
end
private
# Parse JSON data coming from the database. Since PostgreSQL allows
# non JSON data in JSON fields (such as plain numbers and strings),
# we don't want to raise an exception for that.
def _db_parse_json(s)
_wrap_json(_parse_json(s))
rescue Sequel::InvalidValue
raise unless s.is_a?(String)
_wrap_json(_parse_json("[#{s}]").first)
end
# Same as _db_parse_json, but consider the input as jsonb.
def _db_parse_jsonb(s)
_wrap_jsonb(_parse_json(s))
rescue Sequel::InvalidValue
raise unless s.is_a?(String)
_wrap_jsonb(_parse_json("[#{s}]").first)
end
# Parse the given string as json, returning either a JSONArray
# or JSONHash instance (or JSONBArray or JSONBHash instance if jsonb
# argument is true), or a String, Numeric, true, false, or nil
# if the json library used supports that.
def _parse_json(s)
Sequel.parse_json(s)
rescue Sequel.json_parser_error_class => e
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
end
# Wrap the parsed JSON value in the appropriate JSON wrapper class.
# Only wrap primitive values if wrap_json_primitives is set.
def _wrap_json(value)
if klass = JSONDatabaseMethods.json_wrapper(value)
klass.new(value)
elsif klass = JSONDatabaseMethods.json_primitive_wrapper(value)
if wrap_json_primitives
klass.new(value)
else
value
end
else
raise Sequel::InvalidValue, "unhandled json value: #{value.inspect}"
end
end
# Wrap the parsed JSON value in the appropriate JSONB wrapper class.
# Only wrap primitive values if wrap_json_primitives is set.
def _wrap_jsonb(value)
if klass = JSONDatabaseMethods.jsonb_wrapper(value)
klass.new(value)
elsif klass = JSONDatabaseMethods.jsonb_primitive_wrapper(value)
if wrap_json_primitives
klass.new(value)
else
value
end
else
raise Sequel::InvalidValue, "unhandled jsonb value: #{value.inspect}"
end
end
# Make the column type detection recognize the json types.
def schema_column_type(db_type)
case db_type
when 'json'
:json
when 'jsonb'
:jsonb
else
super
end
end
# Set the :callable_default value if the default value is recognized as an empty json/jsonb array/hash.
def schema_post_process(_)
super.each do |a|
h = a[1]
if (h[:type] == :json || h[:type] == :jsonb) && h[:default] =~ /\A'(\{\}|\[\])'::jsonb?\z/
is_array = $1 == '[]'
klass = if h[:type] == :json
if is_array
JSONArray
else
JSONHash
end
elsif is_array
JSONBArray
else
JSONBHash
end
h[:callable_default] = lambda{klass.new(is_array ? [] : {})}
end
end
end
# Convert the value given to a JSON wrapper object.
def typecast_value_json(value)
case value
when JSONObject
value
when String
if typecast_json_strings
JSONString.new(value)
else
_wrap_json(_parse_json(value))
end
when *JSON_WRAP_CLASSES
JSONDatabaseMethods.json_primitive_wrapper(value).new(value)
when JSONBObject
value = value.__getobj__
JSONDatabaseMethods.json_primitive_wrapper(value).new(value)
else
raise Sequel::InvalidValue, "invalid value for json: #{value.inspect}"
end
end
# Convert the value given to a JSONB wrapper object.
def typecast_value_jsonb(value)
case value
when JSONBObject
value
when String
if typecast_json_strings
JSONBString.new(value)
else
_wrap_jsonb(_parse_json(value))
end
when *JSON_WRAP_CLASSES
JSONDatabaseMethods.jsonb_primitive_wrapper(value).new(value)
when JSONObject
value = value.__getobj__
JSONDatabaseMethods.jsonb_primitive_wrapper(value).new(value)
else
raise Sequel::InvalidValue, "invalid value for jsonb: #{value.inspect}"
end
end
end
end
module SQL::Builders
# Wrap the array or hash in a Postgres::JSONArray or Postgres::JSONHash.
# Also handles Postgres::JSONObject and JSONBObjects.
# For other objects, calls +Sequel.pg_json_op+ (which is defined
# by the pg_json_ops extension).
def pg_json(v)
case v
when Postgres::JSONObject
v
when Array
Postgres::JSONArray.new(v)
when Hash
Postgres::JSONHash.new(v)
when Postgres::JSONBObject
v = v.__getobj__
Postgres::JSONDatabaseMethods.json_primitive_wrapper(v).new(v)
else
Sequel.pg_json_op(v)
end
end
# Wraps Ruby array, hash, string, integer, float, true, false, and nil
# values with the appropriate JSON wrapper. Raises an exception for
# other types.
def pg_json_wrap(v)
case v
when *Postgres::JSON_WRAP_CLASSES
Postgres::JSONDatabaseMethods.json_primitive_wrapper(v).new(v)
else
raise Error, "invalid value passed to Sequel.pg_json_wrap: #{v.inspect}"
end
end
# Wrap the array or hash in a Postgres::JSONBArray or Postgres::JSONBHash.
# Also handles Postgres::JSONObject and JSONBObjects.
# For other objects, calls +Sequel.pg_json_op+ (which is defined
# by the pg_json_ops extension).
def pg_jsonb(v)
case v
when Postgres::JSONBObject
v
when Array
Postgres::JSONBArray.new(v)
when Hash
Postgres::JSONBHash.new(v)
when Postgres::JSONObject
v = v.__getobj__
Postgres::JSONDatabaseMethods.jsonb_primitive_wrapper(v).new(v)
else
Sequel.pg_jsonb_op(v)
end
end
# Wraps Ruby array, hash, string, integer, float, true, false, and nil
# values with the appropriate JSONB wrapper. Raises an exception for
# other types.
def pg_jsonb_wrap(v)
case v
when *Postgres::JSON_WRAP_CLASSES
Postgres::JSONDatabaseMethods.jsonb_primitive_wrapper(v).new(v)
else
raise Error, "invalid value passed to Sequel.pg_jsonb_wrap: #{v.inspect}"
end
end
end
Database.register_extension(:pg_json, Postgres::JSONDatabaseMethods)
end
# :nocov:
if Sequel.core_extensions?
class Array
# Return a Sequel::Postgres::JSONArray proxy to the receiver.
# This is mostly useful as a short cut for creating JSONArray
# objects that didn't come from the database.
def pg_json
Sequel::Postgres::JSONArray.new(self)
end
# Return a Sequel::Postgres::JSONArray proxy to the receiver.
# This is mostly useful as a short cut for creating JSONArray
# objects that didn't come from the database.
def pg_jsonb
Sequel::Postgres::JSONBArray.new(self)
end
end
class Hash
# Return a Sequel::Postgres::JSONHash proxy to the receiver.
# This is mostly useful as a short cut for creating JSONHash
# objects that didn't come from the database.
def pg_json
Sequel::Postgres::JSONHash.new(self)
end
# Return a Sequel::Postgres::JSONHash proxy to the receiver.
# This is mostly useful as a short cut for creating JSONHash
# objects that didn't come from the database.
def pg_jsonb
Sequel::Postgres::JSONBHash.new(self)
end
end
end
if defined?(Sequel::CoreRefinements)
module Sequel::CoreRefinements
refine Array do
def pg_json
Sequel::Postgres::JSONArray.new(self)
end
def pg_jsonb
Sequel::Postgres::JSONBArray.new(self)
end
end
refine Hash do
def pg_json
Sequel::Postgres::JSONHash.new(self)
end
def pg_jsonb
Sequel::Postgres::JSONBHash.new(self)
end
end
end
end
# :nocov:
|