public boolean absolute(int row) throws SQLException { return absolute(row, 0); }
public boolean next() throws SQLException{ if (hasNext()) { setCurrentRowNumber(this.currentRowNumber + 1); getCurrentRow(); return true; } if (this.currentRowNumber == highestRowNumber) { setCurrentRowNumber(this.currentRowNumber + 1); } return false; }
@Override public int available() throws SQLException { int current = batchResults.getCurrentRowNumber(); int highest = batchResults.getHighestRowNumber(); return highest - current - getOffset() - (batchResults.isTailLast()?1:0); }
public boolean previous() { if (hasPrevious()) { setCurrentRowNumber(this.currentRowNumber - 1); return true; } if (this.currentRowNumber == 1) { setCurrentRowNumber(this.currentRowNumber - 1); } return false; }
@Test public void testBatching() throws Exception { BatchResults batchResults = getBatchResults(createBatch(1, 10), false); MockBatchFetcher batchFetcher = new MockBatchFetcher(60); batchResults.setBatchFetcher(batchFetcher); for(int i=0; i<45; i++) { assertTrue(batchResults.next()); } for(int i=0; i<44; i++) { assertTrue(batchResults.previous()); assertEquals(new Integer(44 - i), batchResults.getCurrentRow().get(0)); } // verify batch calls checkResults(new int[] { // going forwards - end > begin 11, 21, 31, 41, // going backwards - begin > end // last 3 batches were saved, only need the first 2 again 20, 10, }, batchFetcher.batchCalls); assertTrue(batchResults.absolute(50)); assertEquals(new Integer(50), batchResults.getCurrentRow().get(0)); }
@Test public void testPrevious3() throws Exception{ //one row batch, multiple batches BatchResults batchResults = getBatchResults(createBatch(1, 1), false); assertFalse(batchResults.previous()); assertTrue(batchResults.next()); assertFalse(batchResults.previous()); while(batchResults.next()) { } assertTrue(batchResults.previous()); while(batchResults.previous()) { } batchResults.next(); batchResults.next(); batchResults.next(); batchResults.previous(); List<Integer> expectedResult = new ArrayList<Integer>(); expectedResult.add(new Integer(2)); assertEquals(expectedResult, batchResults.getCurrentRow()); }
@Test public void testGetCurrentRow2() throws Exception{ BatchResults batchResults = getBatchResults(createBatch(1, 10), false); assertNull(batchResults.getCurrentRow()); batchResults.next(); List<Integer> expectedResult = new ArrayList<Integer>(); expectedResult.add(new Integer(1)); assertEquals(batchResults.getCurrentRow(), expectedResult); }
@Test public void testAbsoluteWithLastRow() throws Exception{ Batch batch = new Batch(createBatch(1, 10), 1, 10); batch.setLastRow(50); MockBatchFetcher mbf = new MockBatchFetcher(); mbf.setUseLastRow(true); BatchResults batchResults = new BatchResults(mbf, batch, BatchResults.DEFAULT_SAVED_BATCHES); assertTrue(batchResults.absolute(41)); assertEquals(Arrays.asList(41), batchResults.getCurrentRow()); //check to ensure that we skipped all the other batches assertEquals(Arrays.asList(41), mbf.batchCalls); }
@Test public void testAbsolute2() throws Exception{ //one row batch BatchResults batchResults = getBatchResults(createBatch(1, 1), false); assertFalse(batchResults.absolute(0)); assertTrue(batchResults.absolute(1)); assertTrue(batchResults.absolute(1)); List<Integer> expectedResult = new ArrayList<Integer>(); expectedResult.add(new Integer(1)); assertEquals(batchResults.getCurrentRow(), expectedResult); }
@Test public void testAbsolute3() throws Exception{ BatchResults batchResults = getBatchResults(createBatch(1, 10), false); batchResults.setBatchFetcher(new MockBatchFetcher(200)); assertFalse(batchResults.absolute(0)); assertTrue(batchResults.absolute(11)); List<Integer> expectedResult = new ArrayList<Integer>(); expectedResult.add(new Integer(11)); assertEquals(batchResults.getCurrentRow(), expectedResult); assertTrue(batchResults.absolute(1)); expectedResult = new ArrayList<Integer>(); expectedResult.add(new Integer(1)); assertEquals(batchResults.getCurrentRow(), expectedResult); assertTrue(batchResults.absolute(100)); expectedResult = new ArrayList<Integer>(); expectedResult.add(new Integer(100)); assertEquals(batchResults.getCurrentRow(), expectedResult); }
@Test(expected=SQLException.class) public void testSetException() throws Exception { BatchResults batchResults = getBatchResults(createBatch(1, 1), false); MockBatchFetcher batchFetcher = new MockBatchFetcher(); batchResults.setBatchFetcher(batchFetcher); batchFetcher.throwException(); batchResults.next(); batchResults.hasNext(); }
/** * Assumes forward only cursoring */ public ResultsFuture<Boolean> submitNext() throws SQLException { Boolean hasNext = batchResults.hasNext(getOffset() + 1, false); if (hasNext != null) { return StatementImpl.booleanFuture(next()); } ResultsFuture<ResultsMessage> pendingResult = submitRequestBatch(batchResults.getCurrentRowNumber() + 1); final ResultsFuture<Boolean> result = new ResultsFuture<Boolean>(); pendingResult.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() { @Override public void onCompletion(ResultsFuture<ResultsMessage> future) { try { batchResults.setBatch(processBatch(future.get())); result.getResultsReceiver().receiveResults(next()); } catch (Throwable t) { result.getResultsReceiver().exceptionOccurred(t); } } }); return result; }
@Test public void testCurrentRowNumber() throws Exception { BatchResults batchResults = getBatchResults(createBatch(1, 1), true); assertEquals(0, batchResults.getCurrentRowNumber()); batchResults.next(); assertEquals(1, batchResults.getCurrentRowNumber()); batchResults.next(); assertEquals(2, batchResults.getCurrentRowNumber()); assertFalse(batchResults.next()); assertEquals(2, batchResults.getCurrentRowNumber()); }
@Test public void testHasPrevious2() throws Exception{ //one row batch BatchResults batchResults = getBatchResults(createBatch(1, 1), true); assertFalse(batchResults.hasPrevious()); batchResults.next(); assertFalse(batchResults.hasPrevious()); batchResults.next(); assertTrue(batchResults.hasPrevious()); }
public boolean hasNext() throws SQLException { return hasNext(1, true); }
BatchResults getBatchResults(List<?>[] batch, boolean isLast) { Batch batch2 = new Batch(batch, 1, batch.length); if (isLast) { batch2.setLastRow(batch.length); } BatchResults results = new BatchResults(null, batch2, BatchResults.DEFAULT_SAVED_BATCHES); if (!isLast) { results.setBatchFetcher(new MockBatchFetcher()); } return results; }
/** * Get all values in current record in column order * @return List of Object values in current row * @throws SQLException if an access error occurs. */ public List<?> getCurrentRecord() throws SQLException { checkClosed(); return batchResults.getCurrentRow(); } /*
public boolean previous() throws SQLException { checkClosed(); checkNotForwardOnly(); return batchResults.previous(); }
protected int getAbsoluteRowNumber() { return batchResults.getCurrentRowNumber(); }