-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathJDBCResultPull.java
More file actions
143 lines (130 loc) · 4.89 KB
/
JDBCResultPull.java
File metadata and controls
143 lines (130 loc) · 4.89 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package info.urbanek.Rpackage.RJDBC;
import java.sql.ResultSet;
public class JDBCResultPull {
/** column type: string */
public static final int CT_STRING = 0;
/** column type: numeric (retrieved as doubles) */
public static final int CT_NUMERIC = 1;
public static final int CT_INTEGER = 2;
/** NA double value */
public static final double NA_double = Double.longBitsToDouble(0x7ff00000000007a2L);
public static final int NA_integer = -2147483648;
/** active result set */
ResultSet rs;
/** column types */
int cTypes[];
/** pulled arrays */
Object data[];
/** capacity of the arrays */
int capacity;
/** number of loaded rows */
int count;
/** number of columns */
int cols;
/** create a JDBCResultPull from teh current set with the
* specified column types. The column type definition must match
* the result set, no checks are performed.
* @param rs active result set
* @param cTypes column types (see <code>CT_xx</code> constants)
*/
public JDBCResultPull(ResultSet rs, int cTypes[]) {
this.rs = rs;
this.cTypes = cTypes;
cols = (cTypes == null) ? 0 : cTypes.length;
data = new Object[cols];
capacity = -1;
count = 0;
}
/** retrieve the number of columns */
public int columns() { return cols; }
/** get the number of loaded rows */
public int count() { return count; }
/** allocate arrays for the given capacity. Normally this method
* is not called directly since @link{fetch()} automatically
* allocates necessary space, but it can be used to reduce the
* array sizes when idle (e.g., by setting the capacity to 0).
* @param atMost maximum capacity of the buffers
*/
public void setCapacity(int atMost) {
if (capacity != atMost) {
for (int i = 0; i < cols; i++)
//data[i] = (cTypes[i] == CT_NUMERIC) ? (Object)new double[atMost] : (Object)new String[atMost];
if(cTypes[i] == CT_NUMERIC)
data[i] = (Object)new double[atMost];
else if(cTypes[i] == CT_INTEGER)
data[i] = (Object)new int[atMost];
else
data[i] = (Object)new String[atMost];
capacity = atMost;
}
}
/** fetch records from the result set into column arrays. It
* replaces any existing data in the buffers.
* @param atMost the maximum number of rows to be retrieved
* @param fetchSize fetch size hint to be sent to the driver. Note
* that some databases don't support fetch sizes larger than
* 32767. If less than 1 the fetch size is not changed.
* @return number of rows retrieved
*/
public int fetch(int atMost, int fetchSize) throws java.sql.SQLException {
setCapacity(atMost);
if (fetchSize > 0) {
try { // run in a try since it's a hint, but some bad drivers fail on it anyway (see #11)
rs.setFetchSize(fetchSize);
} catch (java.sql.SQLException e) { } // we can't use SQLFeatureNotSupportedException because that's 1.6+ only
}
count = 0;
while (rs.next()) {
for (int i = 0; i < cols; i++)
if (cTypes[i] == CT_NUMERIC) {
double val = rs.getDouble(i + 1);
if (rs.wasNull()) val = NA_double;
((double[])data[i])[count] = val;
} else if(cTypes[i] == CT_INTEGER) {
int val = rs.getInt(i + 1);
if (rs.wasNull()) val = NA_integer;
((int[])data[i])[count] = val;
} else
((String[])data[i])[count] = rs.getString(i + 1);
count++;
if (count >= capacity)
return count;
}
return count;
}
/** retrieve column data
* @param column 1-based index of the column
* @return column object or <code>null</code> if non-existent */
public Object getColumnData(int column) {
return (column > 0 && column <= cols) ? data[column - 1] : null;
}
/** retrieve string column data truncated to count - performs NO
* checking and can raise exceptions
* @param column 1-based index of the column
* @return column object or <code>null</code> if non-existent */
public String[] getStrings(int column) {
String[] a = (String[]) data[column - 1];
if (count == a.length) return a;
String[] b = new String[count];
if (count > 0) System.arraycopy(a, 0, b, 0, count);
return b;
}
/** retrieve numeric column data truncated to count - performs NO
* checking and can raise exceptions
* @param column 1-based index of the column
* @return column object or <code>null</code> if non-existent */
public double[] getDoubles(int column) {
double[] a = (double[]) data[column - 1];
if (count == a.length) return a;
double[] b = new double[count];
if (count > 0) System.arraycopy(a, 0, b, 0, count);
return b;
}
public int[] getIntegers(int column) {
int[] a = (int[]) data[column - 1];
if(count == a.length) return a;
int[] b = new int[count];
if(count > 0) System.arraycopy(a, 0, b, 0, count);
return b;
}
}