PeriodicUtils { public static Pair<String,Boolean> prepareInnerStatement(String cypherAction, BatchMode batchMode, List<String> columns, String iteratorVariableName) { String names = columns.stream().map(Util::quote).collect(Collectors.joining("|")); boolean withCheck = regNoCaseMultiLine("[{$](" + names + ")\\}?\\s+AS\\s+").matcher(cypherAction).find(); if (withCheck) return Pair.of(cypherAction, false); switch(batchMode) { case SINGLE: return Pair.of(Util.withMapping(columns.stream(), (c) -> Util.param(c) + " AS " + Util.quote(c)) + cypherAction,false); case BATCH: if (regNoCaseMultiLine("UNWIND\\s+[{$]" + iteratorVariableName+"\\}?\\s+AS\\s+").matcher(cypherAction).find()) { return Pair.of(cypherAction, true); } String with = Util.withMapping(columns.stream(), (c) -> Util.quote(iteratorVariableName) + "." + Util.quote(c) + " AS " + Util.quote(c)); return Pair.of("UNWIND "+ Util.param(iteratorVariableName)+" AS "+ Util.quote(iteratorVariableName) + with + " " + cypherAction,true); case BATCH_SINGLE: return Pair.of(cypherAction, true); default: throw new IllegalArgumentException("Unrecognised batch mode: [" + batchMode + "]"); } } private  PeriodicUtils(); static Pair<String,Boolean> prepareInnerStatement(String cypherAction, BatchMode batchMode, List<String> columns, String iteratorVariableName); static Pattern regNoCaseMultiLine(String pattern);  }
@Test public void iterateListPrefixActionStatementWithUnwind() { BatchMode batchMode = BatchMode.fromIterateList(true); Pair<String, Boolean> prepared = prepareInnerStatement("SET n:Actor", batchMode, List.of("n"), "_batch"); assertTrue(prepared.other()); assertEquals("UNWIND $_batch AS _batch WITH _batch.n AS n SET n:Actor", prepared.first()); }
@Test public void dontUnwindAnUnwindOfThe$_batchParameter() { BatchMode batchMode = BatchMode.fromIterateList(true); Pair<String, Boolean> prepared = prepareInnerStatement("UNWIND $_batch AS p SET p:Actor", batchMode, List.of("p"), "_batch"); assertTrue(prepared.other()); assertEquals("UNWIND $_batch AS p SET p:Actor", prepared.first()); }
@Test public void dontUnwindAWithOfColumnNames() { BatchMode batchMode = BatchMode.fromIterateList(true); Pair<String, Boolean> prepared = prepareInnerStatement("WITH $p as p SET p.lastname=p.name REMOVE p.name", batchMode, List.of("p"), "_batch"); assertFalse(prepared.other()); assertEquals("WITH $p as p SET p.lastname=p.name REMOVE p.name", prepared.first()); }
@Test public void noIterateListNoPrefixOnActionStatement() { BatchMode batchMode = BatchMode.fromIterateList(false); Pair<String, Boolean> prepared = prepareInnerStatement("SET n:Actor", batchMode, List.of("n"), "_batch"); assertFalse(prepared.other()); assertEquals(" WITH $n AS n SET n:Actor", prepared.first()); }
@Test public void noIterateListNoPrefixOnMultipleActionStatement() { BatchMode batchMode = BatchMode.fromIterateList(false); Pair<String, Boolean> prepared = prepareInnerStatement("SET n:Actor, p:Person", batchMode, List.of("n", "p"), "_batch"); assertFalse(prepared.other()); assertEquals(" WITH $n AS n,$p AS p SET n:Actor, p:Person", prepared.first()); }
@Test public void dontAddAnExtraWithIfParametersNamedAfterColumnsExistInStatement() { BatchMode batchMode = BatchMode.fromIterateList(false); Pair<String, Boolean> prepared = prepareInnerStatement("WITH $n AS x SET x:Actor", batchMode, List.of("n"), "_batch"); assertFalse(prepared.other()); assertEquals("WITH $n AS x SET x:Actor", prepared.first()); }
@Test public void passThrough$_batchWithNoUnwind() { BatchMode batchMode = BatchMode.BATCH_SINGLE; Pair<String, Boolean> prepared = prepareInnerStatement("UNWIND $_batch AS batch WITH batch.x AS x SET x:Actor", batchMode, List.of("x"), "_batch"); assertTrue(prepared.other()); assertEquals("UNWIND $_batch AS batch WITH batch.x AS x SET x:Actor", prepared.first()); }