Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coerce row values to string for Row#getString #782

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Row> 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<Row> 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) {
Expand Down
20 changes: 17 additions & 3 deletions vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
}
}

/**
Expand Down Expand Up @@ -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
}
}

/**
Expand Down