From 91c814310bb3fbb8cfa2c95dd17afde20978ceba Mon Sep 17 00:00:00 2001 From: Andrew Guibert Date: Fri, 25 Sep 2020 22:52:49 -0500 Subject: [PATCH] Coerce row values to string for Row#getString --- .../vertx/db2client/QueryVariationsTest.java | 41 +++++++++++++++++++ .../main/java/io/vertx/sqlclient/Tuple.java | 20 +++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/vertx-db2-client/src/test/java/io/vertx/db2client/QueryVariationsTest.java b/vertx-db2-client/src/test/java/io/vertx/db2client/QueryVariationsTest.java index 726310cb1..6e09cc803 100644 --- a/vertx-db2-client/src/test/java/io/vertx/db2client/QueryVariationsTest.java +++ b/vertx-db2-client/src/test/java/io/vertx/db2client/QueryVariationsTest.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.function.Consumer; +import java.util.regex.Pattern; import org.junit.Test; import org.junit.runner.RunWith; @@ -44,6 +45,46 @@ public void testRenamedColumns(TestContext ctx) { })); })); } + + private void verifyTimestamp(TestContext ctx, String tStamp) { + // ex: 2020-09-26T03:10:52.263752 + ctx.assertTrue(Pattern.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.*", tStamp), + "Expected to get a timestamp matching the format YYYY-MM-DDThh:mm:ss.nnnnnn but got: " + tStamp); + } + + @Test + public void testCurrentTimestamp(TestContext ctx) { + connect(ctx.asyncAssertSuccess(conn -> { + conn.query("SELECT CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1").execute( + ctx.asyncAssertSuccess(rowSet -> { + ctx.assertEquals(1, rowSet.size()); + RowIterator rows = rowSet.iterator(); + ctx.assertTrue(rows.hasNext()); + Row row = rows.next(); + // Should be able to read the row as a LocalDateTime or String + verifyTimestamp(ctx, row.getLocalDateTime(0).toString()); + verifyTimestamp(ctx, row.getString(0)); + conn.close(); + })); + })); + } + + @Test + public void testCurrentTimestampPrepared(TestContext ctx) { + connect(ctx.asyncAssertSuccess(conn -> { + conn.preparedQuery("SELECT CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1").execute( + ctx.asyncAssertSuccess(rowSet -> { + ctx.assertEquals(1, rowSet.size()); + RowIterator rows = rowSet.iterator(); + ctx.assertTrue(rows.hasNext()); + Row row = rows.next(); + // Should be able to read the row as a LocalDateTime or String + verifyTimestamp(ctx, row.getLocalDateTime(0).toString()); + verifyTimestamp(ctx, row.getString(0)); + conn.close(); + })); + })); + } @Test public void testSubquery(TestContext ctx) { diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java index 5bc225a73..6aea24a27 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java @@ -370,7 +370,7 @@ default String getString(int pos) { } else if (val instanceof Enum) { return ((Enum) val).name(); } else { - throw new ClassCastException(); + return String.valueOf(val); } } @@ -474,7 +474,14 @@ default LocalTime getLocalTime(int pos) { */ @GenIgnore(GenIgnore.PERMITTED_TYPE) default LocalDateTime getLocalDateTime(int pos) { - return (LocalDateTime) getValue(pos); + Object val = getValue(pos); + if (val == null) { + return null; + } else if (val instanceof LocalDateTime) { + return (LocalDateTime) val; + } else { + return (LocalDateTime) val; // Throw CCE + } } /** @@ -508,7 +515,14 @@ default OffsetTime getOffsetTime(int pos) { */ @GenIgnore(GenIgnore.PERMITTED_TYPE) default OffsetDateTime getOffsetDateTime(int pos) { - return (OffsetDateTime) getValue(pos); + Object val = getValue(pos); + if (val == null) { + return null; + } else if (val instanceof OffsetDateTime) { + return (OffsetDateTime)val; + } else { + return (OffsetDateTime) val; // Throw CCE + } } /**