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
This commit is contained in:
Gary D. Gregory 2012-10-14 18:08:58 +00:00
parent 1046db8f90
commit cdef24d918
3 changed files with 64 additions and 19 deletions

View File

@ -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>

View File

@ -25,12 +25,14 @@ import static org.apache.commons.csv.Constants.SP;
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();
}
}
}

View File

@ -21,6 +21,11 @@ import static org.junit.Assert.assertEquals;
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;
@ -79,6 +84,20 @@ public class CSVPrinterTest {
assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
}
@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();