String table = schemaTableName.getTableName(); if (!getSchemaNames().contains(schema)) { throw new PrestoException(NOT_FOUND, "Schema not found: " + schema); String temporaryName = generateTemporaryTableName(); StringBuilder sql = new StringBuilder() .append("CREATE TABLE ") .append(quoted(catalog, schema, temporaryName)) .append(" ("); ImmutableList.Builder<String> columnNames = ImmutableList.builder(); columnTypes.add(column.getType()); columnList.add(new StringBuilder() .append(quoted(columnName)) .append(" ") .append(toSqlType(column.getType())) .toString()); sql.append(")"); execute(connection, sql.toString());
@Override public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHandle tableHandle) { try (Connection connection = connectionFactory.openConnection()) { try (ResultSet resultSet = getColumns(tableHandle, connection.getMetaData())) { List<JdbcColumnHandle> columns = new ArrayList<>(); while (resultSet.next()) { JdbcTypeHandle typeHandle = new JdbcTypeHandle( resultSet.getInt("DATA_TYPE"), resultSet.getInt("COLUMN_SIZE"), resultSet.getInt("DECIMAL_DIGITS")); Optional<ReadMapping> columnMapping = toPrestoType(session, typeHandle); // skip unsupported column types if (columnMapping.isPresent()) { String columnName = resultSet.getString("COLUMN_NAME"); columns.add(new JdbcColumnHandle(connectorId, columnName, typeHandle, columnMapping.get().getType())); } } if (columns.isEmpty()) { // In rare cases (e.g. PostgreSQL) a table might have no columns. throw new TableNotFoundException(tableHandle.getSchemaTableName()); } return ImmutableList.copyOf(columns); } } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } }
protected ResultSet getTables(Connection connection, String schemaName, String tableName) throws SQLException { DatabaseMetaData metadata = connection.getMetaData(); String escape = metadata.getSearchStringEscape(); return metadata.getTables( connection.getCatalog(), escapeNamePattern(schemaName, escape), escapeNamePattern(tableName, escape), new String[] {"TABLE", "VIEW"}); }
@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 List<SchemaTableName> getTableNames(@Nullable String schema) { try (Connection connection = connectionFactory.openConnection()) { DatabaseMetaData metadata = connection.getMetaData(); if (metadata.storesUpperCaseIdentifiers() && (schema != null)) { schema = schema.toUpperCase(ENGLISH); } try (ResultSet resultSet = getTables(connection, schema, null)) { ImmutableList.Builder<SchemaTableName> list = ImmutableList.builder(); while (resultSet.next()) { list.add(getSchemaTableName(resultSet)); } return list.build(); } } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } }
@Override public void dropTable(JdbcTableHandle handle) { StringBuilder sql = new StringBuilder() .append("DROP TABLE ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName())); try (Connection connection = connectionFactory.openConnection()) { execute(connection, sql.toString()); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } }
@Override protected String toSqlType(Type type) { if (VARBINARY.equals(type)) { return "bytea"; } return super.toSqlType(type); } }
@Override protected Type toPrestoType(final int jdbcType) { switch (jdbcType) { case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: return IntType.INT; case Types.FLOAT: case Types.REAL: return FloatType.FLOAT; default: return super.toPrestoType(jdbcType); } }
protected String quoted(String catalog, String schema, String table) { StringBuilder sb = new StringBuilder(); if (!isNullOrEmpty(catalog)) { sb.append(quoted(catalog)).append("."); } if (!isNullOrEmpty(schema)) { sb.append(quoted(schema)).append("."); } sb.append(quoted(table)); return sb.toString(); }
jdbcTableName = jdbcTableName.toUpperCase(ENGLISH); try (ResultSet resultSet = getTables(connection, jdbcSchemaName, jdbcTableName)) { List<JdbcTableHandle> tableHandles = new ArrayList<>(); while (resultSet.next()) {
@Override public PreparedStatement buildSql(JdbcSplit split, List<JdbcColumnHandle> columnHandles) throws SQLException { return new QueryBuilder(identifierQuote).buildSql( getConnection(split), split.getCatalogName(), split.getSchemaName(), split.getTableName(), columnHandles, split.getTupleDomain()); }
return super.toSqlType(type);
@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 dropTable(JdbcTableHandle handle) { StringBuilder sql = new StringBuilder() .append("DROP TABLE ") .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName())); try (Connection connection = driver.connect(connectionUrl, connectionProperties)) { execute(connection, sql.toString()); } catch (SQLException e) { throw new PrestoException(JDBC_ERROR, e); } }
@Override public List<SchemaTableName> getTableNames(@Nullable String schema) { try (Connection connection = driver.connect(connectionUrl, connectionProperties)) { DatabaseMetaData metadata = connection.getMetaData(); if (metadata.storesUpperCaseIdentifiers() && (schema != null)) { schema = schema.toUpperCase(ENGLISH); } try (ResultSet resultSet = getTables(connection, schema, null)) { ImmutableList.Builder<SchemaTableName> list = ImmutableList.builder(); while (resultSet.next()) { list.add(getSchemaTableName(resultSet)); } return list.build(); } } 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(); }
jdbcTableName = jdbcTableName.toUpperCase(ENGLISH); try (ResultSet resultSet = getTables(connection, jdbcSchemaName, jdbcTableName)) { List<JdbcTableHandle> tableHandles = new ArrayList<>(); while (resultSet.next()) {
String table = schemaTableName.getTableName(); if (!getSchemaNames().contains(schema)) { throw new PrestoException(NOT_FOUND, "Schema not found: " + schema); StringBuilder sql = new StringBuilder() .append("CREATE TABLE ") .append(quoted(catalog, schema, temporaryName)) .append(" ("); ImmutableList.Builder<String> columnNames = ImmutableList.builder(); columnTypes.add(column.getType()); columnList.add(new StringBuilder() .append(quoted(columnName)) .append(" ") .append(toSqlType(column.getType())) .toString()); sql.append(")"); execute(connection, sql.toString());
protected String toSqlType(final Type type) { String sqlType = METACAT_SQL_TYPES.get(type); if (sqlType != null) { return sqlType; } else { sqlType = super.toSqlType(type); switch (sqlType) { case "varchar": return "mediumtext"; case "varbinary": return "mediumblob"; case "time with timezone": return "time"; case "timestamp": case "timestamp with timezone": return "datetime"; default: } return sqlType; } }
@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); } }