Search in sources :

Example 1 with InternalTime

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

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

the class MysqlTextValueDecoderTest method testNanosecondParsing.

@Test
public void testNanosecondParsing() {
    // test value factory to extract the parsed nano-seconds
    ValueFactory<Integer> vf = new DefaultValueFactory<Integer>(new DefaultPropertySet()) {

        @Override
        public Integer createFromTime(InternalTime it) {
            return it.getNanos();
        }

        @Override
        public Integer createFromTimestamp(InternalTimestamp its) {
            return its.getNanos();
        }

        public String getTargetTypeName() {
            return Integer.class.getName();
        }

        @Override
        public Integer createFromBytes(byte[] bytes, int offset, int length, Field f) {
            return null;
        }
    };
    // the fractional second part is determined by the # of digits
    assertEquals(new Integer(900000000), this.valueDecoder.decodeTimestamp("2016-03-14 14:34:01.9".getBytes(), 0, 21, 9, vf));
    assertEquals(new Integer(950000000), this.valueDecoder.decodeTimestamp("2016-03-14 14:34:01.95".getBytes(), 0, 22, 9, vf));
    assertEquals(new Integer(956000000), this.valueDecoder.decodeTimestamp("2016-03-14 14:34:01.956".getBytes(), 0, 23, 9, vf));
    assertEquals(new Integer(900000000), this.valueDecoder.decodeTime("14:34:01.9".getBytes(), 0, 10, 9, vf));
    assertEquals(new Integer(950000000), this.valueDecoder.decodeTime("14:34:01.95".getBytes(), 0, 11, 9, vf));
    assertEquals(new Integer(956000000), this.valueDecoder.decodeTime("14:34:01.956".getBytes(), 0, 12, 9, vf));
}
Also used : DefaultPropertySet(com.mysql.cj.conf.DefaultPropertySet) Field(com.mysql.cj.result.Field) DefaultValueFactory(com.mysql.cj.result.DefaultValueFactory) InternalTime(com.mysql.cj.protocol.InternalTime) InternalTimestamp(com.mysql.cj.protocol.InternalTimestamp) Test(org.junit.jupiter.api.Test)

Example 3 with InternalTime

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

the class SqlDateValueFactoryTest method testCreateFromTime.

@Test
public void testCreateFromTime() {
    assertEquals("1970-01-01", this.vf.createFromTime(new InternalTime(12, 20, 02, 4, 9)).toString());
    assertEquals("1970-01-01", this.vf.createFromTime(new InternalTime(1, 1, 1, 1, 9)).toString());
    assertEquals("1970-01-01", this.vf.createFromTime(new InternalTime(-1, 1, 1, 1, 9)).toString());
    assertEquals("1970-01-01", this.vf.createFromTime(new InternalTime(48, 1, 1, 1, 9)).toString());
}
Also used : InternalTime(com.mysql.cj.protocol.InternalTime) Test(org.junit.jupiter.api.Test)

Example 4 with InternalTime

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

the class SqlTimestampValueFactoryTest method testCreateFromTime.

@Test
public void testCreateFromTime() {
    Timestamp ts = this.vf.createFromTime(new InternalTime(12, 20, 02, 4, 9));
    assertEquals("1970-01-01 12:20:02.000000004", ts.toString());
    assertEquals(Timestamp.valueOf(LocalDateTime.of(1970, 1, 1, 1, 1, 1, 1)), this.vf.createFromTime(new InternalTime(1, 1, 1, 1, 9)));
    assertThrows(DataReadException.class, "The value '-1:00:00' is an invalid TIME value. JDBC Time objects represent a wall-clock time and not a duration as MySQL treats them. If you are treating this type as a duration, consider retrieving this value as a string and dealing with it according to your requirements.", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            SqlTimestampValueFactoryTest.this.vf.createFromTime(new InternalTime(-1, 0, 0, 0, 9));
            return null;
        }
    });
    assertThrows(DataReadException.class, "The value '44:00:00' is an invalid TIME value. JDBC Time objects represent a wall-clock time and not a duration as MySQL treats them. If you are treating this type as a duration, consider retrieving this value as a string and dealing with it according to your requirements.", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            SqlTimestampValueFactoryTest.this.vf.createFromTime(new InternalTime(44, 0, 0, 0, 9));
            return null;
        }
    });
}
Also used : Timestamp(java.sql.Timestamp) InternalTimestamp(com.mysql.cj.protocol.InternalTimestamp) InternalTime(com.mysql.cj.protocol.InternalTime) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) DataConversionException(com.mysql.cj.exceptions.DataConversionException) DataReadException(com.mysql.cj.exceptions.DataReadException) Test(org.junit.jupiter.api.Test)

Example 5 with InternalTime

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

the class ValueEncoder method asInternalTime.

private InternalTime asInternalTime(Object value) {
    if (LocalTime.class.isInstance(value)) {
        LocalTime localTime = (LocalTime) value;
        InternalTime internalTime = new InternalTime();
        internalTime.setHours(localTime.getHour());
        internalTime.setMinutes(localTime.getMinute());
        internalTime.setSeconds(localTime.getSecond());
        internalTime.setNanos(localTime.getNano());
        return internalTime;
    }
    if (OffsetTime.class.isInstance(value)) {
        OffsetTime offsetTime = (OffsetTime) value;
        InternalTime internalTime = new InternalTime();
        internalTime.setHours(offsetTime.getHour());
        internalTime.setMinutes(offsetTime.getMinute());
        internalTime.setSeconds(offsetTime.getSecond());
        internalTime.setNanos(offsetTime.getNano());
        return internalTime;
    }
    if (Duration.class.isInstance(value)) {
        Duration duration = (Duration) value;
        Duration durationAbs = duration.abs();
        long fullSeconds = durationAbs.getSeconds();
        int seconds = (int) (fullSeconds % 60);
        long fullMinutes = fullSeconds / 60;
        int minutes = (int) (fullMinutes % 60);
        long fullHours = fullMinutes / 60;
        InternalTime internalTime = new InternalTime();
        internalTime.setNegative(duration.isNegative());
        internalTime.setHours((int) fullHours);
        internalTime.setMinutes(minutes);
        internalTime.setSeconds(seconds);
        internalTime.setNanos(durationAbs.getNano());
        return internalTime;
    }
    if (Time.class.isInstance(value)) {
        Time time = (Time) value;
        Calendar calendar = Calendar.getInstance(this.timezone);
        calendar.setTime(time);
        InternalTime internalTime = new InternalTime();
        internalTime.setHours(calendar.get(Calendar.HOUR_OF_DAY));
        internalTime.setMinutes(calendar.get(Calendar.MINUTE));
        internalTime.setSeconds(calendar.get(Calendar.SECOND));
        internalTime.setNanos((int) TimeUnit.MILLISECONDS.toNanos(calendar.get(Calendar.MILLISECOND)));
        return internalTime;
    }
    throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ValueEncoder.WrongTimeValueType", new Object[] { value.getClass() }));
}
Also used : LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) Calendar(java.util.Calendar) Duration(java.time.Duration) Time(java.sql.Time) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) InternalTime(com.mysql.cj.protocol.InternalTime) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) OffsetDateTime(java.time.OffsetDateTime) InternalTime(com.mysql.cj.protocol.InternalTime)

Aggregations

InternalTime (com.mysql.cj.protocol.InternalTime)9 Test (org.junit.jupiter.api.Test)6 DataReadException (com.mysql.cj.exceptions.DataReadException)5 InternalTimestamp (com.mysql.cj.protocol.InternalTimestamp)3 DefaultPropertySet (com.mysql.cj.conf.DefaultPropertySet)2 DataConversionException (com.mysql.cj.exceptions.DataConversionException)2 Time (java.sql.Time)2 LocalTime (java.time.LocalTime)2 CodedInputStream (com.google.protobuf.CodedInputStream)1 PropertyDefinitions (com.mysql.cj.conf.PropertyDefinitions)1 PropertySet (com.mysql.cj.conf.PropertySet)1 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)1 InternalDate (com.mysql.cj.protocol.InternalDate)1 DefaultValueFactory (com.mysql.cj.result.DefaultValueFactory)1 Field (com.mysql.cj.result.Field)1 IOException (java.io.IOException)1 Timestamp (java.sql.Timestamp)1 DateTimeException (java.time.DateTimeException)1 Duration (java.time.Duration)1 LocalDateTime (java.time.LocalDateTime)1