src/org/objectweb/cjdbc/controller/virtualdatabase/ControllerResultSet.java

説明を見る。
00001 00025 package org.objectweb.cjdbc.controller.virtualdatabase; 00026 00027 import java.io.Serializable; 00028 import java.sql.ResultSet; 00029 import java.sql.ResultSetMetaData; 00030 import java.sql.SQLException; 00031 import java.util.ArrayList; 00032 00033 import org.objectweb.cjdbc.common.sql.AbstractRequest; 00034 import org.objectweb.cjdbc.common.sql.SelectRequest; 00035 import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache; 00036 import org.objectweb.cjdbc.driver.Field; 00037 00048 public class ControllerResultSet implements Serializable 00049 { 00051 private ArrayList data = null; 00053 private Field[] fields = null; 00055 private String cursorName = null; 00057 private int fetchSize = 0; 00059 private ResultSet dbResultSet = null; 00061 private boolean dbResultSetClosed = true; 00063 private boolean hasMoreData = false; 00065 private int maxRows = 0; 00066 00079 public ControllerResultSet(AbstractRequest request, java.sql.ResultSet rs, 00080 MetadataCache metadataCache) throws SQLException 00081 { 00082 try 00083 { 00084 if (rs == null) 00085 throw new SQLException("Null ResultSet"); 00086 00087 // This is already a result coming from another controller. 00088 //if (rs instanceof org.objectweb.cjdbc.driver.ResultSet) 00089 // return (org.objectweb.cjdbc.driver.ResultSet) rs; 00090 00091 // Build the ResultSet metaData 00092 int nbColumn; 00093 if (metadataCache != null) 00094 fields = metadataCache.getMetadata(request); 00095 00096 if (fields == null) 00097 { // Metadata Cache miss 00098 // Build the fields from the MetaData 00099 java.sql.ResultSetMetaData metaData = rs.getMetaData(); 00100 if (metaData == null) 00101 throw new SQLException("unable to fetch metadata"); 00102 nbColumn = metaData.getColumnCount(); 00103 fields = new Field[nbColumn]; 00104 for (int i = 0; i < nbColumn; i++) 00105 { 00106 // 1st column is 1 00107 String columnName = metaData.getColumnName(i + 1); 00108 if (metadataCache != null) 00109 { // Check Field cache 00110 fields[i] = metadataCache.getField(columnName); 00111 if (fields[i] != null) 00112 continue; // Cache hit 00113 } 00114 // Field cache miss 00115 String tableName = null; 00116 try 00117 { 00118 tableName = metaData.getTableName(i + 1); 00119 } 00120 catch (Exception ignore) 00121 { 00122 } 00123 int columnDisplaySize = 0; 00124 try 00125 { 00126 columnDisplaySize = metaData.getColumnDisplaySize(i + 1); 00127 } 00128 catch (Exception ignore) 00129 { 00130 } 00131 int columnType = -1; 00132 try 00133 { 00134 columnType = metaData.getColumnType(i + 1); 00135 } 00136 catch (Exception ignore) 00137 { 00138 } 00139 String columnTypeName = null; 00140 try 00141 { 00142 columnTypeName = metaData.getColumnTypeName(i + 1); 00143 } 00144 catch (Exception ignore) 00145 { 00146 } 00147 String columnClassName = null; 00148 try 00149 { 00150 columnClassName = metaData.getColumnClassName(i + 1); 00151 } 00152 catch (Exception ignore) 00153 { 00154 } 00155 boolean isAutoIncrement = false; 00156 try 00157 { 00158 isAutoIncrement = metaData.isAutoIncrement(i + 1); 00159 } 00160 catch (Exception ignore) 00161 { 00162 } 00163 boolean isCaseSensitive = false; 00164 try 00165 { 00166 isCaseSensitive = metaData.isCaseSensitive(i + 1); 00167 } 00168 catch (Exception ignore) 00169 { 00170 } 00171 boolean isCurrency = false; 00172 try 00173 { 00174 isCurrency = metaData.isCurrency(i + 1); 00175 } 00176 catch (Exception ignore) 00177 { 00178 } 00179 int isNullable = ResultSetMetaData.columnNullableUnknown; 00180 try 00181 { 00182 isNullable = metaData.isNullable(i + 1); 00183 } 00184 catch (Exception ignore) 00185 { 00186 } 00187 boolean isReadOnly = false; 00188 try 00189 { 00190 isReadOnly = metaData.isReadOnly(i + 1); 00191 } 00192 catch (Exception ignore) 00193 { 00194 } 00195 boolean isWritable = false; 00196 try 00197 { 00198 isWritable = metaData.isWritable(i + 1); 00199 } 00200 catch (Exception ignore) 00201 { 00202 } 00203 boolean isDefinitelyWritable = false; 00204 try 00205 { 00206 isReadOnly = metaData.isDefinitelyWritable(i + 1); 00207 } 00208 catch (Exception ignore) 00209 { 00210 } 00211 boolean isSearchable = false; 00212 try 00213 { 00214 isSearchable = metaData.isSearchable(i + 1); 00215 } 00216 catch (Exception ignore) 00217 { 00218 } 00219 boolean isSigned = false; 00220 try 00221 { 00222 isSigned = metaData.isSigned(i + 1); 00223 } 00224 catch (Exception ignore) 00225 { 00226 } 00227 int precision = 0; 00228 try 00229 { 00230 precision = metaData.getPrecision(i + 1); 00231 } 00232 catch (Exception ignore) 00233 { 00234 } 00235 int scale = 0; 00236 try 00237 { 00238 scale = metaData.getScale(i + 1); 00239 } 00240 catch (Exception ignore) 00241 { 00242 } 00243 fields[i] = new Field(tableName, columnName, columnDisplaySize, 00244 columnType, columnTypeName, columnClassName, isAutoIncrement, 00245 isCaseSensitive, isCurrency, isNullable, isReadOnly, isWritable, 00246 isDefinitelyWritable, isSearchable, isSigned, precision, scale); 00247 00248 if (metadataCache != null) 00249 // Add field to cache 00250 metadataCache.addField(columnName, fields[i]); 00251 } 00252 } 00253 else 00254 nbColumn = fields.length; 00255 00256 // Build the ResultSet data 00257 if (rs.next()) 00258 { 00259 if (request instanceof SelectRequest) 00260 { 00261 SelectRequest select = (SelectRequest) request; 00262 cursorName = select.getCursorName(); 00263 fetchSize = select.getFetchSize(); 00264 } 00265 maxRows = request.getMaxRows(); 00266 if (maxRows == 0) 00267 maxRows = Integer.MAX_VALUE; // Infinite number of rows 00268 00269 // Note that fetchData updates the data field 00270 dbResultSet = rs; 00271 fetchData(); 00272 if (hasMoreData && (cursorName == null)) 00273 cursorName = String.valueOf(dbResultSet.hashCode()); 00274 } 00275 else 00276 { 00277 hasMoreData = false; 00278 dbResultSet = null; 00279 dbResultSetClosed = true; 00280 rs.close(); 00281 } 00282 } 00283 catch (SQLException e) 00284 { 00285 throw new SQLException("Error while building C-JDBC ResultSet (" + e 00286 + ")"); 00287 } 00288 } 00289 00298 public ArrayList fetchData(int fetchSize) throws SQLException 00299 { 00300 this.fetchSize = fetchSize; 00301 return fetchData(); 00302 } 00303 00313 public ArrayList fetchData() throws SQLException 00314 { 00315 if (dbResultSet == null) 00316 return null; 00317 00318 Object[] row; 00319 // We directly update the data field 00320 data = new ArrayList(); 00321 int toFetch; 00322 if (fetchSize > 0) 00323 { 00324 toFetch = fetchSize < maxRows ? fetchSize : maxRows; 00325 maxRows -= toFetch; 00326 } 00327 else 00328 toFetch = maxRows; 00329 int nbColumn = fields.length; 00330 Object object; 00331 do 00332 { 00333 row = new Object[nbColumn]; 00334 for (int i = 0; i < nbColumn; i++) 00335 { 00336 object = dbResultSet.getObject(i + 1); 00337 // Convert database native Clob and Blob to C-JDBC Clob/Blobs that are 00338 // Serializable 00339 if (object!=null) 00340 { 00341 if (object instanceof java.sql.Clob) 00342 { 00343 java.sql.Clob clob = (java.sql.Clob) object; 00344 object = clob.getSubString(1, (int) clob.length()); 00345 } 00346 else if (object instanceof java.sql.Blob) 00347 { 00348 java.sql.Blob blob = (java.sql.Blob) object; 00349 object = blob.getBytes(1, (int) blob.length()); 00350 } 00351 } 00352 row[i] = object; 00353 } 00354 data.add(row); 00355 toFetch--; 00356 hasMoreData = dbResultSet.next(); 00357 } 00358 while (hasMoreData && (toFetch > 0)); 00359 if (hasMoreData && (fetchSize > 0) && (maxRows > 0)) 00360 { // More data to fetch later on 00361 maxRows += toFetch; 00362 dbResultSetClosed = false; 00363 } 00364 else 00365 { 00366 hasMoreData = false; 00367 dbResultSet.close(); 00368 dbResultSet = null; 00369 dbResultSetClosed = true; 00370 } 00371 return data; 00372 } 00373 00379 public ArrayList getData() 00380 { 00381 return data; 00382 } 00383 00389 public Field[] getFields() 00390 { 00391 return fields; 00392 } 00393 00399 public String getCursorName() 00400 { 00401 return cursorName; 00402 } 00403 00409 public boolean hasMoreData() 00410 { 00411 return hasMoreData; 00412 } 00413 00418 public void closeResultSet() 00419 { 00420 if ((dbResultSet != null) && !dbResultSetClosed) 00421 { 00422 try 00423 { 00424 dbResultSet.close(); 00425 } 00426 catch (SQLException ignore) 00427 { 00428 } 00429 dbResultSet = null; // to allow GC to work properly 00430 } 00431 } 00432 00433 }

CJDBCversion1.0.4に対してTue Oct 12 15:16:04 2004に生成されました。 doxygen 1.3.8