Skip to content

Instantly share code, notes, and snippets.

@mp911de
Last active June 13, 2022 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mp911de/301bcdb05ca16b6f9d8eda51f76653bb to your computer and use it in GitHub Desktop.
Save mp911de/301bcdb05ca16b6f9d8eda51f76653bb to your computer and use it in GitHub Desktop.
JDBC Native: 2022-06-13 11:21:41.322 (Timestamp)
JDBC OffsetDateTime: 2022-06-13T09:21:41.322Z, Instant: 2022-06-13T09:21:41.322Z
org.postgresql.util.PSQLException: Cannot convert the column of type TIMESTAMPTZ to requested type timestamp.
at org.postgresql.jdbc.PgResultSet.getLocalDateTime(PgResultSet.java:725)
at org.postgresql.jdbc.PgResultSet.getObject(PgResultSet.java:3731)
...
-------------------
R2DBC Native: 2022-06-13T11:21:41.322+02:00 (OffsetDateTime)
R2DBC OffsetDateTime: 2022-06-13T11:21:41.322+02:00, Instant: 2022-06-13T09:21:41.322Z
Instant: 2022-06-13T09:21:41.322Z
LocalDateTime: 2022-06-13T09:21:41.322
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.r2dbc.postgresql;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
/**
* @author Mark Paluch
*/
public class Repro extends AbstractIntegrationTests {
@Test
void name() {
JdbcOperations jdbcOperations = SERVER.getJdbcOperations();
jdbcOperations.execute("DROP TABLE IF EXISTS test;");
jdbcOperations.execute("CREATE TABLE test (value TIMESTAMPTZ)");
Instant now = Instant.now();
Timestamp ts = Timestamp.from(now);
jdbcOperations.update("INSERT INTO test VALUES(?)", ts);
jdbcOperations.query("SELECT * FROM test", (RowMapper<Object>) (rs, rowNum) -> {
Object object = rs.getObject(1);
System.out.println(String.format("JDBC Native: %s (%s)", object, object.getClass().getSimpleName()));
OffsetDateTime odt = rs.getObject(1, OffsetDateTime.class);
System.out.println(String.format("JDBC OffsetDateTime: %s, Instant: %s", odt, odt.toInstant()));
try {
rs.getObject(1, LocalDateTime.class);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
});
System.out.println("-------------------");
connection.createStatement("SELECT * FROM test").execute().flatMap(it -> it.map(readable -> {
Object object = readable.get(0);
System.out.println(String.format("R2DBC Native: %s (%s)", object, object.getClass().getSimpleName()));
OffsetDateTime odt = readable.get(0, OffsetDateTime.class);
System.out.println(String.format("R2DBC OffsetDateTime: %s, Instant: %s", odt, odt.toInstant()));
System.out.println("Instant: " + readable.get(0, Instant.class));
System.out.println("LocalDateTime: " + readable.get(0, LocalDateTime.class));
return new Object();
})).blockLast();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment