Search in sources :

Example 21 with DataReadException

use of com.mysql.cj.exceptions.DataReadException in project aws-mysql-jdbc by awslabs.

the class MysqlBinaryValueDecoder method decodeTimestamp.

public <T> T decodeTimestamp(byte[] bytes, int offset, int length, int scale, ValueFactory<T> vf) {
    if (length == 0) {
        return vf.createFromTimestamp(new InternalTimestamp());
    } else if (length != NativeConstants.BIN_LEN_DATE && length != NativeConstants.BIN_LEN_TIMESTAMP_WITH_MICROS && length != NativeConstants.BIN_LEN_TIMESTAMP_NO_FRAC) {
        // the value can be any of these lengths (check protocol docs)
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIMESTAMP" }));
    }
    int year = 0;
    int month = 0;
    int day = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    int nanos = 0;
    year = (bytes[offset + 0] & 0xff) | ((bytes[offset + 1] & 0xff) << 8);
    month = bytes[offset + 2];
    day = bytes[offset + 3];
    if (length > NativeConstants.BIN_LEN_DATE) {
        hours = bytes[offset + 4];
        minutes = bytes[offset + 5];
        seconds = bytes[offset + 6];
    }
    if (length > NativeConstants.BIN_LEN_TIMESTAMP_NO_FRAC) {
        // MySQL PS protocol uses microseconds
        nanos = 1000 * ((bytes[offset + 7] & 0xff) | ((bytes[offset + 8] & 0xff) << 8) | ((bytes[offset + 9] & 0xff) << 16) | ((bytes[offset + 10] & 0xff) << 24));
    }
    return vf.createFromTimestamp(new InternalTimestamp(year, month, day, hours, minutes, seconds, nanos, scale));
}
Also used : InternalTimestamp(com.mysql.cj.protocol.InternalTimestamp) DataReadException(com.mysql.cj.exceptions.DataReadException)

Example 22 with DataReadException

use of com.mysql.cj.exceptions.DataReadException in project aws-mysql-jdbc by awslabs.

the class MysqlBinaryValueDecoder method decodeTime.

public <T> T decodeTime(byte[] bytes, int offset, int length, int scale, ValueFactory<T> vf) {
    if (length == 0) {
        return vf.createFromTime(new InternalTime());
    } else if (length != NativeConstants.BIN_LEN_TIME_WITH_MICROS && length != NativeConstants.BIN_LEN_TIME_NO_FRAC) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIME" }));
    }
    int days = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    int nanos = 0;
    boolean negative = bytes[offset] == 1;
    days = (bytes[offset + 1] & 0xff) | ((bytes[offset + 2] & 0xff) << 8) | ((bytes[offset + 3] & 0xff) << 16) | ((bytes[offset + 4] & 0xff) << 24);
    hours = bytes[offset + 5];
    minutes = bytes[offset + 6];
    seconds = bytes[offset + 7];
    if (negative) {
        days *= -1;
    }
    if (length > NativeConstants.BIN_LEN_TIME_NO_FRAC) {
        // MySQL PS protocol uses microseconds
        nanos = 1000 * ((bytes[offset + 8] & 0xff) | ((bytes[offset + 9] & 0xff) << 8) | ((bytes[offset + 10] & 0xff) << 16) | ((bytes[offset + 11] & 0xff) << 24));
    }
    return vf.createFromTime(new InternalTime(days * 24 + hours, minutes, seconds, nanos, scale));
}
Also used : InternalTime(com.mysql.cj.protocol.InternalTime) DataReadException(com.mysql.cj.exceptions.DataReadException)

Aggregations

DataReadException (com.mysql.cj.exceptions.DataReadException)22 InternalTimestamp (com.mysql.cj.protocol.InternalTimestamp)9 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)8 Calendar (java.util.Calendar)8 CodedInputStream (com.google.protobuf.CodedInputStream)6 IOException (java.io.IOException)6 Timestamp (java.sql.Timestamp)4 InternalDate (com.mysql.cj.protocol.InternalDate)3 InternalTime (com.mysql.cj.protocol.InternalTime)2 BigInteger (java.math.BigInteger)2 ByteString (com.google.protobuf.ByteString)1 Field (com.mysql.cj.result.Field)1 Row (com.mysql.cj.xdevapi.Row)1 RowResult (com.mysql.cj.xdevapi.RowResult)1 Table (com.mysql.cj.xdevapi.Table)1 BigDecimal (java.math.BigDecimal)1 CharBuffer (java.nio.CharBuffer)1 Test (org.junit.jupiter.api.Test)1