private long appendNodes( PrintWriter out ) { long nodes = 0; for ( Node node : graph.getNodes() ) { appendNode( out, node ); nodes++; } return nodes; }
public void export( PrintWriter out ) { export(out, null, null); }
public static SubGraph from( GraphDatabaseService gdb ) { return new DatabaseSubGraph( gdb ); }
@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 ) ); }
private String doExportGraph( SubGraph graph ) { StringWriter out = new StringWriter(); new SubGraphExporter( graph ).export( new PrintWriter( out ) ); return out.toString(); }
public void export( PrintWriter out, String begin, String commit ) { output( out, begin ); appendIndexes( out ); appendConstraints( out ); output( out, commit, begin ); long nodes = appendNodes( out ); long relationships = appendRelationships( out ); if ( nodes + relationships > 0 ) { out.println( ";" ); } output( out, commit ); }
private long appendRelationships( PrintWriter out ) { long relationships = 0; for ( Node node : graph.getNodes() ) { for ( Relationship rel : node.getRelationships( Direction.OUTGOING ) ) { appendRelationship( out, rel ); relationships++; } } return relationships; }
private String doExportGraph( GraphDatabaseService db ) { SubGraph graph = DatabaseSubGraph.from( db ); return doExportGraph( graph ); }
private void appendNode( PrintWriter out, Node node ) { out.print( "create (" ); out.print( identifier( node ) ); String labels = labelString( node ); if ( !labels.isEmpty() ) { out.print( labels ); } formatProperties( out, node ); out.println( ")" ); }
private void addToGraph( Object value ) { if ( value instanceof Node ) { add( (Node) value ); } if ( value instanceof Relationship ) { add( (Relationship) value ); } if ( value instanceof Iterable ) { for ( Object inner : (Iterable) value ) { addToGraph( inner ); } } }
private void appendConstraints( PrintWriter out ) { for ( String line : exportConstraints() ) { out.print( line ); out.println( ";" ); } }
private void appendIndexes( PrintWriter out ) { for ( String line : exportIndexes() ) { out.print( line ); out.println( ";" ); } }
public void add( Node node ) { final long id = node.getId(); if ( !nodes.containsKey( id ) ) { addNode( id, node ); } }
@Test public void testEmptyGraph() { assertEquals( "", doExportGraph( gdb ) ); }
@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 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 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 ) ); }