From fcadc2f82589c06f3dceaabd95a3ad272caa1e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EC=9A=B0?= Date: Fri, 17 Jan 2025 03:55:45 +0900 Subject: [PATCH] Add connect function for creating connections from limbo db --- Cargo.lock | 4 +-- bindings/java/Cargo.toml | 4 +-- bindings/java/rs_src/limbo_db.rs | 51 ++++++++++++++++++++++++++++++++ bindings/java/rs_src/utils.rs | 4 +-- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6284a9e8c..e94c673e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1062,11 +1062,9 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" name = "java-limbo" version = "0.0.12" dependencies = [ - "anyhow", "jni", - "lazy_static", "limbo_core", - "rand", + "thiserror 2.0.9", ] [[package]] diff --git a/bindings/java/Cargo.toml b/bindings/java/Cargo.toml index 79de553ac..e3b7660c5 100644 --- a/bindings/java/Cargo.toml +++ b/bindings/java/Cargo.toml @@ -12,8 +12,6 @@ crate-type = ["cdylib"] path = "rs_src/lib.rs" [dependencies] -anyhow = "1.0" limbo_core = { path = "../../core" } jni = "0.21.1" -rand = { version = "0.8.5", features = [] } -lazy_static = "1.5.0" +thiserror = "2.0.9" diff --git a/bindings/java/rs_src/limbo_db.rs b/bindings/java/rs_src/limbo_db.rs index 4b589ba9d..e71a849ec 100644 --- a/bindings/java/rs_src/limbo_db.rs +++ b/bindings/java/rs_src/limbo_db.rs @@ -1,3 +1,4 @@ +use crate::errors::{LimboError, Result}; use jni::objects::{JByteArray, JObject}; use jni::sys::{jint, jlong}; use jni::JNIEnv; @@ -50,6 +51,31 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_openUtf8<'loca Box::into_raw(Box::new(db)) as jlong } +#[no_mangle] +pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_connect0<'local>( + mut env: JNIEnv<'local>, + obj: JObject<'local>, + db_pointer: jlong, +) -> jlong { + let db = match to_db(db_pointer) { + Ok(db) => db, + Err(e) => { + set_err_msg_and_throw_exception(&mut env, obj, ERROR_CODE_ETC, e.to_string()); + return 0; + } + }; + + Box::into_raw(Box::new(db.connect())) as jlong +} + +fn to_db(db_pointer: jlong) -> Result<&'static mut Arc> { + if db_pointer == 0 { + Err(LimboError::InvalidDatabasePointer) + } else { + unsafe { Ok(&mut *(db_pointer as *mut Arc)) } + } +} + #[no_mangle] pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_throwJavaException<'local>( mut env: JNIEnv<'local>, @@ -64,6 +90,31 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_throwJavaExcep ); } +fn utf8_byte_arr_to_str(env: &JNIEnv, bytes: JByteArray) -> Result { + let bytes = env + .convert_byte_array(bytes) + .map_err(|e| LimboError::CustomError("Failed to retrieve bytes".to_string()))?; + let str = String::from_utf8(bytes).map_err(|e| { + LimboError::CustomError("Failed to convert utf8 byte array into string".to_string()) + })?; + Ok(str) +} + +/// Sets the error message and throws a Java exception. +/// +/// This function converts the provided error message to a byte array and calls the +/// `throwLimboException` method on the provided Java object to throw an exception. +/// +/// # Parameters +/// - `env`: The JNI environment. +/// - `obj`: The Java object on which the exception will be thrown. +/// - `err_code`: The error code corresponding to the exception. Refer to `org.github.tursodatabase.core.Codes` for the list of error codes. +/// - `err_msg`: The error message to be included in the exception. +/// +/// # Example +/// ```rust +/// set_err_msg_and_throw_exception(env, obj, Codes::SQLITE_ERROR, "An error occurred".to_string()); +/// ``` fn set_err_msg_and_throw_exception<'local>( env: &mut JNIEnv<'local>, obj: JObject<'local>, diff --git a/bindings/java/rs_src/utils.rs b/bindings/java/rs_src/utils.rs index 4fde084fc..8f50be554 100644 --- a/bindings/java/rs_src/utils.rs +++ b/bindings/java/rs_src/utils.rs @@ -1,11 +1,11 @@ -use crate::errors::CustomError; +use crate::errors::LimboError; use jni::objects::{JObject, JValue}; use jni::JNIEnv; pub(crate) fn row_to_obj_array<'local>( env: &mut JNIEnv<'local>, row: &limbo_core::Row, -) -> Result, CustomError> { +) -> Result, LimboError> { let obj_array = env.new_object_array(row.values.len() as i32, "java/lang/Object", JObject::null())?;