Skip to content

Commit 3f983de

Browse files
authored
[NOID] Fixes #3798: apoc.cypher.runFile() - Unexpected behaviour with commented ; (#3993) (#4033)
* [NOID] Fixes #3798: apoc.cypher.runFile() - Unexpected behaviour with commented ; (#3993) * Fixes #3798: apoc.cypher.runFile() - Unexpected behaviour with commented ; * removed unused imports * [NOID] java 11 changes
1 parent c6a9dca commit 3f983de

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

full/src/main/java/apoc/cypher/CypherExtended.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private void runDataStatementsInTx(
250250
String fileName) {
251251
while (scanner.hasNext()) {
252252
String stmt = removeShellControlCommands(scanner.next());
253-
if (stmt.trim().isEmpty()) continue;
253+
if (isCommentOrEmpty(stmt)) continue;
254254

255255
// Periodic operations cannot be schema operations, so no need to check that here (will fail as invalid
256256
// query)
@@ -303,7 +303,7 @@ private void collectError(BlockingQueue<RowResult> queue, boolean reportError, E
303303

304304
private Scanner createScannerFor(Reader reader) {
305305
Scanner scanner = new Scanner(reader);
306-
scanner.useDelimiter(";\r?\n");
306+
scanner.useDelimiter(";\\s*\r?\n");
307307
return scanner;
308308
}
309309

@@ -317,7 +317,7 @@ private void runSchemaStatementsInTx(
317317
String fileName) {
318318
while (scanner.hasNext()) {
319319
String stmt = removeShellControlCommands(scanner.next());
320-
if (stmt.trim().isEmpty()) continue;
320+
if (isCommentOrEmpty(stmt)) continue;
321321
boolean schemaOperation;
322322
try {
323323
schemaOperation = isSchemaOperation(stmt);
@@ -338,6 +338,11 @@ private void runSchemaStatementsInTx(
338338
}
339339
}
340340

341+
private static boolean isCommentOrEmpty(String stmt) {
342+
String trimStatement = stmt.trim();
343+
return trimStatement.isEmpty() || trimStatement.startsWith("//");
344+
}
345+
341346
private static final Pattern shellControl =
342347
Pattern.compile("^:?\\b(begin|commit|rollback)\\b", Pattern.CASE_INSENSITIVE);
343348

full/src/test/java/apoc/cypher/CypherExtendedTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,34 @@ public void testRunFile() throws Exception {
200200
});
201201
}
202202

203+
@Test
204+
public void testRunFileWithCommentsAndEmptyRows() throws Exception {
205+
testResult(db, "CALL apoc.cypher.runFile('return.cypher', {statistics: false})", r -> {
206+
Map<String, Object> row = r.next();
207+
assertEquals(Map.of("\"Step 1\"", "Step 1"), row.get("result"));
208+
209+
row = r.next();
210+
assertEquals(Map.of("row", "Step 3"), row.get("result"));
211+
212+
row = r.next();
213+
assertEquals(Map.of("row", 6L), row.get("result"));
214+
215+
row = r.next();
216+
assertEquals(Map.of("row", 7L), row.get("result"));
217+
218+
row = r.next();
219+
assertEquals(Map.of("'8'", "8"), row.get("result"));
220+
221+
row = r.next();
222+
assertEquals(Map.of("9", 9L), row.get("result"));
223+
224+
row = r.next();
225+
assertEquals(Map.of("10", 10L), row.get("result"));
226+
227+
assertFalse(r.hasNext());
228+
});
229+
}
230+
203231
@Test
204232
public void testRunFileWithAutoTransaction() {
205233
final int expectedCount = 2000;
@@ -640,6 +668,14 @@ public void testSchemaRunFile() {
640668
}
641669
}
642670

671+
@Test
672+
public void testRunSchemaFileWithCommentsAndEmptyRows() {
673+
testResult(db, "CALL apoc.cypher.runSchemaFile('schemaWithCommentsAndEmptyRows.cypher')", r -> {
674+
assertSchemaCypherFile(r);
675+
assertFalse(r.hasNext());
676+
});
677+
}
678+
643679
private void assertSchemaCypherFile(Result r) {
644680
Map<String, Object> row = r.next();
645681
Map result = (Map) row.get("result");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
RETURN "Step 1";
2+
;
3+
// RETURN "Step 2" as row;
4+
RETURN "Step 3" AS row;
5+
// RETURN "Step 4" as row;
6+
7+
// RETURN "Step 5" as row;
8+
9+
RETURN 6 as row;
10+
// RETURN "Step 5" as row; // RETURN "Step 5" as row;
11+
/*comment*/RETURN 7 /* comment*/ AS row;
12+
13+
/*comment*/RETURN '8'/*comment*/;
14+
/*comment*/RETURN 9/*comment*/;
15+
;
16+
/*comment*/RETURN 10/*comment*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE FULLTEXT INDEX CustomerIndex1 FOR (n:Customer1) ON EACH [n.name1];
2+
;
3+
// RETURN "Step 2" as row;
4+
CREATE FULLTEXT INDEX CustomerIndex21 FOR (n:Customer21) ON EACH [n.name12];
5+
// RETURN "Step 4" as row;
6+
7+
// RETURN "Step 5" as row;
8+
9+
// RETURN "Step 5" as row; // RETURN "Step 5" as row;
10+
/*comment*/ CREATE FULLTEXT INDEX CustomerIndex231 FOR (n:Customer213) /* comment*/ ON EACH [n.name123];
11+
;
12+
/*comment*/RETURN '8'/*comment*/;
13+
/*comment*/CREATE INDEX node_index_name FOR (n:Person) ON (n.surname);/*comment*/

0 commit comments

Comments
 (0)