Skip to content

Commit

Permalink
Backport of 5.x ifNeeded quote fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBergstrand committed Aug 29, 2024
1 parent 8d4fa33 commit 64301f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/apoc/export/csv/CsvFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public ProgressInfo dump(SubGraph graph, ExportFileManager writer, Reporter repo
private CSVWriter getCsvWriter(Writer writer, ExportConfig config) {
CSVWriter out;
switch (config.isQuotes()) {
case ExportConfig.NONE_QUOTES:
case ExportConfig.NO_QUOTES:
out = new CSVWriter(
writer,
config.getDelimChar(),
Expand All @@ -110,12 +110,12 @@ private CSVWriter getCsvWriter(Writer writer, ExportConfig config) {
CSVWriter.DEFAULT_LINE_END);
applyQuotesToAll = false;
break;
case ExportConfig.IF_NEEDED_QUUOTES:
case ExportConfig.IF_NEEDED_QUOTES:
out = new CSVWriter(
writer,
config.getDelimChar(),
ExportConfig.QUOTECHAR,
'\0', // escape char
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
applyQuotesToAll = false;
break;
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/apoc/export/util/ExportConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public NodeConfig(Map<String, String> config) {
}

public static final char QUOTECHAR = '"';
public static final String NONE_QUOTES = "none";
public static final String NO_QUOTES = "none";
public static final String ALWAYS_QUOTES = "always";
public static final String IF_NEEDED_QUUOTES = "ifNeeded";
public static final String IF_NEEDED_QUOTES = "ifNeeded";

public static final int DEFAULT_BATCH_SIZE = 20000;
private static final int DEFAULT_UNWIND_BATCH_SIZE = 20;
Expand Down Expand Up @@ -196,12 +196,12 @@ private void exportQuotes(Map<String, Object> config) {
try {
this.quotes = (String) config.getOrDefault("quotes", DEFAULT_QUOTES);

if (!quotes.equals(ALWAYS_QUOTES) && !quotes.equals(NONE_QUOTES) && !quotes.equals(IF_NEEDED_QUUOTES)) {
if (!quotes.equals(ALWAYS_QUOTES) && !quotes.equals(NO_QUOTES) && !quotes.equals(IF_NEEDED_QUOTES)) {
throw new RuntimeException("The string value of the field quote is not valid");
}

} catch (ClassCastException e) { // backward compatibility
this.quotes = toBoolean(config.get("quotes")) ? ALWAYS_QUOTES : NONE_QUOTES;
this.quotes = toBoolean(config.get("quotes")) ? ALWAYS_QUOTES : NO_QUOTES;
}
}

Expand Down
29 changes: 25 additions & 4 deletions core/src/test/java/apoc/export/csv/ExportCsvTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public class ExportCsvTest {
+ "\"Bar Sport\",\"\",\"\",\"[\"\"Address\"\"]\"%n"
+ "\"\",\"\",\"via Benni\",\"[\"\"Address\"\"]\"%n");
private static final String EXPECTED_QUERY_QUOTES_NEEDED = String.format(
"a.name,a.city,a.street,labels(a)%n" + "Andrea,Milano,\"Via Garibaldi, 7\",\"[\"Address1\",\"Address\"]\"%n"
+ "Bar Sport,,,\"[\"Address\"]\"%n"
+ ",,via Benni,\"[\"Address\"]\"%n");
"a.name,a.city,a.street,labels(a)%n" + "Andrea,Milano,\"Via Garibaldi, 7\",\"[\"\"Address1\"\",\"\"Address\"\"]\"%n"
+ "Bar Sport,,,\"[\"\"Address\"\"]\"%n"
+ ",,via Benni,\"[\"\"Address\"\"]\"%n");
private static final String EXPECTED = String.format(
"\"_id\",\"_labels\",\"age\",\"city\",\"kids\",\"male\",\"name\",\"street\",\"_start\",\"_end\",\"_type\"%n"
+ "\"0\",\":User:User1\",\"42\",\"\",\"[\"\"a\"\",\"\"b\"\",\"\"c\"\"]\",\"true\",\"foo\",\"\",,,%n"
Expand Down Expand Up @@ -134,14 +134,24 @@ public class ExportCsvTest {
+ ",,,,,,,,3,4,NEXT_DELIVERY%n");
private static final String EXPECTED_NEEDED_QUOTES =
String.format("_id,_labels,age,city,kids,male,name,street,_start,_end,_type%n"
+ "0,:User:User1,42,,\"[\"a\",\"b\",\"c\"]\",true,foo,,,,%n"
+ "0,:User:User1,42,,\"[\"\"a\"\",\"\"b\"\",\"\"c\"\"]\",true,foo,,,,%n"
+ "1,:User,42,,,,bar,,,,%n"
+ "2,:User,12,,,,,,,,%n"
+ "3,:Address:Address1,,Milano,,,Andrea,\"Via Garibaldi, 7\",,,%n"
+ "4,:Address,,,,,Bar Sport,,,,%n"
+ "5,:Address,,,,,,via Benni,,,%n"
+ ",,,,,,,,0,1,KNOWS%n"
+ ",,,,,,,,3,4,NEXT_DELIVERY%n");
private static final String EXPECTED_QUOTES_ALWAYS =
"\"_id\",\"_labels\",\"age\",\"city\",\"kids\",\"male\",\"name\",\"street\",\"_start\",\"_end\",\"_type\"\n"
+ "\"0\",\":User:User1\",\"42\",\"\",\"[\"\"a\"\",\"\"b\"\",\"\"c\"\"]\",\"true\",\"foo\",\"\",,,\n"
+ "\"1\",\":User\",\"42\",\"\",\"\",\"\",\"bar\",\"\",,,\n"
+ "\"2\",\":User\",\"12\",\"\",\"\",\"\",\"\",\"\",,,\n"
+ "\"3\",\":Address:Address1\",\"\",\"Milano\",\"\",\"\",\"Andrea\",\"Via Garibaldi, 7\",,,\n"
+ "\"4\",\":Address\",\"\",\"\",\"\",\"\",\"Bar Sport\",\"\",,,\n"
+ "\"5\",\":Address\",\"\",\"\",\"\",\"\",\"\",\"via Benni\",,,\n"
+ ",,,,,,,,\"0\",\"1\",\"KNOWS\"\n"
+ ",,,,,,,,\"3\",\"4\",\"NEXT_DELIVERY\"\n";

private static final File directory = new File("target/import");

Expand Down Expand Up @@ -367,6 +377,17 @@ public void testExportAllCsvNeededQuotes() {
assertEquals(EXPECTED_NEEDED_QUOTES, readFile(fileName));
}

@Test
public void testExportAllCsvAlwaysQuotes() {
String fileName = "all.csv";
TestUtil.testCall(
db,
"CALL apoc.export.csv.all($file,{quotes: 'always'})",
map("file", fileName),
(r) -> assertResults(fileName, r, "database"));
assertEquals(EXPECTED_QUOTES_ALWAYS, readFile(fileName));
}

@Test
public void testExportGraphCsv() {
String fileName = "graph.csv";
Expand Down

0 comments on commit 64301f6

Please sign in to comment.