Skip to content

Commit 60709c2

Browse files
committed
Added a predefined format for MySQL (SANDBOX-410)
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297944 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4f3ef66 commit 60709c2

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public class CSVFormat implements Cloneable, Serializable {
6666

6767
/** Tabulation delimited format. */
6868
public static final CSVFormat TDF = new CSVFormat('\t', '"', DISABLED, DISABLED, true, true, false, true);
69+
70+
/**
71+
* Default MySQL format used by the <tt>SELECT INTO OUTFILE</tt> and
72+
* <tt>LOAD DATA INFILE</tt> operations. This is a tabulation delimited
73+
* format with a LF character as the line separator. Values are not quoted
74+
* and special characters are escaped with '\'.
75+
*
76+
* @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-data.html</a>
77+
*/
78+
public static final CSVFormat MYSQL = new CSVFormat('\t', DISABLED, DISABLED, '\\', false, false, false, false).withLineSeparator("\n");
6979

7080

7181
/**

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,20 @@ public void testMultiLineComment() throws IOException {
125125

126126
public void testRandom() throws Exception {
127127
int iter = 10000;
128-
format = CSVFormat.DEFAULT;
129-
doRandom(iter);
130-
format = CSVFormat.EXCEL;
131-
doRandom(iter);
132-
133-
// Format for MySQL
134-
format = new CSVFormat('\t', CSVFormat.DISABLED, CSVFormat.DISABLED, '\\', false, false, false, false);
135-
doRandom(iter);
128+
doRandom(CSVFormat.DEFAULT, iter);
129+
doRandom(CSVFormat.EXCEL, iter);
130+
doRandom(CSVFormat.MYSQL, iter);
136131
}
137132

138-
Random r = new Random();
139-
CSVFormat format;
140-
141-
public void doRandom(int iter) throws Exception {
133+
public void doRandom(CSVFormat format, int iter) throws Exception {
142134
for (int i = 0; i < iter; i++) {
143-
doOneRandom();
135+
doOneRandom(format);
144136
}
145137
}
146138

147-
public void doOneRandom() throws Exception {
139+
public void doOneRandom(CSVFormat format) throws Exception {
140+
Random r = new Random();
141+
148142
int nLines = r.nextInt(4) + 1;
149143
int nCol = r.nextInt(3) + 1;
150144
// nLines=1;nCol=2;
@@ -215,6 +209,8 @@ public static String printable(String s) {
215209
}
216210

217211
public String randStr() {
212+
Random r = new Random();
213+
218214
int sz = r.nextInt(20);
219215
// sz = r.nextInt(3);
220216
char[] buf = new char[sz];

0 commit comments

Comments
 (0)