diff --git a/bindings/java/rs_src/limbo_statement.rs b/bindings/java/rs_src/limbo_statement.rs index c2746dfb9..dd1feb55a 100644 --- a/bindings/java/rs_src/limbo_statement.rs +++ b/bindings/java/rs_src/limbo_statement.rs @@ -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 = 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 diff --git a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboStatement.java b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboStatement.java index ab55ef1a8..bb3bb47e0 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboStatement.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboStatement.java @@ -97,14 +97,106 @@ public class LimboStatement { * @throws SQLException if a database access error occurs while retrieving column names */ public void initializeColumnMetadata() throws SQLException { - final String[] columnNames = this.columnNames(statementPointer); + final String[] columnNames = this.columns(statementPointer); if (columnNames != null) { this.resultSet.setColumnNames(columnNames); } } @Nullable - private native String[] columnNames(long statementPointer) throws SQLException; + private native String[] columns(long statementPointer) throws SQLException; + + /** + * Binds a NULL value to the prepared statement at the specified position. + * + * @param position The index of the SQL parameter to be set to NULL. + * @return Result Codes + * @throws SQLException If a database access error occurs. + */ + public int bindNull(int position) throws SQLException { + final int result = bindNull(statementPointer, position); + if (result != 0) { + throw new SQLException("Exception while binding NULL value at position " + position); + } + return result; + } + + private native int bindNull(long statementPointer, int position) throws SQLException; + + /** + * Binds a long value to the prepared statement at the specified position. + * + * @param position The index of the SQL parameter to be set. + * @param value The value to bind to the parameter. + * @return Result Codes + * @throws SQLException If a database access error occurs. + */ + public int bindLong(int position, long value) throws SQLException { + final int result = bindLong(statementPointer, position, value); + if (result != 0) { + throw new SQLException("Exception while binding long value at position " + position); + } + return result; + } + + private native int bindLong(long statementPointer, int position, long value) throws SQLException; + + /** + * Binds a double value to the prepared statement at the specified position. + * + * @param position The index of the SQL parameter to be set. + * @param value The value to bind to the parameter. + * @return Result Codes + * @throws SQLException If a database access error occurs. + */ + public int bindDouble(int position, double value) throws SQLException { + final int result = bindDouble(statementPointer, position, value); + if (result != 0) { + throw new SQLException("Exception while binding double value at position " + position); + } + return result; + } + + private native int bindDouble(long statementPointer, int position, double value) + throws SQLException; + + /** + * Binds a text value to the prepared statement at the specified position. + * + * @param position The index of the SQL parameter to be set. + * @param value The value to bind to the parameter. + * @return Result Codes + * @throws SQLException If a database access error occurs. + */ + public int bindText(int position, String value) throws SQLException { + final int result = bindText(statementPointer, position, value); + if (result != 0) { + throw new SQLException("Exception while binding text value at position " + position); + } + return result; + } + + private native int bindText(long statementPointer, int position, String value) + throws SQLException; + + /** + * Binds a blob value to the prepared statement at the specified position. + * + * @param position The index of the SQL parameter to be set. + * @param value The value to bind to the parameter. + * @return Result Codes + * @throws SQLException If a database access error occurs. + */ + public int bindBlob(int position, byte[] value) throws SQLException { + final int result = bindBlob(statementPointer, position, value); + if (result != 0) { + throw new SQLException("Exception while binding blob value at position " + position); + } + return result; + } + + private native int bindBlob(long statementPointer, int position, byte[] value) + throws SQLException; /** * Checks if the statement is closed. diff --git a/bindings/java/src/test/java/org/github/tursodatabase/core/LimboStatementTest.java b/bindings/java/src/test/java/org/github/tursodatabase/core/LimboStatementTest.java index a4230d3eb..cd601643c 100644 --- a/bindings/java/src/test/java/org/github/tursodatabase/core/LimboStatementTest.java +++ b/bindings/java/src/test/java/org/github/tursodatabase/core/LimboStatementTest.java @@ -1,7 +1,9 @@ package org.github.tursodatabase.core; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Properties; @@ -41,9 +43,80 @@ class LimboStatementTest { final LimboResultSet rs = stmt.getResultSet(); final String[] columnNames = rs.getColumnNames(); - assertEquals(columnNames[0], "name"); - assertEquals(columnNames[1], "age"); - assertEquals(columnNames[2], "country"); + assertEquals("name", columnNames[0]); + assertEquals("age", columnNames[1]); + assertEquals("country", columnNames[2]); + } + + @Test + void test_bindNull() throws Exception { + runSql("CREATE TABLE test (col1 TEXT);"); + LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);"); + stmt.bindNull(1); + stmt.execute(); + stmt.close(); + + LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;"); + selectStmt.execute(); + assertNull(selectStmt.getResultSet().get(1)); + selectStmt.close(); + } + + @Test + void test_bindLong() throws Exception { + runSql("CREATE TABLE test (col1 BIGINT);"); + LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);"); + stmt.bindLong(1, 123456789L); + stmt.execute(); + stmt.close(); + + LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;"); + selectStmt.execute(); + assertEquals(123456789L, selectStmt.getResultSet().get(1)); + selectStmt.close(); + } + + @Test + void test_bindDouble() throws Exception { + runSql("CREATE TABLE test (col1 DOUBLE);"); + LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);"); + stmt.bindDouble(1, 3.14); + stmt.execute(); + stmt.close(); + + LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;"); + selectStmt.execute(); + assertEquals(3.14, selectStmt.getResultSet().get(1)); + selectStmt.close(); + } + + @Test + void test_bindText() throws Exception { + runSql("CREATE TABLE test (col1 TEXT);"); + LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);"); + stmt.bindText(1, "hello"); + stmt.execute(); + stmt.close(); + + LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;"); + selectStmt.execute(); + assertEquals("hello", selectStmt.getResultSet().get(1)); + selectStmt.close(); + } + + @Test + void test_bindBlob() throws Exception { + runSql("CREATE TABLE test (col1 BLOB);"); + LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);"); + byte[] blob = {1, 2, 3, 4, 5}; + stmt.bindBlob(1, blob); + stmt.execute(); + stmt.close(); + + LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;"); + selectStmt.execute(); + assertArrayEquals(blob, (byte[]) selectStmt.getResultSet().get(1)); + selectStmt.close(); } private void runSql(String sql) throws Exception {