Skip to content

Commit cfd55c9

Browse files
committed
[CSV-123] Add possibility to use ResultSet header meta data as CSV
header apache#11.
1 parent 0b01ea8 commit cfd55c9

3 files changed

Lines changed: 61 additions & 9 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
<action issue="CSV-149" type="fix" dev="ggregory" due-to="Kranthi, Gary Gregory, Brent Worden, dota17">Line number is not proper at EOF.</action>
5656
<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>
5757
<action issue="CSV-267" type="fix" dev="ggregory" due-to="Arturo Bernal">Minor improvements #126, #127.</action>
58-
<!-- UPDATES -->
58+
<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>
59+
<!-- ADD -->
60+
<!-- UPDATE -->
5961
<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>
6062
<action type="update" dev="ggregory" due-to="Gary Gregory">Update tests from Apache Commons Lang 3.9 to 3.11.</action>
6163
<action type="update" dev="ggregory" due-to="Gary Gregory">Update tests from commons-io:commons-io 2.6 to 2.8.0, #108.</action>

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ public void printComment(final String comment) throws IOException {
224224
println();
225225
}
226226

227+
/**
228+
* Prints headers for a result set based on its metadata.
229+
*
230+
* @param resultSet The result set to query for metadata.
231+
* @throws IOException If an I/O error occurs.
232+
* @throws SQLException If a database access error occurs or this method is called on a closed result set.
233+
* @since 1.9.0
234+
*/
235+
public void printHeaders(final ResultSet resultSet) throws IOException, SQLException {
236+
printRecord((Object[]) format.withHeader(resultSet).getHeader());
237+
}
238+
227239
/**
228240
* Outputs the record separator.
229241
*
@@ -388,4 +400,20 @@ public void printRecords(final ResultSet resultSet) throws SQLException, IOExcep
388400
println();
389401
}
390402
}
403+
404+
/**
405+
* Prints all the objects with metadata in the given JDBC result set based on the header boolean.
406+
*
407+
* @param resultSet result set the values to print.
408+
* @param printHeader Boolean value to print header or not.
409+
* @throws IOException If an I/O error occurs
410+
* @throws SQLException if a database access error occurs
411+
* @since 1.9.0
412+
*/
413+
public void printRecords(final ResultSet resultSet, final boolean printHeader) throws SQLException, IOException {
414+
if (printHeader) {
415+
printHeaders(resultSet);
416+
}
417+
printRecords(resultSet);
418+
}
391419
}

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2222
import static org.junit.jupiter.api.Assertions.assertEquals;
2323
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2425
import static org.junit.jupiter.api.Assertions.assertThrows;
2526
import static org.mockito.Mockito.mock;
2627
import static org.mockito.Mockito.never;
@@ -29,13 +30,13 @@
2930

3031
import java.io.CharArrayWriter;
3132
import java.io.File;
33+
import java.io.FileReader;
3234
import java.io.IOException;
3335
import java.io.PrintStream;
36+
import java.io.Reader;
3437
import java.io.StringReader;
3538
import java.io.StringWriter;
3639
import java.io.Writer;
37-
import java.io.Reader;
38-
import java.io.FileReader;
3940
import java.nio.charset.Charset;
4041
import java.nio.charset.StandardCharsets;
4142
import java.sql.BatchUpdateException;
@@ -62,7 +63,7 @@
6263
import org.junit.jupiter.api.Test;
6364

6465
/**
65-
*
66+
* Tests {@link CSVPrinter}.
6667
*/
6768
public class CSVPrinterTest {
6869

@@ -86,8 +87,8 @@ private static String printable(final String s) {
8687
}
8788

8889
private final String recordSeparator = CSVFormat.DEFAULT.getRecordSeparator();
89-
private String longText2;
9090

91+
private String longText2;
9192
private void doOneRandom(final CSVFormat format) throws Exception {
9293
final Random r = new Random();
9394

@@ -140,7 +141,7 @@ private <T> T[] expectNulls(final T[] original, final CSVFormat csvFormat) {
140141
return fixed;
141142
}
142143

143-
private Connection geH2Connection() throws SQLException, ClassNotFoundException {
144+
private Connection getH2Connection() throws SQLException, ClassNotFoundException {
144145
Class.forName("org.h2.Driver");
145146
return DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
146147
}
@@ -620,7 +621,7 @@ public void testInvalidFormat() {
620621
@Test
621622
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
622623
final StringWriter sw = new StringWriter();
623-
try (final Connection connection = geH2Connection()) {
624+
try (final Connection connection = getH2Connection()) {
624625
setUpTable(connection);
625626
try (final Statement stmt = connection.createStatement();
626627
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
@@ -635,7 +636,7 @@ public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLExc
635636
public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException {
636637
final StringWriter sw = new StringWriter();
637638
Class.forName("org.h2.Driver");
638-
try (final Connection connection = geH2Connection()) {
639+
try (final Connection connection = getH2Connection()) {
639640
setUpTable(connection);
640641
try (final Statement stmt = connection.createStatement();
641642
final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");
@@ -647,11 +648,32 @@ public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundExce
647648
+ "\"" + recordSeparator, sw.toString());
648649
}
649650

651+
@Test
652+
public void testJdbcPrinterWithResultSetHeader() throws IOException, ClassNotFoundException, SQLException {
653+
final StringWriter sw = new StringWriter();
654+
try (final Connection connection = getH2Connection()) {
655+
setUpTable(connection);
656+
try (final Statement stmt = connection.createStatement();
657+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);) {
658+
try (final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST")) {
659+
printer.printRecords(resultSet, true);
660+
assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator,
661+
sw.toString());
662+
}
663+
try (final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST")) {
664+
printer.printRecords(resultSet, false);
665+
assertNotEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator,
666+
sw.toString());
667+
}
668+
}
669+
}
670+
}
671+
650672
@Test
651673
public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotFoundException, SQLException {
652674
final StringWriter sw = new StringWriter();
653675
Class.forName("org.h2.Driver");
654-
try (final Connection connection = geH2Connection()) {
676+
try (final Connection connection = getH2Connection()) {
655677
setUpTable(connection);
656678
try (final Statement stmt = connection.createStatement();
657679
final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");

0 commit comments

Comments
 (0)