1 package au.com.bytecode.opencsv;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.io.IOException;
19 import java.io.Reader;
20 import java.math.BigDecimal;
21 import java.sql.*;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27
28
29 public class ResultSetHelperService implements ResultSetHelper {
30 public static final int CLOBBUFFERSIZE = 2048;
31
32
33
34 static final int NVARCHAR = -9;
35 static final int NCHAR = -15;
36 static final int LONGNVARCHAR = -16;
37 static final int NCLOB = 2011;
38
39 public String[] getColumnNames(ResultSet rs) throws SQLException {
40 List<String> names = new ArrayList<String>();
41 ResultSetMetaData metadata = rs.getMetaData();
42
43 for (int i = 0; i < metadata.getColumnCount(); i++) {
44 names.add(metadata.getColumnName(i + 1));
45 }
46
47 String[] nameArray = new String[names.size()];
48 return names.toArray(nameArray);
49 }
50
51 public String[] getColumnValues(ResultSet rs) throws SQLException, IOException {
52 return this.getColumnValues(rs, false);
53 }
54
55 public String[] getColumnValues(ResultSet rs, boolean trim) throws SQLException, IOException {
56
57 List<String> values = new ArrayList<String>();
58 ResultSetMetaData metadata = rs.getMetaData();
59
60 for (int i = 0; i < metadata.getColumnCount(); i++) {
61 values.add(getColumnValue(rs, metadata.getColumnType(i + 1), i + 1, trim));
62 }
63
64 String[] valueArray = new String[values.size()];
65 return values.toArray(valueArray);
66 }
67
68 private String handleObject(Object obj) {
69 return obj == null ? "" : String.valueOf(obj);
70 }
71
72 private String handleBigDecimal(BigDecimal decimal) {
73 return decimal == null ? "" : decimal.toString();
74 }
75
76 private String handleLong(ResultSet rs, int columnIndex) throws SQLException {
77 long lv = rs.getLong(columnIndex);
78 return rs.wasNull() ? "" : Long.toString(lv);
79 }
80
81 private String handleInteger(ResultSet rs, int columnIndex) throws SQLException {
82 int i = rs.getInt(columnIndex);
83 return rs.wasNull() ? "" : Integer.toString(i);
84 }
85
86 private String handleDate(ResultSet rs, int columnIndex) throws SQLException {
87 java.sql.Date date = rs.getDate(columnIndex);
88 String value = null;
89 if (date != null) {
90 SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
91 value = dateFormat.format(date);
92 }
93 return value;
94 }
95
96 private String handleTime(Time time) {
97 return time == null ? null : time.toString();
98 }
99
100 private String handleTimestamp(Timestamp timestamp) {
101 SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
102 return timestamp == null ? null : timeFormat.format(timestamp);
103 }
104
105 private String getColumnValue(ResultSet rs, int colType, int colIndex, boolean trim)
106 throws SQLException, IOException {
107
108 String value = "";
109
110 switch (colType) {
111 case Types.BIT:
112 case Types.JAVA_OBJECT:
113 value = handleObject(rs.getObject(colIndex));
114 break;
115 case Types.BOOLEAN:
116 boolean b = rs.getBoolean(colIndex);
117 value = Boolean.valueOf(b).toString();
118 break;
119 case NCLOB:
120 case Types.CLOB:
121 Clob c = rs.getClob(colIndex);
122 if (c != null) {
123 value = read(c);
124 }
125 break;
126 case Types.BIGINT:
127 value = handleLong(rs, colIndex);
128 break;
129 case Types.DECIMAL:
130 case Types.DOUBLE:
131 case Types.FLOAT:
132 case Types.REAL:
133 case Types.NUMERIC:
134 value = handleBigDecimal(rs.getBigDecimal(colIndex));
135 break;
136 case Types.INTEGER:
137 case Types.TINYINT:
138 case Types.SMALLINT:
139 value = handleInteger(rs, colIndex);
140 break;
141 case Types.DATE:
142 value = handleDate(rs, colIndex);
143 break;
144 case Types.TIME:
145 value = handleTime(rs.getTime(colIndex));
146 break;
147 case Types.TIMESTAMP:
148 value = handleTimestamp(rs.getTimestamp(colIndex));
149 break;
150 case NVARCHAR:
151 case NCHAR:
152 case LONGNVARCHAR:
153 case Types.LONGVARCHAR:
154 case Types.VARCHAR:
155 case Types.CHAR:
156 if (trim) {
157 value = rs.getString(colIndex).trim();
158 } else {
159 value = rs.getString(colIndex);
160 }
161 break;
162 default:
163 value = "";
164 }
165
166
167 if (value == null) {
168 value = "";
169 }
170
171 return value;
172
173 }
174
175 private static String read(Clob c) throws SQLException, IOException {
176 StringBuilder sb = new StringBuilder((int) c.length());
177 Reader r = c.getCharacterStream();
178 char[] cbuf = new char[CLOBBUFFERSIZE];
179 int n;
180 while ((n = r.read(cbuf, 0, cbuf.length)) != -1) {
181 sb.append(cbuf, 0, n);
182 }
183 return sb.toString();
184 }
185 }