1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.supercsv.io;
18
19 import java.io.IOException;
20 import java.io.Writer;
21 import java.sql.ResultSet;
22 import java.sql.ResultSetMetaData;
23 import java.sql.SQLException;
24 import java.util.LinkedList;
25 import java.util.List;
26
27 import org.supercsv.cellprocessor.ift.CellProcessor;
28 import org.supercsv.prefs.CsvPreference;
29 import org.supercsv.util.Util;
30
31
32
33
34
35
36
37
38 public class CsvResultSetWriter extends AbstractCsvWriter implements ICsvResultSetWriter {
39
40
41
42
43
44
45
46
47
48
49
50
51 public CsvResultSetWriter(final Writer writer, final CsvPreference preference) {
52 super(writer, preference);
53 }
54
55
56
57
58 public void write(final ResultSet resultSet) throws SQLException, IOException {
59 if( resultSet == null ) {
60 throw new NullPointerException("ResultSet cannot be null");
61 }
62
63 writeHeaders(resultSet);
64 writeContents(resultSet);
65 }
66
67
68
69
70 public void write(ResultSet resultSet, CellProcessor[] writeProcessors) throws SQLException, IOException {
71 if( resultSet == null ) {
72 throw new NullPointerException("ResultSet cannot be null");
73 }
74 if( writeProcessors == null ) {
75 throw new NullPointerException("CellProcessor[] cannot be null");
76 }
77
78 writeHeaders(resultSet);
79 writeContents(resultSet, writeProcessors);
80 }
81
82 private void writeHeaders(ResultSet resultSet) throws SQLException, IOException {
83 super.incrementRowAndLineNo();
84
85
86 final ResultSetMetaData meta = resultSet.getMetaData();
87 final int numberOfColumns = meta.getColumnCount();
88 final List<Object> headers = new LinkedList<Object>();
89 for( int columnIndex = 1; columnIndex <= numberOfColumns; columnIndex++ ) {
90 headers.add(meta.getColumnName(columnIndex));
91 }
92 super.writeRow(headers);
93 }
94
95 private void writeContents(ResultSet resultSet) throws SQLException, IOException {
96 final int numberOfColumns = resultSet.getMetaData().getColumnCount();
97 final List<Object> objects = new LinkedList<Object>();
98 while( resultSet.next() ) {
99 super.incrementRowAndLineNo();
100
101 objects.clear();
102 for( int columnIndex = 1; columnIndex <= numberOfColumns; columnIndex++ ) {
103 objects.add(resultSet.getObject(columnIndex));
104 }
105 super.writeRow(objects);
106 }
107 }
108
109 private void writeContents(ResultSet resultSet, CellProcessor[] writeProcessors) throws SQLException, IOException {
110 final int numberOfColumns = resultSet.getMetaData().getColumnCount();
111 final List<Object> objects = new LinkedList<Object>();
112 final List<Object> processedColumns = new LinkedList<Object>();
113 while( resultSet.next() ) {
114 super.incrementRowAndLineNo();
115
116 objects.clear();
117 for( int columnIndex = 1; columnIndex <= numberOfColumns; columnIndex++ ) {
118 objects.add(resultSet.getObject(columnIndex));
119 }
120 Util.executeCellProcessors(processedColumns, objects, writeProcessors, getLineNumber(), getRowNumber());
121 super.writeRow(processedColumns);
122 }
123 }
124 }