/* */ package au.com.bytecode.opencsv;
/* */
/* */ import java.io.Closeable;
/* */ import java.io.IOException;
/* */ import java.io.PrintWriter;
/* */ import java.io.Reader;
/* */ import java.io.Writer;
/* */ import java.math.BigDecimal;
/* */ import java.sql.Clob;
/* */ import java.sql.Date;
/* */ import java.sql.ResultSet;
/* */ import java.sql.ResultSetMetaData;
/* */ import java.sql.SQLException;
/* */ import java.sql.Time;
/* */ import java.sql.Timestamp;
/* */ import java.text.SimpleDateFormat;
/* */ import java.util.List;
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public class CSVWriter
/* */ implements Closeable
/* */ {
/* */ public static final int INITIAL_STRING_SIZE = 128;
/* */ private Writer rawWriter;
/* */ private PrintWriter pw;
/* */ private char separator;
/* */ private char quotechar;
/* */ private char escapechar;
/* */ private String lineEnd;
/* */ public static final char DEFAULT_ESCAPE_CHARACTER = '"';
/* */ public static final char DEFAULT_SEPARATOR = ',';
/* */ public static final char DEFAULT_QUOTE_CHARACTER = '"';
/* */ public static final char NO_QUOTE_CHARACTER = '\000';
/* */ public static final char NO_ESCAPE_CHARACTER = '\000';
/* */ public static final String DEFAULT_LINE_END = "\n";
/* */
/* */ public CSVWriter(Writer writer)
/* */ {
/* 85 */ this(writer, ',');
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public CSVWriter(Writer writer, char separator)
/* */ {
/* 97 */ this(writer, separator, '"');
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public CSVWriter(Writer writer, char separator, char quotechar)
/* */ {
/* 111 */ this(writer, separator, quotechar, '"');
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public CSVWriter(Writer writer, char separator, char quotechar, char escapechar)
/* */ {
/* 127 */ this(writer, separator, quotechar, escapechar, "\n");
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public CSVWriter(Writer writer, char separator, char quotechar, String lineEnd)
/* */ {
/* 144 */ this(writer, separator, quotechar, '"', lineEnd);
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd)
/* */ {
/* 164 */ this.rawWriter = writer;
/* 165 */ this.pw = new PrintWriter(writer);
/* 166 */ this.separator = separator;
/* 167 */ this.quotechar = quotechar;
/* 168 */ this.escapechar = escapechar;
/* 169 */ this.lineEnd = lineEnd;
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public void writeAll(List<String[]> allLines)
/* */ {
/* 181 */ for (String[] line : allLines) {
/* 182 */ writeNext(line);
/* */ }
/* */ }
/* */
/* */ protected void writeColumnNames(ResultSetMetaData metadata)
/* */ throws SQLException
/* */ {
/* 189 */ int columnCount = metadata.getColumnCount();
/* */
/* 191 */ String[] nextLine = new String[columnCount];
/* 192 */ for (int i = 0; i < columnCount; i++) {
/* 193 */ nextLine[i] = metadata.getColumnName(i + 1);
/* */ }
/* 195 */ writeNext(nextLine);
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public void writeAll(ResultSet rs, boolean includeColumnNames)
/* */ throws SQLException, IOException
/* */ {
/* 209 */ ResultSetMetaData metadata = rs.getMetaData();
/* */
/* */
/* 212 */ if (includeColumnNames) {
/* 213 */ writeColumnNames(metadata);
/* */ }
/* */
/* 216 */ int columnCount = metadata.getColumnCount();
/* */
/* 218 */ while (rs.next())
/* */ {
/* 220 */ String[] nextLine = new String[columnCount];
/* */
/* 222 */ for (int i = 0; i < columnCount; i++) {
/* 223 */ nextLine[i] = getColumnValue(rs, metadata.getColumnType(i + 1), i + 1);
/* */ }
/* */
/* 226 */ writeNext(nextLine);
/* */ }
/* */ }
/* */
/* */ private static String getColumnValue(ResultSet rs, int colType, int colIndex)
/* */ throws SQLException, IOException
/* */ {
/* 233 */ String value = "";
/* */
/* 235 */ switch (colType)
/* */ {
/* */ case -7:
/* 238 */ Object bit = rs.getObject(colIndex);
/* 239 */ if (bit != null) {
/* 240 */ value = String.valueOf(bit);
/* */ }
/* */ break;
/* */ case 16:
/* 244 */ boolean b = rs.getBoolean(colIndex);
/* 245 */ if (!rs.wasNull()) {
/* 246 */ value = Boolean.valueOf(b).toString();
/* */ }
/* */ break;
/* */ case 2005:
/* 250 */ Clob c = rs.getClob(colIndex);
/* 251 */ if (c != null) {
/* 252 */ value = read(c);
/* */ }
/* */ break;
/* */ case -5:
/* 256 */ long lv = rs.getLong(colIndex);
/* 257 */ if (!rs.wasNull()) {
/* 258 */ value = Long.toString(lv);
/* */ }
/* */ break;
/* */ case 2:
/* */ case 3:
/* */ case 6:
/* */ case 7:
/* */ case 8:
/* 266 */ BigDecimal bd = rs.getBigDecimal(colIndex);
/* 267 */ if (bd != null) {
/* 268 */ value = bd.toString();
/* */ }
/* */ break;
/* */ case -6:
/* */ case 4:
/* */ case 5:
/* 274 */ int intValue = rs.getInt(colIndex);
/* 275 */ if (!rs.wasNull()) {
/* 276 */ value = Integer.toString(intValue);
/* */ }
/* */ break;
/* */ case 2000:
/* 280 */ Object obj = rs.getObject(colIndex);
/* 281 */ if (obj != null) {
/* 282 */ value = String.valueOf(obj);
/* */ }
/* */ break;
/* */ case 91:
/* 286 */ Date date = rs.getDate(colIndex);
/* 287 */ if (date != null) {
/* 288 */ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
/* 289 */ value = dateFormat.format(date); }
/* 290 */ break;
/* */
/* */ case 92:
/* 293 */ Time t = rs.getTime(colIndex);
/* 294 */ if (t != null) {
/* 295 */ value = t.toString();
/* */ }
/* */ break;
/* */ case 93:
/* 299 */ Timestamp tstamp = rs.getTimestamp(colIndex);
/* 300 */ if (tstam