Public Member Functions | |
ControllerResultSet (AbstractRequest request, java.sql.ResultSet rs, MetadataCache metadataCache, Statement s) throws SQLException | |
ArrayList | fetchData (int fetchSize) throws SQLException |
ArrayList | fetchData () throws SQLException |
ArrayList | getData () |
Field[] | getFields () |
String | getCursorName () |
boolean | hasMoreData () |
void | closeResultSet () |
ControllerResultSet
is a lightweight ResultSet for the controller side. It only contains row data and column metadata. The real ResultSet is constructed on by the driver on the client side from the ControllerResultSet information.
Definition at line 49 of file ControllerResultSet.java.
|
Build a C-JDBC ResultSet from a database specific ResultSet. The metadata can be retrieved from the MetadataCache if provided. If a metadata cache is provided but the data is not in the cache, the MetadataCache is updated accordingly. The remaining code is a straightforward copy of both metadata and data. The statement used to execute the query will be closed when the ResultSet has been completely copied or when the ResultSet is closed while in streaming mode.
Definition at line 87 of file ControllerResultSet.java. References org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet.fetchData(), org.objectweb.cjdbc.common.sql.SelectRequest.getCursorName(), org.objectweb.cjdbc.common.sql.AbstractRequest.getFetchSize(), and org.objectweb.cjdbc.common.sql.AbstractRequest.getMaxRows(). 00089 { 00090 this.owningStatement = s; 00091 try 00092 { 00093 if (rs == null) 00094 throw new SQLException("Null ResultSet"); 00095 00096 // This is already a result coming from another controller. 00097 //if (rs instanceof org.objectweb.cjdbc.driver.ResultSet) 00098 // return (org.objectweb.cjdbc.driver.ResultSet) rs; 00099 00100 // Build the ResultSet metaData 00101 int nbColumn; 00102 if (metadataCache != null) 00103 fields = metadataCache.getMetadata(request); 00104 00105 if (fields == null) 00106 { // Metadata Cache miss 00107 // Build the fields from the MetaData 00108 java.sql.ResultSetMetaData metaData = rs.getMetaData(); 00109 if (metaData == null) 00110 throw new SQLException("Unable to fetch metadata"); 00111 nbColumn = metaData.getColumnCount(); 00112 fields = new Field[nbColumn]; 00113 for (int i = 0; i < nbColumn; i++) 00114 { 00115 // 1st column is 1 00116 String columnName = metaData.getColumnName(i + 1); 00117 String tableName = null; 00118 try 00119 { 00120 tableName = metaData.getTableName(i + 1); 00121 } 00122 catch (Exception ignore) 00123 { 00124 } 00125 if (metadataCache != null) 00126 { // Check Field cache 00127 fields[i] = metadataCache.getField(tableName + "." + columnName); 00128 if (fields[i] != null) 00129 continue; // Cache hit 00130 } 00131 // Field cache miss 00132 int columnDisplaySize = 0; 00133 try 00134 { 00135 columnDisplaySize = metaData.getColumnDisplaySize(i + 1); 00136 } 00137 catch (Exception ignore) 00138 { 00139 } 00140 int columnType = -1; 00141 try 00142 { 00143 columnType = metaData.getColumnType(i + 1); 00144 } 00145 catch (Exception ignore) 00146 { 00147 } 00148 String columnTypeName = null; 00149 try 00150 { 00151 columnTypeName = metaData.getColumnTypeName(i + 1); 00152 } 00153 catch (Exception ignore) 00154 { 00155 } 00156 String columnClassName = null; 00157 try 00158 { 00159 columnClassName = metaData.getColumnClassName(i + 1); 00160 } 00161 catch (Exception ignore) 00162 { 00163 } 00164 boolean isAutoIncrement = false; 00165 try 00166 { 00167 isAutoIncrement = metaData.isAutoIncrement(i + 1); 00168 } 00169 catch (Exception ignore) 00170 { 00171 } 00172 boolean isCaseSensitive = false; 00173 try 00174 { 00175 isCaseSensitive = metaData.isCaseSensitive(i + 1); 00176 } 00177 catch (Exception ignore) 00178 { 00179 } 00180 boolean isCurrency = false; 00181 try 00182 { 00183 isCurrency = metaData.isCurrency(i + 1); 00184 } 00185 catch (Exception ignore) 00186 { 00187 } 00188 int isNullable = ResultSetMetaData.columnNullableUnknown; 00189 try 00190 { 00191 isNullable = metaData.isNullable(i + 1); 00192 } 00193 catch (Exception ignore) 00194 { 00195 } 00196 boolean isReadOnly = false; 00197 try 00198 { 00199 isReadOnly = metaData.isReadOnly(i + 1); 00200 } 00201 catch (Exception ignore) 00202 { 00203 } 00204 boolean isWritable = false; 00205 try 00206 { 00207 isWritable = metaData.isWritable(i + 1); 00208 } 00209 catch (Exception ignore) 00210 { 00211 } 00212 boolean isDefinitelyWritable = false; 00213 try 00214 { 00215 isReadOnly = metaData.isDefinitelyWritable(i + 1); 00216 } 00217 catch (Exception ignore) 00218 { 00219 } 00220 boolean isSearchable = false; 00221 try 00222 { 00223 isSearchable = metaData.isSearchable(i + 1); 00224 } 00225 catch (Exception ignore) 00226 { 00227 } 00228 boolean isSigned = false; 00229 try 00230 { 00231 isSigned = metaData.isSigned(i + 1); 00232 } 00233 catch (Exception ignore) 00234 { 00235 } 00236 int precision = 0; 00237 try 00238 { 00239 precision = metaData.getPrecision(i + 1); 00240 } 00241 catch (Exception ignore) 00242 { 00243 } 00244 int scale = 0; 00245 try 00246 { 00247 scale = metaData.getScale(i + 1); 00248 } 00249 catch (Exception ignore) 00250 { 00251 } 00252 fields[i] = new Field(tableName, columnName, columnDisplaySize, 00253 columnType, columnTypeName, columnClassName, isAutoIncrement, 00254 isCaseSensitive, isCurrency, isNullable, isReadOnly, isWritable, 00255 isDefinitelyWritable, isSearchable, isSigned, precision, scale); 00256 00257 if (metadataCache != null) 00258 // Add field to cache 00259 metadataCache.addField(tableName + "." + columnName, fields[i]); 00260 } // for 00261 if (metadataCache != null) 00262 metadataCache.addMetadata(request, fields); 00263 } 00264 else 00265 nbColumn = fields.length; 00266 00267 // Build the ResultSet data 00268 if (rs.next()) 00269 { 00270 if (request instanceof SelectRequest) 00271 { 00272 SelectRequest select = (SelectRequest) request; 00273 cursorName = select.getCursorName(); 00274 fetchSize = select.getFetchSize(); 00275 } 00276 maxRows = request.getMaxRows(); 00277 if (maxRows == 0) 00278 maxRows = Integer.MAX_VALUE; // Infinite number of rows 00279 00280 // Note that fetchData updates the data field 00281 dbResultSet = rs; 00282 fetchData(); 00283 if (hasMoreData && (cursorName == null)) 00284 cursorName = String.valueOf(dbResultSet.hashCode()); 00285 } 00286 else 00287 { 00288 hasMoreData = false; 00289 dbResultSet = null; 00290 dbResultSetClosed = true; 00291 rs.close(); 00292 if (owningStatement != null) 00293 { 00294 try 00295 { 00296 owningStatement.close(); 00297 } 00298 catch (SQLException ignore) 00299 { 00300 } 00301 owningStatement = null; 00302 } 00303 } 00304 } 00305 catch (SQLException e) 00306 { 00307 throw new SQLException("Error while building C-JDBC ResultSet (" + e 00308 + ")"); 00309 } 00310 }
|
|
Closes the database ResultSet to release the resource and garbage collect data. Definition at line 457 of file ControllerResultSet.java. Referenced by org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabaseWorkerThread.run(). 00458 { 00459 if ((dbResultSet != null) && !dbResultSetClosed) 00460 { 00461 try 00462 { 00463 dbResultSet.close(); 00464 } 00465 catch (SQLException ignore) 00466 { 00467 } 00468 dbResultSet = null; // to allow GC to work properly 00469 if (owningStatement != null) 00470 { 00471 try 00472 { 00473 owningStatement.close(); 00474 } 00475 catch (SQLException ignore) 00476 { 00477 } 00478 owningStatement = null; 00479 } 00480 } 00481 }
|
|
Fetch the next rows of data from dbResultSet according to fetchSize and maxRows parameters. This methods directly updates the data and hasMoreData fields and returns the same Object as subsequent calls to getData().
Definition at line 350 of file ControllerResultSet.java. Referenced by org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet.ControllerResultSet(), and org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet.fetchData(). 00351 { 00352 if (dbResultSet == null) 00353 return null; 00354 00355 Object[] row; 00356 // We directly update the data field 00357 data = new ArrayList(); 00358 int toFetch; 00359 if (fetchSize > 0) 00360 { 00361 toFetch = fetchSize < maxRows ? fetchSize : maxRows; 00362 maxRows -= toFetch; 00363 } 00364 else 00365 toFetch = maxRows; 00366 int nbColumn = fields.length; 00367 Object object; 00368 do 00369 { 00370 row = new Object[nbColumn]; 00371 for (int i = 0; i < nbColumn; i++) 00372 { 00373 object = dbResultSet.getObject(i + 1); 00374 // Convert database native Clob and Blob to C-JDBC Clob/Blobs that are 00375 // Serializable 00376 if (object != null) 00377 { 00378 if (object instanceof java.sql.Clob) 00379 { 00380 java.sql.Clob clob = (java.sql.Clob) object; 00381 object = clob.getSubString(1, (int) clob.length()); 00382 } 00383 else if (object instanceof java.sql.Blob) 00384 { 00385 java.sql.Blob blob = (java.sql.Blob) object; 00386 object = blob.getBytes(1, (int) blob.length()); 00387 } 00388 } 00389 row[i] = object; 00390 } 00391 data.add(row); 00392 toFetch--; 00393 hasMoreData = dbResultSet.next(); 00394 } 00395 while (hasMoreData && (toFetch > 0)); 00396 if (hasMoreData && (fetchSize > 0) && (maxRows > 0)) 00397 { // More data to fetch later on 00398 maxRows += toFetch; 00399 dbResultSetClosed = false; 00400 } 00401 else 00402 { 00403 hasMoreData = false; 00404 dbResultSet.close(); 00405 if (owningStatement != null) 00406 owningStatement.close(); 00407 dbResultSet = null; 00408 dbResultSetClosed = true; 00409 } 00410 return data; 00411 }
|
|
Sets the fetch size and calls fetchData()
Definition at line 320 of file ControllerResultSet.java. References org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet.fetchData(). 00321 { 00322 this.fetchSize = fetchSize; 00323 ArrayList rows = fetchData(); 00324 if (!hasMoreData) 00325 { 00326 if (owningStatement != null) 00327 { 00328 try 00329 { 00330 owningStatement.close(); 00331 } 00332 catch (SQLException ignore) 00333 { 00334 } 00335 owningStatement = null; 00336 } 00337 } 00338 return rows; 00339 }
|
|
Get the name of the SQL cursor used by this ResultSet
Definition at line 438 of file ControllerResultSet.java. 00439 {
00440 return cursorName;
00441 }
|
|
Returns the data value.
Definition at line 418 of file ControllerResultSet.java. Referenced by org.objectweb.cjdbc.controller.cache.result.ResultCache.needInvalidate(). 00419 {
00420 return data;
00421 }
|
|
Returns the fields value.
Definition at line 428 of file ControllerResultSet.java. Referenced by org.objectweb.cjdbc.controller.cache.result.ResultCache.needInvalidate(). 00429 {
00430 return fields;
00431 }
|
|
Returns the hasMoreData value.
Definition at line 448 of file ControllerResultSet.java. 00449 {
00450 return hasMoreData;
00451 }
|