Skip to content

Commit

Permalink
Allow passing parameters to apoc.cypher.runFile (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
thobe authored and jexp committed Jun 8, 2018
1 parent 57771aa commit 79350eb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main/java/apoc/cypher/Cypher.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@ public Stream<MapResult> run(@Name("cypher") String statement, @Name("params") M
}

@Procedure(mode = WRITE)
@Description("apoc.cypher.runFile(file or url,[{statistics:true,timeout:10}]) - runs each kernelTransaction in the file, all semicolon separated - currently no schema operations")
@Description("apoc.cypher.runFile(file or url,[{statistics:true,timeout:10,parameters:{}}]) - runs each kernelTransaction in the file, all semicolon separated - currently no schema operations")
public Stream<RowResult> runFile(@Name("file") String fileName, @Name(value = "config",defaultValue = "{}") Map<String,Object> config) {
return runFiles(singletonList(fileName),config);
}

@Procedure(mode = WRITE)
@Description("apoc.cypher.runFiles([files or urls],[{statistics:true,timeout:10}])) - runs each kernelTransaction in the files, all semicolon separated")
@Description("apoc.cypher.runFiles([files or urls],[{statistics:true,timeout:10,parameters:{}}])) - runs each kernelTransaction in the files, all semicolon separated")
public Stream<RowResult> runFiles(@Name("file") List<String> fileNames, @Name(value = "config",defaultValue = "{}") Map<String,Object> config) {
boolean addStatistics = Util.toBoolean(config.getOrDefault("statistics",true));
int timeout = Util.toInteger(config.getOrDefault("timeout",10));
List<RowResult> result = new ArrayList<>();
@SuppressWarnings( "unchecked" )
Map<String,Object> parameters = (Map<String,Object>)config.getOrDefault("parameters",Collections.emptyMap());
for (String f : fileNames) {
List<RowResult> rowResults = runManyStatements(readerForFile(f), Collections.emptyMap(), false, addStatistics, timeout).collect(Collectors.toList());
List<RowResult> rowResults = runManyStatements(readerForFile(f), parameters, false, addStatistics, timeout).collect(Collectors.toList());
result.addAll(rowResults);
}
return result.stream();
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/apoc/cypher/CypherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static apoc.util.TestUtil.testCall;
import static apoc.util.TestUtil.testResult;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -240,6 +241,26 @@ public void testRunFileWithResults() throws Exception {
assertEquals(false, r.hasNext());
});
}
@Test
public void testRunFileWithParameters() throws Exception {
testResult(db, "CALL apoc.cypher.runFile('src/test/resources/parameterized.cypher', {statistics:false,parameters:{foo:123,bar:'baz'}})",
r -> {
assertTrue("first row",r.hasNext());
Map<String,Object> result = (Map<String,Object>)r.next().get("result");
assertEquals(result.toString(), 1, result.size());
assertThat( result, hasEntry("one", 123L));
assertTrue("second row",r.hasNext());
result = (Map<String,Object>)r.next().get("result");
assertEquals(result.toString(), 1, result.size());
assertThat(result, hasEntry("two", "baz"));
assertTrue("third row",r.hasNext());
result = (Map<String,Object>)r.next().get("result");
assertEquals(result.toString(), 2, result.size());
assertThat(result, hasEntry("foo", 123L));
assertThat(result, hasEntry("bar", "baz"));
assertFalse("fourth row",r.hasNext());
});
}

@Test
public void testRunFilesMultiple() throws Exception {
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/parameterized.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RETURN $foo AS one;
RETURN $bar AS two;
RETURN $foo AS foo, $bar AS bar;

0 comments on commit 79350eb

Please sign in to comment.