Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement QueryBind #95

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/main/rust/surreal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T>(
surreal: &Surreal<Any>,
query: &str,
Expand Down
24 changes: 20 additions & 4 deletions src/test/java/com/surrealdb/QueryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<String, ?> 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()) {
Expand Down
Loading