Class: Sequel::Postgres::JSONTableOp

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

Overview

Object representing json_table calls

Instance Method Summary collapse

Methods inherited from SQL::Expression

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

Constructor Details

#initialize(expr, path, opts = OPTS, &block) ⇒ JSONTableOp

See JSONBaseOp#table for documentation on the options.



1208
1209
1210
1211
1212
1213
1214
1215
# File 'lib/sequel/extensions/pg_json_ops.rb', line 1208

def initialize(expr, path, opts=OPTS, &block)
  @expr = expr
  @path = path
  @passing = opts[:passing]
  @on_error = opts[:on_error]
  @columns = opts[:_columns] || ColumnDSL.columns(&block)
  freeze
end

Instance Method Details

#sequel_ast_transform(transformer) ⇒ Object

Support transforming of json_table expression



1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
# File 'lib/sequel/extensions/pg_json_ops.rb', line 1244

def sequel_ast_transform(transformer)
  opts = {:on_error=>@on_error, :_columns=>@columns}

  if @passing
    passing = opts[:passing] = {}
    @passing.each do |k, v|
      passing[k] = transformer.call(v)
    end
  end

  self.class.new(transformer.call(@expr), @path, opts)
end

#to_s_append(ds, sql) ⇒ Object

Append the json_table function call expression to the SQL



1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
# File 'lib/sequel/extensions/pg_json_ops.rb', line 1218

def to_s_append(ds, sql)
  sql << 'json_table('
  ds.literal_append(sql, @expr)
  sql << ', '
  default_literal_append(ds, sql, @path)

  if (passing = @passing) && !passing.empty?
    sql << ' PASSING '
    comma = false
    passing.each do |k, v|
      if comma
        sql << ', '
      else
        comma = true
      end
      ds.literal_append(sql, v)
      sql << " AS " << k.to_s
    end
  end

  to_s_append_columns(ds, sql, @columns)
  sql << TABLE_ON_ERROR_SQL.fetch(@on_error) if @on_error
  sql << ')'
end