Implement bindXXX functions on rust and java side

This commit is contained in:
김선우
2025-02-07 11:25:23 +09:00
parent d574e2c277
commit 21d6f33c6b
3 changed files with 306 additions and 16 deletions

View File

@@ -1,11 +1,12 @@
use crate::errors::Result;
use crate::errors::{LimboError, LIMBO_ETC};
use crate::errors::{Result, SQLITE_ERROR, SQLITE_OK};
use crate::limbo_connection::LimboConnection;
use crate::utils::set_err_msg_and_throw_exception;
use jni::objects::{JObject, JObjectArray, JValue};
use jni::sys::jlong;
use jni::objects::{JByteArray, JObject, JObjectArray, JString, JValue};
use jni::sys::{jdouble, jint, jlong};
use jni::JNIEnv;
use limbo_core::{Statement, StepResult};
use limbo_core::{Statement, StepResult, Value};
use std::num::NonZero;
pub const STEP_RESULT_ID_ROW: i32 = 10;
#[allow(dead_code)]
@@ -126,26 +127,150 @@ fn row_to_obj_array<'local>(
}
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_columnNames<'local>(
pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_columns<'local>(
mut env: JNIEnv<'local>,
_obj: JObject<'local>,
stmt_ptr: jlong,
) -> JObject<'local> {
let stmt = to_limbo_statement(stmt_ptr).unwrap();
let columns = stmt.stmt.columns();
let num_columns = stmt.stmt.num_columns();
let obj_arr: JObjectArray = env
.new_object_array(columns.len() as i32, "java/lang/String", JObject::null())
.new_object_array(num_columns as i32, "java/lang/String", JObject::null())
.unwrap();
for (i, value) in columns.iter().enumerate() {
let str = env.new_string(value).unwrap();
env.set_object_array_element(&obj_arr, i as i32, str)
.unwrap();
for i in 0..num_columns {
if let Some(column_name) = stmt.stmt.get_column_name(i) {
let str = env.new_string(column_name).unwrap();
env.set_object_array_element(&obj_arr, i as i32, str)
.unwrap();
}
}
obj_arr.into()
}
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindNull<'local>(
mut env: JNIEnv<'local>,
obj: JObject<'local>,
stmt_ptr: jlong,
position: jint,
) -> jint {
let stmt = match to_limbo_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;
}
};
stmt.stmt
.bind_at(NonZero::new(position as usize).unwrap(), Value::Null);
SQLITE_OK
}
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindLong<'local>(
mut env: JNIEnv<'local>,
obj: JObject<'local>,
stmt_ptr: jlong,
position: jint,
value: jlong,
) -> jint {
let stmt = match to_limbo_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;
}
};
stmt.stmt.bind_at(
NonZero::new(position as usize).unwrap(),
Value::Integer(value),
);
SQLITE_OK
}
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindDouble<'local>(
mut env: JNIEnv<'local>,
obj: JObject<'local>,
stmt_ptr: jlong,
position: jint,
value: jdouble,
) -> jint {
let stmt = match to_limbo_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;
}
};
stmt.stmt.bind_at(
NonZero::new(position as usize).unwrap(),
Value::Float(value),
);
SQLITE_OK
}
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindText<'local>(
mut env: JNIEnv<'local>,
obj: JObject<'local>,
stmt_ptr: jlong,
position: jint,
value: JString<'local>,
) -> jint {
let stmt = match to_limbo_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,
};
stmt.stmt.bind_at(
NonZero::new(position as usize).unwrap(),
Value::Text(text.as_str()),
);
SQLITE_OK
}
#[no_mangle]
pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindBlob<'local>(
mut env: JNIEnv<'local>,
obj: JObject<'local>,
stmt_ptr: jlong,
position: jint,
value: JByteArray<'local>,
) -> jint {
let stmt = match to_limbo_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,
};
stmt.stmt.bind_at(
NonZero::new(position as usize).unwrap(),
Value::Blob(blob.as_ref()),
);
SQLITE_OK
}
/// Converts an optional `JObject` into Java's `LimboStepResult`.
///
/// This function takes an optional `JObject` and converts it into a Java object