Example usage for java.sql SQLDataException SQLDataException

List of usage examples for java.sql SQLDataException SQLDataException

Introduction

In this page you can find the example usage for java.sql SQLDataException SQLDataException.

Prototype

public SQLDataException(String reason, Throwable cause) 

Source Link

Document

Constructs a SQLDataException object with a given reason and cause.

Usage

From source file:com.netflix.metacat.connector.jdbc.services.JdbcConnectorTableService.java

/**
 * Rebuild a source type definition./*w w  w .  j  av a2 s .com*/
 *
 * @param type      The base type e.g. VARCHAR
 * @param size      The size if applicable to the {@code type}
 * @param precision The precision if applicable to the {@code type} e.g. DECIMAL's
 * @return The representation of source type e.g. INTEGER, VARCHAR(50) or DECIMAL(20, 10)
 * @throws SQLDataException When size or precision can't be parsed to integers if non null
 */
protected String buildSourceType(@Nonnull @NonNull final String type, @Nullable final String size,
        @Nullable final String precision) throws SQLDataException {
    if (size != null) {
        final int sizeInt;
        try {
            sizeInt = Integer.parseInt(size);
        } catch (final NumberFormatException nfe) {
            throw new SQLDataException("Size field could not be converted to integer", nfe);
        }
        // Make sure if the type is unsigned it's created correctly
        final String baseType;
        final String afterMagnitude;
        final int unsignedIndex = StringUtils.indexOfIgnoreCase(type, UNSIGNED);
        if (unsignedIndex != -1) {
            baseType = StringUtils.trim(type.substring(0, unsignedIndex));
            afterMagnitude = type.substring(unsignedIndex);
        } else {
            baseType = type;
            afterMagnitude = null;
        }

        if (precision != null) {
            final int precisionInt;
            try {
                precisionInt = Integer.parseInt(precision);
            } catch (final NumberFormatException nfe) {
                throw new SQLDataException("Precision field could not be converted to integer", nfe);
            }
            return baseType + LEFT_PAREN + sizeInt + COMMA_SPACE + precisionInt + RIGHT_PAREN
                    + (afterMagnitude != null ? SPACE + afterMagnitude : EMPTY);
        } else {
            return baseType + LEFT_PAREN + sizeInt + RIGHT_PAREN
                    + (afterMagnitude != null ? SPACE + afterMagnitude : EMPTY);
        }
    } else {
        return type;
    }
}