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

Response improvements #94

Merged
merged 2 commits into from
Nov 12, 2024
Merged
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
10 changes: 10 additions & 0 deletions src/main/java/com/surrealdb/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Response extends Native {
super(ptr);
}

private static native int size(long ptr);

@Override
final native boolean deleteInstance(long ptr);

Expand All @@ -17,6 +19,14 @@ public Value take(int num) {
return new Value(take(getPtr(), num));
}

public <T> T take(Class<T> type, int num) {
return take(num).get(type);
}

public int size() {
return size(getPtr());
}

@Override
final String toString(long ptr) {
return getClass().getName() + "[ptr=" + ptr + "]";
Expand Down
10 changes: 10 additions & 0 deletions src/main/rust/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ pub extern "system" fn Java_com_surrealdb_Response_take<'local>(
};
create_instance(Arc::new(value), JniTypes::Value)
}

#[no_mangle]
pub extern "system" fn Java_com_surrealdb_Response_size<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
ptr: jlong,
) -> jint {
let response = get_response_instance!(&mut env, ptr, || 0);
return response.lock().num_statements() as jint;
}
30 changes: 30 additions & 0 deletions src/test/java/com/surrealdb/QueryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,34 @@ void queryClassValueIterator() throws SurrealException {
}
}
}

@Test
void queryClass() throws SurrealException {
try (final Surreal surreal = new Surreal()) {
surreal.connect("memory").useNs("test_ns").useDb("test_db");
{
final String sql = "CREATE ONLY person:1 SET name = 'Tobie';";
final Response response = surreal.query(sql);
{
final Person create = response.take(Person.class, 0);
assertEquals(create.name, "Tobie");
}
}
}
}

@Test
void queryResponseSize() throws SurrealException {
try (final Surreal surreal = new Surreal()) {
surreal.connect("memory").useNs("test_ns").useDb("test_db");
{
final String sql = "RETURN 'one'; RETURN 'two'; RETURN 'three'";
final Response response = surreal.query(sql);
{
final int size = response.size();
assertEquals(size, 3);
}
}
}
}
}
Loading