Skip to content

Commit 74afb17

Browse files
committed
[CSV-207]
Provide a CSV Format for printing PostgreSQL CSV and Text formats.
1 parent 4806809 commit 74afb17

File tree

4 files changed

+382
-57
lines changed

4 files changed

+382
-57
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<action issue="CSV-191" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(Path, Charset)</action>
5151
<action issue="CSV-192" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVParser.parse(Path, Charset, CSVFormat)</action>
5252
<action issue="CSV-205" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat#printer() to print to System.out</action>
53+
<action issue="CSV-207" type="add" dev="ggregory" due-to="Gary Gregory">Provide a CSV Format for printing PostgreSQL CSV and Text formats.</action>
5354
</release>
5455
<release version="1.4" date="2016-05-28" description="Feature and bug fix release">
5556
<action issue="CSV-181" type="update" dev="ggregory" due-to="Gary Gregory">Make CSVPrinter.print(Object) GC-free.</action>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.apache.commons.csv.Constants.BACKSLASH;
2121
import static org.apache.commons.csv.Constants.COMMA;
2222
import static org.apache.commons.csv.Constants.COMMENT;
23+
import static org.apache.commons.csv.Constants.EMPTY;
2324
import static org.apache.commons.csv.Constants.CR;
2425
import static org.apache.commons.csv.Constants.CRLF;
2526
import static org.apache.commons.csv.Constants.DOUBLE_QUOTE_CHAR;
@@ -190,6 +191,17 @@ public enum Predefined {
190191
*/
191192
MySQL(CSVFormat.MYSQL),
192193

194+
/**
195+
* @see CSVFormat#POSTGRESQL_CSV
196+
* @since 1.5
197+
*/
198+
PostgreSQLCsv(CSVFormat.POSTGRESQL_CSV),
199+
200+
/**
201+
* @see CSVFormat#POSTGRESQL_CSV
202+
*/
203+
PostgreSQLText(CSVFormat.POSTGRESQL_TEXT),
204+
193205
/**
194206
* @see CSVFormat#RFC4180
195207
*/
@@ -367,6 +379,80 @@ public CSVFormat getFormat() {
367379
.withQuoteMode(QuoteMode.ALL_NON_NULL);
368380
// @formatter:off
369381

382+
/**
383+
* Default PostgreSQL CSV format used by the {@code COPY} operation.
384+
*
385+
* <p>
386+
* This is a comma-delimited format with a LF character as the line separator. Values are double quoted and special
387+
* characters are escaped with {@code '"'}. The default NULL string is {@code ""}.
388+
* </p>
389+
*
390+
* <p>
391+
* Settings are:
392+
* </p>
393+
* <ul>
394+
* <li>withDelimiter(',')</li>
395+
* <li>withQuote('"')</li>
396+
* <li>withRecordSeparator('\n')</li>
397+
* <li>withIgnoreEmptyLines(false)</li>
398+
* <li>withEscape('\\')</li>
399+
* <li>withNullString("")</li>
400+
* <li>withQuoteMode(QuoteMode.ALL_NON_NULL)</li>
401+
* </ul>
402+
*
403+
* @see Predefined#MySQL
404+
* @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load
405+
* -data.html</a>
406+
* @since 1.5
407+
*/
408+
// @formatter:off
409+
public static final CSVFormat POSTGRESQL_CSV = DEFAULT
410+
.withDelimiter(COMMA)
411+
.withEscape(DOUBLE_QUOTE_CHAR)
412+
.withIgnoreEmptyLines(false)
413+
.withQuote(DOUBLE_QUOTE_CHAR)
414+
.withRecordSeparator(LF)
415+
.withNullString(EMPTY)
416+
.withQuoteMode(QuoteMode.ALL_NON_NULL);
417+
// @formatter:off
418+
419+
/**
420+
* Default PostgreSQL text format used by the {@code COPY} operation.
421+
*
422+
* <p>
423+
* This is a tab-delimited format with a LF character as the line separator. Values are double quoted and special
424+
* characters are escaped with {@code '"'}. The default NULL string is {@code "\\N"}.
425+
* </p>
426+
*
427+
* <p>
428+
* Settings are:
429+
* </p>
430+
* <ul>
431+
* <li>withDelimiter('\t')</li>
432+
* <li>withQuote('"')</li>
433+
* <li>withRecordSeparator('\n')</li>
434+
* <li>withIgnoreEmptyLines(false)</li>
435+
* <li>withEscape('\\')</li>
436+
* <li>withNullString("\\N")</li>
437+
* <li>withQuoteMode(QuoteMode.ALL_NON_NULL)</li>
438+
* </ul>
439+
*
440+
* @see Predefined#MySQL
441+
* @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load
442+
* -data.html</a>
443+
* @since 1.5
444+
*/
445+
// @formatter:off
446+
public static final CSVFormat POSTGRESQL_TEXT = DEFAULT
447+
.withDelimiter(TAB)
448+
.withEscape(DOUBLE_QUOTE_CHAR)
449+
.withIgnoreEmptyLines(false)
450+
.withQuote(DOUBLE_QUOTE_CHAR)
451+
.withRecordSeparator(LF)
452+
.withNullString("\\N")
453+
.withQuoteMode(QuoteMode.ALL_NON_NULL);
454+
// @formatter:off
455+
370456
/**
371457
* Comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.
372458
*
Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,67 @@
1-
/*
2-
* Licensed to the Apache Software Foundation (ASF) under one or more
3-
* contributor license agreements. See the NOTICE file distributed with
4-
* this work for additional information regarding copyright ownership.
5-
* The ASF licenses this file to You under the Apache License, Version 2.0
6-
* (the "License"); you may not use this file except in compliance with
7-
* the License. You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
16-
*/
17-
18-
package org.apache.commons.csv;
19-
20-
import org.junit.Assert;
21-
import org.junit.Test;
22-
23-
/**
24-
* Tests {@link CSVFormat.Predefined}.
25-
*/
26-
public class CSVFormatPredefinedTest {
27-
28-
private void test(final CSVFormat format, final String enumName) {
29-
Assert.assertEquals(format, CSVFormat.Predefined.valueOf(enumName).getFormat());
30-
Assert.assertEquals(format, CSVFormat.valueOf(enumName));
31-
}
32-
33-
@Test
34-
public void testDefault() {
35-
test(CSVFormat.DEFAULT, "Default");
36-
}
37-
38-
@Test
39-
public void testExcel() {
40-
test(CSVFormat.EXCEL, "Excel");
41-
}
42-
43-
@Test
44-
public void testMySQL() {
45-
test(CSVFormat.MYSQL, "MySQL");
46-
}
47-
48-
@Test
49-
public void testRFC4180() {
50-
test(CSVFormat.RFC4180, "RFC4180");
51-
}
52-
53-
@Test
54-
public void testTDF() {
55-
test(CSVFormat.TDF, "TDF");
56-
}
57-
}
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.csv;
19+
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
/**
24+
* Tests {@link CSVFormat.Predefined}.
25+
*/
26+
public class CSVFormatPredefinedTest {
27+
28+
private void test(final CSVFormat format, final String enumName) {
29+
Assert.assertEquals(format, CSVFormat.Predefined.valueOf(enumName).getFormat());
30+
Assert.assertEquals(format, CSVFormat.valueOf(enumName));
31+
}
32+
33+
@Test
34+
public void testDefault() {
35+
test(CSVFormat.DEFAULT, "Default");
36+
}
37+
38+
@Test
39+
public void testExcel() {
40+
test(CSVFormat.EXCEL, "Excel");
41+
}
42+
43+
@Test
44+
public void testMySQL() {
45+
test(CSVFormat.MYSQL, "MySQL");
46+
}
47+
48+
@Test
49+
public void testPostgreSqlCsv() {
50+
test(CSVFormat.POSTGRESQL_CSV, "PostgreSQLCsv");
51+
}
52+
53+
@Test
54+
public void testPostgreSqlText() {
55+
test(CSVFormat.POSTGRESQL_TEXT, "PostgreSQLText");
56+
}
57+
58+
@Test
59+
public void testRFC4180() {
60+
test(CSVFormat.RFC4180, "RFC4180");
61+
}
62+
63+
@Test
64+
public void testTDF() {
65+
test(CSVFormat.TDF, "TDF");
66+
}
67+
}

0 commit comments

Comments
 (0)