Initial pass on step function

This commit is contained in:
김선우 2025-01-18 00:36:11 +09:00
parent 5fc5f650cd
commit a3a31e787c
8 changed files with 100 additions and 38 deletions

View file

@ -2,21 +2,25 @@ use crate::errors::{
LimboError, Result, LIMBO_ETC, LIMBO_FAILED_TO_PARSE_BYTE_ARRAY,
LIMBO_FAILED_TO_PREPARE_STATEMENT,
};
use crate::limbo_statement::CoreStatement;
use crate::limbo_statement::LimboStatement;
use crate::utils::{set_err_msg_and_throw_exception, utf8_byte_arr_to_str};
use jni::objects::{JByteArray, JClass, JObject};
use jni::objects::{JByteArray, JObject};
use jni::sys::jlong;
use jni::JNIEnv;
use limbo_core::Connection;
use std::rc::Rc;
#[allow(dead_code)]
#[derive(Clone)]
pub struct LimboConnection {
pub(crate) conn: Rc<limbo_core::Connection>,
pub(crate) conn: Rc<Connection>,
pub(crate) io: Rc<dyn limbo_core::IO>,
}
impl LimboConnection {
pub fn new(conn: Rc<Connection>, io: Rc<dyn limbo_core::IO>) -> Self {
LimboConnection { conn, io }
}
pub fn to_ptr(self) -> jlong {
Box::into_raw(Box::new(self)) as jlong
}
@ -78,7 +82,7 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboConnection_prepar
};
match connection.conn.prepare(sql) {
Ok(stmt) => CoreStatement::new(stmt).to_ptr(),
Ok(stmt) => LimboStatement::new(stmt).to_ptr(),
Err(e) => {
set_err_msg_and_throw_exception(
&mut env,

View file

@ -127,10 +127,10 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_connect0<'loca
}
},
};
let conn = LimboConnection {
conn: db.db.connect(),
let conn = LimboConnection::new(
db.db.connect(),
io,
};
);
conn.to_ptr()
}

View file

@ -1,34 +1,73 @@
use crate::errors::LimboError;
use crate::errors::Result;
use crate::errors::{LimboError, LIMBO_ETC};
use crate::utils::set_err_msg_and_throw_exception;
use jni::objects::{JObject, JValue};
use jni::sys::jlong;
use jni::JNIEnv;
use limbo_core::Statement;
use limbo_core::{Statement, StepResult};
pub struct CoreStatement {
pub struct LimboStatement {
pub(crate) stmt: Statement,
}
impl CoreStatement {
impl LimboStatement {
pub fn new(stmt: Statement) -> Self {
LimboStatement { stmt }
}
pub fn to_ptr(self) -> jlong {
Box::into_raw(Box::new(self)) as jlong
}
pub fn new(stmt: Statement) -> Self {
CoreStatement { stmt }
}
#[allow(dead_code)]
pub fn drop(ptr: jlong) {
let _boxed = unsafe { Box::from_raw(ptr as *mut CoreStatement) };
let _boxed = unsafe { Box::from_raw(ptr as *mut LimboStatement) };
}
}
pub fn to_statement(ptr: jlong) -> Result<&'static mut CoreStatement> {
pub fn to_limbo_statement(ptr: jlong) -> Result<&'static mut LimboStatement> {
if ptr == 0 {
Err(LimboError::InvalidConnectionPointer)
} else {
unsafe { Ok(&mut *(ptr as *mut CoreStatement)) }
unsafe { Ok(&mut *(ptr as *mut LimboStatement)) }
}
}
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_step<'local>(
mut env: JNIEnv<'local>,
obj: JObject<'local>,
stmt_ptr: jlong,
) -> JObject<'local> {
println!("statement pointer: {:?}", stmt_ptr);
let stmt = match to_limbo_statement(stmt_ptr) {
Ok(stmt) => stmt,
Err(e) => {
println!("error occurred");
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
return JObject::null();
}
};
match stmt.stmt.step() {
Ok(StepResult::Row(row)) => match row_to_obj_array(&mut env, &row) {
Ok(row) => row,
Err(e) => {
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
JObject::null()
}
},
Ok(StepResult::IO) => match env.new_object_array(0, "java/lang/Object", JObject::null()) {
Ok(row) => row.into(),
Err(e) => {
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
JObject::null()
}
},
_ => JObject::null(),
}
}