Skip to content

Commit

Permalink
Response improvements (#94)
Browse files Browse the repository at this point in the history
Brilliant!
  • Loading branch information
macjuul authored Nov 12, 2024
1 parent f1eb226 commit c7a3b86
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
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);
}
}
}
}
}

0 comments on commit c7a3b86

Please sign in to comment.