Search in sources :

Example 1 with InternalDate

use of com.mysql.cj.protocol.InternalDate 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 InternalDate

use of com.mysql.cj.protocol.InternalDate 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 3 with InternalDate

use of com.mysql.cj.protocol.InternalDate in project aws-mysql-jdbc by awslabs.

the class ValueEncoder method asInternalDate.

private InternalDate asInternalDate(Object value) {
    if (LocalDate.class.isInstance(value)) {
        LocalDate localDate = (LocalDate) value;
        InternalDate internalDate = new InternalDate();
        internalDate.setYear(localDate.getYear());
        internalDate.setMonth(localDate.getMonthValue());
        internalDate.setDay(localDate.getDayOfMonth());
        return internalDate;
    }
    if (Date.class.isInstance(value)) {
        Calendar calendar = Calendar.getInstance(this.timezone);
        calendar.setTime((Date) value);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        InternalDate internalDate = new InternalDate();
        internalDate.setYear(calendar.get(Calendar.YEAR));
        internalDate.setMonth(calendar.get(Calendar.MONTH) + 1);
        internalDate.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        return internalDate;
    }
    throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ValueEncoder.WrongDateValueType", new Object[] { value.getClass() }));
}
Also used : InternalDate(com.mysql.cj.protocol.InternalDate) Calendar(java.util.Calendar) LocalDate(java.time.LocalDate)

Example 4 with InternalDate

use of com.mysql.cj.protocol.InternalDate in project aws-mysql-jdbc by awslabs.

the class ResultSetRegressionTest method testBug23702040.

/**
 * Tests fix for BUG#23702040 - JDBCDATEVALUEFACTORY FAILS TO PARSE SOME DATES.
 *
 * @throws Exception
 */
@Test
public void testBug23702040() throws Exception {
    SimpleDateFormat sdf = TimeUtil.getSimpleDateFormat(null, "yyyy-MM-dd", TimeZone.getTimeZone("Europe/Bucharest"));
    sdf.setLenient(false);
    java.util.Date expected = sdf.parse("1994-03-27");
    Date fromFactory = new SqlDateValueFactory(new DefaultPropertySet(), null, TimeZone.getTimeZone("Europe/Bucharest")).createFromDate(new InternalDate(1994, 3, 27));
    assertEquals(expected.getTime(), fromFactory.getTime());
}
Also used : DefaultPropertySet(com.mysql.cj.conf.DefaultPropertySet) SqlDateValueFactory(com.mysql.cj.result.SqlDateValueFactory) InternalDate(com.mysql.cj.protocol.InternalDate) SimpleDateFormat(java.text.SimpleDateFormat) InternalDate(com.mysql.cj.protocol.InternalDate) LocalDate(java.time.LocalDate) Date(java.sql.Date) Test(org.junit.jupiter.api.Test)

Example 5 with InternalDate

use of com.mysql.cj.protocol.InternalDate in project aws-mysql-jdbc by awslabs.

the class StringValueFactoryTest method testCreateFromDate.

@Test
public void testCreateFromDate() {
    this.vf.createFromDate(new InternalDate(2006, 1, 1));
    // May 1st
    assertEquals("2015-05-01", this.vf.createFromDate(new InternalDate(2015, 5, 1)));
}
Also used : InternalDate(com.mysql.cj.protocol.InternalDate) Test(org.junit.jupiter.api.Test)

Aggregations

InternalDate (com.mysql.cj.protocol.InternalDate)10 Test (org.junit.jupiter.api.Test)6 DataReadException (com.mysql.cj.exceptions.DataReadException)4 InternalTimestamp (com.mysql.cj.protocol.InternalTimestamp)4 CodedInputStream (com.google.protobuf.CodedInputStream)2 DefaultPropertySet (com.mysql.cj.conf.DefaultPropertySet)2 IOException (java.io.IOException)2 LocalDate (java.time.LocalDate)2 PropertyDefinitions (com.mysql.cj.conf.PropertyDefinitions)1 PropertySet (com.mysql.cj.conf.PropertySet)1 InternalTime (com.mysql.cj.protocol.InternalTime)1 SqlDateValueFactory (com.mysql.cj.result.SqlDateValueFactory)1 Date (java.sql.Date)1 Timestamp (java.sql.Timestamp)1 SimpleDateFormat (java.text.SimpleDateFormat)1 DateTimeException (java.time.DateTimeException)1 Calendar (java.util.Calendar)1