Neo4jCypherInterpreter extends Interpreter { @Override public InterpreterResult interpret(String cypherQuery, InterpreterContext interpreterContext) { LOGGER.info("Opening session"); if (StringUtils.isBlank(cypherQuery)) { return new InterpreterResult(Code.SUCCESS); } try { StatementResult result = this.neo4jConnectionManager.execute(cypherQuery, interpreterContext); Set<Node> nodes = new HashSet<>(); Set<Relationship> relationships = new HashSet<>(); List<String> columns = new ArrayList<>(); List<List<String>> lines = new ArrayList<List<String>>(); while (result.hasNext()) { Record record = result.next(); List<Pair<String, Value>> fields = record.fields(); List<String> line = new ArrayList<>(); for (Pair<String, Value> field : fields) { if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.NODE())) { nodes.add(field.value().asNode()); } else if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.RELATIONSHIP())) { relationships.add(field.value().asRelationship()); } else if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.PATH())) { nodes.addAll(Iterables.asList(field.value().asPath().nodes())); relationships.addAll(Iterables.asList(field.value().asPath().relationships())); } else { setTabularResult(field.key(), field.value(), columns, line, InternalTypeSystem.TYPE_SYSTEM); } } if (!line.isEmpty()) { lines.add(line); } } if (!nodes.isEmpty()) { return renderGraph(nodes, relationships); } else { return renderTable(columns, lines); } } catch (Exception e) { LOGGER.error("Exception while interpreting cypher query", e); return new InterpreterResult(Code.ERROR, e.getMessage()); } } Neo4jCypherInterpreter(Properties properties); @Override void open(); @Override void close(); Map<String, String> getLabels(boolean refresh); @Override InterpreterResult interpret(String cypherQuery, InterpreterContext interpreterContext); @Override Scheduler getScheduler(); @Override int getProgress(InterpreterContext context); @Override FormType getFormType(); @Override void cancel(InterpreterContext context); static final String NEW_LINE; static final String TAB; }
@Test public void testDates() { InterpreterResult result = interpreter.interpret( "RETURN datetime('2015-06-24T12:50:35.556+0100') AS theDateTime", context); assertEquals(Code.SUCCESS, result.code()); assertEquals("theDateTime\n2015-06-24T12:50:35.556+01:00\n", result.toString() .replace(TABLE_RESULT_PREFIX, StringUtils.EMPTY)); result = interpreter.interpret("RETURN localdatetime('2015185T19:32:24') AS theLocalDateTime", context); assertEquals(Code.SUCCESS, result.code()); assertEquals("theLocalDateTime\n2015-07-04T19:32:24\n", result.toString() .replace(TABLE_RESULT_PREFIX, StringUtils.EMPTY)); result = interpreter.interpret("RETURN date('+2015-W13-4') AS theDate", context); assertEquals(Code.SUCCESS, result.code()); assertEquals("theDate\n2015-03-26\n", result.toString() .replace(TABLE_RESULT_PREFIX, StringUtils.EMPTY)); result = interpreter.interpret("RETURN time('125035.556+0100') AS theTime", context); assertEquals(Code.SUCCESS, result.code()); assertEquals("theTime\n12:50:35.556+01:00\n", result.toString().replace(TABLE_RESULT_PREFIX, StringUtils.EMPTY)); result = interpreter.interpret("RETURN localtime('12:50:35.556') AS theLocalTime", context); assertEquals(Code.SUCCESS, result.code()); assertEquals("theLocalTime\n12:50:35.556\n", result.toString() .replace(TABLE_RESULT_PREFIX, StringUtils.EMPTY)); }
@Test public void testDuration() { InterpreterResult result = interpreter.interpret( "RETURN duration('P14DT16H12M') AS theDuration", context); assertEquals(Code.SUCCESS, result.code()); assertEquals("theDuration\nP0M14DT58320S\n", result.toString() .replace(TABLE_RESULT_PREFIX, StringUtils.EMPTY)); }
@Test public void testPoint() { InterpreterResult result = interpreter.interpret("RETURN point({ x:3, y:0 }) AS cartesian_2d," + "point({ x:0, y:4, z:1 }) AS cartesian_3d," + "point({ latitude: 12, longitude: 56 }) AS geo_2d," + "point({ latitude: 12, longitude: 56, height: 1000 }) AS geo_3d", context); assertEquals(Code.SUCCESS, result.code()); assertEquals("cartesian_2d\tcartesian_3d\tgeo_2d\tgeo_3d\n" + "Point{srid=7203, x=3.0, y=0.0}\tPoint{srid=9157, x=0.0, y=4.0, z=1.0}\t" + "Point{srid=4326, x=56.0, y=12.0}\tPoint{srid=4979, x=56.0, y=12.0, z=1000.0}\n", result.toString().replace(TABLE_RESULT_PREFIX, StringUtils.EMPTY)); result = interpreter.interpret( "WITH point({ latitude: 12, longitude: 56, height: 1000 }) AS geo_3d " + "RETURN geo_3d.latitude AS latitude, geo_3d.longitude AS longitude, " + "geo_3d.height AS height", context); assertEquals(Code.SUCCESS, result.code()); assertEquals("latitude\tlongitude\theight\n" + "12.0\t56.0\t1000.0\n", result.toString().replace(TABLE_RESULT_PREFIX, StringUtils.EMPTY)); }