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

feat: setTime #4

Merged
merged 1 commit into from
May 2, 2024
Merged
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
7 changes: 7 additions & 0 deletions src/jni/duckdb_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ static jmethodID J_DuckDBDate_getDaysSinceEpoch;

static jmethodID J_Object_toString;

static jclass J_DuckDBTime;

void ThrowJNI(JNIEnv *env, const char *message) {
D_ASSERT(J_SQLException);
env->ThrowNew(J_SQLException, message);
Expand Down Expand Up @@ -170,6 +172,8 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
J_DuckDBDate_getDaysSinceEpoch = env->GetMethodID(J_DuckDBDate, "getDaysSinceEpoch", "()J");
D_ASSERT(J_DuckDBDate_getDaysSinceEpoch);

J_DuckDBTime = GetClassRef(env, "org/duckdb/DuckDBTime");

tmpLocalRef = env->FindClass("java/math/BigDecimal");
J_Decimal = (jclass)env->NewGlobalRef(tmpLocalRef);
env->DeleteLocalRef(tmpLocalRef);
Expand Down Expand Up @@ -595,6 +599,9 @@ jobject _duckdb_jdbc_execute(JNIEnv *env, jclass, jobject stmt_ref_buf, jobjectA
duckdb_params.push_back(
Value::DATE((date_t)env->CallLongMethod(param, J_DuckDBDate_getDaysSinceEpoch)));

} else if (env->IsInstanceOf(param, J_DuckDBTime)) {
duckdb_params.push_back(Value::TIME((dtime_t)env->CallLongMethod(param, J_Timestamp_getMicrosEpoch)));

} else if (env->IsInstanceOf(param, J_Timestamp)) {
duckdb_params.push_back(
Value::TIMESTAMP((timestamp_t)env->CallLongMethod(param, J_Timestamp_getMicrosEpoch)));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/duckdb/DuckDBPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ public void setObject(int parameterIndex, Object x) throws SQLException {
x = new DuckDBTimestampTZ((OffsetDateTime) x);
} else if (x instanceof Date) {
x = new DuckDBDate((Date) x);
} else if (x instanceof Time) {
x = new DuckDBTime((Time) x);
}
params[parameterIndex - 1] = x;
}
Expand Down Expand Up @@ -609,7 +611,7 @@ public void setDate(int parameterIndex, Date x) throws SQLException {

@Override
public void setTime(int parameterIndex, Time x) throws SQLException {
throw new SQLFeatureNotSupportedException("setTime");
setObject(parameterIndex, x);
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/duckdb/DuckDBTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.duckdb;

import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

public class DuckDBTime extends DuckDBTimestamp {
public DuckDBTime(Time time) {
super(TimeUnit.MILLISECONDS.toMicros(time.getTime()));
}
}
12 changes: 12 additions & 0 deletions src/test/java/org/duckdb/TestDuckDBJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,18 @@ public static void test_set_date() throws Exception {
}
}

public static void test_set_time() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL);
PreparedStatement stmt = conn.prepareStatement("SELECT ?::VARCHAR")) {
Time time = Time.valueOf("12:40:00");
stmt.setTime(1, time);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
assertEquals(rs.getTime(1), time);
}
}
}

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