diff --git a/src/main/rust/surreal.rs b/src/main/rust/surreal.rs index f56d5ac8..e1198e75 100644 --- a/src/main/rust/surreal.rs +++ b/src/main/rust/surreal.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use crate::error::SurrealError; use crate::{check_query_result, check_value_table, convert_up_type, get_long_array, get_rust_string, get_rust_string_array, get_surreal_instance, get_value_instance, get_value_mut_instance, new_jlong_array, new_string, parse_value, release_instance, return_unexpected_result, return_value_array_first, return_value_array_iter, return_value_array_iter_sync, take_one_result, JniTypes, TOKIO_RUNTIME}; -use jni::objects::{JClass, JLongArray, JObjectArray, JString}; +use jni::objects::{JClass, JLongArray, JObject, JObjectArray, JString}; use jni::sys::{jboolean, jint, jlong, jlongArray, jstring}; use jni::JNIEnv; use parking_lot::Mutex; @@ -207,6 +207,26 @@ pub extern "system" fn Java_com_surrealdb_Surreal_query<'local>( JniTypes::new_response(Arc::new(Mutex::new(res))) } +#[no_mangle] +pub extern "system" fn Java_com_surrealdb_Surreal_queryBind<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, ptr: jlong, + query: JString<'local>, _params: JObject) -> jlong { + // Retrieve the Surreal instance + let surreal = get_surreal_instance!(&mut env, ptr, || 0); + // Retrieve the query + let query: String = match env.get_string(&query) { + Ok(s) => s.into(), + Err(_) => return 0, + }; + // Execute the query + let res = surrealdb_query::<()>(&surreal, &query, None); + // Check the result + let res = check_query_result!(&mut env, res, || 0); + // Build a response instance + JniTypes::new_response(Arc::new(Mutex::new(res))) +} + fn surrealdb_query( surreal: &Surreal, query: &str, diff --git a/src/test/java/com/surrealdb/QueryTests.java b/src/test/java/com/surrealdb/QueryTests.java index 14adee8b..8d4e698b 100644 --- a/src/test/java/com/surrealdb/QueryTests.java +++ b/src/test/java/com/surrealdb/QueryTests.java @@ -4,10 +4,7 @@ import org.junit.jupiter.api.Test; import java.awt.geom.Point2D; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.UUID; +import java.util.*; import java.util.function.Consumer; import static org.junit.jupiter.api.Assertions.*; @@ -66,6 +63,25 @@ void query() throws SurrealException { } } + @Test + void queryBind() throws SurrealException { + try (final Surreal surreal = new Surreal()) { + surreal.connect("memory").useNs("test_ns").useDb("test_db"); + { + final String sql = "CREATE person:1 SET name = $name;"; + final Map params = Collections.singletonMap("name", "Tobie"); + final Response response = surreal.queryBind(sql, params); + { // Check CREATE result + final Value create = response.take(0); + assertTrue(create.isArray()); + final Array createArray = create.getArray(); + assertEquals(createArray.len(), 1); + assertEquals("[{ id: person:1, name: 'Tobie' }]", createArray.toString()); + } + } + } + } + @Test void queryPrimitiveFields() throws SurrealException { try (final Surreal surreal = new Surreal()) {