-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRow.java
95 lines (83 loc) · 2.82 KB
/
Row.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package denodo_integration_mojo;
import java.math.BigDecimal;
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.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Arun Aryasomayajula
*
*/
public class Row {
public List<Entry<Object, Class>> row;
public static Map<String, Class> TYPE;
static {
TYPE = new HashMap<String, Class>();
TYPE.put("INTEGER", Integer.class);
TYPE.put("TINYINT", Byte.class);
TYPE.put("SMALLINT", Short.class);
TYPE.put("BIGINT", Long.class);
TYPE.put("REAL", Float.class);
TYPE.put("FLOAT", Double.class);
TYPE.put("DOUBLE", Double.class);
TYPE.put("DECIMAL", BigDecimal.class);
TYPE.put("NUMERIC", BigDecimal.class);
TYPE.put("BOOLEAN", Boolean.class);
TYPE.put("CHAR", String.class);
TYPE.put("VARCHAR", String.class);
TYPE.put("LONGVARCHAR", String.class);
TYPE.put("DATE", Date.class);
TYPE.put("TIME", Time.class);
TYPE.put("TIMESTAMP", Timestamp.class);
TYPE.put("SERIAL",Integer.class);
TYPE.put("INT", Integer.class);
// ...
}
public Row() {
row = new ArrayList<Entry<Object, Class>>();
}
public <T> void add(T data) {
row.add(new AbstractMap.SimpleImmutableEntry<Object,Class>(data, data.getClass()));
}
public void add(Object data, String sqlType) {
Class castType = Row.TYPE.get(sqlType.toUpperCase());
try {
if (data != null) {
this.add(castType.cast(data));}
} catch (NullPointerException e) {
e.printStackTrace();
Logger lgr = Logger.getLogger(Row.class.getName());
lgr.log(Level.SEVERE, e.getMessage()+" Add the type "+sqlType+" to the TYPE hash map in the Row class.", e);
throw e;
}
}
public static void formTable(ResultSet rs, List<Row> table)
throws SQLException {
if (rs == null)
return;
ResultSetMetaData rsmd;
try {
rsmd = rs.getMetaData();
int NumOfCol = rsmd.getColumnCount();
while (rs.next()) {
Row current_row = new Row();
for (int i = 1; i <= NumOfCol; i++) {
current_row.add(rs.getObject(i), rsmd.getColumnTypeName(i));
}
table.add(current_row);
}
} catch (SQLException e) {
throw e;
}
}
}