@Test public void testFromSimpleCypherResult() { Node n = gdb.createNode(); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + ")" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testFromRelCypherResult() { Node n = gdb.createNode(); final Relationship rel = n.createRelationshipTo( n, RelationshipType.withName( "REL" ) ); final ExecutionResult result = result( "rel", rel ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); assertEquals( "create (_0)" + lineSeparator() + "create (_0)-[:`REL`]->(_0)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testSingleNode() { Node n = gdb.createNode(); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + ")" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testEscapingOfRelationshipStringPropertyValue() { Node n = gdb.createNode(); final Relationship rel = n.createRelationshipTo( n, RelationshipType.withName( "REL" ) ); rel.setProperty( "name", "Brutus \"Brutal\" Howell" ); final ExecutionResult result = result( "rel", rel ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); assertEquals( "create (_0)" + lineSeparator() + "create (_0)-[:`REL` {`name`:\"Brutus \\\"Brutal\\\" Howell\"}]->(_0)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testEscapingOfRelationshipStringArrayPropertyValue() { Node n = gdb.createNode(); final Relationship rel = n.createRelationshipTo( n, RelationshipType.withName( "REL" ) ); rel.setProperty( "name", new String[]{"Brutus \"Brutal\" Howell", "Dr."} ); final ExecutionResult result = result( "rel", rel ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); assertEquals( "create (_0)" + lineSeparator() + "create (_0)-[:`REL` {`name`:[\"Brutus \\\"Brutal\\\" Howell\", \"Dr.\"]}]->(_0)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testEscapingOfNodeStringPropertyValue() { Node n = gdb.createNode(); n.setProperty( "name", "Brutus \"Brutal\" Howell" ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + " {`name`:\"Brutus \\\"Brutal\\\" Howell\"})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testEscapingStringPropertyWithBackslash() { Node n = gdb.createNode(); n.setProperty( "name", "Some\\thing" ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + " {`name`:\"Some\\\\thing\"})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testEscapingOfNodeStringArrayPropertyValue() { Node n = gdb.createNode(); n.setProperty( "name", new String[]{"Brutus \"Brutal\" Howell", "Dr."} ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + " {`name`:[\"Brutus \\\"Brutal\\\" Howell\", \"Dr.\"]})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testEscapingStringPropertyWithBackslashAndDoubleQuote() { Node n = gdb.createNode(); n.setProperty( "name", "Some\\\"thing" ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + " {`name`:\"Some\\\\\\\"thing\"})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testSingleNodeWithArrayProperties() { Node n = gdb.createNode(); n.setProperty( "name", new String[]{"a", "b"} ); n.setProperty( "age", new int[]{1, 2} ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + " {`age`:[1, 2], `name`:[\"a\", \"b\"]})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testFromPathCypherResult() { Node n1 = gdb.createNode(); Node n2 = gdb.createNode(); final Relationship rel = n1.createRelationshipTo( n2, RelationshipType.withName( "REL" ) ); final Path path = new PathImpl.Builder( n1 ).push( rel ).build(); final ExecutionResult result = result( "path", path ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); assertEquals( "create (_0)" + lineSeparator() + "create (_1)" + lineSeparator() + "create (_0)-[:`REL`]->(_1)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testSingleNodeWithProperties() { Node n = gdb.createNode(); n.setProperty( "name", "Node1" ); n.setProperty( "age", 42 ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + " {`age`:42, `name`:\"Node1\"})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testSingleNodeLabels() { Node n = gdb.createNode(); n.addLabel( Label.label( "Foo" ) ); n.addLabel( Label.label( "Bar" ) ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + ":`Foo`:`Bar`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testExportConstraintsViaCypherResult() { final Label label = Label.label( "Foo" ); gdb.schema().constraintFor( label ).assertPropertyIsUnique( "bar" ).create(); gdb.schema().constraintFor( label ).assertPropertyIsUnique( "bar2" ).create(); commitAndStartNewTransactionAfterSchemaChanges(); Node n = gdb.createNode( label ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); assertEquals( "create constraint on (n:`Foo`) assert n.`bar2` is unique;" + lineSeparator() + "create constraint on (n:`Foo`) assert n.`bar` is unique;" + lineSeparator() + "create (_0:`Foo`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void testExportIndexesViaCypherResult() { final Label label = Label.label( "Foo" ); gdb.schema().indexFor( label ).on( "bar" ).create(); gdb.schema().indexFor( label ).on( "bar2" ).create(); commitAndStartNewTransactionAfterSchemaChanges(); Node n = gdb.createNode( label ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); assertEquals( "create index on :`Foo`(`bar2`);" + lineSeparator() + "create index on :`Foo`(`bar`);" + lineSeparator() + "create (_0:`Foo`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Procedure @Description("apoc.export.graphml.query(query,file,config) - exports nodes and relationships from the cypher statement as graphml to the provided file") public Stream<ProgressInfo> query(@Name("query") String query, @Name("file") String fileName, @Name("config") Map<String, Object> config) throws Exception { ExportConfig c = new ExportConfig(config); Result result = db.execute(query); SubGraph graph = CypherResultSubGraph.from(result, db, c.getRelsInBetween()); String source = String.format("statement: nodes(%d), rels(%d)", Iterables.count(graph.getNodes()), Iterables.count(graph.getRelationships())); return exportGraphML(fileName, source, graph, c); }
@Procedure @Description("apoc.export.cypher.query(query,file,config) - exports nodes and relationships from the cypher statement incl. indexes as cypher statements to the provided file") public Stream<DataProgressInfo> query(@Name("query") String query, @Name(value = "file",defaultValue = "") String fileName, @Name(value = "config",defaultValue = "{}") Map<String, Object> config) throws IOException { if (Util.isNullOrEmpty(fileName)) fileName=null; ExportConfig c = new ExportConfig(config); Result result = db.execute(query); SubGraph graph = CypherResultSubGraph.from(result, db, c.getRelsInBetween()); String source = String.format("statement: nodes(%d), rels(%d)", Iterables.count(graph.getNodes()), Iterables.count(graph.getRelationships())); return exportCypher(fileName, source, graph, c, false); }
private SubGraph cypherResultSubGraph(String query, boolean relsBetween) { GraphDatabaseAPI db = getServer().getDb(); try (Transaction tx = db.beginTx()) { Result result = db.execute(query); SubGraph subGraph = CypherResultSubGraph.from(result, db, relsBetween); tx.success(); return subGraph; } } }
private SubGraph cypherResultSubGraph(String query, boolean relsBetween) { GraphDatabaseService db = getServer().getDb(); try (Transaction tx = db.beginTx()) { Result result = db.execute(query); SubGraph subGraph = CypherResultSubGraph.from(result, db, relsBetween); tx.success(); return subGraph; } }
private SubGraph cypherResultSubGraph(String query, boolean relsBetween) { GraphDatabaseAPI db = getServer().getDb(); try (Transaction tx = db.beginTx()) { Result result = db.execute(query); SubGraph subGraph = CypherResultSubGraph.from(result, db, relsBetween); tx.success(); return subGraph; } }