root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / db / JDBCSupport.java @ 10627
History | View | Annotate | Download (7.86 KB)
1 |
package com.hardcode.gdbms.engine.data.db; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.ResultSet; |
5 |
import java.sql.SQLException; |
6 |
import java.sql.Statement; |
7 |
import java.sql.Time; |
8 |
import java.sql.Timestamp; |
9 |
import java.sql.Types; |
10 |
import java.util.Date; |
11 |
|
12 |
import com.hardcode.gdbms.driver.exceptions.BadFieldDriverException; |
13 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
14 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
15 |
import com.hardcode.gdbms.engine.data.driver.ReadAccess; |
16 |
import com.hardcode.gdbms.engine.values.Value; |
17 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
18 |
|
19 |
|
20 |
/**
|
21 |
* DBDrivers helper class
|
22 |
*/
|
23 |
public class JDBCSupport implements ReadAccess { |
24 |
private ResultSet resultSet; |
25 |
private int rowCount = -1; |
26 |
|
27 |
/**
|
28 |
* Creates a new JDBCSupport object.
|
29 |
*
|
30 |
* @param r ResultSet that will be used to return the methods values
|
31 |
*/
|
32 |
JDBCSupport(ResultSet r) {
|
33 |
resultSet = r; |
34 |
} |
35 |
|
36 |
/**
|
37 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
|
38 |
* int)
|
39 |
*/
|
40 |
public Value getFieldValue(long rowIndex, int fieldId) |
41 |
throws ReadDriverException {
|
42 |
Value value = null;
|
43 |
|
44 |
try {
|
45 |
fieldId += 1;
|
46 |
resultSet.absolute((int) rowIndex + 1); |
47 |
|
48 |
int type = resultSet.getMetaData().getColumnType(fieldId);
|
49 |
|
50 |
switch (type) {
|
51 |
case Types.BIGINT: |
52 |
value = ValueFactory.createValue(resultSet.getLong(fieldId)); |
53 |
|
54 |
break;
|
55 |
|
56 |
case Types.BIT: |
57 |
case Types.BOOLEAN: |
58 |
value = ValueFactory.createValue(resultSet.getBoolean( |
59 |
fieldId)); |
60 |
|
61 |
break;
|
62 |
|
63 |
case Types.CHAR: |
64 |
case Types.VARCHAR: |
65 |
case Types.LONGVARCHAR: |
66 |
String auxString = resultSet.getString(fieldId);
|
67 |
if (auxString != null) { |
68 |
value = ValueFactory.createValue(auxString); |
69 |
} |
70 |
|
71 |
break;
|
72 |
|
73 |
case Types.DATE: |
74 |
Date auxDate = resultSet.getDate(fieldId);
|
75 |
if (auxDate != null){ |
76 |
value = ValueFactory.createValue(auxDate); |
77 |
} |
78 |
|
79 |
break;
|
80 |
|
81 |
case Types.DECIMAL: |
82 |
case Types.NUMERIC: |
83 |
case Types.FLOAT: |
84 |
case Types.DOUBLE: |
85 |
value = ValueFactory.createValue(resultSet.getDouble( |
86 |
fieldId)); |
87 |
|
88 |
break;
|
89 |
|
90 |
case Types.INTEGER: |
91 |
value = ValueFactory.createValue(resultSet.getInt(fieldId)); |
92 |
|
93 |
break;
|
94 |
|
95 |
case Types.REAL: |
96 |
value = ValueFactory.createValue(resultSet.getFloat(fieldId)); |
97 |
|
98 |
break;
|
99 |
|
100 |
case Types.SMALLINT: |
101 |
value = ValueFactory.createValue(resultSet.getShort(fieldId)); |
102 |
|
103 |
break;
|
104 |
|
105 |
case Types.TINYINT: |
106 |
value = ValueFactory.createValue(resultSet.getByte(fieldId)); |
107 |
|
108 |
break;
|
109 |
|
110 |
case Types.BINARY: |
111 |
case Types.VARBINARY: |
112 |
case Types.LONGVARBINARY: |
113 |
byte[] auxByteArray = resultSet.getBytes(fieldId); |
114 |
if (auxByteArray != null){ |
115 |
value = ValueFactory.createValue(auxByteArray); |
116 |
} |
117 |
|
118 |
break;
|
119 |
|
120 |
case Types.TIMESTAMP: |
121 |
try
|
122 |
{ |
123 |
Timestamp auxTimeStamp = resultSet.getTimestamp(fieldId);
|
124 |
value = ValueFactory.createValue(auxTimeStamp); |
125 |
} |
126 |
catch (SQLException e) |
127 |
{ |
128 |
value = ValueFactory.createValue(new Timestamp(0)); |
129 |
} |
130 |
|
131 |
break;
|
132 |
|
133 |
case Types.TIME: |
134 |
try
|
135 |
{ |
136 |
Time auxTime = resultSet.getTime(fieldId);
|
137 |
value = ValueFactory.createValue(auxTime); |
138 |
} |
139 |
catch (SQLException e) |
140 |
{ |
141 |
value = ValueFactory.createValue(new Time(0)); |
142 |
} |
143 |
|
144 |
|
145 |
break;
|
146 |
|
147 |
default:
|
148 |
auxString = resultSet.getString(fieldId); |
149 |
if (auxString != null) { |
150 |
value = ValueFactory.createValue(auxString); |
151 |
} |
152 |
|
153 |
break;
|
154 |
} |
155 |
|
156 |
if (resultSet.wasNull()) {
|
157 |
return ValueFactory.createNullValue();
|
158 |
} else {
|
159 |
return value;
|
160 |
} |
161 |
} catch (SQLException e) { |
162 |
throw new BadFieldDriverException("JDBC",e); |
163 |
} |
164 |
} |
165 |
|
166 |
/**
|
167 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
|
168 |
*/
|
169 |
public int getFieldCount() throws ReadDriverException { |
170 |
try {
|
171 |
return resultSet.getMetaData().getColumnCount();
|
172 |
} catch (SQLException e) { |
173 |
throw new ReadDriverException("JDBC",e); |
174 |
} |
175 |
} |
176 |
|
177 |
/**
|
178 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
|
179 |
*/
|
180 |
public String getFieldName(int fieldId) throws ReadDriverException { |
181 |
try {
|
182 |
return resultSet.getMetaData().getColumnName(fieldId + 1); |
183 |
} catch (SQLException e) { |
184 |
throw new ReadDriverException("JDBC",e); |
185 |
} |
186 |
} |
187 |
|
188 |
/**
|
189 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
|
190 |
*/
|
191 |
public long getRowCount() throws ReadDriverException { |
192 |
try {
|
193 |
if (rowCount == -1) { |
194 |
resultSet.last(); |
195 |
rowCount = resultSet.getRow(); |
196 |
} |
197 |
|
198 |
return rowCount;
|
199 |
} catch (SQLException e) { |
200 |
throw new RuntimeException(e); |
201 |
} |
202 |
} |
203 |
|
204 |
/**
|
205 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
|
206 |
*/
|
207 |
public int getFieldType(int i) throws ReadDriverException { |
208 |
try {
|
209 |
return resultSet.getMetaData().getColumnType(i + 1); |
210 |
} catch (SQLException e) { |
211 |
throw new ReadDriverException("JDBC",e); |
212 |
} |
213 |
} |
214 |
|
215 |
/**
|
216 |
* Closes the internal data source
|
217 |
*
|
218 |
* @throws SQLException if the operation fails
|
219 |
*/
|
220 |
public void close() throws SQLException { |
221 |
resultSet.close(); |
222 |
} |
223 |
|
224 |
/**
|
225 |
* Creates a new JDBCSuuport object with the data retrieved from the
|
226 |
* connection with the given sql
|
227 |
*
|
228 |
* @param con Connection to the database
|
229 |
* @param sql SQL defining the data to use
|
230 |
*
|
231 |
* @return JDBCSupport
|
232 |
*
|
233 |
* @throws SQLException If the data cannot be retrieved
|
234 |
*/
|
235 |
public static JDBCSupport newJDBCSupport(Connection con, String sql) |
236 |
throws SQLException { |
237 |
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, |
238 |
ResultSet.CONCUR_READ_ONLY);
|
239 |
ResultSet res = st.executeQuery(sql);
|
240 |
|
241 |
return new JDBCSupport(res); |
242 |
} |
243 |
|
244 |
/**
|
245 |
* Executes a query with the 'con' connection
|
246 |
*
|
247 |
* @param con connection
|
248 |
* @param sql instruction to execute
|
249 |
*
|
250 |
* @throws SQLException if execution fails
|
251 |
*/
|
252 |
public static void execute(Connection con, String sql) |
253 |
throws SQLException { |
254 |
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, |
255 |
ResultSet.CONCUR_UPDATABLE);
|
256 |
st.execute(sql); |
257 |
} |
258 |
|
259 |
/**
|
260 |
* @return
|
261 |
*/
|
262 |
public ResultSet getResultSet() { |
263 |
return resultSet;
|
264 |
} |
265 |
|
266 |
public int getFieldWidth(int i) throws ReadDriverException { |
267 |
int width;
|
268 |
try {
|
269 |
width = resultSet.getMetaData().getColumnDisplaySize(i + 1);
|
270 |
} catch (SQLException e) { |
271 |
throw new ReadDriverException("JDBC",e); |
272 |
} |
273 |
if (width < 0) return 255; |
274 |
return width;
|
275 |
|
276 |
} |
277 |
} |