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
1231 def initialize(expr, path, opts=OPTS, &block)
1232   @expr = expr
1233   @path = path
1234   @passing = opts[:passing]
1235   @on_error = opts[:on_error]
1236   @columns = opts[:_columns] || ColumnDSL.columns(&block)
1237   freeze
1238 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
1267 def sequel_ast_transform(transformer)
1268   opts = {:on_error=>@on_error, :_columns=>@columns}
1269 
1270   if @passing
1271     passing = opts[:passing] = {}
1272     @passing.each do |k, v|
1273       passing[k] = transformer.call(v)
1274     end
1275   end
1276 
1277   self.class.new(transformer.call(@expr), @path, opts)
1278 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
1241 def to_s_append(ds, sql)
1242   sql << 'json_table('
1243   ds.literal_append(sql, @expr)
1244   sql << ', '
1245   default_literal_append(ds, sql, @path)
1246 
1247   if (passing = @passing) && !passing.empty?
1248     sql << ' PASSING '
1249     comma = false
1250     passing.each do |k, v|
1251       if comma
1252         sql << ', '
1253       else
1254         comma = true
1255       end
1256       ds.literal_append(sql, v)
1257       sql << " AS " << k.to_s
1258     end
1259   end
1260 
1261   to_s_append_columns(ds, sql, @columns)
1262   sql << TABLE_ON_ERROR_SQL.fetch(@on_error) if @on_error
1263   sql << ')'
1264 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
1353 def default_literal_append(ds, sql, v)
1354   if sql.respond_to?(:skip_auto_param)
1355     sql.skip_auto_param do
1356       ds.literal_append(sql, v)
1357     end
1358   else
1359     ds.literal_append(sql, v)
1360   end
1361 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
1300 def to_s_append_column(ds, sql, column)
1301   case column[0]
1302   when :column
1303     _, name, type, opts = column
1304     ds.literal_append(sql, name)
1305     sql << ' ' << ds.db.send(:type_literal, opts.merge(:type=>type)).to_s
1306     sql << ' FORMAT JSON' if opts[:format] == :json
1307     to_s_append_path(ds, sql, opts[:path])
1308     sql << WRAPPER.fetch(opts[:wrapper]) if opts[:wrapper]
1309     to_s_append_on_value(ds, sql, opts[:on_empty], " ON EMPTY")
1310     to_s_append_on_value(ds, sql, opts[:on_error], " ON ERROR")
1311   when :ordinality
1312     ds.literal_append(sql, column[1])
1313     sql << ' FOR ORDINALITY'
1314   when :exists
1315     _, name, type, opts = column
1316     ds.literal_append(sql, name)
1317     sql << ' ' << ds.db.send(:type_literal, opts.merge(:type=>type)).to_s
1318     sql << ' EXISTS'
1319     to_s_append_path(ds, sql, opts[:path])
1320     unless (on_error = opts[:on_error]).nil?
1321       sql << EXISTS_ON_ERROR_SQL.fetch(on_error) << " ON ERROR"
1322     end
1323   else # when :nested
1324     _, path, columns = column
1325     sql << 'NESTED '
1326     default_literal_append(ds, sql, path)
1327     to_s_append_columns(ds, sql, columns)
1328   end
1329 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
1284 def to_s_append_columns(ds, sql, columns)
1285   sql << ' COLUMNS('
1286   comma = nil
1287   columns.each do |column|
1288     if comma
1289       sql << comma
1290     else
1291       comma = ', '
1292     end
1293     to_s_append_column(ds, sql, column)
1294   end
1295   sql << ')'
1296 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
1332 def to_s_append_on_value(ds, sql, value, cond)
1333   if value
1334     if v = COLUMN_ON_SQL[value]
1335       sql << v
1336     else
1337       sql << ' DEFAULT '
1338       default_literal_append(ds, sql, value)
1339     end
1340     sql << cond
1341   end
1342 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
1345 def to_s_append_path(ds, sql, path)
1346   if path
1347     sql << ' PATH '
1348     default_literal_append(ds, sql, path)
1349   end
1350 end