Search in sources :

Example 1 with TimestampWithTimeZone

use of org.h2.api.TimestampWithTimeZone in project h2database by h2database.

the class DataType method convertToValue1.

private static Value convertToValue1(SessionInterface session, Object x, int type) {
    if (x == null) {
        return ValueNull.INSTANCE;
    }
    if (type == Value.JAVA_OBJECT) {
        return ValueJavaObject.getNoCopy(x, null, session.getDataHandler());
    }
    if (x instanceof String) {
        return ValueString.get((String) x);
    } else if (x instanceof Value) {
        return (Value) x;
    } else if (x instanceof Long) {
        return ValueLong.get(((Long) x).longValue());
    } else if (x instanceof Integer) {
        return ValueInt.get(((Integer) x).intValue());
    } else if (x instanceof BigInteger) {
        return ValueDecimal.get(new BigDecimal((BigInteger) x));
    } else if (x instanceof BigDecimal) {
        return ValueDecimal.get((BigDecimal) x);
    } else if (x instanceof Boolean) {
        return ValueBoolean.get(((Boolean) x).booleanValue());
    } else if (x instanceof Byte) {
        return ValueByte.get(((Byte) x).byteValue());
    } else if (x instanceof Short) {
        return ValueShort.get(((Short) x).shortValue());
    } else if (x instanceof Float) {
        return ValueFloat.get(((Float) x).floatValue());
    } else if (x instanceof Double) {
        return ValueDouble.get(((Double) x).doubleValue());
    } else if (x instanceof byte[]) {
        return ValueBytes.get((byte[]) x);
    } else if (x instanceof Date) {
        return ValueDate.get((Date) x);
    } else if (x instanceof Time) {
        return ValueTime.get((Time) x);
    } else if (x instanceof Timestamp) {
        return ValueTimestamp.get((Timestamp) x);
    } else if (x instanceof java.util.Date) {
        return ValueTimestamp.fromMillis(((java.util.Date) x).getTime());
    } else if (x instanceof java.io.Reader) {
        Reader r = new BufferedReader((java.io.Reader) x);
        return session.getDataHandler().getLobStorage().createClob(r, -1);
    } else if (x instanceof java.sql.Clob) {
        try {
            java.sql.Clob clob = (java.sql.Clob) x;
            Reader r = new BufferedReader(clob.getCharacterStream());
            return session.getDataHandler().getLobStorage().createClob(r, clob.length());
        } catch (SQLException e) {
            throw DbException.convert(e);
        }
    } else if (x instanceof java.io.InputStream) {
        return session.getDataHandler().getLobStorage().createBlob((java.io.InputStream) x, -1);
    } else if (x instanceof java.sql.Blob) {
        try {
            java.sql.Blob blob = (java.sql.Blob) x;
            return session.getDataHandler().getLobStorage().createBlob(blob.getBinaryStream(), blob.length());
        } catch (SQLException e) {
            throw DbException.convert(e);
        }
    } else if (x instanceof java.sql.Array) {
        java.sql.Array array = (java.sql.Array) x;
        try {
            return convertToValue(session, array.getArray(), Value.ARRAY);
        } catch (SQLException e) {
            throw DbException.convert(e);
        }
    } else if (x instanceof ResultSet) {
        if (x instanceof SimpleResultSet) {
            return ValueResultSet.get((ResultSet) x);
        }
        return ValueResultSet.getCopy((ResultSet) x, Integer.MAX_VALUE);
    } else if (x instanceof UUID) {
        return ValueUuid.get((UUID) x);
    }
    Class<?> clazz = x.getClass();
    if (x instanceof Object[]) {
        // (a.getClass().isArray());
        // (a.getClass().getComponentType().isPrimitive());
        Object[] o = (Object[]) x;
        int len = o.length;
        Value[] v = new Value[len];
        for (int i = 0; i < len; i++) {
            v[i] = convertToValue(session, o[i], type);
        }
        return ValueArray.get(clazz.getComponentType(), v);
    } else if (x instanceof Character) {
        return ValueStringFixed.get(((Character) x).toString());
    } else if (isGeometry(x)) {
        return ValueGeometry.getFromGeometry(x);
    } else if (clazz == LocalDateTimeUtils.LOCAL_DATE) {
        return LocalDateTimeUtils.localDateToDateValue(x);
    } else if (clazz == LocalDateTimeUtils.LOCAL_TIME) {
        return LocalDateTimeUtils.localTimeToTimeValue(x);
    } else if (clazz == LocalDateTimeUtils.LOCAL_DATE_TIME) {
        return LocalDateTimeUtils.localDateTimeToValue(x);
    } else if (clazz == LocalDateTimeUtils.INSTANT) {
        return LocalDateTimeUtils.instantToValue(x);
    } else if (clazz == LocalDateTimeUtils.OFFSET_DATE_TIME) {
        return LocalDateTimeUtils.offsetDateTimeToValue(x);
    } else if (x instanceof TimestampWithTimeZone) {
        return ValueTimestampTimeZone.get((TimestampWithTimeZone) x);
    } else {
        if (JdbcUtils.customDataTypesHandler != null) {
            return JdbcUtils.customDataTypesHandler.getValue(type, x, session.getDataHandler());
        }
        return ValueJavaObject.getNoCopy(x, null, session.getDataHandler());
    }
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) SQLException(java.sql.SQLException) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Array(java.sql.Array) TimestampWithTimeZone(org.h2.api.TimestampWithTimeZone) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) UUID(java.util.UUID) Clob(java.sql.Clob) JdbcBlob(org.h2.jdbc.JdbcBlob) Blob(java.sql.Blob) InputStream(java.io.InputStream) BigDecimal(java.math.BigDecimal) Date(java.sql.Date) BigInteger(java.math.BigInteger) Array(java.sql.Array) JdbcArray(org.h2.jdbc.JdbcArray) BufferedReader(java.io.BufferedReader) BigInteger(java.math.BigInteger) JdbcClob(org.h2.jdbc.JdbcClob) Clob(java.sql.Clob) Blob(java.sql.Blob)

Example 2 with TimestampWithTimeZone

use of org.h2.api.TimestampWithTimeZone in project spring-data-jdbc by spring-projects.

the class JdbcH2DialectTests method TimestampWithTimeZone2OffsetDateTimeConverterConvertsProperly.

@Test
void TimestampWithTimeZone2OffsetDateTimeConverterConvertsProperly() {
    H2TimestampWithTimeZoneToOffsetDateTimeConverter converter = H2TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE;
    long dateValue = 123456789;
    long timeNanos = 987654321;
    int timeZoneOffsetSeconds = 4 * 60 * 60;
    TimestampWithTimeZone timestampWithTimeZone = new TimestampWithTimeZone(dateValue, timeNanos, timeZoneOffsetSeconds);
    OffsetDateTime offsetDateTime = converter.convert(timestampWithTimeZone);
    assertThat(offsetDateTime.getOffset().getTotalSeconds()).isEqualTo(timeZoneOffsetSeconds);
    assertThat(offsetDateTime.getNano()).isEqualTo(timeNanos);
    assertThat(offsetDateTime.toEpochSecond()).isEqualTo(DateTimeUtils.getEpochSeconds(dateValue, timeNanos, timeZoneOffsetSeconds));
}
Also used : OffsetDateTime(java.time.OffsetDateTime) TimestampWithTimeZone(org.h2.api.TimestampWithTimeZone) Test(org.junit.jupiter.api.Test)

Example 3 with TimestampWithTimeZone

use of org.h2.api.TimestampWithTimeZone in project h2database by h2database.

the class TestTimeStampWithTimeZone method test1.

private void test1() throws SQLException {
    Connection conn = getConnection(getTestName());
    Statement stat = conn.createStatement();
    stat.execute("create table test(id identity, t1 timestamp(9) with time zone)");
    stat.execute("insert into test(t1) values('1970-01-01 12:00:00.00+00:15')");
    // verify NanosSinceMidnight is in local time and not UTC
    stat.execute("insert into test(t1) values('2016-09-24 00:00:00.000000001+00:01')");
    stat.execute("insert into test(t1) values('2016-09-24 00:00:00.000000001-00:01')");
    // verify year month day is in local time and not UTC
    stat.execute("insert into test(t1) values('2016-01-01 05:00:00.00+10:00')");
    stat.execute("insert into test(t1) values('2015-12-31 19:00:00.00-10:00')");
    ResultSet rs = stat.executeQuery("select t1 from test");
    rs.next();
    assertEquals("1970-01-01 12:00:00+00:15", rs.getString(1));
    TimestampWithTimeZone ts = (TimestampWithTimeZone) rs.getObject(1);
    assertEquals(1970, ts.getYear());
    assertEquals(1, ts.getMonth());
    assertEquals(1, ts.getDay());
    assertEquals(15, ts.getTimeZoneOffsetMins());
    assertEquals(new TimestampWithTimeZone(1008673L, 43200000000000L, (short) 15), ts);
    if (LocalDateTimeUtils.isJava8DateApiPresent()) {
        assertEquals("1970-01-01T12:00+00:15", rs.getObject(1, LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
    }
    rs.next();
    ts = (TimestampWithTimeZone) rs.getObject(1);
    assertEquals(2016, ts.getYear());
    assertEquals(9, ts.getMonth());
    assertEquals(24, ts.getDay());
    assertEquals(1, ts.getTimeZoneOffsetMins());
    assertEquals(1L, ts.getNanosSinceMidnight());
    if (LocalDateTimeUtils.isJava8DateApiPresent()) {
        assertEquals("2016-09-24T00:00:00.000000001+00:01", rs.getObject(1, LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
    }
    rs.next();
    ts = (TimestampWithTimeZone) rs.getObject(1);
    assertEquals(2016, ts.getYear());
    assertEquals(9, ts.getMonth());
    assertEquals(24, ts.getDay());
    assertEquals(-1, ts.getTimeZoneOffsetMins());
    assertEquals(1L, ts.getNanosSinceMidnight());
    if (LocalDateTimeUtils.isJava8DateApiPresent()) {
        assertEquals("2016-09-24T00:00:00.000000001-00:01", rs.getObject(1, LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
    }
    rs.next();
    ts = (TimestampWithTimeZone) rs.getObject(1);
    assertEquals(2016, ts.getYear());
    assertEquals(1, ts.getMonth());
    assertEquals(1, ts.getDay());
    if (LocalDateTimeUtils.isJava8DateApiPresent()) {
        assertEquals("2016-01-01T05:00+10:00", rs.getObject(1, LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
    }
    rs.next();
    ts = (TimestampWithTimeZone) rs.getObject(1);
    assertEquals(2015, ts.getYear());
    assertEquals(12, ts.getMonth());
    assertEquals(31, ts.getDay());
    if (LocalDateTimeUtils.isJava8DateApiPresent()) {
        assertEquals("2015-12-31T19:00-10:00", rs.getObject(1, LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
    }
    ResultSetMetaData metaData = rs.getMetaData();
    int columnType = metaData.getColumnType(1);
    // 2014 is the value of Types.TIMESTAMP_WITH_TIMEZONE
    // use the value instead of the reference because the code has to
    // compile (on Java 1.7). Can be replaced with
    // Types.TIMESTAMP_WITH_TIMEZONE
    // once Java 1.8 is required.
    assertEquals(2014, columnType);
    rs.close();
    stat.close();
    conn.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) TimestampWithTimeZone(org.h2.api.TimestampWithTimeZone) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Aggregations

TimestampWithTimeZone (org.h2.api.TimestampWithTimeZone)3 ResultSet (java.sql.ResultSet)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Array (java.sql.Array)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 Connection (java.sql.Connection)1 Date (java.sql.Date)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 OffsetDateTime (java.time.OffsetDateTime)1 UUID (java.util.UUID)1