@Override public void commitCreateTable(JdbcOutputTableHandle handle) { StringBuilder sql = new StringBuilder() .append("ALTER TABLE ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) .append(" RENAME TO ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName())); try (Connection connection = getConnection(handle)) { execute(connection, sql.toString()); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } }
@Override public String buildInsertSql(JdbcOutputTableHandle handle) { String vars = Joiner.on(',').join(nCopies(handle.getColumnNames().size(), "?")); return new StringBuilder() .append("INSERT INTO ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) .append(" VALUES (").append(vars).append(")") .toString(); }
@Override public void rollbackCreateTable(JdbcOutputTableHandle handle) { dropTable(new JdbcTableHandle( handle.getConnectorId(), new SchemaTableName(handle.getSchemaName(), handle.getTemporaryTableName()), handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())); }
@Test public void testJsonRoundTrip() { JdbcOutputTableHandle handle = new JdbcOutputTableHandle( "connectorId", "catalog", "schema", "table", ImmutableList.of("abc", "xyz"), ImmutableList.of(VARCHAR, VARCHAR), "tmp_table"); assertJsonRoundTrip(OUTPUT_TABLE_CODEC, handle); } }
public JdbcRecordSink(JdbcOutputTableHandle handle, JdbcClient jdbcClient) { try { connection = jdbcClient.getConnection(handle); connection.setAutoCommit(false); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } try { statement = connection.prepareStatement(jdbcClient.buildInsertSql(handle)); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } fieldCount = handle.getColumnNames().size(); columnTypes = handle.getColumnTypes(); }
public JdbcPageSink(JdbcOutputTableHandle handle, JdbcClient jdbcClient) { try { connection = jdbcClient.getConnection(handle); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } try { connection.setAutoCommit(false); statement = connection.prepareStatement(jdbcClient.buildInsertSql(handle)); } catch (SQLException e) { closeWithSuppression(connection, e); throw new PrestoException(JDBC_ERROR, e); } columnTypes = handle.getColumnTypes(); }
return new JdbcOutputTableHandle( connectorId, catalog,
@Override public void commitCreateTable(JdbcOutputTableHandle handle) { // PostgreSQL does not allow qualifying the target of a rename StringBuilder sql = new StringBuilder() .append("ALTER TABLE ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) .append(" RENAME TO ") .append(quoted(handle.getTableName())); try (Connection connection = getConnection(handle)) { execute(connection, sql.toString()); } catch (SQLException e) { throw new RuntimeException(e); } }
@Override public String buildInsertSql(JdbcOutputTableHandle handle) { String vars = Joiner.on(',').join(nCopies(handle.getColumnNames().size(), "?")); return new StringBuilder() .append("INSERT INTO ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) .append(" VALUES (").append(vars).append(")") .toString(); }
@Test public void testJsonRoundTrip() { JdbcOutputTableHandle handle = new JdbcOutputTableHandle( "connectorId", "catalog", "schema", "table", ImmutableList.of("abc", "xyz"), ImmutableList.of(VARCHAR, VARCHAR), "tmp_table"); assertJsonRoundTrip(OUTPUT_TABLE_CODEC, handle); } }
@Override public void commitCreateTable(JdbcOutputTableHandle handle) { StringBuilder sql = new StringBuilder() .append("sp_rename ") .append(singleQuote(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) .append(", ") .append(singleQuote(handle.getTableName())); try (Connection connection = getConnection(handle)) { execute(connection, sql.toString()); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } }
return new JdbcOutputTableHandle( connectorId, catalog,
@Override public void commitCreateTable(JdbcOutputTableHandle handle) { // Redshift does not allow qualifying the target of a rename StringBuilder sql = new StringBuilder() .append("ALTER TABLE ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) .append(" RENAME TO ") .append(quoted(handle.getTableName())); try (Connection connection = getConnection(handle)) { execute(connection, sql.toString()); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } }
@Override public void finishInsertTable(JdbcOutputTableHandle handle) { String temporaryTable = quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName()); String targetTable = quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName()); String insertSql = format("INSERT INTO %s SELECT * FROM %s", targetTable, temporaryTable); String cleanupSql = "DROP TABLE " + temporaryTable; try (Connection connection = getConnection(handle)) { execute(connection, insertSql); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } try (Connection connection = getConnection(handle)) { execute(connection, cleanupSql); } catch (SQLException e) { log.warn(e, "Failed to cleanup temporary table: %s", temporaryTable); } }
@Override public void commitCreateTable(JdbcOutputTableHandle handle, Collection<Slice> fragments) { StringBuilder sql = new StringBuilder() .append("ALTER TABLE ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) .append(" RENAME TO ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName())); try (Connection connection = getConnection(handle)) { execute(connection, sql.toString()); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } }
@Override public void commitCreateTable(JdbcOutputTableHandle handle) { // PostgreSQL does not allow qualifying the target of a rename StringBuilder sql = new StringBuilder() .append("ALTER TABLE ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) .append(" RENAME TO ") .append(quoted(handle.getTableName())); try (Connection connection = getConnection(handle)) { execute(connection, sql.toString()); } catch (SQLException e) { throw new RuntimeException(e); } }
@Override public void commitCreateTable(final JdbcOutputTableHandle handle, final Collection<Slice> fragments) { // PostgreSQL does not allow qualifying the target of a rename final StringBuilder sql = new StringBuilder() .append("ALTER TABLE ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) .append(" RENAME TO ") .append(quoted(handle.getTableName())); try (Connection connection = getConnection(handle)) { execute(connection, sql.toString()); } catch (SQLException e) { throw Throwables.propagate(e); } } }