mirror of
https://github.com/allankoechke/turso.git
synced 2026-12-04 17:23:05 +00:00
columns() was the one native statement function that unwrapped the pointer check, so a zero pointer would abort the JVM instead of surfacing a Java exception like every other entry point does. Add an InvalidStatementPointer error so statement pointer failures no longer report themselves as connection pointer failures.
442 lines
13 KiB
Rust
442 lines
13 KiB
Rust
use crate::errors::{Result, SQLITE_ERROR, SQLITE_OK};
|
|
use crate::errors::{TursoError, TURSO_ETC};
|
|
use crate::turso_connection::TursoConnection;
|
|
use crate::utils::set_err_msg_and_throw_exception;
|
|
use jni::objects::{JByteArray, JObject, JObjectArray, JString, JValue};
|
|
use jni::sys::{jdouble, jint, jlong};
|
|
use jni::JNIEnv;
|
|
use std::num::NonZero;
|
|
use turso_core::{LimboError, Statement, Value};
|
|
|
|
pub const STEP_RESULT_ID_ROW: i32 = 10;
|
|
#[allow(dead_code)]
|
|
pub const STEP_RESULT_ID_IO: i32 = 20;
|
|
pub const STEP_RESULT_ID_DONE: i32 = 30;
|
|
pub const STEP_RESULT_ID_INTERRUPT: i32 = 40;
|
|
pub const STEP_RESULT_ID_BUSY: i32 = 50;
|
|
pub const STEP_RESULT_ID_ERROR: i32 = 60;
|
|
|
|
pub struct TursoStatement {
|
|
pub(crate) stmt: Statement,
|
|
pub(crate) connection: TursoConnection,
|
|
}
|
|
|
|
impl TursoStatement {
|
|
pub fn new(stmt: Statement, connection: TursoConnection) -> Self {
|
|
TursoStatement { stmt, connection }
|
|
}
|
|
|
|
#[allow(clippy::wrong_self_convention)]
|
|
pub fn to_ptr(self) -> jlong {
|
|
Box::into_raw(Box::new(self)) as jlong
|
|
}
|
|
|
|
pub fn drop(ptr: jlong) {
|
|
let _boxed = unsafe { Box::from_raw(ptr as *mut TursoStatement) };
|
|
}
|
|
}
|
|
|
|
pub fn to_turso_statement(ptr: jlong) -> Result<&'static mut TursoStatement> {
|
|
if ptr == 0 {
|
|
Err(TursoError::InvalidStatementPointer)
|
|
} else {
|
|
unsafe { Ok(&mut *(ptr as *mut TursoStatement)) }
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_step<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
) -> JObject<'local> {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
let err_msg = e.to_string();
|
|
set_err_msg_and_throw_exception(&mut env, obj, TURSO_ETC, err_msg.clone());
|
|
return to_turso_step_result_error(&mut env, &err_msg);
|
|
}
|
|
};
|
|
|
|
let result = stmt.stmt.run_one_step_blocking(|| Ok(()), || Ok(()));
|
|
|
|
match result {
|
|
Ok(Some(row)) => match row_to_obj_array(&mut env, row) {
|
|
Ok(row) => to_turso_step_result(&mut env, STEP_RESULT_ID_ROW, Some(row)),
|
|
Err(e) => {
|
|
let err_msg = e.to_string();
|
|
set_err_msg_and_throw_exception(&mut env, obj, TURSO_ETC, err_msg.clone());
|
|
to_turso_step_result_error(&mut env, &err_msg)
|
|
}
|
|
},
|
|
Ok(None) => {
|
|
// Done
|
|
to_turso_step_result(&mut env, STEP_RESULT_ID_DONE, None)
|
|
}
|
|
Err(LimboError::Interrupt) => {
|
|
to_turso_step_result(&mut env, STEP_RESULT_ID_INTERRUPT, None)
|
|
}
|
|
Err(LimboError::Busy) => to_turso_step_result(&mut env, STEP_RESULT_ID_BUSY, None),
|
|
Err(err) => {
|
|
let err_msg = err.to_string();
|
|
set_err_msg_and_throw_exception(&mut env, obj, TURSO_ETC, err_msg.clone());
|
|
to_turso_step_result_error(&mut env, &err_msg)
|
|
}
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement__1close<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
) {
|
|
if stmt_ptr == 0 {
|
|
let e = TursoError::InvalidStatementPointer;
|
|
set_err_msg_and_throw_exception(&mut env, obj, TURSO_ETC, e.to_string());
|
|
return;
|
|
}
|
|
TursoStatement::drop(stmt_ptr);
|
|
}
|
|
|
|
fn row_to_obj_array<'local>(
|
|
env: &mut JNIEnv<'local>,
|
|
row: &turso_core::Row,
|
|
) -> Result<JObject<'local>> {
|
|
let obj_array = env.new_object_array(row.len() as i32, "java/lang/Object", JObject::null())?;
|
|
|
|
for (i, value) in row.get_values().enumerate() {
|
|
let obj = match value {
|
|
turso_core::Value::Null => JObject::null(),
|
|
turso_core::Value::Numeric(turso_core::Numeric::Integer(i)) => {
|
|
env.new_object("java/lang/Long", "(J)V", &[JValue::Long(*i)])?
|
|
}
|
|
turso_core::Value::Numeric(turso_core::Numeric::Float(f)) => {
|
|
env.new_object("java/lang/Double", "(D)V", &[JValue::Double(f64::from(*f))])?
|
|
}
|
|
turso_core::Value::Text(s) => env.new_string(s.as_str())?.into(),
|
|
turso_core::Value::Blob(b) => env.byte_array_from_slice(b.as_slice())?.into(),
|
|
};
|
|
if let Err(e) = env.set_object_array_element(&obj_array, i as i32, obj) {
|
|
eprintln!("Error on parsing row: {e:?}");
|
|
}
|
|
}
|
|
|
|
Ok(obj_array.into())
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_columns<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
) -> JObject<'local> {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, TURSO_ETC, e.to_string());
|
|
return JObject::null();
|
|
}
|
|
};
|
|
let num_columns = stmt.stmt.num_columns();
|
|
let obj_arr: JObjectArray = env
|
|
.new_object_array(num_columns as i32, "java/lang/String", JObject::null())
|
|
.unwrap();
|
|
|
|
for i in 0..num_columns {
|
|
let column_name = stmt.stmt.get_column_name(i);
|
|
let str = env.new_string(column_name.into_owned()).unwrap();
|
|
env.set_object_array_element(&obj_arr, i as i32, str)
|
|
.unwrap();
|
|
}
|
|
|
|
obj_arr.into()
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_bindNull<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
position: jint,
|
|
) -> jint {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
return SQLITE_ERROR;
|
|
}
|
|
};
|
|
|
|
match stmt
|
|
.stmt
|
|
.bind_at(NonZero::new(position as usize).unwrap(), Value::Null)
|
|
{
|
|
Ok(()) => SQLITE_OK,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
SQLITE_ERROR
|
|
}
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_bindLong<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
position: jint,
|
|
value: jlong,
|
|
) -> jint {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
return SQLITE_ERROR;
|
|
}
|
|
};
|
|
|
|
match stmt.stmt.bind_at(
|
|
NonZero::new(position as usize).unwrap(),
|
|
Value::from_i64(value),
|
|
) {
|
|
Ok(()) => SQLITE_OK,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
SQLITE_ERROR
|
|
}
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_bindDouble<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
position: jint,
|
|
value: jdouble,
|
|
) -> jint {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
return SQLITE_ERROR;
|
|
}
|
|
};
|
|
|
|
match stmt.stmt.bind_at(
|
|
NonZero::new(position as usize).unwrap(),
|
|
Value::from_f64(value),
|
|
) {
|
|
Ok(()) => SQLITE_OK,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
SQLITE_ERROR
|
|
}
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_bindText<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
position: jint,
|
|
value: JString<'local>,
|
|
) -> jint {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
return SQLITE_ERROR;
|
|
}
|
|
};
|
|
|
|
let text: String = match env.get_string(&value) {
|
|
Ok(s) => s.into(),
|
|
Err(_) => return SQLITE_ERROR,
|
|
};
|
|
|
|
match stmt.stmt.bind_at(
|
|
NonZero::new(position as usize).unwrap(),
|
|
Value::build_text(text),
|
|
) {
|
|
Ok(()) => SQLITE_OK,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
SQLITE_ERROR
|
|
}
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_bindBlob<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
position: jint,
|
|
value: JByteArray<'local>,
|
|
) -> jint {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
return SQLITE_ERROR;
|
|
}
|
|
};
|
|
|
|
let blob: Vec<u8> = match env.convert_byte_array(value) {
|
|
Ok(b) => b,
|
|
Err(_) => return SQLITE_ERROR,
|
|
};
|
|
|
|
match stmt
|
|
.stmt
|
|
.bind_at(NonZero::new(position as usize).unwrap(), Value::Blob(blob))
|
|
{
|
|
Ok(()) => SQLITE_OK,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
SQLITE_ERROR
|
|
}
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_totalChanges<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
) -> jlong {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
return -1;
|
|
}
|
|
};
|
|
|
|
stmt.connection.conn.total_changes()
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_changes<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
) -> jlong {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
return -1;
|
|
}
|
|
};
|
|
|
|
stmt.connection.conn.changes()
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_parameterCount<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
) -> jint {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
return -1;
|
|
}
|
|
};
|
|
|
|
stmt.stmt.parameters_count() as jint
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_tech_turso_core_TursoStatement_reset<'local>(
|
|
mut env: JNIEnv<'local>,
|
|
obj: JObject<'local>,
|
|
stmt_ptr: jlong,
|
|
) -> jint {
|
|
let stmt = match to_turso_statement(stmt_ptr) {
|
|
Ok(stmt) => stmt,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
return -1;
|
|
}
|
|
};
|
|
|
|
match stmt.stmt.reset() {
|
|
Ok(_) => 0,
|
|
Err(e) => {
|
|
set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string());
|
|
-1
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Converts an optional `JObject` into Java's `TursoStepResult`.
|
|
///
|
|
/// This function takes an optional `JObject` and converts it into a Java object
|
|
/// of type `TursoStepResult`. The conversion is done by creating a new Java object with the
|
|
/// appropriate constructor arguments.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `env` - A mutable reference to the JNI environment.
|
|
/// * `id` - An integer representing the type of `StepResult`.
|
|
/// * `result` - An optional `JObject` that contains the result data.
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// A `JObject` representing the `TursoStepResult` in Java. If the object creation fails,
|
|
/// a null `JObject` is returned
|
|
fn to_turso_step_result<'local>(
|
|
env: &mut JNIEnv<'local>,
|
|
id: i32,
|
|
result: Option<JObject<'local>>,
|
|
) -> JObject<'local> {
|
|
let mut ctor_args = vec![JValue::Int(id)];
|
|
if let Some(res) = result {
|
|
ctor_args.push(JValue::Object(&res));
|
|
env.new_object(
|
|
"tech/turso/core/TursoStepResult",
|
|
"(I[Ljava/lang/Object;)V",
|
|
&ctor_args,
|
|
)
|
|
} else {
|
|
env.new_object("tech/turso/core/TursoStepResult", "(I)V", &ctor_args)
|
|
}
|
|
.unwrap_or_else(|_| JObject::null())
|
|
}
|
|
|
|
/// Creates an error `TursoStepResult` with an error message.
|
|
fn to_turso_step_result_error<'local>(
|
|
env: &mut JNIEnv<'local>,
|
|
error_message: &str,
|
|
) -> JObject<'local> {
|
|
// Clear any pending exception first, as JNI calls won't work with a pending exception.
|
|
// This can happen if set_err_msg_and_throw_exception was called before this function.
|
|
if env.exception_check().unwrap_or(false) {
|
|
let _ = env.exception_clear();
|
|
}
|
|
|
|
let error_str = match env.new_string(error_message) {
|
|
Ok(s) => s,
|
|
Err(_) => return JObject::null(),
|
|
};
|
|
let error_obj: JObject = error_str.into();
|
|
let ctor_args = vec![
|
|
JValue::Int(STEP_RESULT_ID_ERROR),
|
|
JValue::Object(&error_obj),
|
|
];
|
|
env.new_object(
|
|
"tech/turso/core/TursoStepResult",
|
|
"(ILjava/lang/String;)V",
|
|
&ctor_args,
|
|
)
|
|
.unwrap_or_else(|_| JObject::null())
|
|
}
|