|
| 1 | +#include "chdb_jni.h" |
| 2 | +#include "chdb.h" |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +local_result_v2 * queryToBuffer( |
| 8 | + const std::string & queryStr, |
| 9 | + const std::string & output_format = "CSV", |
| 10 | + const std::string & path = {}, |
| 11 | + const std::string & udfPath = {}) |
| 12 | +{ |
| 13 | + std::vector<std::string> argv = {"clickhouse", "--multiquery"}; |
| 14 | + |
| 15 | + if (output_format == "Debug" || output_format == "debug") |
| 16 | + { |
| 17 | + argv.push_back("--verbose"); |
| 18 | + argv.push_back("--log-level=trace"); |
| 19 | + argv.push_back("--output-format=CSV"); |
| 20 | + } |
| 21 | + else |
| 22 | + { |
| 23 | + argv.push_back("--output-format=" + output_format); |
| 24 | + } |
| 25 | + |
| 26 | + if (!path.empty()) |
| 27 | + { |
| 28 | + argv.push_back("--path=" + path); |
| 29 | + } |
| 30 | + |
| 31 | + argv.push_back("--query=" + queryStr); |
| 32 | + |
| 33 | + if (!udfPath.empty()) |
| 34 | + { |
| 35 | + argv.push_back("--"); |
| 36 | + argv.push_back("--user_scripts_path=" + udfPath); |
| 37 | + argv.push_back("--user_defined_executable_functions_config=" + udfPath + "/*.xml"); |
| 38 | + } |
| 39 | + |
| 40 | + std::vector<char *> argv_char; |
| 41 | + for (auto & arg : argv) |
| 42 | + { |
| 43 | + argv_char.push_back(&arg[0]); |
| 44 | + } |
| 45 | + argv_char.push_back(nullptr); |
| 46 | + |
| 47 | + return query_stable_v2(static_cast<int>(argv_char.size() - 1), argv_char.data()); |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +JNIEXPORT jobject JNICALL Java_org_chdb_jdbc_ChdbJniUtil_executeQuery(JNIEnv *env, jclass clazz, jstring query) { |
| 52 | + // 1. Convert Java String to C++ string |
| 53 | + |
| 54 | + std::cout << "call func: ChdbJniUtil_executeQuery!" << std::endl; |
| 55 | + |
| 56 | + const char *queryStr = env->GetStringUTFChars(query, nullptr); |
| 57 | + if (queryStr == nullptr) { |
| 58 | + std::cerr << "Error: Failed to convert Java string to C++ string" << std::endl; |
| 59 | + return nullptr; |
| 60 | + } |
| 61 | + |
| 62 | + // 2. Call the native query function |
| 63 | + local_result_v2 *result = queryToBuffer(queryStr); |
| 64 | + |
| 65 | + // 3. Release the Java string resources |
| 66 | + env->ReleaseStringUTFChars(query, queryStr); |
| 67 | + |
| 68 | + // 4. Check if the result is null (indicates an error) |
| 69 | + if (result == nullptr) { |
| 70 | + std::cerr << "Error: result is null" << std::endl; |
| 71 | + return nullptr; |
| 72 | + } |
| 73 | + |
| 74 | + // 5. Find the Java class and its constructor |
| 75 | + jclass resultClass = env->FindClass("org/chdb/jdbc/LocalResultV2"); |
| 76 | + if (resultClass == nullptr) { |
| 77 | + std::cerr << "Error: resultClass is null" << std::endl; |
| 78 | + free_result_v2(result); // Ensure to free the result even on error |
| 79 | + return nullptr; |
| 80 | + } |
| 81 | + |
| 82 | + jmethodID constructor = env->GetMethodID(resultClass, "<init>", "(Ljava/nio/ByteBuffer;JJDLjava/lang/String;)V"); |
| 83 | + if (constructor == nullptr) { |
| 84 | + std::cerr << "Error: constructor is null" << std::endl; |
| 85 | + free_result_v2(result); // Ensure to free the result even on error |
| 86 | + return nullptr; |
| 87 | + } |
| 88 | + |
| 89 | + // 6. Create a direct ByteBuffer for the result buffer |
| 90 | + jobject buffer = env->NewDirectByteBuffer(result->buf, result->len); |
| 91 | + if (buffer == nullptr) { |
| 92 | + std::cerr << "Error: Failed to create ByteBuffer" << std::endl; |
| 93 | + free_result_v2(result); |
| 94 | + return nullptr; |
| 95 | + } |
| 96 | + |
| 97 | + // 7. Create the Java String for the error message, if present |
| 98 | + jstring errorMessage = result->error_message ? env->NewStringUTF(result->error_message) : nullptr; |
| 99 | + |
| 100 | + // 8. Create a new Java object to hold the result |
| 101 | + jobject resultObj = env->NewObject(resultClass, constructor, buffer, result->rows_read, result->bytes_read, result->elapsed, errorMessage); |
| 102 | + if (resultObj == nullptr) { |
| 103 | + std::cerr << "Error: Failed to create result object" << std::endl; |
| 104 | + free_result_v2(result); |
| 105 | + return nullptr; |
| 106 | + } |
| 107 | + |
| 108 | + // 9. Free the native result structure |
| 109 | + free_result_v2(result); |
| 110 | + |
| 111 | + // 10. Return the Java object |
| 112 | + return resultObj; |
| 113 | +} |
0 commit comments