com.mysql.cj.exceptions.DataConversionException Java Examples

The following examples show how to use com.mysql.cj.exceptions.DataConversionException. 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 File: MySQLJDBCReflections.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
void registerExceptionsForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJCommunicationsException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, CJConnectionFeatureNotAvailableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJTimeoutException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJPacketTooBigException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, AssertionFailedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ClosedOnExpiredPasswordException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ConnectionIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataConversionException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataReadException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataTruncationException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DeadlockTimeoutRollbackMarker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, FeatureNotAvailableException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, InvalidConnectionAttributeException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NumberOutOfRange.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, OperationCancelledException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PasswordExpiredException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PropertyNotModifiableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, RSAException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, SSLParamsException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StatementIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StreamingNotifiable.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, UnableToConnectException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, UnsupportedConnectionStringException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, WrongArgumentException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, "com.mysql.cj.jdbc.MysqlXAException"));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, StandardLoadBalanceExceptionChecker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NdbLoadBalanceExceptionChecker.class.getName()));
}
 
Example #2
Source File: StringConverterTest.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEmptyStringException() {
    this.stringConverter.setEmptyStringsConvertToZero(false);
    try {
        this.stringConverter.createFromBytes(new byte[] {}, 0, 0);
        fail("Empty string should not convert to anything");
    } catch (DataConversionException ex) {
        // expected
    }
}
 
Example #3
Source File: StringConverterTest.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testBooleanFromString() {
    // true/false are the only values we support
    ValueFactory<Boolean> sc = new StringConverter<>(null, new BooleanValueFactory());
    assertEquals(true, sc.createFromBytes("true".getBytes(), 0, 4));
    assertEquals(false, sc.createFromBytes("false".getBytes(), 0, 5));
    try {
        sc.createFromBytes("xyz".getBytes(), 0, 3);
        fail("Cannot get boolean from arbitrary strings");
    } catch (DataConversionException ex) {
        // expected
    }
}
 
Example #4
Source File: SQLExceptionsMapping.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static SQLException translateException(Throwable ex, ExceptionInterceptor interceptor) {
    if (ex instanceof SQLException) {
        return (SQLException) ex;

    } else if (ex.getCause() != null && ex.getCause() instanceof SQLException) {
        return (SQLException) ex.getCause();

    } else if (ex instanceof CJCommunicationsException) {
        return SQLError.createCommunicationsException(ex.getMessage(), ex, interceptor);

    } else if (ex instanceof CJConnectionFeatureNotAvailableException) {
        return new ConnectionFeatureNotAvailableException(ex.getMessage(), ex);

    } else if (ex instanceof SSLParamsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_BAD_SSL_PARAMS, 0, false, ex, interceptor);

    } else if (ex instanceof ConnectionIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_CONNECTION_NOT_OPEN, ex, interceptor);

    } else if (ex instanceof InvalidConnectionAttributeException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE, ex, interceptor);

    } else if (ex instanceof UnableToConnectException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, ex, interceptor);

    } else if (ex instanceof StatementIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof WrongArgumentException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof StringIndexOutOfBoundsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof NumberOutOfRange) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE, ex, interceptor);

    } else if (ex instanceof DataConversionException) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CHARACTER_VALUE_FOR_CAST, ex, interceptor);

    } else if (ex instanceof DataReadException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof DataTruncationException) {
        return new MysqlDataTruncation(((DataTruncationException) ex).getMessage(), ((DataTruncationException) ex).getIndex(),
                ((DataTruncationException) ex).isParameter(), ((DataTruncationException) ex).isRead(), ((DataTruncationException) ex).getDataSize(),
                ((DataTruncationException) ex).getTransferSize(), ((DataTruncationException) ex).getVendorCode());

    } else if (ex instanceof CJPacketTooBigException) {
        return new PacketTooBigException(ex.getMessage());

    } else if (ex instanceof OperationCancelledException) {
        return new MySQLStatementCancelledException(ex.getMessage());

    } else if (ex instanceof CJTimeoutException) {
        return new MySQLTimeoutException(ex.getMessage());

    } else if (ex instanceof CJOperationNotSupportedException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof UnsupportedOperationException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof CJException) {
        return SQLError.createSQLException(ex.getMessage(), ((CJException) ex).getSQLState(), ((CJException) ex).getVendorCode(),
                ((CJException) ex).isTransient(), interceptor);

    } else {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, ex, interceptor);
    }
}
 
Example #5
Source File: DefaultValueFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private T unsupported(String sourceType) {
    throw new DataConversionException(Messages.getString("ResultSet.UnsupportedConversion", new Object[] { sourceType, getTargetTypeName() }));
}
 
Example #6
Source File: StringConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Implement the logic for indirect conversions.
 */
@Override
public T createFromBytes(byte[] origBytes, int offset, int length) {
    MysqlTextValueDecoder stringInterpreter = new MysqlTextValueDecoder();

    // TODO: Too expensive to convert from other charset to ASCII here? UTF-8 (e.g.) doesn't need any conversion before being sent to the decoder
    String s = StringUtils.toString(origBytes, offset, length, this.encoding);
    byte[] bytes = s.getBytes();

    ValueFactory<T> vf = this.targetVf;

    issueConversionViaParsingWarning();

    if (s.length() == 0) {
        if (this.emptyStringsConvertToZero) {
            // use long=0 as this is mainly numerical oriented
            return this.targetVf.createFromLong(0);
        }
        // Else throw exception down below
    } else if (s.equalsIgnoreCase("true")) {
        return vf.createFromLong(1);
    } else if (s.equalsIgnoreCase("false")) {
        return vf.createFromLong(0);
    } else if (s.contains("e") || s.contains("E") || s.matches("-?(\\d+)?\\.\\d+")) {
        // floating point
        return stringInterpreter.decodeDouble(bytes, 0, bytes.length, vf);
    } else if (s.matches("-?\\d+")) {
        // integer
        if (s.charAt(0) == '-') {
            return stringInterpreter.decodeInt8(bytes, 0, bytes.length, vf);
        }
        return stringInterpreter.decodeUInt8(bytes, 0, bytes.length, vf);
    } else if (s.length() == MysqlTextValueDecoder.DATE_BUF_LEN && s.charAt(4) == '-' && s.charAt(7) == '-') {
        return stringInterpreter.decodeDate(bytes, 0, bytes.length, vf);
    } else if (s.length() >= MysqlTextValueDecoder.TIME_STR_LEN_MIN && s.length() <= MysqlTextValueDecoder.TIME_STR_LEN_MAX && s.charAt(2) == ':'
            && s.charAt(5) == ':') {
        return stringInterpreter.decodeTime(bytes, 0, bytes.length, vf);
    } else if (s.length() >= MysqlTextValueDecoder.TIMESTAMP_NOFRAC_STR_LEN
            && (s.length() <= MysqlTextValueDecoder.TIMESTAMP_STR_LEN_MAX || s.length() == MysqlTextValueDecoder.TIMESTAMP_STR_LEN_WITH_NANOS)
            && s.charAt(4) == '-' && s.charAt(7) == '-' && s.charAt(10) == ' ' && s.charAt(13) == ':' && s.charAt(16) == ':') {
        return stringInterpreter.decodeTimestamp(bytes, 0, bytes.length, vf);
    }
    throw new DataConversionException(Messages.getString("ResultSet.UnableToInterpretString", new Object[] { s }));
}
 
Example #7
Source File: SQLExceptionsMapping.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public static SQLException translateException(Throwable ex, ExceptionInterceptor interceptor) {
    if (ex instanceof SQLException) {
        return (SQLException) ex;

    } else if (ex.getCause() != null && ex.getCause() instanceof SQLException) {
        return (SQLException) ex.getCause();

    } else if (ex instanceof CJCommunicationsException) {
        return SQLError.createCommunicationsException(ex.getMessage(), ex, interceptor);

    } else if (ex instanceof CJConnectionFeatureNotAvailableException) {
        return new ConnectionFeatureNotAvailableException(ex.getMessage(), ex);

    } else if (ex instanceof SSLParamsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_BAD_SSL_PARAMS, 0, false, ex, interceptor);

    } else if (ex instanceof ConnectionIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_CONNECTION_NOT_OPEN, ex, interceptor);

    } else if (ex instanceof InvalidConnectionAttributeException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE, ex, interceptor);

    } else if (ex instanceof UnableToConnectException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, ex, interceptor);

    } else if (ex instanceof StatementIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof WrongArgumentException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof StringIndexOutOfBoundsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof NumberOutOfRange) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE, ex, interceptor);

    } else if (ex instanceof DataConversionException) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CHARACTER_VALUE_FOR_CAST, ex, interceptor);

    } else if (ex instanceof DataReadException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof DataTruncationException) {
        return new MysqlDataTruncation(((DataTruncationException) ex).getMessage(), ((DataTruncationException) ex).getIndex(),
                ((DataTruncationException) ex).isParameter(), ((DataTruncationException) ex).isRead(), ((DataTruncationException) ex).getDataSize(),
                ((DataTruncationException) ex).getTransferSize(), ((DataTruncationException) ex).getVendorCode());

    } else if (ex instanceof CJPacketTooBigException) {
        return new PacketTooBigException(ex.getMessage());

    } else if (ex instanceof OperationCancelledException) {
        return new MySQLStatementCancelledException(ex.getMessage());

    } else if (ex instanceof CJTimeoutException) {
        return new MySQLTimeoutException(ex.getMessage());

    } else if (ex instanceof CJOperationNotSupportedException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof UnsupportedOperationException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof CJException) {
        return SQLError.createSQLException(ex.getMessage(), ((CJException) ex).getSQLState(), ((CJException) ex).getVendorCode(),
                ((CJException) ex).isTransient(), interceptor);

    } else {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, ex, interceptor);
    }
}
 
Example #8
Source File: DefaultValueFactory.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
private T unsupported(String sourceType) {
    throw new DataConversionException(Messages.getString("ResultSet.UnsupportedConversion", new Object[] { sourceType, getTargetTypeName() }));
}
 
Example #9
Source File: StringConverter.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Implement the logic for indirect conversions.
 */
@Override
public T createFromBytes(byte[] origBytes, int offset, int length) {
    MysqlTextValueDecoder stringInterpreter = new MysqlTextValueDecoder();

    // TODO: Too expensive to convert from other charset to ASCII here? UTF-8 (e.g.) doesn't need any conversion before being sent to the decoder
    String s = StringUtils.toString(origBytes, offset, length, this.encoding);
    byte[] bytes = s.getBytes();

    ValueFactory<T> vf = this.targetVf;

    issueConversionViaParsingWarning();

    if (s.length() == 0) {
        if (this.emptyStringsConvertToZero) {
            // use long=0 as this is mainly numerical oriented
            return this.targetVf.createFromLong(0);
        }
        // Else throw exception down below
    } else if (s.equalsIgnoreCase("true")) {
        return vf.createFromLong(1);
    } else if (s.equalsIgnoreCase("false")) {
        return vf.createFromLong(0);
    } else if (s.contains("e") || s.contains("E") || s.matches("-?(\\d+)?\\.\\d+")) {
        // floating point
        return stringInterpreter.decodeDouble(bytes, 0, bytes.length, vf);
    } else if (s.matches("-?\\d+")) {
        // integer
        if (s.charAt(0) == '-') {
            return stringInterpreter.decodeInt8(bytes, 0, bytes.length, vf);
        }
        return stringInterpreter.decodeUInt8(bytes, 0, bytes.length, vf);
    } else if (s.length() == MysqlTextValueDecoder.DATE_BUF_LEN && s.charAt(4) == '-' && s.charAt(7) == '-') {
        return stringInterpreter.decodeDate(bytes, 0, bytes.length, vf);
    } else if (s.length() >= MysqlTextValueDecoder.TIME_STR_LEN_MIN && s.length() <= MysqlTextValueDecoder.TIME_STR_LEN_MAX && s.charAt(2) == ':'
            && s.charAt(5) == ':') {
        return stringInterpreter.decodeTime(bytes, 0, bytes.length, vf);
    } else if (s.length() >= MysqlTextValueDecoder.TIMESTAMP_NOFRAC_STR_LEN
            && (s.length() <= MysqlTextValueDecoder.TIMESTAMP_STR_LEN_MAX || s.length() == MysqlTextValueDecoder.TIMESTAMP_STR_LEN_WITH_NANOS)
            && s.charAt(4) == '-' && s.charAt(7) == '-' && s.charAt(10) == ' ' && s.charAt(13) == ':' && s.charAt(16) == ':') {
        return stringInterpreter.decodeTimestamp(bytes, 0, bytes.length, vf);
    }
    throw new DataConversionException(Messages.getString("ResultSet.UnableToInterpretString", new Object[] { s }));
}
 
Example #10
Source File: LocalTimeValueFactoryTest.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testBasics() {
    LocalTimeValueFactory vf = new LocalTimeValueFactory(new WarningListener() {
        @Override
        public void warningEncountered(String warning) {
            assertEquals("Precision lost converting DATETIME/TIMESTAMP to java.time.LocalTime", warning);
        }
    });

    assertThrows(DataConversionException.class, "Unsupported conversion from DECIMAL to java.time.LocalTime", new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            vf.createFromBigDecimal(new BigDecimal("2018"));
            return null;
        }
    });

    assertThrows(DataConversionException.class, "Unsupported conversion from BIGINT to java.time.LocalTime", new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            vf.createFromBigInteger(new BigInteger("2018"));
            return null;
        }
    });

    assertThrows(DataConversionException.class, "Unsupported conversion from BIT to java.time.LocalTime", new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            vf.createFromBit(new byte[] { 1 }, 0, 2);
            return null;
        }
    });

    assertThrows(DataConversionException.class, "Unsupported conversion from VARCHAR/TEXT/BLOB to java.time.LocalTime", new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            vf.createFromBytes(new byte[] { 1 }, 0, 2);
            return null;
        }
    });

    assertThrows(DataConversionException.class, "Unsupported conversion from DATE to java.time.LocalTime", new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            vf.createFromDate(2018, 1, 1);
            return null;
        }
    });

    assertThrows(DataConversionException.class, "Unsupported conversion from DOUBLE to java.time.LocalTime", new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            vf.createFromDouble(new Double(2018));
            return null;
        }
    });

    assertThrows(DataConversionException.class, "Unsupported conversion from LONG to java.time.LocalTime", new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            vf.createFromLong(22L);
            return null;
        }
    });

    assertNull(vf.createFromNull());

    assertThrows(DataReadException.class,
            "The value '-1:0:0' is an invalid TIME value. JDBC Time objects represent a wall-clock time and not a duration as MySQL treats them. If you are treating this type as a duration, consider retrieving this value as a string and dealing with it according to your requirements.",
            new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    vf.createFromTime(-1, 0, 0, 0);
                    return null;
                }
            });

    assertThrows(DataReadException.class,
            "The value '44:0:0' is an invalid TIME value. JDBC Time objects represent a wall-clock time and not a duration as MySQL treats them. If you are treating this type as a duration, consider retrieving this value as a string and dealing with it according to your requirements.",
            new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    vf.createFromTime(44, 0, 0, 0);
                    return null;
                }
            });

    assertEquals(LocalTime.of(1, 1, 1, 1), vf.createFromTimestamp(2018, 1, 1, 1, 1, 1, 1));

    assertEquals("java.time.LocalTime", vf.getTargetTypeName());

    LocalTimeValueFactory vf2 = new LocalTimeValueFactory();
    assertEquals(LocalTime.of(1, 1, 1, 1), vf2.createFromTimestamp(2018, 1, 1, 1, 1, 1, 1));
}