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



864
865
866
867
868
# File 'lib/sequel/extensions/pg_json_ops.rb', line 864

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

Instance Attribute Details

#expressionObject (readonly)

The expression being subscripted



858
859
860
# File 'lib/sequel/extensions/pg_json_ops.rb', line 858

def expression
  @expression
end

#subObject (readonly)

The subscript to use



861
862
863
# File 'lib/sequel/extensions/pg_json_ops.rb', line 861

def sub
  @sub
end

Instance Method Details

#sequel_ast_transform(transformer) ⇒ Object

Support transforming of jsonb subscripts



878
879
880
# File 'lib/sequel/extensions/pg_json_ops.rb', line 878

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+



871
872
873
874
875
# File 'lib/sequel/extensions/pg_json_ops.rb', line 871

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