Skip to content
This repository was archived by the owner on Sep 14, 2025. It is now read-only.

Commit a9daab6

Browse files
committed
[CSV-209] Create CSVFormat.ORACLE preset. Also: Fix and complete
documentation for other formats.
1 parent 83cd808 commit a9daab6

6 files changed

Lines changed: 72 additions & 11 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<action issue="CSV-172" type="fix" dev="ggregory" due-to="Andrew Pennebaker">Don't quote cells just because they have UTF-8 encoded characters.</action>
4545
<action issue="CSV-220" type="add" dev="ggregory" due-to="Gary Gregory">Add API org.apache.commons.csv.CSVFormat.withSystemRecordSeparator().</action>
4646
<action issue="CSV-223" type="fix" dev="ggregory" due-to="Samuel Martin">Inconsistency between Javadoc of CSVFormat DEFAULT EXCEL.</action>
47+
<action issue="CSV-209" type="fix" dev="ggregory" due-to="Gary Gregory">Create CSVFormat.ORACLE preset.</action>
4748
</release>
4849
<release version="1.5" date="2017-09-03" description="Feature and bug fix release">
4950
<action issue="CSV-203" type="fix" dev="ggregory" due-to="Richard Wheeldon, Kai Paroth">withNullString value is printed without quotes when QuoteMode.ALL is specified; add QuoteMode.ALL_NON_NULL. PR #17.</action>

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

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ public enum Predefined {
189189
*/
190190
MySQL(CSVFormat.MYSQL),
191191

192+
/**
193+
* @see CSVFormat#ORACLE
194+
*/
195+
Oracle(CSVFormat.ORACLE),
196+
192197
/**
193198
* @see CSVFormat#POSTGRESQL_CSV
194199
* @since 1.5
@@ -227,7 +232,7 @@ public CSVFormat getFormat() {
227232
}
228233

229234
/**
230-
* Standard comma separated format, as for {@link #RFC4180} but allowing empty lines.
235+
* Standard Comma Separated Value format, as for {@link #RFC4180} but allowing empty lines.
231236
*
232237
* <p>
233238
* Settings are:
@@ -377,6 +382,44 @@ public CSVFormat getFormat() {
377382
.withQuoteMode(QuoteMode.ALL_NON_NULL);
378383
// @formatter:off
379384

385+
/**
386+
* Default Oracle format used by the SQL*Loader utility.
387+
*
388+
* <p>
389+
* This is a comma-delimited format with the system line separator character as the record separator. Values are double quoted when needed and special
390+
* characters are escaped with {@code '"'}. The default NULL string is {@code ""}. Values are trimmed.
391+
* </p>
392+
*
393+
* <p>
394+
* Settings are:
395+
* </p>
396+
* <ul>
397+
* <li>withDelimiter(',') // default is {@code FIELDS TERMINATED BY ','}</li>
398+
* <li>withQuote('"') // default is {@code OPTIONALLY ENCLOSED BY '"'}</li>
399+
* <li>withSystemRecordSeparator()</li>
400+
* <li>withTrim()</li>
401+
* <li>withIgnoreEmptyLines(false)</li>
402+
* <li>withEscape('\\')</li>
403+
* <li>withNullString("\\N")</li>
404+
* <li>withQuoteMode(QuoteMode.MINIMAL)</li>
405+
* </ul>
406+
*
407+
* @see Predefined#Oracle
408+
* @see <a href="https://docs.oracle.com/database/121/SUTIL/GUID-D1762699-8154-40F6-90DE-EFB8EB6A9AB0.htm#SUTIL4217">https://docs.oracle.com/database/121/SUTIL/GUID-D1762699-8154-40F6-90DE-EFB8EB6A9AB0.htm#SUTIL4217</a>
409+
* @since 1.6
410+
*/
411+
// @formatter:off
412+
public static final CSVFormat ORACLE = DEFAULT
413+
.withDelimiter(COMMA)
414+
.withEscape(BACKSLASH)
415+
.withIgnoreEmptyLines(false)
416+
.withQuote(DOUBLE_QUOTE_CHAR)
417+
.withNullString("\\N")
418+
.withTrim()
419+
.withSystemRecordSeparator()
420+
.withQuoteMode(QuoteMode.MINIMAL);
421+
// @formatter:off
422+
380423
/**
381424
* Default PostgreSQL CSV format used by the {@code COPY} operation.
382425
*
@@ -399,7 +442,7 @@ public CSVFormat getFormat() {
399442
* </ul>
400443
*
401444
* @see Predefined#MySQL
402-
* @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
445+
* @see <a href="https://www.postgresql.org/docs/current/static/sql-copy.html"> https://www.postgresql.org/docs/current/static/sql-copy.html
403446
* -data.html</a>
404447
* @since 1.5
405448
*/
@@ -436,8 +479,7 @@ public CSVFormat getFormat() {
436479
* </ul>
437480
*
438481
* @see Predefined#MySQL
439-
* @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
440-
* -data.html</a>
482+
* @see <a href="https://www.postgresql.org/docs/current/static/sql-copy.html"> https://www.postgresql.org/docs/current/static/sql-copy.html</a>
441483
* @since 1.5
442484
*/
443485
// @formatter:off
@@ -1934,7 +1976,7 @@ public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) {
19341976
* only works for inputs with '\n', '\r' and "\r\n"
19351977
* </p>
19361978
*
1937-
* @return A new CSVFormat that is equal to this but with the operating system's line separator stringr
1979+
* @return A new CSVFormat that is equal to this but with the operating system's line separator string.
19381980
* @since 1.6
19391981
*/
19401982
public CSVFormat withSystemRecordSeparator() {

src/site/xdoc/index.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ limitations under the License.
3030
<li><a href="http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">Informix UNLOAD</a></li>
3131
<li><a href="http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">Informix UNLOAD CSV</a></li>
3232
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/mysqldump-delimited-text.html">MySQL</a></li>
33+
<li><a href="hhttps://docs.oracle.com/database/121/SUTIL/GUID-D1762699-8154-40F6-90DE-EFB8EB6A9AB0.htm#SUTIL4217">Oracle</a></li>
34+
<li><a href="https://www.postgresql.org/docs/current/static/sql-copy.html">PostgreSQL CSV</a></li>
35+
<li><a href="https://www.postgresql.org/docs/current/static/sql-copy.html">PostgreSQL Text</a></li>
3336
<li><a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a></li>
3437
<li><a href="http://en.wikipedia.org/wiki/Tab-separated_values">TDF</a></li>
3538
</ul>

src/site/xdoc/user-guide.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ limitations under the License.
3434
The CSVFormat class provides some commonly used CSV variants:
3535

3636
<dl>
37-
<dt>EXCEL</dt><dd>The Microsoft Excel CSV format.</dd>
38-
<dt>INFORMIX_UNLOAD</dt><dd>Informix <a href="http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">UNLOAD</a> format used by the <code>UNLOAD TO file_name</code> operation.</dd>
39-
<dt>INFORMIX_UNLOAD_CSV</dt><dd>Informix <a href="http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">CSV UNLOAD</a> format used by the <code>UNLOAD TO file_name</code> operation (escaping is disabled.)</dd>
40-
<dt>MYSQL</dt><dd>The Oracle MySQL CSV format.</dd>
41-
<dt>RFC-4180</dt><dd>The RFC-4180 format defined by <a href="https://tools.ietf.org/html/rfc4180">RFC-4180</a></dd>
42-
<dt>TDF</dt><dd>A tab delimited format</dd>
37+
<dr><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#DEFAULT">DEFAULT</a></dr><dd>Standard Comma Separated Value format, as for RFC4180 but allowing empty lines.</dd>
38+
<dt><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#EXCEL">EXCEL</a></dt><dd>The Microsoft Excel CSV format.</dd>
39+
<dt><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#INFORMIX_UNLOAD">INFORMIX_UNLOAD</a></dt><dd>Informix <a href="http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">UNLOAD</a> format used by the <code>UNLOAD TO file_name</code> operation.</dd>
40+
<dt><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#INFORMIX_UNLOAD_CSV">INFORMIX_UNLOAD_CSV</a></dt><dd>Informix <a href="http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">CSV UNLOAD</a> format used by the <code>UNLOAD TO file_name</code> operation (escaping is disabled.)</dd>
41+
<dt><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#MYSQL">MYSQL</a></dt><dd>The MySQL CSV format.</dd>
42+
<dt><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#ORACLE">ORACLE</a></dt><dd>Default Oracle format used by the SQL*Loader utility.</dd>
43+
<dt><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#POSTGRESSQL_CSV">POSTGRESSQL_CSV</a></dt><dd>Default PostgreSQL CSV format used by the COPY operation.</dd>
44+
<dt><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#POSTGRESSQL_TEXT">POSTGRESSQL_TEXT</a></dt><dd>Default PostgreSQL text format used by the COPY operation.</dd>
45+
<dt><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#RFC4180">RFC-4180</a></dt><dd>The RFC-4180 format defined by <a href="https://tools.ietf.org/html/rfc4180">RFC-4180</a>.</dd>
46+
<dt><a href="http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#TDF">TDF</a></dt><dd>A tab delimited format.</dd>
4347
</dl>
4448

4549
<subsection name="Example: Parsing an Excel CSV File">

src/test/java/org/apache/commons/csv/CSVFormatPredefinedTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public void testMySQL() {
4545
test(CSVFormat.MYSQL, "MySQL");
4646
}
4747

48+
@Test
49+
public void testOracle() {
50+
test(CSVFormat.ORACLE, "Oracle");
51+
}
52+
4853
@Test
4954
public void testPostgreSqlCsv() {
5055
test(CSVFormat.POSTGRESQL_CSV, "PostgreSQLCsv");

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,12 @@ public void testRandomMySql() throws Exception {
11821182
doRandom(CSVFormat.MYSQL, ITERATIONS_FOR_RANDOM_TEST);
11831183
}
11841184

1185+
@Test
1186+
@Ignore
1187+
public void testRandomOracle() throws Exception {
1188+
doRandom(CSVFormat.ORACLE, ITERATIONS_FOR_RANDOM_TEST);
1189+
}
1190+
11851191
@Test
11861192
@Ignore
11871193
public void testRandomPostgreSqlCsv() throws Exception {

0 commit comments

Comments
 (0)