Search in sources :

Example 11 with DataReadException

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

the class UtilCalendarValueFactory method localCreateFromTime.

/**
 * Create a Calendar from a TIME value.
 *
 * @return a Calendar at the given time on 1970 Jan 1.
 */
@Override
public Calendar localCreateFromTime(InternalTime it) {
    if (it.getHours() < 0 || it.getHours() >= 24) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidTimeValue", new Object[] { it.toString() }));
    }
    try {
        Calendar c = Calendar.getInstance(this.defaultTimeZone, Locale.US);
        c.set(1970, 0, 1, it.getHours(), it.getMinutes(), it.getSeconds());
        c.set(Calendar.MILLISECOND, it.getNanos() / 1000000);
        c.setLenient(false);
        return c;
    } catch (IllegalArgumentException e) {
        throw ExceptionFactory.createException(WrongArgumentException.class, e.getMessage(), e);
    }
}
Also used : Calendar(java.util.Calendar) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) DataReadException(com.mysql.cj.exceptions.DataReadException)

Example 12 with DataReadException

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

the class UtilCalendarValueFactory method localCreateFromDate.

/**
 * Create a Calendar from a DATE value.
 *
 * @return a Calendar at midnight on the day given by the DATE value
 */
@Override
public Calendar localCreateFromDate(InternalDate idate) {
    if (idate.getYear() == 0 && idate.getMonth() == 0 && idate.getDay() == 0) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidZeroDate"));
    }
    try {
        Calendar c = Calendar.getInstance(this.defaultTimeZone, Locale.US);
        c.set(idate.getYear(), idate.getMonth() - 1, idate.getDay(), 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.setLenient(false);
        return c;
    } catch (IllegalArgumentException e) {
        throw ExceptionFactory.createException(WrongArgumentException.class, e.getMessage(), e);
    }
}
Also used : Calendar(java.util.Calendar) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) DataReadException(com.mysql.cj.exceptions.DataReadException)

Example 13 with DataReadException

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

the class ResultTest method exceptionForNonExistingColumns.

@Test
public void exceptionForNonExistingColumns() {
    try {
        sqlUpdate("drop table if exists testx");
        sqlUpdate("create table testx (x int)");
        sqlUpdate("insert into testx values (1), (2), (3)");
        Table table = this.schema.getTable("testx");
        RowResult rows = table.select("x").orderBy("x").execute();
        Row r = rows.next();
        r.getString("x");
        try {
            r.getString("non_existing");
        } catch (DataReadException ex) {
            assertTrue(ex.getMessage().contains("Invalid column"));
        }
        r.getString(0);
        try {
            r.getString(1);
        } catch (DataReadException ex) {
            assertTrue(ex.getMessage().contains("Invalid column"));
        }
    } finally {
        sqlUpdate("drop table if exists testx");
    }
}
Also used : RowResult(com.mysql.cj.xdevapi.RowResult) Table(com.mysql.cj.xdevapi.Table) Row(com.mysql.cj.xdevapi.Row) DataReadException(com.mysql.cj.exceptions.DataReadException) Test(org.junit.jupiter.api.Test)

Example 14 with DataReadException

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

the class XProtocolDecoder method decodeTimestamp.

@Override
public <T> T decodeTimestamp(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.createFromTimestamp(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 15 with DataReadException

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

the class XProtocolDecoder method decodeDecimal.

@Override
public <T> T decodeDecimal(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // packed BCD format (c.f. wikipedia)
        // TODO: optimization possibilities include using int/long if the digits is < X and scale = 0
        byte scale = inputStream.readRawByte();
        // we allocate an extra char for the sign
        CharBuffer unscaledString = CharBuffer.allocate(2 * inputStream.getBytesUntilLimit());
        unscaledString.position(1);
        byte sign = 0;
        // read until we encounter the sign bit
        while (true) {
            int b = 0xFF & inputStream.readRawByte();
            if ((b >> 4) > 9) {
                sign = (byte) (b >> 4);
                break;
            }
            unscaledString.append((char) ((b >> 4) + '0'));
            if ((b & 0x0f) > 9) {
                sign = (byte) (b & 0x0f);
                break;
            }
            unscaledString.append((char) ((b & 0x0f) + '0'));
        }
        if (inputStream.getBytesUntilLimit() > 0) {
            throw AssertionFailedException.shouldNotHappen("Did not read all bytes while decoding decimal. Bytes left: " + inputStream.getBytesUntilLimit());
        }
        switch(sign) {
            case 0xa:
            case 0xc:
            case 0xe:
            case 0xf:
                unscaledString.put(0, '+');
                break;
            case 0xb:
            case 0xd:
                unscaledString.put(0, '-');
                break;
        }
        // may have filled the CharBuffer or one remaining. need to remove it before toString()
        int characters = unscaledString.position();
        // reset position
        unscaledString.clear();
        BigInteger unscaled = new BigInteger(unscaledString.subSequence(0, characters).toString());
        return vf.createFromBigDecimal(new BigDecimal(unscaled, scale));
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) CharBuffer(java.nio.CharBuffer) BigInteger(java.math.BigInteger) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) 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