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