Java Code Examples for oracle.jdbc.OracleTypes#NUMBER

The following examples show how to use oracle.jdbc.OracleTypes#NUMBER . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: presto   File: OracleClient.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Optional<ColumnMapping> toPrestoType(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle)
{
    int columnSize = typeHandle.getColumnSize();

    switch (typeHandle.getJdbcType()) {
        case Types.SMALLINT:
            return Optional.of(smallintColumnMapping());
        case OracleTypes.BINARY_FLOAT:
            return Optional.of(ColumnMapping.longMapping(
                    REAL,
                    (resultSet, columnIndex) -> floatToRawIntBits(resultSet.getFloat(columnIndex)),
                    oracleRealWriteFunction()));

        case OracleTypes.BINARY_DOUBLE:
        case OracleTypes.FLOAT:
            return Optional.of(ColumnMapping.doubleMapping(
                    DOUBLE,
                    ResultSet::getDouble,
                    oracleDoubleWriteFunction()));
        case OracleTypes.NUMBER:
            int decimalDigits = typeHandle.getDecimalDigits();
            // Map negative scale to decimal(p+s, 0).
            int precision = columnSize + max(-decimalDigits, 0);
            int scale = max(decimalDigits, 0);
            Optional<Integer> numberDefaultScale = getNumberDefaultScale(session);
            RoundingMode roundingMode = getNumberRoundingMode(session);
            if (precision < scale) {
                if (roundingMode == RoundingMode.UNNECESSARY) {
                    break;
                }
                scale = min(Decimals.MAX_PRECISION, scale);
                precision = scale;
            }
            else if (numberDefaultScale.isPresent() && precision == PRECISION_OF_UNSPECIFIED_NUMBER) {
                precision = Decimals.MAX_PRECISION;
                scale = numberDefaultScale.get();
            }
            else if (precision > Decimals.MAX_PRECISION || columnSize <= 0) {
                break;
            }
            DecimalType decimalType = createDecimalType(precision, scale);
            int finalScale = scale;
            // JDBC driver can return BigDecimal with lower scale than column's scale when there are trailing zeroes
            if (decimalType.isShort()) {
                return Optional.of(ColumnMapping.longMapping(
                        decimalType,
                        (resultSet, columnIndex) -> encodeShortScaledValue(resultSet.getBigDecimal(columnIndex), finalScale, roundingMode),
                        shortDecimalWriteFunction(decimalType)));
            }
            return Optional.of(ColumnMapping.sliceMapping(
                    decimalType,
                    (resultSet, columnIndex) -> encodeScaledValue(resultSet.getBigDecimal(columnIndex), finalScale, roundingMode),
                    longDecimalWriteFunction(decimalType)));

        case OracleTypes.CHAR:
        case OracleTypes.NCHAR:
            CharType charType = createCharType(columnSize);
            return Optional.of(ColumnMapping.sliceMapping(
                    charType,
                    charReadFunction(),
                    oracleCharWriteFunction(charType)));

        case OracleTypes.VARCHAR:
        case OracleTypes.NVARCHAR:
            return Optional.of(ColumnMapping.sliceMapping(
                    createVarcharType(columnSize),
                    (varcharResultSet, varcharColumnIndex) -> utf8Slice(varcharResultSet.getString(varcharColumnIndex)),
                    varcharWriteFunction()));

        case OracleTypes.CLOB:
        case OracleTypes.NCLOB:
            return Optional.of(ColumnMapping.sliceMapping(
                    createUnboundedVarcharType(),
                    (resultSet, columnIndex) -> utf8Slice(resultSet.getString(columnIndex)),
                    varcharWriteFunction(),
                    DISABLE_PUSHDOWN));

        case OracleTypes.VARBINARY: // Oracle's RAW(n)
        case OracleTypes.BLOB:
            return Optional.of(ColumnMapping.sliceMapping(
                    VARBINARY,
                    (resultSet, columnIndex) -> wrappedBuffer(resultSet.getBytes(columnIndex)),
                    varbinaryWriteFunction(),
                    DISABLE_PUSHDOWN));

        // This mapping covers both DATE and TIMESTAMP, as Oracle's DATE has second precision.
        case OracleTypes.TIMESTAMP:
            return Optional.of(oracleTimestampColumnMapping(session));
        case OracleTypes.TIMESTAMPTZ:
            return Optional.of(oracleTimestampWithTimeZoneColumnMapping());
    }
    if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
        return mapToUnboundedVarchar(typeHandle);
    }
    return Optional.empty();
}
 
Example 2
@Override
public void readSchema(Tables tables, String databaseCatalog, String schemaNamePattern, TableFilter tableFilter,
                       ColumnNameFilter columnFilter, boolean removeTablesNotFoundInJdbc)
        throws SQLException {

    super.readSchema(tables, null, schemaNamePattern, null, columnFilter, removeTablesNotFoundInJdbc);

    Set<TableId> tableIds = tables.tableIds().stream().filter(x -> schemaNamePattern.equals(x.schema())).collect(Collectors.toSet());

    for (TableId tableId : tableIds) {
        // super.readSchema() populates ids without the catalog; hence we apply the filtering only
        // here and if a table is included, overwrite it with a new id including the catalog
        TableId tableIdWithCatalog = new TableId(databaseCatalog, tableId.schema(), tableId.table());

        if (tableFilter.isIncluded(tableIdWithCatalog)) {
            TableEditor editor = tables.editTable(tableId);
            editor.tableId(tableIdWithCatalog);

            List<String> columnNames = new ArrayList<>(editor.columnNames());
            for (String columnName : columnNames) {
                Column column = editor.columnWithName(columnName);
                if (column.jdbcType() == Types.TIMESTAMP) {
                    editor.addColumn(
                            column.edit()
                                    .length(column.scale().orElse(Column.UNSET_INT_VALUE))
                                    .scale(null)
                                    .create());
                }
                // NUMBER columns without scale value have it set to -127 instead of null;
                // let's rectify that
                else if (column.jdbcType() == OracleTypes.NUMBER) {
                    column.scale()
                            .filter(s -> s == ORACLE_UNSET_SCALE)
                            .ifPresent(s -> {
                                editor.addColumn(
                                        column.edit()
                                                .scale(null)
                                                .create());
                            });
                }
            }
            tables.overwriteTable(editor.create());
        }

        tables.removeTable(tableId);
    }
}