Search in sources :

Example 16 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.

the class TestHanaClient method testRenameColumn.

/**
 * testRenameColumn
 */
@Test
public void testRenameColumn() {
    String schemaName = getNameByUpperCaseIdentifiers(database.getSchema());
    // get actual table table_with_float_col_*
    String tableName = getNameByUpperCaseIdentifiers(database.getActualTable("table_with_float_col"));
    List<String> expectedColumns = Arrays.asList("col1", "col2", "col3", "col4");
    List<String> actualColumns = getActualColumns(schemaName, tableName);
    assertEquals(actualColumns, expectedColumns);
    JdbcIdentity identity = JdbcIdentity.from(SESSION);
    SchemaTableName schemaTableName = new SchemaTableName(schemaName, tableName);
    JdbcTableHandle tableHandle = hanaClient.getTableHandle(identity, schemaTableName).get();
    List<JdbcColumnHandle> columns = hanaClient.getColumns(SESSION, tableHandle);
    JdbcColumnHandle columnHandle = null;
    for (JdbcColumnHandle column : columns) {
        if ("col4".equalsIgnoreCase(column.getColumnName())) {
            columnHandle = column;
            break;
        }
    }
    hanaClient.renameColumn(identity, tableHandle, columnHandle, "newcol4");
    expectedColumns = Arrays.asList("col1", "col2", "col3", "newcol4");
    actualColumns = getActualColumns(schemaName, tableName);
    assertEquals(actualColumns, expectedColumns);
}
Also used : JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) JdbcTableHandle(io.prestosql.plugin.jdbc.JdbcTableHandle) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) JdbcIdentity(io.prestosql.plugin.jdbc.JdbcIdentity) Test(org.testng.annotations.Test)

Example 17 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.

the class BasePostgreSqlClient method getColumns.

@Override
public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHandle tableHandle) {
    try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session))) {
        Map<String, Integer> arrayColumnDimensions = getArrayColumnDimensions(connection, tableHandle);
        try (ResultSet resultSet = optimizedGetColumns(connection, tableHandle)) {
            List<JdbcColumnHandle> columns = new ArrayList<>();
            while (resultSet.next()) {
                String columnName = resultSet.getString("COLUMN_NAME");
                JdbcTypeHandle typeHandle = new JdbcTypeHandle(resultSet.getInt("DATA_TYPE"), Optional.of(resultSet.getString("TYPE_NAME")), resultSet.getInt("COLUMN_SIZE"), resultSet.getInt("DECIMAL_DIGITS"), Optional.ofNullable(arrayColumnDimensions.get(columnName)));
                Optional<ColumnMapping> columnMapping = toPrestoType(session, connection, typeHandle);
                // skip unsupported column types
                if (columnMapping.isPresent()) {
                    boolean nullable = (resultSet.getInt("NULLABLE") != columnNoNulls);
                    columns.add(new JdbcColumnHandle(columnName, typeHandle, columnMapping.get().getType(), nullable));
                }
            }
            if (columns.isEmpty()) {
                // In rare cases a table might have no columns.
                throw new TableNotFoundException(tableHandle.getSchemaTableName());
            }
            return ImmutableList.copyOf(columns);
        }
    } catch (SQLException e) {
        throw new PrestoException(JDBC_ERROR, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PgConnection(org.postgresql.jdbc.PgConnection) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) ArrayList(java.util.ArrayList) PrestoException(io.prestosql.spi.PrestoException) TableNotFoundException(io.prestosql.spi.connector.TableNotFoundException) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) ResultSet(java.sql.ResultSet) ColumnMapping(io.prestosql.plugin.jdbc.ColumnMapping)

Aggregations

JdbcColumnHandle (io.prestosql.plugin.jdbc.JdbcColumnHandle)17 JdbcTypeHandle (io.prestosql.plugin.jdbc.JdbcTypeHandle)8 PrestoException (io.prestosql.spi.PrestoException)8 Type (io.prestosql.spi.type.Type)8 SQLException (java.sql.SQLException)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 ColumnMapping (io.prestosql.plugin.jdbc.ColumnMapping)7 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)7 Connection (java.sql.Connection)7 VarcharType (io.prestosql.spi.type.VarcharType)6 PreparedStatement (java.sql.PreparedStatement)6 JdbcTableHandle (io.prestosql.plugin.jdbc.JdbcTableHandle)5 SchemaTableName (io.prestosql.spi.connector.SchemaTableName)5 DecimalType (io.prestosql.spi.type.DecimalType)5 JdbcIdentity (io.prestosql.plugin.jdbc.JdbcIdentity)4 ResultSetMetaData (java.sql.ResultSetMetaData)4 Test (org.testng.annotations.Test)4 SuppressFBWarnings (io.prestosql.spi.SuppressFBWarnings)3 BigintType (io.prestosql.spi.type.BigintType)3 CharType (io.prestosql.spi.type.CharType)3