1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/CursorFetchTest.java,v 1.13 2008/01/08 06:56:30 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2;
import java.sql.*;
import junit.framework.TestCase;
import org.postgresql.test.TestUtil;
/*
* Tests for using non-zero setFetchSize().
*/
public class CursorFetchTest extends TestCase
{
private Connection con;
public CursorFetchTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
con = TestUtil.openDB();
TestUtil.createTable(con, "test_fetch", "value integer");
con.setAutoCommit(false);
}
protected void tearDown() throws Exception
{
con.rollback();
con.setAutoCommit(true);
TestUtil.dropTable(con, "test_fetch");
TestUtil.closeDB(con);
}
protected void createRows(int count) throws Exception
{
PreparedStatement stmt = con.prepareStatement("insert into test_fetch(value) values(?)");
for (int i = 0; i < count; ++i)
{
stmt.setInt(1, i);
stmt.executeUpdate();
}
}
// Test various fetchsizes.
public void testBasicFetch() throws Exception
{
createRows(100);
PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value");
int[] testSizes = { 0, 1, 49, 50, 51, 99, 100, 101 };
for (int i = 0; i < testSizes.length; ++i)
{
stmt.setFetchSize(testSizes[i]);
assertEquals(testSizes[i], stmt.getFetchSize());
ResultSet rs = stmt.executeQuery();
assertEquals(testSizes[i], rs.getFetchSize());
int count = 0;
while (rs.next())
{
assertEquals("query value error with fetch size " + testSizes[i], count, rs.getInt(1));
++count;
}
assertEquals("total query size error with fetch size " + testSizes[i], 100, count);
}
}
// Similar, but for scrollable resultsets.
public void testScrollableFetch() throws Exception
{
createRows(100);
PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value",
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
int[] testSizes = { 0, 1, 49, 50, 51, 99, 100, 101 };
for (int i = 0; i < testSizes.length; ++i)
{
stmt.setFetchSize(testSizes[i]);
assertEquals(testSizes[i], stmt.getFetchSize());
ResultSet rs = stmt.executeQuery();
assertEquals(testSizes[i], rs.getFetchSize());
for (int j = 0; j <= 50; ++j)
{
assertTrue("ran out of rows at position " + j + " with fetch size " + testSizes[i], rs.next());
assertEquals("query value error with fetch size " + testSizes[i], j, rs.getInt(1));
}
int position = 50;
for (int j = 1; j < 100; ++j)
{
for (int k = 0; k < j; ++k)
{
if (j % 2 == 0)
{
++position;
assertTrue("ran out of rows doing a forward fetch on iteration " + j + "/" + k + " at position " + position + " with fetch size " + testSizes[i], rs.next());
}
else
{
--position;
assertTrue("ran out of rows doing a reverse fetch on iteration " + j + "/" + k + " at position " + position + " with fetch size " + testSizes[i], rs.previous());
}
assertEquals("query value error on iteration " + j + "/" + k + " with fetch size " + testSizes[i], position, rs.getInt(1));
}
}
}
}
public void testScrollableAbsoluteFetch() throws Exception
{
createRows(100);
PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value",
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
int[] testSizes = { 0, 1, 49, 50, 51, 99, 100, 101 };
for (int i = 0; i < testSizes.length; ++i)
{
stmt.setFetchSize(testSizes[i]);
assertEquals(testSizes[i], stmt.getFetchSize());
ResultSet rs = stmt.executeQuery();
assertEquals(testSizes[i], rs.getFetchSize());
int position = 50;
assertTrue("ran out of rows doing an absolute fetch at " + position + " with fetch size " + testSizes[i], rs.absolute(position + 1));
assertEquals("query value error with fetch size " + testSizes[i], position, rs.getInt(1));
for (int j = 1; j < 100; ++j)
{
if (j % 2 == 0)
position += j;
else
position -= j;
assertTrue("ran out of rows doing an absolute fetch at " + position + " on iteration " + j + " with fetchsize" + testSizes[i], rs.absolute(position + 1));
assertEquals("query value error with fetch size " + testSizes[i], position, rs.getInt(1));
}
}
}
//
// Tests for ResultSet.setFetchSize().
//
// test one:
// set fetchsize = 0
// run query (all rows should be fetched)
// set fetchsize = 50 (should have no effect)
// process results
public void testResultSetFetchSizeOne() throws Exception
{
createRows(100);
PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value");
stmt.setFetchSize(0);
ResultSet rs = stmt.executeQuery();
rs.setFetchSize(50); // Should have no effect.
int count = 0;
while (rs.next())
{
assertEquals(count, rs.getInt(1));
++count;
}
assertEquals(100, count);
}
// test two:
// set fetchsize = 25
// run query (25 rows fetched)
// set fetchsize = 0
// process results:
// process 25 rows
// should do a FETCH ALL to get more data
// process 75 rows
public void testResultSetFetchSizeTwo() throws Exception
{
createRows(100);
PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value");
stmt.setFetchSize(25);
ResultSet rs = stmt.executeQuery();
rs.setFetchSize(0);
int count = 0;
while (rs.next())
{
assertEquals(count, rs.getInt(1));
++count;
}
assertEquals(100, count);
}
// test three:
// set fetchsize = 25
// run query (25 rows fetched)
// set fetchsize = 50
// process results:
// process 25 rows. should NOT hit end-of-results here.
// do a FETCH FORWARD 50
// process 50 rows
// do a FETCH FORWARD 50
// process 25 rows. end of results.
public void testResultSetFetchSizeThree() throws Exception
{
createRows(100);
PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value");
stmt.setFetchSize(25);
ResultSet rs = stmt.executeQuery();
rs.setFetchSize(50);
int count = 0;
while (rs.next())
{
assertEquals(count, rs.getInt(1));
++count;
}
assertEquals(100, count);
}
// test four:
// set fetchsize = 50
// run query (50 rows fetched)
// set fetchsize = 25
// process results:
// process 50 rows.
// do a FETCH FORWARD 25
// process 25 rows
// do a FETCH FORWARD 25
// process 25 rows. end of results.
public void testResultSetFetchSizeFour() throws Exception
{
createRows(100);
PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value");
stmt.setFetchSize(50);
ResultSet rs = stmt.executeQuery();
rs.setFetchSize(25);
int count = 0;
while (rs.next())
{
assertEquals(count, rs.getInt(1));
++count;
}
assertEquals(100, count);
}
public void testSingleRowResultPositioning() throws Exception
{
String msg;
createRows(1);
int[] sizes = { 0, 1, 10 };
for (int i = 0; i < sizes.length; ++i)
{
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
stmt.setFetchSize(sizes[i]);
// Create a one row result set.
ResultSet rs = stmt.executeQuery("select * from test_fetch order by value");
msg = "before-first row positioning error with fetchsize=" + sizes[i];
assertTrue(msg, rs.isBeforeFirst());
assertTrue(msg, !rs.isAfterLast());
assertTrue(msg, !rs.isFirst());
assertTrue(msg, !rs.isLast());
msg = "row 1 positioning error with fetchsize=" + sizes[i];
assertTrue(msg, rs.next());
assertTrue(msg, !rs.isBeforeFirst());
assertTrue(msg, !rs.isAfterLast());
assertTrue(msg, rs.isFirst());
assertTrue(msg, rs.isLast());
assertEquals(msg, 0, rs.getInt(1));
msg = "after-last row positioning error with fetchsize=" + sizes[i];
assertTrue(msg, !rs.next());
assertTrue(msg, !rs.isBeforeFirst());
assertTrue(msg, rs.isAfterLast());
assertTrue(msg, !rs.isFirst());
assertTrue(msg, !rs.isLast());
rs.close();
stmt.close();
}
}
public void testMultiRowResultPositioning() throws Exception
{
String msg;
createRows(100);
int[] sizes = { 0, 1, 10, 100 };
for (int i = 0; i < sizes.length; ++i)
{
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
stmt.setFetchSize(sizes[i]);
ResultSet rs = stmt.executeQuery("select * from test_fetch order by value");
msg = "before-first row positioning error with fetchsize=" + sizes[i];
assertTrue(msg, rs.isBeforeFirst());
assertTrue(msg, !rs.isAfterLast());
assertTrue(msg, !rs.isFirst());
assertTrue(msg, !rs.isLast());
for (int j = 0; j < 100; ++j)
{
msg = "row " + j + " positioning error with fetchsize=" + sizes[i];
assertTrue(msg, rs.next());
assertEquals(msg, j, rs.getInt(1));
assertTrue(msg, !rs.isBeforeFirst());
assertTrue(msg, !rs.isAfterLast());
if (j == 0)
assertTrue(msg, rs.isFirst());
else
assertTrue(msg, !rs.isFirst());
if (j == 99)
assertTrue(msg, rs.isLast());
else
assertTrue(msg, !rs.isLast());
}
msg = "after-last row positioning error with fetchsize=" + sizes[i];
assertTrue(msg, !rs.next());
assertTrue(msg, !rs.isBeforeFirst());
assertTrue(msg, rs.isAfterLast());
assertTrue(msg, !rs.isFirst());
assertTrue(msg, !rs.isLast());
rs.close();
stmt.close();
}
}
// Test odd queries that should not be transformed into cursor-based fetches.
public void testInsert() throws Exception
{
// INSERT should not be transformed.
PreparedStatement stmt = con.prepareStatement("insert into test_fetch(value) values(1)");
stmt.setFetchSize(100); // Should be meaningless.
stmt.executeUpdate();
}
public void testMultistatement() throws Exception
{
// Queries with multiple statements should not be transformed.
createRows(100); // 0 .. 99
PreparedStatement stmt = con.prepareStatement("insert into test_fetch(value) values(100); select * from test_fetch order by value");
stmt.setFetchSize(10);
assertTrue(!stmt.execute()); // INSERT
assertTrue(stmt.getMoreResults()); // SELECT
ResultSet rs = stmt.getResultSet();
int count = 0;
while (rs.next())
{
assertEquals(count, rs.getInt(1));
++count;
}
assertEquals(101, count);
}
// if the driver tries to use a cursor with autocommit on
// it will fail because the cursor will disappear partway
// through execution
public void testNoCursorWithAutoCommit() throws Exception
{
createRows(10); // 0 .. 9
con.setAutoCommit(true);
Statement stmt = con.createStatement();
stmt.setFetchSize(3);
ResultSet rs = stmt.executeQuery("SELECT * FROM test_fetch ORDER BY value");
int count = 0;
while (rs.next())
{
assertEquals(count++, rs.getInt(1));
}
assertEquals(10, count);
}
public void testGetRow() throws SQLException
{
Statement stmt = con.createStatement();
stmt.setFetchSize(1);
ResultSet rs = stmt.executeQuery("SELECT 1 UNION SELECT 2 UNION SELECT 3");
int count = 0;
while (rs.next())
{
count++;
assertEquals(count, rs.getInt(1));
assertEquals(count, rs.getRow());
}
assertEquals(3, count);
}
}
|