From cfd55c94732c85861cdea82b819b34fbbb5377bb Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 22 Feb 2021 21:32:23 -0500 Subject: [PATCH] [CSV-123] Add possibility to use ResultSet header meta data as CSV header #11. --- src/changes/changes.xml | 4 +- .../org/apache/commons/csv/CSVPrinter.java | 28 ++++++++++++++ .../apache/commons/csv/CSVPrinterTest.java | 38 +++++++++++++++---- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 81571ac3..9315846d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -55,7 +55,9 @@ Line number is not proper at EOF. Parser iterates over the last CSV Record twice. Minor improvements #126, #127. - + Add possibility to use ResultSet header meta data as CSV header #11. + + Update org.junit.jupiter:junit-jupiter from 5.6.0 to 5.7.0, #84 #109 Update tests from Apache Commons Lang 3.9 to 3.11. Update tests from commons-io:commons-io 2.6 to 2.8.0, #108. diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java index a0cc6126..d3c262e1 100644 --- a/src/main/java/org/apache/commons/csv/CSVPrinter.java +++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java @@ -224,6 +224,18 @@ public final class CSVPrinter implements Flushable, Closeable { println(); } + /** + * Prints headers for a result set based on its metadata. + * + * @param resultSet The result set to query for metadata. + * @throws IOException If an I/O error occurs. + * @throws SQLException If a database access error occurs or this method is called on a closed result set. + * @since 1.9.0 + */ + public void printHeaders(final ResultSet resultSet) throws IOException, SQLException { + printRecord((Object[]) format.withHeader(resultSet).getHeader()); + } + /** * Outputs the record separator. * @@ -388,4 +400,20 @@ public final class CSVPrinter implements Flushable, Closeable { println(); } } + + /** + * Prints all the objects with metadata in the given JDBC result set based on the header boolean. + * + * @param resultSet result set the values to print. + * @param printHeader Boolean value to print header or not. + * @throws IOException If an I/O error occurs + * @throws SQLException if a database access error occurs + * @since 1.9.0 + */ + public void printRecords(final ResultSet resultSet, final boolean printHeader) throws SQLException, IOException { + if (printHeader) { + printHeaders(resultSet); + } + printRecords(resultSet); + } } diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 39e83b01..120c857d 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -21,6 +21,7 @@ import static org.apache.commons.csv.Constants.CR; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -29,13 +30,13 @@ import static org.mockito.Mockito.verify; import java.io.CharArrayWriter; import java.io.File; +import java.io.FileReader; import java.io.IOException; import java.io.PrintStream; +import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; -import java.io.Reader; -import java.io.FileReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.sql.BatchUpdateException; @@ -62,7 +63,7 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; /** - * + * Tests {@link CSVPrinter}. */ public class CSVPrinterTest { @@ -86,8 +87,8 @@ public class CSVPrinterTest { } private final String recordSeparator = CSVFormat.DEFAULT.getRecordSeparator(); - private String longText2; + private String longText2; private void doOneRandom(final CSVFormat format) throws Exception { final Random r = new Random(); @@ -140,7 +141,7 @@ public class CSVPrinterTest { return fixed; } - private Connection geH2Connection() throws SQLException, ClassNotFoundException { + private Connection getH2Connection() throws SQLException, ClassNotFoundException { Class.forName("org.h2.Driver"); return DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", ""); } @@ -620,7 +621,7 @@ public class CSVPrinterTest { @Test public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); - try (final Connection connection = geH2Connection()) { + try (final Connection connection = getH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { @@ -635,7 +636,7 @@ public class CSVPrinterTest { public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); Class.forName("org.h2.Driver"); - try (final Connection connection = geH2Connection()) { + try (final Connection connection = getH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST"); @@ -647,11 +648,32 @@ public class CSVPrinterTest { + "\"" + recordSeparator, sw.toString()); } + @Test + public void testJdbcPrinterWithResultSetHeader() throws IOException, ClassNotFoundException, SQLException { + final StringWriter sw = new StringWriter(); + try (final Connection connection = getH2Connection()) { + setUpTable(connection); + try (final Statement stmt = connection.createStatement(); + final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);) { + try (final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST")) { + printer.printRecords(resultSet, true); + assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator, + sw.toString()); + } + try (final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST")) { + printer.printRecords(resultSet, false); + assertNotEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator, + sw.toString()); + } + } + } + } + @Test public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); Class.forName("org.h2.Driver"); - try (final Connection connection = geH2Connection()) { + try (final Connection connection = getH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");