Skip to content

Commit

Permalink
Nearly working query with params
Browse files Browse the repository at this point in the history
  • Loading branch information
phughk committed Aug 21, 2023
1 parent c536c4c commit 1d855d6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/intTest/java/com/surrealdb/refactor/DemoScenarioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public class DemoScenarioTest extends BaseIntegrationTest {
@Test
@Disabled("Functionality is unimplemented, but having the tests shows the design")
// @Disabled("Functionality is unimplemented, but having the tests shows the design")
public void testDemoScenario() throws Exception {
// Setup
URI address =
Expand Down Expand Up @@ -49,7 +49,7 @@ public void testDemoScenario() throws Exception {
List<Value> results = surrealDB.query(query.toString(), params);

// Validate the results of the multi-statement query
assertEquals(results.size(), 3);
assertEquals(results.size(), 3, results.toString());
assertEquals(
results.get(0).intoJson(),
asJson(
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/surrealdb/refactor/driver/QueryMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.surrealdb.refactor.driver;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.surrealdb.refactor.types.Param;
import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Getter
@ToString
public class QueryMessage {
private final String method = "query";
private final String id;

private final String[] params;

public QueryMessage(String requestID, String query, List<Param> params) {
id = requestID;
List<String> list = new ArrayList<>();
list.add(query);
for (Param param: params) {
JsonObject paramEntry = new JsonObject();
paramEntry.add(param.getIdentifier(), param.getValue().intoJson());
list.add(paramEntry.toString());
}
this.params = list.toArray(new String[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import com.surrealdb.refactor.exception.UnhandledSurrealDBNettyState;
import com.surrealdb.refactor.exception.UnknownResponseToRequest;
import com.surrealdb.refactor.types.Credentials;
import com.surrealdb.refactor.types.Param;
import io.netty.channel.*;
import io.netty.handler.codec.http.websocketx.*;
import io.netty.util.concurrent.Promise;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -82,6 +86,13 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ctx.close();
}

public Future<Object> query(String requestID, String query, List<Param> params) {
String method = "query";
checkChannelAndThrow(method);
QueryMessage queryMessage = new QueryMessage(requestID, query, params);
return sendAndPromise(method, requestID, new Gson().toJson(queryMessage));
}

public Future<Object> signin(Credentials credentials) {
return signin(UUID.randomUUID().toString(), credentials);
}
Expand All @@ -96,11 +107,11 @@ public Future<Object> signin(String requestID, Credentials credentials) {
return sendAndPromise(method, requestID, new Gson().toJson(signinMessage));
}

public Promise<Object> use(String namespace, String database) {
public Future<Object> use(String namespace, String database) {
return use(UUID.randomUUID().toString(), namespace, database);
}

public Promise<Object> use(String requestID, String namespace, String database) {
public Future<Object> use(String requestID, String namespace, String database) {
String method = "use";
checkChannelAndThrow(method);
UseMessage useMessage = new UseMessage(requestID, namespace, database);
Expand Down Expand Up @@ -145,4 +156,5 @@ private void registerRequest(String requestID, Promise<Object> promise) {
requestID));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -53,9 +55,14 @@ public UnusedSurrealDB<BidirectionalSurrealDB> authenticate(Credentials credenti

@Override
public List<Value> query(String query, List<Param> params) {
throw new SurrealDBUnimplementedException(
"https://github.com/surrealdb/surrealdb.java/issues/62",
"Plaintext websocket connections are not supported yet");
Object resp = null;
try {
resp = srdbHandler.query(UUID.randomUUID().toString(), query, params).get(2, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new RuntimeException(e);
}
List<Value> casted = Arrays.asList(new Value(resp.toString()));
return casted;
}
};
return new UnusedSurrealDB<>() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/surrealdb/refactor/types/Param.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.surrealdb.refactor.types;

import com.surrealdb.refactor.types.surrealdb.Value;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public class Param {
private final String identifier;
private final Value value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import com.google.gson.JsonPrimitive;
import com.surrealdb.refactor.exception.SurrealDBUnimplementedException;
import com.surrealdb.refactor.types.IntoJson;
import lombok.ToString;

import java.util.Optional;

@ToString
public class Value implements IntoJson {

private final String string;
Expand Down

0 comments on commit 1d855d6

Please sign in to comment.