Commit 7ecb6ede authored by Dave Cramer's avatar Dave Cramer

Patches from Oliver Jowett to fix CursorFetchTest, 7.4 now does not automatically delete cursors

parent 15c6764b
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Copyright (c) 2003, PostgreSQL Global Development Group * Copyright (c) 2003, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/BaseConnection.java,v 1.3 2003/05/29 03:21:32 barry Exp $ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/BaseConnection.java,v 1.4 2003/10/29 02:39:09 davec Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -26,7 +26,7 @@ public interface BaseConnection extends PGConnection ...@@ -26,7 +26,7 @@ public interface BaseConnection extends PGConnection
public void cancelQuery() throws SQLException; public void cancelQuery() throws SQLException;
public Statement createStatement() throws SQLException; public Statement createStatement() throws SQLException;
public BaseResultSet execSQL(String s) throws SQLException; public BaseResultSet execSQL(String s) throws SQLException;
public boolean getAutoCommit() throws SQLException; public boolean getAutoCommit();
public String getCursorName() throws SQLException; public String getCursorName() throws SQLException;
public Encoding getEncoding() throws SQLException; public Encoding getEncoding() throws SQLException;
public DatabaseMetaData getMetaData() throws SQLException; public DatabaseMetaData getMetaData() throws SQLException;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Copyright (c) 2003, PostgreSQL Global Development Group * Copyright (c) 2003, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/BaseStatement.java,v 1.5 2003/08/24 22:10:09 barry Exp $ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/BaseStatement.java,v 1.6 2003/10/29 02:39:09 davec Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -30,11 +30,11 @@ public interface BaseStatement extends org.postgresql.PGStatement ...@@ -30,11 +30,11 @@ public interface BaseStatement extends org.postgresql.PGStatement
*/ */
public void addWarning(String p_warning) throws SQLException; public void addWarning(String p_warning) throws SQLException;
public void close() throws SQLException; public void close() throws SQLException;
public int getFetchSize() throws SQLException; public int getFetchSize();
public int getMaxFieldSize() throws SQLException; public int getMaxFieldSize() throws SQLException;
public int getMaxRows() throws SQLException; public int getMaxRows() throws SQLException;
public int getResultSetConcurrency() throws SQLException; public int getResultSetConcurrency() throws SQLException;
public String getStatementName(); public String getFetchingCursorName();
public SQLWarning getWarnings() throws SQLException; public SQLWarning getWarnings() throws SQLException;
public void setMaxFieldSize(int max) throws SQLException; public void setMaxFieldSize(int max) throws SQLException;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Copyright (c) 2003, PostgreSQL Global Development Group * Copyright (c) 2003, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Connection.java,v 1.26 2003/09/13 04:02:15 barry Exp $ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Connection.java,v 1.27 2003/10/29 02:39:09 davec Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1270,10 +1270,9 @@ public abstract class AbstractJdbc1Connection implements BaseConnection ...@@ -1270,10 +1270,9 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
* gets the current auto-commit state * gets the current auto-commit state
* *
* @return Current state of the auto-commit mode * @return Current state of the auto-commit mode
* @exception SQLException (why?)
* @see setAutoCommit * @see setAutoCommit
*/ */
public boolean getAutoCommit() throws SQLException public boolean getAutoCommit()
{ {
return this.autoCommit; return this.autoCommit;
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Copyright (c) 2003, PostgreSQL Global Development Group * Copyright (c) 2003, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.21 2003/09/22 04:54:59 barry Exp $ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.22 2003/10/29 02:39:09 davec Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -61,6 +61,9 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet ...@@ -61,6 +61,9 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
private SimpleDateFormat m_tstzFormat = null; private SimpleDateFormat m_tstzFormat = null;
private SimpleDateFormat m_dateFormat = null; private SimpleDateFormat m_dateFormat = null;
private int fetchSize; // Fetch size for next read (might be 0).
private int lastFetchSize; // Fetch size of last read (might be 0).
public abstract ResultSetMetaData getMetaData() throws SQLException; public abstract ResultSetMetaData getMetaData() throws SQLException;
public AbstractJdbc1ResultSet(BaseStatement statement, public AbstractJdbc1ResultSet(BaseStatement statement,
...@@ -82,6 +85,8 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet ...@@ -82,6 +85,8 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
this.this_row = null; this.this_row = null;
this.current_row = -1; this.current_row = -1;
this.binaryCursor = binaryCursor; this.binaryCursor = binaryCursor;
this.lastFetchSize = this.fetchSize = (statement == null ? 0 : statement.getFetchSize());
} }
public BaseStatement getPGStatement() { public BaseStatement getPGStatement() {
...@@ -111,7 +116,21 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet ...@@ -111,7 +116,21 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
this.current_row = -1; this.current_row = -1;
this.binaryCursor = binaryCursor; this.binaryCursor = binaryCursor;
} }
//
// Part of the JDBC2 support, but convenient to implement here.
//
public void setFetchSize(int rows) throws SQLException
{
fetchSize = rows;
}
public int getFetchSize() throws SQLException
{
return fetchSize;
}
public boolean next() throws SQLException public boolean next() throws SQLException
{ {
...@@ -120,30 +139,32 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet ...@@ -120,30 +139,32 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
if (++current_row >= rows.size()) if (++current_row >= rows.size())
{ {
int fetchSize = statement.getFetchSize(); String cursorName = statement.getFetchingCursorName();
// Must be false if we weren't batching. if (cursorName == null || lastFetchSize == 0 || rows.size() < lastFetchSize)
if (fetchSize == 0) return false; // Not doing a cursor-based fetch or the last fetch was the end of the query
return false;
// Use the ref to the statement to get // Use the ref to the statement to get
// the details we need to do another cursor // the details we need to do another cursor
// query - it will use reinit() to repopulate this // query - it will use reinit() to repopulate this
// with the right data. // with the right data.
String[] sql = new String[1];
String[] binds = new String[0]; // NB: We can reach this point with fetchSize == 0
// Is this the correct query??? // if the fetch size is changed halfway through reading results.
String cursorName = statement.getStatementName(); // Use "FETCH FORWARD ALL" in that case to complete the query.
//if cursorName is null, we are not batching (likely because the String[] sql = new String[] {
//query itself can't be batched) fetchSize == 0 ? ("FETCH FORWARD ALL FROM " + cursorName) :
if (cursorName == null) ("FETCH FORWARD " + fetchSize + " FROM " + cursorName)
return false; };
sql[0] = "FETCH FORWARD " + fetchSize + " FROM " + cursorName;
QueryExecutor.execute(sql, QueryExecutor.execute(sql,
binds, new String[0],
this); this);
// Test the new rows array. // Test the new rows array.
if (rows.size() == 0) lastFetchSize = fetchSize;
return false; if (rows.size() == 0)
return false;
// Otherwise reset the counter and let it go on... // Otherwise reset the counter and let it go on...
current_row = 0; current_row = 0;
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Copyright (c) 2003, PostgreSQL Global Development Group * Copyright (c) 2003, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.24 2003/09/17 05:14:52 barry Exp $ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.25 2003/10/29 02:39:09 davec Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -389,13 +389,6 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra ...@@ -389,13 +389,6 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
} }
public int getFetchSize() throws SQLException
{
// Returning the current batch size seems the right thing to do.
return rows.size();
}
public Object getObject(String columnName, java.util.Map map) throws SQLException public Object getObject(String columnName, java.util.Map map) throws SQLException
{ {
return getObject(findColumn(columnName), map); return getObject(findColumn(columnName), map);
...@@ -518,13 +511,6 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra ...@@ -518,13 +511,6 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
} }
public void setFetchSize(int rows) throws SQLException
{
// Sub-classes should implement this as part of their cursor support
throw org.postgresql.Driver.notImplemented();
}
public synchronized void cancelRowUpdates() public synchronized void cancelRowUpdates()
throws SQLException throws SQLException
{ {
......
...@@ -51,7 +51,10 @@ public class CursorFetchTest extends TestCase ...@@ -51,7 +51,10 @@ public class CursorFetchTest extends TestCase
int[] testSizes = { 0, 1, 49, 50, 51, 99, 100, 101 }; int[] testSizes = { 0, 1, 49, 50, 51, 99, 100, 101 };
for (int i = 0; i < testSizes.length; ++i) { for (int i = 0; i < testSizes.length; ++i) {
stmt.setFetchSize(testSizes[i]); stmt.setFetchSize(testSizes[i]);
assertEquals(testSizes[i], stmt.getFetchSize());
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
assertEquals(testSizes[i], rs.getFetchSize());
int count = 0; int count = 0;
while (rs.next()) { while (rs.next()) {
...@@ -63,6 +66,115 @@ public class CursorFetchTest extends TestCase ...@@ -63,6 +66,115 @@ public class CursorFetchTest extends TestCase
} }
} }
//
// 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();
stmt.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();
stmt.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();
stmt.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();
stmt.setFetchSize(25);
int count = 0;
while (rs.next()) {
assertEquals(count, rs.getInt(1));
++count;
}
assertEquals(100, count);
}
// Test odd queries that should not be transformed into cursor-based fetches. // Test odd queries that should not be transformed into cursor-based fetches.
public void TODO_FAILS_testInsert() throws Exception public void TODO_FAILS_testInsert() throws Exception
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment