[CSV-123] Add possibility to use ResultSet header meta data as CSV
header #11.
This commit is contained in:
parent
0b01ea8211
commit
cfd55c9473
|
@ -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-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-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>
|
<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 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 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>
|
<action type="update" dev="ggregory" due-to="Gary Gregory">Update tests from commons-io:commons-io 2.6 to 2.8.0, #108.</action>
|
||||||
|
|
|
@ -224,6 +224,18 @@ public final class CSVPrinter implements Flushable, Closeable {
|
||||||
println();
|
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.
|
* Outputs the record separator.
|
||||||
*
|
*
|
||||||
|
@ -388,4 +400,20 @@ public final class CSVPrinter implements Flushable, Closeable {
|
||||||
println();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
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.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
|
@ -29,13 +30,13 @@ import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
import java.io.CharArrayWriter;
|
import java.io.CharArrayWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.BatchUpdateException;
|
import java.sql.BatchUpdateException;
|
||||||
|
@ -62,7 +63,7 @@ import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Tests {@link CSVPrinter}.
|
||||||
*/
|
*/
|
||||||
public class CSVPrinterTest {
|
public class CSVPrinterTest {
|
||||||
|
|
||||||
|
@ -86,8 +87,8 @@ public class CSVPrinterTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private final String recordSeparator = CSVFormat.DEFAULT.getRecordSeparator();
|
private final String recordSeparator = CSVFormat.DEFAULT.getRecordSeparator();
|
||||||
private String longText2;
|
|
||||||
|
|
||||||
|
private String longText2;
|
||||||
private void doOneRandom(final CSVFormat format) throws Exception {
|
private void doOneRandom(final CSVFormat format) throws Exception {
|
||||||
final Random r = new Random();
|
final Random r = new Random();
|
||||||
|
|
||||||
|
@ -140,7 +141,7 @@ public class CSVPrinterTest {
|
||||||
return fixed;
|
return fixed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Connection geH2Connection() throws SQLException, ClassNotFoundException {
|
private Connection getH2Connection() throws SQLException, ClassNotFoundException {
|
||||||
Class.forName("org.h2.Driver");
|
Class.forName("org.h2.Driver");
|
||||||
return DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
|
return DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
|
||||||
}
|
}
|
||||||
|
@ -620,7 +621,7 @@ public class CSVPrinterTest {
|
||||||
@Test
|
@Test
|
||||||
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
|
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
try (final Connection connection = geH2Connection()) {
|
try (final Connection connection = getH2Connection()) {
|
||||||
setUpTable(connection);
|
setUpTable(connection);
|
||||||
try (final Statement stmt = connection.createStatement();
|
try (final Statement stmt = connection.createStatement();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
|
@ -635,7 +636,7 @@ public class CSVPrinterTest {
|
||||||
public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException {
|
public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
Class.forName("org.h2.Driver");
|
Class.forName("org.h2.Driver");
|
||||||
try (final Connection connection = geH2Connection()) {
|
try (final Connection connection = getH2Connection()) {
|
||||||
setUpTable(connection);
|
setUpTable(connection);
|
||||||
try (final Statement stmt = connection.createStatement();
|
try (final Statement stmt = connection.createStatement();
|
||||||
final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");
|
final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");
|
||||||
|
@ -647,11 +648,32 @@ public class CSVPrinterTest {
|
||||||
+ "\"" + recordSeparator, sw.toString());
|
+ "\"" + 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
|
@Test
|
||||||
public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotFoundException, SQLException {
|
public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotFoundException, SQLException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
Class.forName("org.h2.Driver");
|
Class.forName("org.h2.Driver");
|
||||||
try (final Connection connection = geH2Connection()) {
|
try (final Connection connection = getH2Connection()) {
|
||||||
setUpTable(connection);
|
setUpTable(connection);
|
||||||
try (final Statement stmt = connection.createStatement();
|
try (final Statement stmt = connection.createStatement();
|
||||||
final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");
|
final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");
|
||||||
|
|
Loading…
Reference in New Issue