Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CSVWriter |
|
| 2.375;2.375 |
1 | package liquibase.util.csv; | |
2 | ||
3 | import liquibase.util.ISODateFormat; | |
4 | ||
5 | import java.io.IOException; | |
6 | import java.io.Writer; | |
7 | import java.sql.ResultSet; | |
8 | import java.sql.ResultSetMetaData; | |
9 | import java.sql.SQLException; | |
10 | ||
11 | public class CSVWriter extends liquibase.util.csv.opencsv.CSVWriter { | |
12 | 0 | private static final ISODateFormat ISO_DATE_FORMAT = new ISODateFormat(); |
13 | ||
14 | public CSVWriter(Writer writer) { | |
15 | 0 | super(writer); |
16 | 0 | } |
17 | ||
18 | public CSVWriter(Writer writer, char c) { | |
19 | 0 | super(writer, c); |
20 | 0 | } |
21 | ||
22 | public CSVWriter(Writer writer, char c, char c1) { | |
23 | 0 | super(writer, c, c1); |
24 | 0 | } |
25 | ||
26 | public CSVWriter(Writer writer, char c, char c1, char c2) { | |
27 | 0 | super(writer, c, c1, c2); |
28 | 0 | } |
29 | ||
30 | public CSVWriter(Writer writer, char c, char c1, String s) { | |
31 | 0 | super(writer, c, c1, s); |
32 | 0 | } |
33 | ||
34 | public CSVWriter(Writer writer, char c, char c1, char c2, String s) { | |
35 | 0 | super(writer, c, c1, c2, s); |
36 | 0 | } |
37 | ||
38 | @Override | |
39 | public void writeAll(ResultSet rs, boolean includeColumnNames) throws SQLException, IOException { | |
40 | 0 | ResultSetMetaData metadata = rs.getMetaData(); |
41 | ||
42 | 0 | if (includeColumnNames) { |
43 | 0 | writeColumnNames(metadata); |
44 | } | |
45 | ||
46 | 0 | int columnCount = metadata.getColumnCount(); |
47 | ||
48 | 0 | while (rs.next()) { |
49 | 0 | String[] nextLine = new String[columnCount]; |
50 | ||
51 | 0 | for (int i = 0; i < columnCount; i++) { |
52 | 0 | nextLine[i] = getColumnValue(rs, metadata.getColumnType(i + 1), i + 1); |
53 | } | |
54 | ||
55 | 0 | writeNext(nextLine); |
56 | 0 | } |
57 | 0 | } |
58 | ||
59 | private String getColumnValue(ResultSet rs, int colType, int colIndex) throws SQLException, IOException { | |
60 | ||
61 | 0 | Object value = rs.getObject(colIndex); |
62 | 0 | if (rs.wasNull()) { |
63 | 0 | return "NULL"; |
64 | } | |
65 | ||
66 | 0 | if (value instanceof java.sql.Date) { |
67 | 0 | return ISO_DATE_FORMAT.format((java.sql.Date) value); |
68 | 0 | } else if (value instanceof java.sql.Time) { |
69 | 0 | return ISO_DATE_FORMAT.format((java.sql.Time) value); |
70 | 0 | } else if (value instanceof java.sql.Timestamp) { |
71 | 0 | return ISO_DATE_FORMAT.format((java.sql.Timestamp) value); |
72 | // } else if (value instanceof oracle.sql.TIMESTAMP) { | |
73 | // return ISO_DATE_FORMAT.format((oracle.sql.TIMESTAMP) value); | |
74 | } else { | |
75 | 0 | return value.toString(); |
76 | } | |
77 | ||
78 | // if (colType == Types.BIT) { | |
79 | // Object bit = rs.getObject(colIndex); | |
80 | // if (rs.wasNull()) { | |
81 | // return null; | |
82 | // } else { | |
83 | // return String.valueOf(bit); | |
84 | // } | |
85 | // } else if (colType == Types.BOOLEAN) { | |
86 | // boolean b = rs.getBoolean(colIndex); | |
87 | // if (rs.wasNull()) { | |
88 | // return null; | |
89 | // } else { | |
90 | // return Boolean.valueOf(b).toString(); | |
91 | // } | |
92 | // } else if (colType == Types.CLOB) { | |
93 | // Clob c = rs.getClob(colIndex); | |
94 | // if (rs.wasNull()) { | |
95 | // return null; | |
96 | // } else { | |
97 | // return read(c); | |
98 | // } | |
99 | // } else if (colType == Types.BIGINT | |
100 | // || colType == Types.DECIMAL | |
101 | // || colType == Types.DOUBLE | |
102 | // || colType == Types.FLOAT | |
103 | // || colType == Types.REAL | |
104 | // || colType == Types.NUMERIC) { | |
105 | // BigDecimal bd = rs.getBigDecimal(colIndex); | |
106 | // if (rs.wasNull()) { | |
107 | // return null; | |
108 | // } else { | |
109 | // return String.valueOf(bd.doubleValue()); | |
110 | // } | |
111 | // | |
112 | // } else if (colType == Types.INTEGER | |
113 | // || colType == Types.TINYINT | |
114 | // || colType == Types.SMALLINT) { | |
115 | // int intValue = rs.getInt(colIndex); | |
116 | // if (rs.wasNull()) { | |
117 | // return null; | |
118 | // } else { | |
119 | // return String.valueOf(intValue); | |
120 | // } | |
121 | // } | |
122 | // | |
123 | // case Types.JAVA_OBJECT: | |
124 | // Object obj = rs.getObject(colIndex); | |
125 | // if (obj != null) { | |
126 | // value = String.valueOf(obj); | |
127 | // } | |
128 | // break; | |
129 | // case Types.DATE: | |
130 | // java.sql.Date date = rs.getDate(colIndex); | |
131 | // if (date != null) { | |
132 | // value = ISO_DATE_FORMAT.format(date); | |
133 | // ; | |
134 | // } | |
135 | // break; | |
136 | // case Types.TIME: | |
137 | // Time t = rs.getTime(colIndex); | |
138 | // if (t != null) { | |
139 | // value = t.toString(); | |
140 | // } | |
141 | // break; | |
142 | // case Types.TIMESTAMP: | |
143 | // Timestamp tstamp = rs.getTimestamp(colIndex); | |
144 | // if (tstamp != null) { | |
145 | // value = ISO_DATE_FORMAT.format(tstamp); | |
146 | // } | |
147 | // break; | |
148 | // case Types.LONGVARCHAR: | |
149 | // case Types.VARCHAR: | |
150 | // case Types.CHAR: | |
151 | // value = rs.getString(colIndex); | |
152 | // break; | |
153 | // default: | |
154 | // value = ""; | |
155 | // } | |
156 | ||
157 | // if (value == null) | |
158 | // | |
159 | // { | |
160 | // value = ""; | |
161 | // } | |
162 | // | |
163 | // return value; | |
164 | ||
165 | } | |
166 | ||
167 | // private static String read(Clob c) throws SQLException, IOException { | |
168 | // StringBuffer sb = new StringBuffer((int) c.length()); | |
169 | // Reader r = c.getCharacterStream(); | |
170 | // char[] cbuf = new char[2048]; | |
171 | // int n = 0; | |
172 | // while ((n = r.read(cbuf, 0, cbuf.length)) != -1) { | |
173 | // if (n > 0) { | |
174 | // sb.append(cbuf, 0, n); | |
175 | // } | |
176 | // } | |
177 | // return sb.toString(); | |
178 | // } | |
179 | ||
180 | } |