Search in sources :

Example 1 with DataReadException

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

the class XProtocolDecoder method decodeDatetime.

@Override
public <T> T decodeDatetime(byte[] bytes, int offset, int length, int scale, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        int year = (int) inputStream.readUInt64();
        int month = (int) inputStream.readUInt64();
        int day = (int) inputStream.readUInt64();
        // do we have a time too?
        if (inputStream.getBytesUntilLimit() > 0) {
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            int nanos = 0;
            if (!inputStream.isAtEnd()) {
                hours = (int) inputStream.readInt64();
                if (!inputStream.isAtEnd()) {
                    minutes = (int) inputStream.readInt64();
                    if (!inputStream.isAtEnd()) {
                        seconds = (int) inputStream.readInt64();
                        if (!inputStream.isAtEnd()) {
                            nanos = 1000 * (int) inputStream.readInt64();
                        }
                    }
                }
            }
            return vf.createFromDatetime(new InternalTimestamp(year, month, day, hours, minutes, seconds, nanos, scale));
        }
        return vf.createFromDate(new InternalDate(year, month, day));
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
Also used : InternalDate(com.mysql.cj.protocol.InternalDate) CodedInputStream(com.google.protobuf.CodedInputStream) IOException(java.io.IOException) InternalTimestamp(com.mysql.cj.protocol.InternalTimestamp) DataReadException(com.mysql.cj.exceptions.DataReadException)

Example 2 with DataReadException

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

the class XProtocolDecoder method decodeTime.

@Override
public <T> T decodeTime(byte[] bytes, int offset, int length, int scale, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        boolean negative = inputStream.readRawByte() > 0;
        int hours = 0;
        int minutes = 0;
        int seconds = 0;
        int nanos = 0;
        if (!inputStream.isAtEnd()) {
            hours = (int) inputStream.readInt64();
            if (!inputStream.isAtEnd()) {
                minutes = (int) inputStream.readInt64();
                if (!inputStream.isAtEnd()) {
                    seconds = (int) inputStream.readInt64();
                    if (!inputStream.isAtEnd()) {
                        nanos = 1000 * (int) inputStream.readInt64();
                    }
                }
            }
        }
        return vf.createFromTime(new InternalTime(negative ? -1 * hours : hours, minutes, seconds, nanos, scale));
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) IOException(java.io.IOException) InternalTime(com.mysql.cj.protocol.InternalTime) DataReadException(com.mysql.cj.exceptions.DataReadException)

Example 3 with DataReadException

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

the class XProtocolDecoder method decodeByteArray.

@Override
public <T> T decodeByteArray(byte[] bytes, int offset, int length, Field f, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // c.f. Streaming_command_delegate::get_string()
        int size = inputStream.getBytesUntilLimit();
        // for null terminator
        size--;
        return vf.createFromBytes(inputStream.readRawBytes(size), 0, size, f);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) IOException(java.io.IOException) DataReadException(com.mysql.cj.exceptions.DataReadException)

Example 4 with DataReadException

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

the class MysqlBinaryValueDecoder method decodeDate.

public <T> T decodeDate(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length == 0) {
        return vf.createFromDate(new InternalDate());
    } else if (length != NativeConstants.BIN_LEN_DATE) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "DATE" }));
    }
    int year = (bytes[offset] & 0xff) | ((bytes[offset + 1] & 0xff) << 8);
    int month = bytes[offset + 2];
    int day = bytes[offset + 3];
    return vf.createFromDate(new InternalDate(year, month, day));
}
Also used : InternalDate(com.mysql.cj.protocol.InternalDate) DataReadException(com.mysql.cj.exceptions.DataReadException)

Example 5 with DataReadException

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

the class MysqlBinaryValueDecoder method decodeDatetime.

public <T> T decodeDatetime(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.createFromDatetime(new InternalTimestamp(year, month, day, hours, minutes, seconds, nanos, scale));
}
Also used : InternalTimestamp(com.mysql.cj.protocol.InternalTimestamp) 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