Class: Sequel::Postgres::JSONBSubscriptOp

Inherits:
SQL::Expression show all
Defined in:
lib/sequel/extensions/pg_json_ops.rb

Overview

Represents JSONB subscripts. This is abstracted because the subscript support depends on the database version.

Constant Summary collapse

SUBSCRIPT =
["".freeze, "[".freeze, "]".freeze].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SQL::Expression

#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect

Constructor Details

#initialize(expression, sub) ⇒ JSONBSubscriptOp

Set the expression and subscript to the given arguments



689
690
691
692
693
# File 'lib/sequel/extensions/pg_json_ops.rb', line 689

def initialize(expression, sub)
  @expression = expression
  @sub = sub
  freeze
end

Instance Attribute Details

#expressionObject (readonly)

The expression being subscripted



683
684
685
# File 'lib/sequel/extensions/pg_json_ops.rb', line 683

def expression
  @expression
end

#subObject (readonly)

The subscript to use



686
687
688
# File 'lib/sequel/extensions/pg_json_ops.rb', line 686

def sub
  @sub
end

Instance Method Details

#sequel_ast_transform(transformer) ⇒ Object

Support transforming of jsonb subscripts



703
704
705
# File 'lib/sequel/extensions/pg_json_ops.rb', line 703

def sequel_ast_transform(transformer)
  self.class.new(transformer.call(@expression), transformer.call(@sub))
end

#to_s_append(ds, sql) ⇒ Object

Use subscripts instead of -> operator on PostgreSQL 14+



696
697
698
699
700
# File 'lib/sequel/extensions/pg_json_ops.rb', line 696

def to_s_append(ds, sql)
  server_version = ds.db.server_version
  frag = server_version && server_version >= 140000 ? SUBSCRIPT : JSONOp::GET
  ds.literal_append(sql, Sequel::SQL::PlaceholderLiteralString.new(frag, [@expression, @sub]))
end