@@ -105,6 +105,13 @@ static jmethodID J_Object_toString;
105105
106106static jclass J_DuckDBTime;
107107
108+ static jobject J_ProfilerPrintFormat_QUERY_TREE;
109+ static jobject J_ProfilerPrintFormat_JSON;
110+ static jobject J_ProfilerPrintFormat_QUERY_TREE_OPTIMIZER;
111+ static jobject J_ProfilerPrintFormat_NO_OUTPUT;
112+ static jobject J_ProfilerPrintFormat_HTML;
113+ static jobject J_ProfilerPrintFormat_GRAPHVIZ;
114+
108115void ThrowJNI (JNIEnv *env, const char *message) {
109116 D_ASSERT (J_SQLException);
110117 env->ThrowNew (J_SQLException, message);
@@ -123,6 +130,12 @@ static jclass GetClassRef(JNIEnv *env, const string &name) {
123130 return globalRef;
124131}
125132
133+ static jobject GetObjectStaticField (JNIEnv *env, jclass clazz, const char *name, const char *sig) {
134+ auto field = env->GetStaticFieldID (clazz, name, sig);
135+ auto obj = env->GetStaticObjectField (clazz, field);
136+ return env->NewGlobalRef (obj);
137+ }
138+
126139JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM *vm, void *reserved) {
127140 // Get JNIEnv from vm
128141 JNIEnv *env;
@@ -291,6 +304,21 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
291304 J_Object_toString = env->GetMethodID (tmpLocalRef, " toString" , " ()Ljava/lang/String;" );
292305 env->DeleteLocalRef (tmpLocalRef);
293306
307+ tmpLocalRef = env->FindClass (" org/duckdb/ProfilerPrintFormat" );
308+
309+ J_ProfilerPrintFormat_QUERY_TREE =
310+ GetObjectStaticField (env, tmpLocalRef, " QUERY_TREE" , " Lorg/duckdb/ProfilerPrintFormat;" );
311+ J_ProfilerPrintFormat_JSON = GetObjectStaticField (env, tmpLocalRef, " JSON" , " Lorg/duckdb/ProfilerPrintFormat;" );
312+ J_ProfilerPrintFormat_QUERY_TREE_OPTIMIZER =
313+ GetObjectStaticField (env, tmpLocalRef, " QUERY_TREE_OPTIMIZER" , " Lorg/duckdb/ProfilerPrintFormat;" );
314+ J_ProfilerPrintFormat_NO_OUTPUT =
315+ GetObjectStaticField (env, tmpLocalRef, " NO_OUTPUT" , " Lorg/duckdb/ProfilerPrintFormat;" );
316+ J_ProfilerPrintFormat_HTML = GetObjectStaticField (env, tmpLocalRef, " HTML" , " Lorg/duckdb/ProfilerPrintFormat;" );
317+ J_ProfilerPrintFormat_GRAPHVIZ =
318+ GetObjectStaticField (env, tmpLocalRef, " GRAPHVIZ" , " Lorg/duckdb/ProfilerPrintFormat;" );
319+
320+ env->DeleteLocalRef (tmpLocalRef);
321+
294322 return JNI_VERSION;
295323}
296324
@@ -317,6 +345,12 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
317345 env->DeleteGlobalRef (J_DuckResultSetMeta);
318346 env->DeleteGlobalRef (J_DuckVector);
319347 env->DeleteGlobalRef (J_ByteBuffer);
348+ env->DeleteGlobalRef (J_ProfilerPrintFormat_QUERY_TREE);
349+ env->DeleteGlobalRef (J_ProfilerPrintFormat_JSON);
350+ env->DeleteGlobalRef (J_ProfilerPrintFormat_QUERY_TREE_OPTIMIZER);
351+ env->DeleteGlobalRef (J_ProfilerPrintFormat_NO_OUTPUT);
352+ env->DeleteGlobalRef (J_ProfilerPrintFormat_HTML);
353+ env->DeleteGlobalRef (J_ProfilerPrintFormat_GRAPHVIZ);
320354
321355 for (auto &clazz : toFree) {
322356 env->DeleteGlobalRef (clazz);
@@ -1269,3 +1303,34 @@ void _duckdb_jdbc_create_extension_type(JNIEnv *env, jclass, jobject conn_buf) {
12691303 byte_test_type_type.SetAlias (" byte_test_type" );
12701304 ExtensionUtil::RegisterType (db_instance, " byte_test_type" , byte_test_type_type);
12711305}
1306+
1307+ static ProfilerPrintFormat GetProfilerPrintFormat (JNIEnv *env, jobject format) {
1308+ if (env->IsSameObject (format, J_ProfilerPrintFormat_QUERY_TREE)) {
1309+ return ProfilerPrintFormat::QUERY_TREE;
1310+ }
1311+ if (env->IsSameObject (format, J_ProfilerPrintFormat_JSON)) {
1312+ return ProfilerPrintFormat::JSON;
1313+ }
1314+ if (env->IsSameObject (format, J_ProfilerPrintFormat_QUERY_TREE_OPTIMIZER)) {
1315+ return ProfilerPrintFormat::QUERY_TREE_OPTIMIZER;
1316+ }
1317+ if (env->IsSameObject (format, J_ProfilerPrintFormat_NO_OUTPUT)) {
1318+ return ProfilerPrintFormat::NO_OUTPUT;
1319+ }
1320+ if (env->IsSameObject (format, J_ProfilerPrintFormat_HTML)) {
1321+ return ProfilerPrintFormat::HTML;
1322+ }
1323+ if (env->IsSameObject (format, J_ProfilerPrintFormat_GRAPHVIZ)) {
1324+ return ProfilerPrintFormat::GRAPHVIZ;
1325+ }
1326+ }
1327+
1328+ jstring _duckdb_jdbc_get_profiling_information (JNIEnv *env, jclass, jobject conn_ref_buf, jobject j_format) {
1329+ auto connection = get_connection (env, conn_ref_buf);
1330+ if (!connection) {
1331+ throw InvalidInputException (" Invalid connection" );
1332+ }
1333+ auto format = GetProfilerPrintFormat (env, j_format);
1334+ auto profiling_info = connection->GetProfilingInformation (format);
1335+ return env->NewStringUTF (profiling_info.c_str ());
1336+ }
0 commit comments