View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * CsvResultSetWriter writes a CSV file by mapping each column of the {@code ResultSet} to a column in CSV file using
33   * the column names stored in {@code ResultSetMetaData}
34   * 
35   * @author SingularityFX
36   * @since 2.4.0
37   */
38  public class CsvResultSetWriter extends AbstractCsvWriter implements ICsvResultSetWriter {
39  	
40  	/**
41  	 * Constructs a new {@code CsvResultSetWriter} with the supplied {@code Writer} and CSV preferences. Note that the
42  	 * {@code writer} will be wrapped in a {@code BufferedWriter} before accessed.
43  	 * 
44  	 * @param writer
45  	 *            the writer
46  	 * @param preference
47  	 *            the CSV preferences
48  	 * @throws NullPointerException
49  	 *             if writer or preference are null
50  	 */
51  	public CsvResultSetWriter(final Writer writer, final CsvPreference preference) {
52  		super(writer, preference);
53  	}
54  	
55  	/**
56  	 * {@inheritDoc}
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); // increments row and line number
64  		writeContents(resultSet); // increments row and line number before writing of each row
65  	}
66  	
67  	/**
68  	 * {@inheritDoc}
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); // increments row and line number
79  		writeContents(resultSet, writeProcessors); // increments row and line number before writing of each row
80  	}
81  	
82  	private void writeHeaders(ResultSet resultSet) throws SQLException, IOException {
83  		super.incrementRowAndLineNo(); // This will allow the correct row/line numbers to be used in any exceptions
84  										// thrown before writing occurs
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(); // This will allow the correct row/line numbers to be used in any exceptions
100 											// thrown before writing occurs
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(); // This will allow the correct row/line numbers to be used in any exceptions
115 											// thrown before writing occurs
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 }