added result set class as list of lists
This commit is contained in:
parent
224f02f429
commit
063f94e382
|
@ -38,7 +38,7 @@ public class QueryRequest {
|
|||
/**
|
||||
* @return the query as JsonObject
|
||||
*/
|
||||
public JsonObject toJson() {
|
||||
public JsonObject asJson() {
|
||||
|
||||
final JsonObject rootJ = new JsonObject();
|
||||
rootJ.addProperty(Tags.Json.OBJECT_TYPE, "QueryRequest");
|
||||
|
|
|
@ -1,13 +1,6 @@
|
|||
package li.strolch.soql.core;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import li.strolch.model.StrolchRootElement;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.json.StrolchElementToJsonVisitor;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author msmock
|
||||
|
@ -16,28 +9,25 @@ public class QueryResponse extends QueryRequest {
|
|||
|
||||
// the returned objects
|
||||
static final String RESULT_SET = "resultSet";
|
||||
public List<StrolchRootElement> resultSet = new ArrayList<>();
|
||||
|
||||
// an exception or error message in case of error
|
||||
public String message;
|
||||
|
||||
//
|
||||
public final ResultSet resultSet = new ResultSet();
|
||||
|
||||
/**
|
||||
* @return the query as JsonObject
|
||||
*/
|
||||
public JsonObject toJson(final StrolchElementToJsonVisitor visitor) {
|
||||
public JsonObject asJson() {
|
||||
|
||||
final JsonObject rootJ = super.toJson();
|
||||
final JsonObject rootJ = super.asJson();
|
||||
|
||||
if (message != null && !message.isEmpty()) {
|
||||
rootJ.addProperty("Message", message);
|
||||
}
|
||||
|
||||
final JsonArray resultSetJ = new JsonArray();
|
||||
rootJ.add(RESULT_SET, resultSetJ);
|
||||
|
||||
for (Iterator<StrolchRootElement> iter = resultSet.iterator(); iter.hasNext(); ) {
|
||||
resultSetJ.add(iter.next().accept(visitor));
|
||||
}
|
||||
rootJ.add(RESULT_SET, resultSet.asJson());
|
||||
|
||||
return rootJ;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
package li.strolch.soql.core;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import li.strolch.model.StrolchRootElement;
|
||||
import li.strolch.model.json.StrolchElementToJsonVisitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The query result set as List of Lists
|
||||
* <p>
|
||||
*
|
||||
* TODO: the result set should carry arbitrary objects, not only StrolchRootElements
|
||||
*
|
||||
* @author msmock
|
||||
*/
|
||||
public class ResultSet {
|
||||
|
||||
final StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor();
|
||||
|
||||
public final List<List<StrolchRootElement>> rows = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* @param row the result of the execution of a single statement
|
||||
*/
|
||||
public void add(final List<StrolchRootElement> row) {
|
||||
rows.add(row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all rows as JSON Array
|
||||
*/
|
||||
public JsonArray asJson() {
|
||||
|
||||
final JsonArray rowsAsJson = new JsonArray();
|
||||
|
||||
for (Iterator<List<StrolchRootElement>> rowsIter = rows.iterator(); rowsIter.hasNext(); )
|
||||
rowsAsJson.add(row2Json(rowsIter.next()));
|
||||
|
||||
return rowsAsJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a single row as JSON Array
|
||||
*/
|
||||
private JsonArray row2Json(final List<StrolchRootElement> evalResult) {
|
||||
final JsonArray rowAsJson = new JsonArray();
|
||||
for (Iterator<StrolchRootElement> iterator = evalResult.iterator(); iterator.hasNext(); ) {
|
||||
rowAsJson.add(iterator.next().accept(visitor));
|
||||
}
|
||||
return rowAsJson;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,10 +2,8 @@ package li.strolch.soql.core;
|
|||
|
||||
import com.google.gson.JsonObject;
|
||||
import li.strolch.model.StrolchRootElement;
|
||||
import li.strolch.model.json.StrolchElementToJsonVisitor;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -15,8 +13,6 @@ import static org.junit.Assert.assertEquals;
|
|||
*/
|
||||
public class SerialisationTest extends BaseTest {
|
||||
|
||||
final StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor();
|
||||
|
||||
private QueryRequest buildTestRequest() {
|
||||
String s = "SELECT a FROM Activity a WHERE a.getId() = :p";
|
||||
final QueryRequest request = new QueryRequest();
|
||||
|
@ -33,23 +29,11 @@ public class SerialisationTest extends BaseTest {
|
|||
return response;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialization() {
|
||||
final StrolchRootElement element = getTestResource("testId");
|
||||
final JsonObject jsonObject = element.accept(visitor);
|
||||
// System.out.println(jsonObject);
|
||||
|
||||
String expected = "{\"objectType\":\"Resource\",\"id\":\"testId\",\"name\":null,\"type\":null,\"parameterBags\"" +
|
||||
":{\"testBag\":{\"id\":\"testBag\",\"name\":null,\"type\":null,\"parameters\":{\"testId\":" +
|
||||
"{\"id\":\"testId\",\"name\":null,\"type\":\"Float\",\"value\":\"100.0\"}}}}}\n";
|
||||
|
||||
assertEquals(expected.trim(), jsonObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery2JSON() {
|
||||
final QueryRequest query = buildTestRequest();
|
||||
JsonObject jsonObject = query.toJson();
|
||||
final QueryRequest request = buildTestRequest();
|
||||
JsonObject jsonObject = request.asJson();
|
||||
|
||||
String expected = "{\"objectType\":\"QueryRequest\",\"statement\":\"SELECT a FROM Activity a WHERE a.getId() " +
|
||||
"= :p\",\"parameter\":{\"r\":\"Just a string!\"}}";
|
||||
|
@ -64,7 +48,7 @@ public class SerialisationTest extends BaseTest {
|
|||
final QueryRequest initial = new QueryRequest();
|
||||
initial.setStatement(s);
|
||||
initial.getParameter().put("p", "10010");
|
||||
final JsonObject jsonObject = initial.toJson();
|
||||
final JsonObject jsonObject = initial.asJson();
|
||||
|
||||
final QueryRequest query = new QueryRequest();
|
||||
query.fromJson(jsonObject);
|
||||
|
@ -76,18 +60,18 @@ public class SerialisationTest extends BaseTest {
|
|||
@Test
|
||||
public void testResponse2JSON() {
|
||||
final QueryResponse response = buildTestResponse();
|
||||
response.resultSet = getTestRessources(2);
|
||||
final List<StrolchRootElement> evalResult = getTestRessources(2);
|
||||
response.resultSet.add(evalResult);
|
||||
|
||||
String expected = "{\"objectType\":\"QueryRequest\",\"statement\":\"SELECT a FROM Activity a WHERE a.getId() " +
|
||||
"= :p\",\"parameter\":{\"r\":\"Just a string!\"},\"resultSet\":[{\"objectType\":" +
|
||||
"\"Resource\",\"id\":\"2\",\"name\":null,\"type\":null,\"parameterBags\":" +
|
||||
"{\"testBag\":{\"id\":\"testBag\",\"name\":null,\"type\":null,\"parameters\":{\"testId\":{\"id\":" +
|
||||
"\"testId\",\"name\":null,\"type\":\"Float\",\"value\":\"100.0\"}}}}},{\"objectType\":\"Resource\"," +
|
||||
"\"id\":\"2\",\"name\":null,\"type\":null,\"parameterBags\":{\"testBag\":{\"id\":\"testBag\",\"name\"" +
|
||||
":null,\"type\":null,\"parameters\":{\"testId\":{\"id\":\"testId\",\"name\":null,\"type\":\"Float\"," +
|
||||
"\"value\":\"100.0\"}}}}}]}\n";
|
||||
"= :p\",\"parameter\":{\"r\":\"Just a string!\"},\"resultSet\":[[{\"objectType\":\"Resource\"," +
|
||||
"\"id\":\"2\",\"name\":null,\"type\":null,\"parameterBags\":{\"testBag\":{\"id\":\"testBag\"," +
|
||||
"\"name\":null,\"type\":null,\"parameters\":{\"testId\":{\"id\":\"testId\",\"name\":null,\"type\"" +
|
||||
":\"Float\",\"value\":\"100.0\"}}}}},{\"objectType\":\"Resource\",\"id\":\"2\",\"name\":null,\"type\"" +
|
||||
":null,\"parameterBags\":{\"testBag\":{\"id\":\"testBag\",\"name\":null,\"type\":null,\"parameters\"" +
|
||||
":{\"testId\":{\"id\":\"testId\",\"name\":null,\"type\":\"Float\",\"value\":\"100.0\"}}}}}]]}";
|
||||
|
||||
final JsonObject jsonObject = response.toJson(visitor);
|
||||
final JsonObject jsonObject = response.asJson();
|
||||
assertEquals(expected.trim(), jsonObject.toString());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue