1 24 25 package org.objectweb.cjdbc.controller.virtualdatabase; 26 27 import java.io.Serializable ; 28 import java.sql.ResultSet ; 29 import java.sql.ResultSetMetaData ; 30 import java.sql.SQLException ; 31 import java.sql.Statement ; 32 import java.util.ArrayList ; 33 34 import org.objectweb.cjdbc.common.sql.AbstractRequest; 35 import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache; 36 import org.objectweb.cjdbc.driver.DriverResultSet; 37 import org.objectweb.cjdbc.driver.Field; 38 39 49 public class ControllerResultSet implements Serializable 50 { 51 private static final ArrayList EMPTY_LIST = new ArrayList (0); 52 53 private ArrayList data = EMPTY_LIST; 54 55 private Field[] fields = null; 56 57 private String cursorName = null; 58 59 private int fetchSize = 0; 60 61 private transient ResultSet dbResultSet = null; 62 63 private transient Statement owningStatement = null; 64 65 private boolean dbResultSetClosed = true; 66 67 private boolean hasMoreData = false; 68 69 private int maxRows = 0; 70 74 DriverResultSet driverResultSet; 75 76 93 public ControllerResultSet(AbstractRequest request, java.sql.ResultSet rs, 94 MetadataCache metadataCache, Statement s) throws SQLException 95 { 96 this.owningStatement = s; 97 try 98 { 99 if (rs == null) 100 throw new SQLException ("Null ResultSet"); 101 102 106 int nbColumn; 108 if (metadataCache != null) 109 fields = metadataCache.getMetadata(request); 110 111 if (fields == null) 112 { java.sql.ResultSetMetaData metaData = rs.getMetaData(); 115 if (metaData == null) 116 throw new SQLException ("Unable to fetch metadata"); 117 nbColumn = metaData.getColumnCount(); 118 fields = new Field[nbColumn]; 119 for (int i = 0; i < nbColumn; i++) 120 { 121 String columnName = metaData.getColumnName(i + 1); 123 String tableName = null; 124 try 125 { 126 tableName = metaData.getTableName(i + 1); 127 } 128 catch (Exception ignore) 129 { 130 } 131 if (metadataCache != null) 132 { fields[i] = metadataCache.getField(tableName + "." + columnName); 134 if (fields[i] != null) 135 continue; } 137 int columnDisplaySize = 0; 139 try 140 { 141 columnDisplaySize = metaData.getColumnDisplaySize(i + 1); 142 } 143 catch (Exception ignore) 144 { 145 } 146 int columnType = -1; 147 try 148 { 149 columnType = metaData.getColumnType(i + 1); 150 } 151 catch (Exception ignore) 152 { 153 } 154 String columnTypeName = null; 155 try 156 { 157 columnTypeName = metaData.getColumnTypeName(i + 1); 158 } 159 catch (Exception ignore) 160 { 161 } 162 String columnClassName = null; 163 try 164 { 165 columnClassName = metaData.getColumnClassName(i + 1); 166 } 167 catch (Exception ignore) 168 { 169 } 170 boolean isAutoIncrement = false; 171 try 172 { 173 isAutoIncrement = metaData.isAutoIncrement(i + 1); 174 } 175 catch (Exception ignore) 176 { 177 } 178 boolean isCaseSensitive = false; 179 try 180 { 181 isCaseSensitive = metaData.isCaseSensitive(i + 1); 182 } 183 catch (Exception ignore) 184 { 185 } 186 boolean isCurrency = false; 187 try 188 { 189 isCurrency = metaData.isCurrency(i + 1); 190 } 191 catch (Exception ignore) 192 { 193 } 194 int isNullable = ResultSetMetaData.columnNullableUnknown; 195 try 196 { 197 isNullable = metaData.isNullable(i + 1); 198 } 199 catch (Exception ignore) 200 { 201 } 202 boolean isReadOnly = false; 203 try 204 { 205 isReadOnly = metaData.isReadOnly(i + 1); 206 } 207 catch (Exception ignore) 208 { 209 } 210 boolean isWritable = false; 211 try 212 { 213 isWritable = metaData.isWritable(i + 1); 214 } 215 catch (Exception ignore) 216 { 217 } 218 boolean isDefinitelyWritable = false; 219 try 220 { 221 isReadOnly = metaData.isDefinitelyWritable(i + 1); 222 } 223 catch (Exception ignore) 224 { 225 } 226 boolean isSearchable = false; 227 try 228 { 229 isSearchable = metaData.isSearchable(i + 1); 230 } 231 catch (Exception ignore) 232 { 233 } 234 boolean isSigned = false; 235 try 236 { 237 isSigned = metaData.isSigned(i + 1); 238 } 239 catch (Exception ignore) 240 { 241 } 242 int precision = 0; 243 try 244 { 245 precision = metaData.getPrecision(i + 1); 246 } 247 catch (Exception ignore) 248 { 249 } 250 int scale = 0; 251 try 252 { 253 scale = metaData.getScale(i + 1); 254 } 255 catch (Exception ignore) 256 { 257 } 258 fields[i] = new Field(tableName, columnName, columnDisplaySize, 259 columnType, columnTypeName, columnClassName, isAutoIncrement, 260 isCaseSensitive, isCurrency, isNullable, isReadOnly, isWritable, 261 isDefinitelyWritable, isSearchable, isSigned, precision, scale); 262 263 if (metadataCache != null) 264 metadataCache.addField(tableName + "." + columnName, fields[i]); 266 } if (metadataCache != null) 268 metadataCache.addMetadata(request, fields); 269 } 270 else 271 nbColumn = fields.length; 272 273 if (rs.next()) 275 { 276 cursorName = request.getCursorName(); 277 fetchSize = request.getFetchSize(); 278 maxRows = request.getMaxRows(); 279 if (maxRows == 0) 280 maxRows = Integer.MAX_VALUE; 282 dbResultSet = rs; 284 fetchData(); 285 if (hasMoreData && (cursorName == null)) 286 cursorName = String.valueOf(dbResultSet.hashCode()); 289 } 290 else 291 { 292 hasMoreData = false; 293 dbResultSet = null; 294 dbResultSetClosed = true; 295 rs.close(); 296 if (owningStatement != null) 297 { 298 try 299 { 300 owningStatement.close(); 301 } 302 catch (SQLException ignore) 303 { 304 } 305 owningStatement = null; 306 } 307 } 308 } 309 catch (SQLException e) 310 { 311 throw (SQLException ) new SQLException ( 312 "Error while building C-JDBC ResultSet (" + e.getLocalizedMessage() 313 + ")").initCause(e); 314 } 315 } 316 317 324 public void fetchData(int fetchSizeParam) throws SQLException 325 { 326 this.fetchSize = fetchSizeParam; 327 fetchData(); 328 if (!hasMoreData) 329 { 330 if (owningStatement != null) 331 { 332 try 333 { 334 owningStatement.close(); 335 } 336 catch (SQLException ignore) 337 { 338 } 339 owningStatement = null; 340 } 341 } 342 } 343 344 352 public void fetchData() throws SQLException 353 { 354 if (dbResultSet == null) 355 throw new SQLException ("Backend ResultSet is closed"); 356 357 Object [] row; 358 if (data == EMPTY_LIST) 360 data = new ArrayList (); 361 else 362 data.clear(); 364 int toFetch; 365 if (fetchSize > 0) 366 { 367 toFetch = fetchSize < maxRows ? fetchSize : maxRows; 368 maxRows -= toFetch; 371 } 372 else 373 toFetch = maxRows; 374 int nbColumn = fields.length; 375 Object object; 376 do 377 { 378 row = new Object [nbColumn]; 379 for (int i = 0; i < nbColumn; i++) 380 { 381 object = dbResultSet.getObject(i + 1); 382 if (object != null) 385 { 386 if (object instanceof java.sql.Clob ) 387 { 388 java.sql.Clob clob = (java.sql.Clob ) object; 389 object = clob.getSubString(1, (int) clob.length()); 390 } 391 else if (object instanceof java.sql.Blob ) 392 { 393 java.sql.Blob blob = (java.sql.Blob ) object; 394 object = blob.getBytes(1, (int) blob.length()); 395 } 396 } 397 row[i] = object; 398 } 399 data.add(row); 400 toFetch--; 401 hasMoreData = dbResultSet.next(); 402 } 403 while (hasMoreData && (toFetch > 0)); 404 if (hasMoreData && (fetchSize > 0) && (maxRows > 0)) 405 { maxRows += toFetch; 407 dbResultSetClosed = false; 408 } 409 else 410 { 411 hasMoreData = false; 412 dbResultSet.close(); 413 if (owningStatement != null) 414 owningStatement.close(); 415 dbResultSet = null; 416 dbResultSetClosed = true; 417 } 418 } 419 420 425 public ArrayList getData() 426 { 427 return data; 428 } 429 430 435 public Field[] getFields() 436 { 437 return fields; 438 } 439 440 445 public String getCursorName() 446 { 447 return cursorName; 448 } 449 450 455 public boolean hasMoreData() 456 { 457 return hasMoreData; 458 } 459 460 464 public void closeResultSet() 465 { 466 if ((dbResultSet != null) && !dbResultSetClosed) 467 { 468 try 469 { 470 dbResultSet.close(); 471 } 472 catch (SQLException ignore) 473 { 474 } 475 dbResultSet = null; if (owningStatement != null) 477 { 478 try 479 { 480 owningStatement.close(); 481 } 482 catch (SQLException ignore) 483 { 484 } 485 owningStatement = null; 486 } 487 } 488 } 489 490 } | Popular Tags |