@Override public Connection getConnection(JdbcOutputTableHandle handle) throws SQLException { return connectionFactory.openConnection(); }
@PreDestroy public void destroy() throws Exception { connectionFactory.close(); }
@Override public Connection getConnection(JdbcSplit split) throws SQLException { Connection connection = connectionFactory.openConnection(); try { connection.setReadOnly(true); } catch (SQLException e) { connection.close(); throw e; } return connection; }
@Override public Set<String> getSchemaNames() { // for MySQL, we need to list catalogs instead of schemas try (Connection connection = connectionFactory.openConnection(); ResultSet resultSet = connection.getMetaData().getCatalogs()) { ImmutableSet.Builder<String> schemaNames = ImmutableSet.builder(); while (resultSet.next()) { String schemaName = resultSet.getString("TABLE_CAT").toLowerCase(ENGLISH); // skip internal schemas if (!schemaName.equals("information_schema") && !schemaName.equals("mysql")) { schemaNames.add(schemaName); } } return schemaNames.build(); } catch (SQLException e) { throw new RuntimeException(e); } }
@Override public Set<String> getSchemaNames() { try (Connection connection = connectionFactory.openConnection(); ResultSet resultSet = connection.getMetaData().getSchemas()) { ImmutableSet.Builder<String> schemaNames = ImmutableSet.builder(); while (resultSet.next()) { String schemaName = resultSet.getString("TABLE_SCHEM").toLowerCase(ENGLISH); // skip internal schemas if (!schemaName.equals("information_schema")) { schemaNames.add(schemaName); } } return schemaNames.build(); } 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); } }
public JdbcTableHandle getTableHandle(SchemaTableName schemaTableName) try (Connection connection = connectionFactory.openConnection()) { DatabaseMetaData metadata = connection.getMetaData(); String jdbcSchemaName = schemaTableName.getSchemaName();
@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); } }
try (Connection connection = connectionFactory.openConnection()) { boolean uppercase = connection.getMetaData().storesUpperCaseIdentifiers(); if (uppercase) {
@Override public Set<String> getSchemaNames() { // for MySQL, we need to list catalogs instead of schemas try (Connection connection = connectionFactory.openConnection(); ResultSet resultSet = connection.getMetaData().getCatalogs()) { ImmutableSet.Builder<String> schemaNames = ImmutableSet.builder(); while (resultSet.next()) { String schemaName = resultSet.getString("TABLE_CAT").toLowerCase(ENGLISH); // skip internal schemas if (!schemaName.equals("information_schema") && !schemaName.equals("mysql")) { schemaNames.add(schemaName); } } return schemaNames.build(); } catch (SQLException e) { throw new RuntimeException(e); } }