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);
}
}
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);
}
}
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");
}
}
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);
}
}
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);
}
}
Aggregations