[CSV-123] Add possibility to use ResultSet header meta data as CSV
header #11.
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 81571ac..9315846 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -55,7 +55,9 @@
<action issue="CSV-149" type="fix" dev="ggregory" due-to="Kranthi, Gary Gregory, Brent Worden, dota17">Line number is not proper at EOF.</action>
<action issue="CSV-195" type="fix" dev="ggregory" due-to="Rodolfo Duldulao, Rodolfo Duldulao, Michael Vitz, dota17">Parser iterates over the last CSV Record twice.</action>
<action issue="CSV-267" type="fix" dev="ggregory" due-to="Arturo Bernal">Minor improvements #126, #127.</action>
- <!-- UPDATES -->
+ <action issue="CSV-123" type="fix" dev="ggregory" due-to="Emmanuel Bourg, Benedikt Ritter, shivakrishnaah, Gary Gregory">Add possibility to use ResultSet header meta data as CSV header #11.</action>
+ <!-- ADD -->
+ <!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Update org.junit.jupiter:junit-jupiter from 5.6.0 to 5.7.0, #84 #109</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Update tests from Apache Commons Lang 3.9 to 3.11.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Update tests from commons-io:commons-io 2.6 to 2.8.0, #108.</action>
diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java
index a0cc612..d3c262e 100644
--- a/src/main/java/org/apache/commons/csv/CSVPrinter.java
+++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java
@@ -225,6 +225,18 @@
}
/**
+ * 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.
*
* @throws IOException
@@ -388,4 +400,20 @@
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 39e83b0..120c857 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.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 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.Test;
/**
- *
+ * Tests {@link CSVPrinter}.
*/
public class CSVPrinterTest {
@@ -86,8 +87,8 @@
}
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 @@
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 @@
@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 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");
@@ -648,10 +649,31 @@
}
@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");