You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/10/14 20:08:58 UTC

svn commit: r1398108 - in /commons/proper/csv/trunk: pom.xml src/main/java/org/apache/commons/csv/CSVPrinter.java src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Author: ggregory
Date: Sun Oct 14 18:08:58 2012
New Revision: 1398108

URL: http://svn.apache.org/viewvc?rev=1398108&view=rev
Log:
Printer can now use a JDBC result set as input. Use H2 as lightweight in-memory JDBC database for easy test set up.

Modified:
    commons/proper/csv/trunk/pom.xml
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Modified: commons/proper/csv/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/pom.xml?rev=1398108&r1=1398107&r2=1398108&view=diff
==============================================================================
--- commons/proper/csv/trunk/pom.xml (original)
+++ commons/proper/csv/trunk/pom.xml Sun Oct 14 18:08:58 2012
@@ -47,6 +47,12 @@ CSV files of various types.
       <version>2.2</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.h2database</groupId>
+      <artifactId>h2</artifactId>
+      <version>1.3.168</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <developers>

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1398108&r1=1398107&r2=1398108&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java Sun Oct 14 18:08:58 2012
@@ -25,12 +25,14 @@ import static org.apache.commons.csv.Con
 
 import java.io.Flushable;
 import java.io.IOException;
+import java.sql.ResultSet;
+import java.sql.SQLException;
 
 /**
  * Prints values in a CSV format.
  */
 public class CSVPrinter {
-    
+
     /** The place that the values get written. */
     private final Appendable out;
     private final CSVFormat format;
@@ -43,7 +45,7 @@ public class CSVPrinter {
      * <p/>
      * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats
      * (encapsulation and escaping with a different character) are not supported.
-     *
+     * 
      * @param out
      *            stream to which to print.
      * @param format
@@ -62,7 +64,7 @@ public class CSVPrinter {
     // ======================================================
 
     /**
-     * Outputs a blank line
+     * Outputs a the line separator.
      */
     public void println() throws IOException {
         out.append(format.getLineSeparator());
@@ -71,7 +73,7 @@ public class CSVPrinter {
 
     /**
      * Flushes the underlying stream.
-     *
+     * 
      * @throws IOException
      */
     public void flush() throws IOException {
@@ -83,7 +85,7 @@ public class CSVPrinter {
     /**
      * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
      * characters will be escaped.
-     *
+     * 
      * @param values
      *            values to output.
      */
@@ -97,7 +99,7 @@ public class CSVPrinter {
     /**
      * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
      * characters will be escaped.
-     *
+     * 
      * @param values
      *            values to output.
      */
@@ -109,12 +111,12 @@ public class CSVPrinter {
     }
 
     /**
-     * Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line and
-     * occupy a least one full line. The character specified to start comments and a space will be inserted at the
+     * Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line
+     * and occupy a least one full line. The character specified to start comments and a space will be inserted at the
      * beginning of each new line in the comment.
      * <p/>
      * If comments are disabled in the current CSV format this method does nothing.
-     *
+     * 
      * @param comment
      *            the comment to output
      */
@@ -293,11 +295,11 @@ public class CSVPrinter {
     /**
      * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed if
      * checkForEscape==true
-     *
+     * 
      * @param object
      *            value to output.
-     * @throws  IOException
-     *          If an I/O error occurs
+     * @throws IOException
+     *             If an I/O error occurs
      */
     public void print(Object object, final boolean checkForEscape) throws IOException {
         // null values are considered empty
@@ -313,11 +315,11 @@ public class CSVPrinter {
 
     /**
      * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
-     *
+     * 
      * @param value
      *            value to be output.
-     * @throws  IOException
-     *          If an I/O error occurs
+     * @throws IOException
+     *             If an I/O error occurs
      */
     public void print(final Object value) throws IOException {
         print(value, true);
@@ -328,8 +330,8 @@ public class CSVPrinter {
      * 
      * @param values
      *            the values to print.
-     * @throws  IOException
-     *          If an I/O error occurs
+     * @throws IOException
+     *             If an I/O error occurs
      */
     public void printRecords(Object[] values) throws IOException {
         for (Object value : values) {
@@ -348,8 +350,8 @@ public class CSVPrinter {
      * 
      * @param values
      *            the values to print.
-     * @throws  IOException
-     *          If an I/O error occurs
+     * @throws IOException
+     *             If an I/O error occurs
      */
     public void printRecords(Iterable<?> values) throws IOException {
         for (Object value : values) {
@@ -362,4 +364,22 @@ public class CSVPrinter {
             }
         }
     }
+
+    /**
+     * Prints all the objects in the given JDBC result set.
+     * 
+     * @param resultSet result set
+     *            the values to print.
+     * @throws IOException
+     *             If an I/O error occurs
+     */
+    public void printRecords(ResultSet resultSet) throws SQLException, IOException {
+        int columnCount = resultSet.getMetaData().getColumnCount();
+        while (resultSet.next()) {
+            for (int i = 1; i <= columnCount; i++) {
+                print(resultSet.getString(i));
+            }
+            println();
+        }
+    }
 }

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1398108&r1=1398107&r2=1398108&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java Sun Oct 14 18:08:58 2012
@@ -21,6 +21,11 @@ import static org.junit.Assert.assertEqu
 
 import java.io.IOException;
 import java.io.StringWriter;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Random;
@@ -80,6 +85,20 @@ public class CSVPrinterTest {
     }
 
     @Test
+    public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
+        final StringWriter sw = new StringWriter();
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
+        Class.forName("org.h2.Driver");
+        final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
+        final Statement stmt = connection.createStatement();
+        stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
+        stmt.execute("insert into TEST values(1, 'r1')");
+        stmt.execute("insert into TEST values(2, 'r2')");
+        printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
+        assertEquals("1,r1" + lineSeparator + "2,r2" + lineSeparator, sw.toString());
+    }
+
+    @Test
     public void testPrinter7() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);