use of org.neo4j.server.rest.repr.CypherResultRepresentation in project neo4j by neo4j.
the class CypherService method cypher.
@POST
@SuppressWarnings({ "unchecked", "ParameterCanBeLocal" })
public Response cypher(String body, @Context HttpServletRequest request, @QueryParam(INCLUDE_STATS_PARAM) boolean includeStats, @QueryParam(INCLUDE_PLAN_PARAM) boolean includePlan, @QueryParam(PROFILE_PARAM) boolean profile) throws BadInputException {
usage.get(features).flag(http_cypher_endpoint);
Map<String, Object> command = input.readMap(body);
if (!command.containsKey(QUERY_KEY)) {
return output.badRequest(new InvalidArgumentsException("You have to provide the 'query' parameter."));
}
String query = (String) command.get(QUERY_KEY);
Map<String, Object> params;
try {
params = (Map<String, Object>) (command.containsKey(PARAMS_KEY) && command.get(PARAMS_KEY) != null ? command.get(PARAMS_KEY) : new HashMap<String, Object>());
} catch (ClassCastException e) {
return output.badRequest(new IllegalArgumentException("Parameters must be a JSON map"));
}
try {
QueryExecutionEngine executionEngine = cypherExecutor.getExecutionEngine();
boolean periodicCommitQuery = executionEngine.isPeriodicCommit(query);
CommitOnSuccessfulStatusCodeRepresentationWriteHandler handler = (CommitOnSuccessfulStatusCodeRepresentationWriteHandler) output.getRepresentationWriteHandler();
if (periodicCommitQuery) {
handler.closeTransaction();
}
TransactionalContext tc = cypherExecutor.createTransactionContext(query, params, request);
Result result;
if (profile) {
result = executionEngine.profileQuery(query, params, tc);
includePlan = true;
} else {
result = executionEngine.executeQuery(query, params, tc);
includePlan = result.getQueryExecutionType().requestedExecutionPlanDescription();
}
if (periodicCommitQuery) {
handler.setTransaction(database.beginTx());
}
return output.ok(new CypherResultRepresentation(result, includeStats, includePlan));
} catch (Throwable e) {
if (e.getCause() instanceof CypherException) {
return output.badRequest(e.getCause());
} else {
return output.badRequest(e);
}
}
}
Aggregations