class Sequel::Postgres::JSONTableOp

Object representing json_table calls

Constants

COLUMN_ON_SQL
EXISTS_ON_ERROR_SQL
TABLE_ON_ERROR_SQL
WRAPPER

Public Class Methods

new(expr, path, opts=OPTS, &block) click to toggle source

See JSONBaseOp#table for documentation on the options.

     # File lib/sequel/extensions/pg_json_ops.rb
1208 def initialize(expr, path, opts=OPTS, &block)
1209   @expr = expr
1210   @path = path
1211   @passing = opts[:passing]
1212   @on_error = opts[:on_error]
1213   @columns = opts[:_columns] || ColumnDSL.columns(&block)
1214   freeze
1215 end

Public Instance Methods

sequel_ast_transform(transformer) click to toggle source

Support transforming of json_table expression

     # File lib/sequel/extensions/pg_json_ops.rb
1244 def sequel_ast_transform(transformer)
1245   opts = {:on_error=>@on_error, :_columns=>@columns}
1246 
1247   if @passing
1248     passing = opts[:passing] = {}
1249     @passing.each do |k, v|
1250       passing[k] = transformer.call(v)
1251     end
1252   end
1253 
1254   self.class.new(transformer.call(@expr), @path, opts)
1255 end
to_s_append(ds, sql) click to toggle source

Append the json_table function call expression to the SQL

     # File lib/sequel/extensions/pg_json_ops.rb
1218 def to_s_append(ds, sql)
1219   sql << 'json_table('
1220   ds.literal_append(sql, @expr)
1221   sql << ', '
1222   default_literal_append(ds, sql, @path)
1223 
1224   if (passing = @passing) && !passing.empty?
1225     sql << ' PASSING '
1226     comma = false
1227     passing.each do |k, v|
1228       if comma
1229         sql << ', '
1230       else
1231         comma = true
1232       end
1233       ds.literal_append(sql, v)
1234       sql << " AS " << k.to_s
1235     end
1236   end
1237 
1238   to_s_append_columns(ds, sql, @columns)
1239   sql << TABLE_ON_ERROR_SQL.fetch(@on_error) if @on_error
1240   sql << ')'
1241 end

Private Instance Methods

default_literal_append(ds, sql, v) click to toggle source

Do not auto paramterize default value or path value, as PostgreSQL doesn’t allow it.

     # File lib/sequel/extensions/pg_json_ops.rb
1330 def default_literal_append(ds, sql, v)
1331   if sql.respond_to?(:skip_auto_param)
1332     sql.skip_auto_param do
1333       ds.literal_append(sql, v)
1334     end
1335   else
1336     ds.literal_append(sql, v)
1337   end
1338 end
to_s_append_column(ds, sql, column) click to toggle source

Append the column information to the SQL. Handles the various types of json_table columns.

     # File lib/sequel/extensions/pg_json_ops.rb
1277 def to_s_append_column(ds, sql, column)
1278   case column[0]
1279   when :column
1280     _, name, type, opts = column
1281     ds.literal_append(sql, name)
1282     sql << ' ' << ds.db.send(:type_literal, opts.merge(:type=>type)).to_s
1283     sql << ' FORMAT JSON' if opts[:format] == :json
1284     to_s_append_path(ds, sql, opts[:path])
1285     sql << WRAPPER.fetch(opts[:wrapper]) if opts[:wrapper]
1286     to_s_append_on_value(ds, sql, opts[:on_empty], " ON EMPTY")
1287     to_s_append_on_value(ds, sql, opts[:on_error], " ON ERROR")
1288   when :ordinality
1289     ds.literal_append(sql, column[1])
1290     sql << ' FOR ORDINALITY'
1291   when :exists
1292     _, name, type, opts = column
1293     ds.literal_append(sql, name)
1294     sql << ' ' << ds.db.send(:type_literal, opts.merge(:type=>type)).to_s
1295     sql << ' EXISTS'
1296     to_s_append_path(ds, sql, opts[:path])
1297     unless (on_error = opts[:on_error]).nil?
1298       sql << EXISTS_ON_ERROR_SQL.fetch(on_error) << " ON ERROR"
1299     end
1300   else # when :nested
1301     _, path, columns = column
1302     sql << 'NESTED '
1303     default_literal_append(ds, sql, path)
1304     to_s_append_columns(ds, sql, columns)
1305   end
1306 end
to_s_append_columns(ds, sql, columns) click to toggle source

Append the set of column information to the SQL. Separated to handle nested sets of columns.

     # File lib/sequel/extensions/pg_json_ops.rb
1261 def to_s_append_columns(ds, sql, columns)
1262   sql << ' COLUMNS('
1263   comma = nil
1264   columns.each do |column|
1265     if comma
1266       sql << comma
1267     else
1268       comma = ', '
1269     end
1270     to_s_append_column(ds, sql, column)
1271   end
1272   sql << ')'
1273 end
to_s_append_on_value(ds, sql, value, cond) click to toggle source

Handle DEFAULT values in ON EMPTY/ON ERROR fragments

     # File lib/sequel/extensions/pg_json_ops.rb
1309 def to_s_append_on_value(ds, sql, value, cond)
1310   if value
1311     if v = COLUMN_ON_SQL[value]
1312       sql << v
1313     else
1314       sql << ' DEFAULT '
1315       default_literal_append(ds, sql, value)
1316     end
1317     sql << cond
1318   end
1319 end
to_s_append_path(ds, sql, path) click to toggle source

Append path caluse to the SQL

     # File lib/sequel/extensions/pg_json_ops.rb
1322 def to_s_append_path(ds, sql, path)
1323   if path
1324     sql << ' PATH '
1325     default_literal_append(ds, sql, path)
1326   end
1327 end