Skip to content

Commit

Permalink
Merge pull request #60 from Stongtong/main
Browse files Browse the repository at this point in the history
DuckDBResultSet bug fix
  • Loading branch information
Mause authored Jul 29, 2024
2 parents 0334cd0 + f1c87b5 commit 46d8ea4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/main/java/org/duckdb/DuckDBResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.sql.Struct;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
Expand Down Expand Up @@ -1249,6 +1250,16 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
} else {
throw new SQLException("Can't convert value to Timestamp " + type.toString());
}
} else if (type == LocalDate.class) {
if (sqlType == DuckDBColumnType.DATE) {
final Date date = getDate(columnIndex);
if (date == null) {
return null;
}
return type.cast(date.toLocalDate());
} else {
throw new SQLException("Can't convert value to LocalDate " + type.toString());
}
} else if (type == LocalDateTime.class) {
if (isTimestamp(sqlType)) {
return type.cast(getLocalDateTime(columnIndex));
Expand Down Expand Up @@ -1286,7 +1297,15 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
}

public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
throw new SQLFeatureNotSupportedException("getObject");
if (type == null) {
throw new SQLException("type is null");
}
if (columnLabel == null || columnLabel.isEmpty()) {
throw new SQLException("columnLabel is null");
}

int index = findColumn(columnLabel);
return getObject(index, type);
}

@Override
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/duckdb/TestDuckDBJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,33 @@ public static void test_duckdb_localdatetime() throws Exception {
conn.close();
}

public static void test_duckdb_localdate() throws Exception {
Connection conn = DriverManager.getConnection(JDBC_URL);
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE x (dt Date)");

LocalDate ld = LocalDate.of(2024, 7, 22);
Date date = Date.valueOf(ld);

PreparedStatement ps1 = conn.prepareStatement("INSERT INTO x VALUES (?)");
ps1.setObject(1, date);
ps1.execute();
ps1.close();

PreparedStatement ps2 = conn.prepareStatement("SELECT * FROM x");
ResultSet rs2 = ps2.executeQuery();

rs2.next();
assertEquals(rs2.getDate(1), rs2.getObject(1, Date.class));
assertEquals(rs2.getObject(1, LocalDate.class), ld);
assertEquals(rs2.getObject("dt", LocalDate.class), ld);

rs2.close();
ps2.close();
stmt.close();
conn.close();
}

public static void test_duckdb_getObject_with_class() throws Exception {
Connection conn = DriverManager.getConnection(JDBC_URL);
Statement stmt = conn.createStatement();
Expand Down

0 comments on commit 46d8ea4

Please sign in to comment.