You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2012/03/07 12:29:32 UTC

svn commit: r1297944 - in /commons/sandbox/csv/trunk/src: main/java/org/apache/commons/csv/CSVFormat.java test/java/org/apache/commons/csv/CSVPrinterTest.java

Author: ebourg
Date: Wed Mar  7 11:29:32 2012
New Revision: 1297944

URL: http://svn.apache.org/viewvc?rev=1297944&view=rev
Log:
Added a predefined format for MySQL (SANDBOX-410)

Modified:
    commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
    commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1297944&r1=1297943&r2=1297944&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original)
+++ commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Wed Mar  7 11:29:32 2012
@@ -66,6 +66,16 @@ public class CSVFormat implements Clonea
 
     /** Tabulation delimited format. */
     public static final CSVFormat TDF = new CSVFormat('\t', '"', DISABLED, DISABLED, true, true, false, true);
+    
+    /**
+     * Default MySQL format used by the <tt>SELECT INTO OUTFILE</tt> and
+     * <tt>LOAD DATA INFILE</tt> operations. This is a tabulation delimited
+     * format with a LF character as the line separator. Values are not quoted
+     * and special characters are escaped with '\'.
+     * 
+     * @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>
+     */
+    public static final CSVFormat MYSQL = new CSVFormat('\t', DISABLED, DISABLED, '\\', false, false, false, false).withLineSeparator("\n");
 
 
     /**

Modified: commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1297944&r1=1297943&r2=1297944&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java (original)
+++ commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java Wed Mar  7 11:29:32 2012
@@ -125,26 +125,20 @@ public class CSVPrinterTest extends Test
 
     public void testRandom() throws Exception {
         int iter = 10000;
-        format = CSVFormat.DEFAULT;
-        doRandom(iter);
-        format = CSVFormat.EXCEL;
-        doRandom(iter);
-
-        // Format for MySQL
-        format = new CSVFormat('\t',  CSVFormat.DISABLED,  CSVFormat.DISABLED, '\\', false, false, false, false);
-        doRandom(iter);
+        doRandom(CSVFormat.DEFAULT, iter);
+        doRandom(CSVFormat.EXCEL, iter);
+        doRandom(CSVFormat.MYSQL, iter);
     }
 
-    Random r = new Random();
-    CSVFormat format;
-
-    public void doRandom(int iter) throws Exception {
+    public void doRandom(CSVFormat format, int iter) throws Exception {
         for (int i = 0; i < iter; i++) {
-            doOneRandom();
+            doOneRandom(format);
         }
     }
 
-    public void doOneRandom() throws Exception {
+    public void doOneRandom(CSVFormat format) throws Exception {
+        Random r = new Random();
+        
         int nLines = r.nextInt(4) + 1;
         int nCol = r.nextInt(3) + 1;
         // nLines=1;nCol=2;
@@ -215,6 +209,8 @@ public class CSVPrinterTest extends Test
     }
 
     public String randStr() {
+        Random r = new Random();
+        
         int sz = r.nextInt(20);
         // sz = r.nextInt(3);
         char[] buf = new char[sz];