Skip to content

Commit cdef24d

Browse files
committed
Printer can now use a JDBC result set as input. Use H2 as lightweight in-memory JDBC database for easy test set up.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1398108 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1046db8 commit cdef24d

3 files changed

Lines changed: 64 additions & 19 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ CSV files of various types.
4747
<version>2.2</version>
4848
<scope>test</scope>
4949
</dependency>
50+
<dependency>
51+
<groupId>com.h2database</groupId>
52+
<artifactId>h2</artifactId>
53+
<version>1.3.168</version>
54+
<scope>test</scope>
55+
</dependency>
5056
</dependencies>
5157

5258
<developers>

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

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525

2626
import java.io.Flushable;
2727
import java.io.IOException;
28+
import java.sql.ResultSet;
29+
import java.sql.SQLException;
2830

2931
/**
3032
* Prints values in a CSV format.
3133
*/
3234
public class CSVPrinter {
33-
35+
3436
/** The place that the values get written. */
3537
private final Appendable out;
3638
private final CSVFormat format;
@@ -43,7 +45,7 @@ public class CSVPrinter {
4345
* <p/>
4446
* Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats
4547
* (encapsulation and escaping with a different character) are not supported.
46-
*
48+
*
4749
* @param out
4850
* stream to which to print.
4951
* @param format
@@ -62,7 +64,7 @@ public CSVPrinter(final Appendable out, final CSVFormat format) {
6264
// ======================================================
6365

6466
/**
65-
* Outputs a blank line
67+
* Outputs a the line separator.
6668
*/
6769
public void println() throws IOException {
6870
out.append(format.getLineSeparator());
@@ -71,7 +73,7 @@ public void println() throws IOException {
7173

7274
/**
7375
* Flushes the underlying stream.
74-
*
76+
*
7577
* @throws IOException
7678
*/
7779
public void flush() throws IOException {
@@ -83,7 +85,7 @@ public void flush() throws IOException {
8385
/**
8486
* Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
8587
* characters will be escaped.
86-
*
88+
*
8789
* @param values
8890
* values to output.
8991
*/
@@ -97,7 +99,7 @@ public void printRecord(final Object... values) throws IOException {
9799
/**
98100
* Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
99101
* characters will be escaped.
100-
*
102+
*
101103
* @param values
102104
* values to output.
103105
*/
@@ -109,12 +111,12 @@ public void printRecord(final Iterable<?> values) throws IOException {
109111
}
110112

111113
/**
112-
* Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line and
113-
* occupy a least one full line. The character specified to start comments and a space will be inserted at the
114+
* Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line
115+
* and occupy a least one full line. The character specified to start comments and a space will be inserted at the
114116
* beginning of each new line in the comment.
115117
* <p/>
116118
* If comments are disabled in the current CSV format this method does nothing.
117-
*
119+
*
118120
* @param comment
119121
* the comment to output
120122
*/
@@ -293,11 +295,11 @@ void printAndQuote(final CharSequence value, final int offset, final int len) th
293295
/**
294296
* Prints the string as the next value on the line. The value will be escaped or encapsulated as needed if
295297
* checkForEscape==true
296-
*
298+
*
297299
* @param object
298300
* value to output.
299-
* @throws IOException
300-
* If an I/O error occurs
301+
* @throws IOException
302+
* If an I/O error occurs
301303
*/
302304
public void print(Object object, final boolean checkForEscape) throws IOException {
303305
// null values are considered empty
@@ -313,11 +315,11 @@ public void print(Object object, final boolean checkForEscape) throws IOExceptio
313315

314316
/**
315317
* Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
316-
*
318+
*
317319
* @param value
318320
* value to be output.
319-
* @throws IOException
320-
* If an I/O error occurs
321+
* @throws IOException
322+
* If an I/O error occurs
321323
*/
322324
public void print(final Object value) throws IOException {
323325
print(value, true);
@@ -328,8 +330,8 @@ public void print(final Object value) throws IOException {
328330
*
329331
* @param values
330332
* the values to print.
331-
* @throws IOException
332-
* If an I/O error occurs
333+
* @throws IOException
334+
* If an I/O error occurs
333335
*/
334336
public void printRecords(Object[] values) throws IOException {
335337
for (Object value : values) {
@@ -348,8 +350,8 @@ public void printRecords(Object[] values) throws IOException {
348350
*
349351
* @param values
350352
* the values to print.
351-
* @throws IOException
352-
* If an I/O error occurs
353+
* @throws IOException
354+
* If an I/O error occurs
353355
*/
354356
public void printRecords(Iterable<?> values) throws IOException {
355357
for (Object value : values) {
@@ -362,4 +364,22 @@ public void printRecords(Iterable<?> values) throws IOException {
362364
}
363365
}
364366
}
367+
368+
/**
369+
* Prints all the objects in the given JDBC result set.
370+
*
371+
* @param resultSet result set
372+
* the values to print.
373+
* @throws IOException
374+
* If an I/O error occurs
375+
*/
376+
public void printRecords(ResultSet resultSet) throws SQLException, IOException {
377+
int columnCount = resultSet.getMetaData().getColumnCount();
378+
while (resultSet.next()) {
379+
for (int i = 1; i <= columnCount; i++) {
380+
print(resultSet.getString(i));
381+
}
382+
println();
383+
}
384+
}
365385
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
import java.io.IOException;
2323
import java.io.StringWriter;
24+
import java.sql.Connection;
25+
import java.sql.DriverManager;
26+
import java.sql.ResultSet;
27+
import java.sql.SQLException;
28+
import java.sql.Statement;
2429
import java.util.Arrays;
2530
import java.util.List;
2631
import java.util.Random;
@@ -79,6 +84,20 @@ public void testPrinter6() throws IOException {
7984
assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
8085
}
8186

87+
@Test
88+
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
89+
final StringWriter sw = new StringWriter();
90+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
91+
Class.forName("org.h2.Driver");
92+
final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
93+
final Statement stmt = connection.createStatement();
94+
stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
95+
stmt.execute("insert into TEST values(1, 'r1')");
96+
stmt.execute("insert into TEST values(2, 'r2')");
97+
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
98+
assertEquals("1,r1" + lineSeparator + "2,r2" + lineSeparator, sw.toString());
99+
}
100+
82101
@Test
83102
public void testPrinter7() throws IOException {
84103
final StringWriter sw = new StringWriter();

0 commit comments

Comments
 (0)