Implement rust side connect and prepare function

This commit is contained in:
김선우
2025-01-17 04:25:23 +09:00
parent 7e78ec448b
commit 0819963b2f
7 changed files with 167 additions and 323 deletions

View File

@@ -1,14 +1,19 @@
use crate::cursor::Cursor;
use jni::objects::JClass;
use crate::errors::{
LimboError, Result, LIMBO_ETC, LIMBO_FAILED_TO_PARSE_BYTE_ARRAY,
LIMBO_FAILED_TO_PREPARE_STATEMENT,
};
use crate::utils::utf8_byte_arr_to_str;
use jni::objects::{JByteArray, JClass, JObject};
use jni::sys::jlong;
use jni::JNIEnv;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
#[allow(dead_code)]
#[derive(Clone)]
pub struct Connection {
pub(crate) conn: Arc<Mutex<Rc<limbo_core::Connection>>>,
pub(crate) io: Arc<limbo_core::PlatformIO>,
pub(crate) conn: Rc<limbo_core::Connection>,
pub(crate) io: Arc<dyn limbo_core::IO>,
}
/// Returns a pointer to a `Cursor` object.
@@ -26,20 +31,45 @@ pub struct Connection {
///
/// A `jlong` representing the pointer to the newly created `Cursor` object.
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_limbo_Connection_cursor<'local>(
_env: JNIEnv<'local>,
_class: JClass<'local>,
pub extern "system" fn Java_org_github_tursodatabase_limbo_LimboConnection_prepareUtf8<'local>(
mut env: JNIEnv<'local>,
obj: JObject<'local>,
connection_ptr: jlong,
sql_bytes: JByteArray<'local>,
) -> jlong {
let connection = to_connection(connection_ptr);
let cursor = Cursor {
array_size: 1,
conn: connection.clone(),
description: None,
rowcount: -1,
smt: None,
let connection = match to_connection(connection_ptr) {
Ok(conn) => conn,
Err(e) => {
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
return 0;
}
};
Box::into_raw(Box::new(cursor)) as jlong
let sql = match utf8_byte_arr_to_str(&env, sql_bytes) {
Ok(sql) => sql,
Err(e) => {
set_err_msg_and_throw_exception(
&mut env,
obj,
LIMBO_FAILED_TO_PARSE_BYTE_ARRAY,
e.to_string(),
);
return 0;
}
};
match connection.conn.prepare(sql) {
Ok(stmt) => Box::into_raw(Box::new(stmt)) as jlong,
Err(e) => {
set_err_msg_and_throw_exception(
&mut env,
obj,
LIMBO_FAILED_TO_PREPARE_STATEMENT,
e.to_string(),
);
0
}
}
}
/// Closes the connection and releases the associated resources.
@@ -61,24 +91,34 @@ pub unsafe extern "system" fn Java_org_github_tursodatabase_limbo_Connection_clo
let _boxed_connection = Box::from_raw(connection_ptr as *mut Connection);
}
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_limbo_Connection_commit<'local>(
_env: &mut JNIEnv<'local>,
_class: JClass<'local>,
_connection_id: jlong,
) {
unimplemented!()
fn to_connection(connection_ptr: jlong) -> Result<&'static mut Rc<Connection>> {
if connection_ptr == 0 {
Err(LimboError::InvalidConnectionPointer)
} else {
unsafe { Ok(&mut *(connection_ptr as *mut Rc<Connection>)) }
}
}
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_limbo_Connection_rollback<'local>(
_env: &mut JNIEnv<'local>,
_class: JClass<'local>,
_connection_id: jlong,
fn set_err_msg_and_throw_exception<'local>(
env: &mut JNIEnv<'local>,
obj: JObject<'local>,
err_code: i32,
err_msg: String,
) {
unimplemented!()
}
fn to_connection(connection_ptr: jlong) -> &'static mut Connection {
unsafe { &mut *(connection_ptr as *mut Connection) }
let error_message_bytes = env
.byte_array_from_slice(err_msg.as_bytes())
.expect("Failed to convert to byte array");
match env.call_method(
obj,
"throwLimboException",
"(I[B)V",
&[err_code.into(), (&error_message_bytes).into()],
) {
Ok(_) => {
// do nothing because above method will always return Err
}
Err(_e) => {
// do nothing because our java app will handle Err
}
}
}