1 package com.daffodilwoods.daffodildb.client; 2 3 import com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata. 4 _SelectColumnCharacteristics; 5 6 import com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata. 7 _RowReader; 8 9 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents. 10 _SelectIterator; 11 12 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table. 13 SelectIterator; 14 15 import java.sql.SQLException ; 16 import com.daffodilwoods.database.resource.DException; 17 import com.daffodilwoods.daffodildb.server.datadictionarysystem. 18 _ColumnCharacteristics; 19 20 import java.util.*; 21 import com.daffodilwoods.daffodildb.server.sql99.common.*; 22 23 public class RecordSet 24 implements _RecordSetBuffer { 25 26 27 28 private int fetchSize = 50; 29 30 31 private _RowReader rowReader; 32 33 34 _SelectIterator selectIterator; 35 36 private _ColumnCharacteristics columnCharacteristics; 37 38 private _SelectColumnCharacteristics scc; 39 40 private _RecordSetBufferIterator recordSetBufferIterator; 41 42 ArrayList recordList; 43 boolean topFetched; boolean bottomFetched; 46 private int fetchDirection = 0; private int maxRows = 0; private int maxFieldSize = 0; private boolean lastFetchedDirection; private int noOfRecordMoved; 51 private boolean afterLast; 52 53 boolean isUpdateable; 54 private String query = ""; 55 56 public RecordSet() { 57 recordList = new ArrayList(fetchSize); 58 } 59 60 public void setSelectIterator(_SelectIterator selectIterator0) throws 61 DException { 62 this.selectIterator = selectIterator0; 63 rowReader = selectIterator.getRowReader(); 64 columnCharacteristics = selectIterator.getColumnCharacteristics(); 65 scc = selectIterator.getSelectColumnCharacteristics(); 66 } 67 68 public _RecordSetBufferIterator getIterator() throws SQLException { 69 if (recordSetBufferIterator == null) { 70 recordSetBufferIterator = new RecordSetBufferIterator(); 71 recordSetBufferIterator.setRecordSetBuffer(this); 72 } 73 return recordSetBufferIterator; 74 } 75 76 public void addDataOperationListener(DataOperationListener listener) { 77 throw new java.lang.UnsupportedOperationException ( 78 "Method addDataOperationListener() not yet implemented."); 79 } 80 81 public void removeDataOperationListener(DataOperationListener listener) { 82 throw new java.lang.UnsupportedOperationException ( 83 "Method removeDataOperationListener() not yet implemented."); 84 } 85 86 public void loadRecordForKey(_Record record, Object key) throws SQLException { 87 if (key == null) 88 throw new SQLException (new DException("DSE550", null).getMessage()); 89 int index = key.hashCode(); 90 if (index < 0 && index > recordList.size()) 91 throw new SQLException (new DException("DSE552", new Object [] {key}). 92 getMessage()); 93 Object row = recordList.get(index); 94 JDBCRecord drecord = (JDBCRecord) record; 95 drecord.setBuffer(this); 96 drecord.setValues(row); 97 drecord.setKey(key); 98 } 99 100 public void loadRecordForIdentity(_Record record, Object identity) throws 101 SQLException { 102 throw new java.lang.UnsupportedOperationException ( 103 "Method loadRecordForIdentity() not yet implemented."); 104 } 105 106 public Object getKeyForIdentity(Object identity) { 107 throw new java.lang.UnsupportedOperationException ( 108 "Method getKeyForIdentity() not yet implemented."); 109 } 110 111 public Object getIdentityForKey(Object key) { 112 throw new java.lang.UnsupportedOperationException ( 113 "Method getIdentityForKey() not yet implemented."); 114 } 115 116 public String getTable() throws SQLException { 117 return ""; 118 } 119 120 public Object getTopKey() throws SQLException { 121 try { 122 int size = recordList.size(); 123 if (topFetched) 124 return size == 0 ? null : new Integer (0); 125 selectIterator.beforeFirst(); 126 int noOfRecordsToBeFetched = maxRows != 0 && fetchSize > maxRows 127 ? maxRows : fetchSize; 128 Object [] fetchedResult = selectIterator.fetchForward(noOfRecordsToBeFetched); 129 topFetched = true; 130 int numberOfRecordsFetched = fetchedResult == null ? 0 : 131 fetchedResult.length; 132 lastFetchedDirection = true; 133 if (numberOfRecordsFetched == 0) { 134 bottomFetched = true; 135 afterLast = true; 136 return null; 137 } 138 noOfRecordMoved = numberOfRecordsFetched; 139 bottomFetched = maxRows != 0 && maxRows < fetchSize 140 ? numberOfRecordsFetched <= maxRows 141 : numberOfRecordsFetched < fetchSize; 142 afterLast = bottomFetched && numberOfRecordsFetched < fetchSize; 143 recordList.clear(); 144 recordList.addAll(Arrays.asList(fetchedResult)); 145 return new Integer (0); 146 } 147 catch (DException ex) { 148 throw ex.getSqlException(null); 149 } 150 } 151 152 public Object getBottomKey() throws SQLException { 153 try { 154 int size = recordList.size(); 155 if (bottomFetched) 156 return size == 0 ? null : new Integer (size - 1); 157 int rowCount = selectIterator.getRowCount(); 158 if(rowCount < maxRows || maxRows == 0 ){ 159 selectIterator.afterLast(); 160 }else{ 161 selectIterator.beforeFirst(); 162 selectIterator.move(maxRows+1); 163 } 164 Object [] fetchedResult = selectIterator.fetchBackward(fetchSize); 165 bottomFetched = true; 166 afterLast = false; 167 int numberOfRecordsFetched = fetchedResult == null ? 0 : 168 fetchedResult.length; 169 lastFetchedDirection = false; 170 if (numberOfRecordsFetched == 0) { 171 topFetched = true; 172 return size == 0 ? null : new Integer (size - 1); 173 } 174 175 topFetched = numberOfRecordsFetched < fetchSize; 176 noOfRecordMoved = maxRows; 177 recordList.clear(); 178 for (int i = numberOfRecordsFetched - 1; i >= 0; i--) 179 recordList.add(fetchedResult[i]); 180 return new Integer (numberOfRecordsFetched - 1); 181 } 182 catch (DException ex) { 183 throw ex.getSqlException(null); 184 } 185 } 186 187 public Object getNextKey(Object key) throws SQLException { 188 try { 189 if (key == null) 190 throw new DException("DSE550", null); 191 int index = key.hashCode(); 192 if (index == -1) 193 throw new DException("DSE552", new Object [] {key}); 194 int size = recordList.size(); 195 if (bottomFetched || size - 1 > index) 196 return size - 1 == index ? null : new Integer (index + 1); 197 int noOfRecordsLeft = maxRows - noOfRecordMoved; 198 if(maxRows !=0 && noOfRecordsLeft == 0){ 199 bottomFetched = true; 200 afterLast = true; 201 return null; 202 } 203 if (!lastFetchedDirection) 204 selectIterator.move(topFetched ? recordList.size() : 205 recordList.size() - 1); 206 lastFetchedDirection = true; 207 int noOfRecordsToBeFetched = maxRows != 0 && fetchSize > noOfRecordsLeft 208 ? noOfRecordsLeft : fetchSize; 209 Object [] fetchedResult = selectIterator.fetchForward(noOfRecordsToBeFetched); 210 211 int numberOfRecordsFetched = fetchedResult == null ? 0 : 212 fetchedResult.length; 213 if (numberOfRecordsFetched == 0) { 214 bottomFetched = true; 215 afterLast = true; 216 return null; 217 } 218 topFetched = false; 219 noOfRecordMoved+= numberOfRecordsFetched; 220 bottomFetched = maxRows != 0 && noOfRecordsLeft <= fetchSize 221 ? numberOfRecordsFetched <= noOfRecordsToBeFetched 222 : numberOfRecordsFetched < fetchSize; 223 afterLast = bottomFetched && numberOfRecordsFetched < noOfRecordsLeft; 224 recordList.clear(); 225 recordList.addAll(Arrays.asList(fetchedResult)); 226 return new Integer (0); 227 } 228 catch (DException ex) { 229 throw ex.getSqlException(null); 230 } 231 } 232 233 public Object getPreviousKey(Object key) throws SQLException { 234 try { 235 if (key == null) 236 throw new DException("DSE550", null); 237 int index = key.hashCode(); 238 if (index == -1) 239 throw new DException("DSE552", new Object [] {key}); 240 int size = recordList.size(); 241 if (topFetched || index != 0) 242 return index == 0 ? null : new Integer (index - 1); 243 if (lastFetchedDirection) 244 selectIterator.move(bottomFetched ? (maxRows == 0 || afterLast ? -recordList.size() : -recordList.size() + 1) : 245 -recordList.size() + 1); 246 lastFetchedDirection = false; 247 afterLast = false; 248 Object [] fetchedResult = selectIterator.fetchBackward(fetchSize); 249 int numberOfRecordsFetched = fetchedResult == null ? 0 : 250 fetchedResult.length; 251 if (numberOfRecordsFetched == 0) { 252 topFetched = true; 253 return null; 254 } 255 bottomFetched = false; 256 257 topFetched = numberOfRecordsFetched < fetchSize; 258 noOfRecordMoved-= size; 259 recordList.clear(); 260 for (int i = numberOfRecordsFetched - 1; i >= 0; i--) 261 recordList.add(fetchedResult[i]); 262 return new Integer (numberOfRecordsFetched - 1); 263 } 264 catch (DException ex) { 265 throw ex.getSqlException(null); 266 } 267 } 268 269 public int getDistanceBetweenKeys(Object key1, Object key2) throws 270 SQLException { 271 throw new java.lang.UnsupportedOperationException ( 272 "Method getDistanceBetweenKeys() not yet implemented."); 273 } 274 275 public Object getKeyAtDistance(Object key, int distance) throws SQLException { 276 throw new java.lang.UnsupportedOperationException ( 277 "Method getKeyAtDistance() not yet implemented."); 278 } 279 280 public Object insertInitiate() throws DException { 281 throw new java.lang.UnsupportedOperationException ( 282 "Method insertInitiate() not yet implemented."); 283 } 284 285 public Object updateInitiate(Object currentKey, String columnName, 286 Object value) throws SQLException { 287 throw new java.lang.UnsupportedOperationException ( 288 "Method updateInitiate() not yet implemented."); 289 } 290 291 public Object deleteInitiate(Object deleteRecord) throws DException { 292 throw new java.lang.UnsupportedOperationException ( 293 "Method deleteInitiate() not yet implemented."); 294 } 295 296 public Object deleteRow(_Record deleteRecord) throws DException { 297 throw new java.lang.UnsupportedOperationException ( 298 "Method deleteInitiate() not yet implemented."); 299 } 300 301 public Object [] getParameters() { 302 throw new java.lang.UnsupportedOperationException ( 303 "Method getParameters() not yet implemented."); 304 } 305 306 public Object locateNearestKey(Object key) throws SQLException { 307 throw new java.lang.UnsupportedOperationException ( 308 "Method locateNearestKey() not yet implemented."); 309 } 310 311 public _SelectColumnCharacteristics getColumnCharacteristics() { 312 return scc; 313 } 314 315 public Object seek(String clause) throws SQLException { 316 throw new java.lang.UnsupportedOperationException ( 317 "Method seek() not yet implemented."); 318 } 319 320 public void setFetchDirection(int direction) { 321 fetchDirection = direction; 322 } 323 324 public void setFetchSize(int fetchSize0) { 325 fetchSize = fetchSize0; 326 } 327 328 public void setMaxRows(int maxRows0) { 329 maxRows = maxRows0; 330 } 331 332 public void setMaxFieldSize(int maxSize) { 333 maxFieldSize = maxSize; 334 } 335 336 public String getQuery() { 337 return query; 338 } 339 340 public void setQuery(String query0) { 341 query = query0; 342 } 343 344 public void updateRow(_Record record) throws DException, SQLException { 345 throw new java.lang.UnsupportedOperationException ( 346 "Method updateRow() not yet implemented."); 347 } 348 349 public _RowReader getRowReader() throws SQLException { 350 return rowReader; 351 } 352 353 public Object insertInitiate(String [] columnNames, Object [] values) throws 354 DException { 355 throw new java.lang.UnsupportedOperationException ( 356 "Method insertInitiate() not yet implemented."); 357 } 358 359 public Object updateInitiate(Object currentKey, String [] columnNames, 360 Object [] values) throws SQLException { 361 throw new java.lang.UnsupportedOperationException ( 362 "Method updateInitiate() not yet implemented."); 363 } 364 365 public int getRowCount() throws SQLException { 366 try { 367 return selectIterator.getRowCount(); 368 } 369 catch (DException ex) { 370 throw ex.getSqlException(null); 371 } 372 } 373 374 public Object convertIntoParameter() throws SQLException , DException { 375 throw new java.lang.UnsupportedOperationException ( 376 "Method convertIntoParameter() not yet implemented."); 377 } 378 379 public boolean isBottom(Object key) throws SQLException { 380 try { 381 if (key == null) 382 throw new DException("DSE550", null); 383 int index = key.hashCode(); 384 if (index == -1) 385 throw new DException("DSE552", new Object [] {key}); 386 int size = recordList.size(); 387 if (bottomFetched || size - 1 > index) 388 return size - 1 == index; 389 int noOfRecordsLeft = maxRows - noOfRecordMoved; 390 if(maxRows !=0 && noOfRecordsLeft == 0){ 391 bottomFetched = true; 392 afterLast = true; 393 return true; 394 } 395 if (!lastFetchedDirection) 396 selectIterator.move(topFetched ? recordList.size() : 397 recordList.size() - 1); 398 int noOfRecordsToBeFetched = maxRows != 0 && fetchSize > noOfRecordsLeft 399 ? noOfRecordsLeft : fetchSize; 400 Object [] fetchedResult = selectIterator.fetchForward(noOfRecordsToBeFetched); 401 int numberOfRecordsFetched = fetchedResult == null ? 0 : 402 fetchedResult.length; 403 if (numberOfRecordsFetched == 0) { 404 bottomFetched = true; 405 afterLast = true; 406 return true; 407 } 408 topFetched = false; 409 lastFetchedDirection = true; 410 Object lastValue = recordList.get(index); 411 noOfRecordMoved+= numberOfRecordsFetched; 412 bottomFetched = maxRows != 0 && noOfRecordsToBeFetched < fetchSize 413 ? numberOfRecordsFetched <= noOfRecordsToBeFetched 414 : numberOfRecordsFetched < fetchSize; 415 afterLast = bottomFetched && numberOfRecordsFetched < noOfRecordsLeft; 416 recordList.clear(); 417 recordList.add(lastValue); 418 recordList.addAll(Arrays.asList(fetchedResult)); 419 recordSetBufferIterator.moveToKey(new Integer (0)); 420 return false; 421 } 422 catch (DException ex) { 423 throw ex.getSqlException(null); 424 } 425 } 426 427 public boolean isTop(Object key) throws SQLException { 428 try { 429 if (key == null) 430 throw new DException("DSE550", null); 431 int index = key.hashCode(); 432 if (index == -1) 433 throw new DException("DSE552", new Object [] {key}); 434 int size = recordList.size(); 435 if (topFetched || index != 0) 436 return index == 0; 437 if (lastFetchedDirection) 438 selectIterator.move(bottomFetched ? (maxRows == 0 || afterLast ? -recordList.size() : -recordList.size() + 1) : 439 -recordList.size() + 1); 440 Object [] fetchedResult = selectIterator.fetchBackward(fetchSize); 441 int numberOfRecordsFetched = fetchedResult == null ? 0 : 442 fetchedResult.length; 443 if (numberOfRecordsFetched == 0) { 444 topFetched = true; 445 return true; 446 } 447 bottomFetched = false; 448 lastFetchedDirection = false; 449 topFetched = numberOfRecordsFetched < fetchSize; 450 noOfRecordMoved-= (size -1); 451 Object lastValue = recordList.get(0); 452 recordList.clear(); 453 for (int i = numberOfRecordsFetched - 1; i >= 0; i--) 454 recordList.add(fetchedResult[i]); 455 recordList.add(lastValue); 456 recordSetBufferIterator.moveToKey(new Integer (numberOfRecordsFetched)); 457 return false; 458 } 459 catch (DException ex) { 460 throw ex.getSqlException(null); 461 } 462 } 463 464 public void setUpdateMode(boolean immediate) { 465 throw new java.lang.UnsupportedOperationException ( 466 "Method setUpdateMode() not yet implemented."); 467 } 468 469 public _Record getRecordInstance() { 470 return new JDBCRecord(isUpdateable); 471 } 472 473 public _Record getInsertedRecord() throws DException { 474 throw new java.lang.UnsupportedOperationException ( 475 "Method getInsertedRecord() not yet implemented."); 476 } 477 478 public Object insertRow() throws DException { 479 throw new java.lang.UnsupportedOperationException ( 480 "Method insertRow() not yet implemented."); 481 } 482 483 public _ExecutionPlan getExecutionPlan() throws DException { 484 return selectIterator.getExecutionPlan(); 485 } 486 487 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 488 return selectIterator.getExecutionPlanForBrowser(); 489 } 490 491 public _QueryPlan getQueryPlan() throws DException { 492 return selectIterator.getQueryPlan(); 493 } 494 495 public _Record getDummyRecord() throws SQLException { 496 497 throw new java.lang.UnsupportedOperationException ( 498 "Method getDummyRecord() not yet implemented."); 499 } 500 501 public void flushRecords(Object parm1, boolean parm2) throws com. 502 daffodilwoods.database.resource.DException { 503 504 throw new java.lang.UnsupportedOperationException ( 505 "Method flushRecords() not yet implemented."); 506 } 507 508 public void setAutoFlush(boolean flushData0) throws DException { 509 throw new java.lang.UnsupportedOperationException ( 510 "Method not yet implemented."); 511 } 512 513 public void setBufferSize(int bufferSize0) throws DException { 514 throw new java.lang.UnsupportedOperationException ( 515 "Method not yet implemented."); 516 } 517 518 void decrementNumberOfRecodsMoved(){ 519 if(maxRows >0 ){ 520 noOfRecordMoved--; 521 bottomFetched = false; 522 } 523 } 524 525 void updateBottomFetched(){ 526 if (maxRows == 0 || maxRows > noOfRecordMoved) { 527 bottomFetched = false; 528 afterLast = false; 529 } 530 } 531 } 532 | Popular Tags |