1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-18 17:41:14 +03:00

When deleteRow() is called on an updateable ResultSet the ResultSet

should be positioned on the previous row.

Reported by Bob Messenger and Chris Pesarchick.
This commit is contained in:
Kris Jurka 2004-06-16 05:12:13 +00:00
parent d28e714509
commit 7e95c30953
2 changed files with 162 additions and 155 deletions

View File

@ -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.25.2.5 2004/04/24 01:54:45 jurka Exp $ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.25.2.6 2004/06/16 05:11:44 jurka Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -584,6 +584,8 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
deleteStatement.executeUpdate(); deleteStatement.executeUpdate();
rows.removeElementAt(current_row); rows.removeElementAt(current_row);
current_row--;
moveToCurrentRow();
} }
@ -738,22 +740,19 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
public boolean rowDeleted() throws SQLException public boolean rowDeleted() throws SQLException
{ {
// only sub-classes implement CONCURuPDATEABLE return false;
throw Driver.notImplemented();
} }
public boolean rowInserted() throws SQLException public boolean rowInserted() throws SQLException
{ {
// only sub-classes implement CONCURuPDATEABLE return false;
throw Driver.notImplemented();
} }
public boolean rowUpdated() throws SQLException public boolean rowUpdated() throws SQLException
{ {
// only sub-classes implement CONCURuPDATEABLE return false;
throw Driver.notImplemented();
} }
@ -938,9 +937,12 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
{ {
throw new PSQLException( "postgresql.updateable.notupdateable" ); throw new PSQLException( "postgresql.updateable.notupdateable" );
} }
if (onInsertRow)
throw new PSQLException("postgresql.res.oninsertrow");
if (isBeforeFirst() || isAfterLast())
return;
try
{
StringBuffer selectSQL = new StringBuffer( "select "); StringBuffer selectSQL = new StringBuffer( "select ");
final int numColumns = java.lang.reflect.Array.getLength(fields); final int numColumns = java.lang.reflect.Array.getLength(fields);
@ -999,14 +1001,6 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
selectStatement = null; selectStatement = null;
} }
catch (Exception e)
{
if ( Driver.logDebug )
Driver.debug(e.getClass().getName() + e);
throw new SQLException( e.getMessage() );
}
}
public synchronized void updateRow() public synchronized void updateRow()

View File

@ -42,6 +42,34 @@ public class UpdateableResultTest extends TestCase
TestUtil.closeDB(con); TestUtil.closeDB(con);
} }
public void testDeleteRows() throws SQLException
{
Statement st = con.createStatement();
st.executeUpdate("INSERT INTO second values (2,'two')");
st.executeUpdate("INSERT INTO second values (3,'three')");
st.executeUpdate("INSERT INTO second values (4,'four')");
st.close();
st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select id1,name1 from second order by id1");
assertTrue(rs.next());
assertEquals(1, rs.getInt("id1"));
rs.deleteRow();
assertTrue(rs.isBeforeFirst());
assertTrue(rs.next());
assertTrue(rs.next());
assertEquals(3, rs.getInt("id1"));
rs.deleteRow();
assertEquals(2, rs.getInt("id1"));
rs.close();
st.close();
}
public void testCancelRowUpdates() throws Exception public void testCancelRowUpdates() throws Exception
{ {
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
@ -84,9 +112,7 @@ public class UpdateableResultTest extends TestCase
public void testUpdateable() public void testUpdateable() throws SQLException
{
try
{ {
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select * from updateable"); ResultSet rs = st.executeQuery( "select * from updateable");
@ -102,9 +128,9 @@ public class UpdateableResultTest extends TestCase
rs.updateString( "name", "dave" ); rs.updateString( "name", "dave" );
rs.updateRow(); rs.updateRow();
assertTrue( rs.getInt("id") == 2 ); assertEquals(2, rs.getInt("id"));
assertTrue( rs.getString("name").equals("dave")); assertEquals("dave", rs.getString("name"));
assertTrue( rs.getString("notselected").equals("avalue") ); assertEquals("avalue", rs.getString("notselected"));
rs.deleteRow(); rs.deleteRow();
rs.moveToInsertRow(); rs.moveToInsertRow();
@ -112,11 +138,15 @@ public class UpdateableResultTest extends TestCase
rs.updateString("name", "paul"); rs.updateString("name", "paul");
rs.insertRow(); rs.insertRow();
rs.refreshRow();
assertTrue( rs.getInt("id") == 3 );
assertTrue( rs.getString("name").equals("paul"));
assertTrue( rs.getString("notselected") == null );
try {
rs.refreshRow();
fail("Can't refresh when on the insert row.");
} catch (SQLException sqle) { }
assertEquals(3, rs.getInt("id"));
assertEquals("paul", rs.getString("name"));
assertNull(rs.getString("notselected"));
rs.close(); rs.close();
@ -131,21 +161,18 @@ public class UpdateableResultTest extends TestCase
} }
assertTrue( "should not get here, update should fail", false ); fail("should not get here, update should fail");
} }
catch (SQLException ex) catch (SQLException ex)
{} {}
try
{
rs = st.executeQuery("select oid,* from updateable"); rs = st.executeQuery("select oid,* from updateable");
if ( rs.first() ) assertTrue(rs.first());
{
rs.updateInt( "id", 3 ); rs.updateInt( "id", 3 );
rs.updateString( "name", "dave3"); rs.updateString( "name", "dave3");
rs.updateRow(); rs.updateRow();
assertTrue(rs.getInt("id") == 3 ); assertEquals(3, rs.getInt("id"));
assertTrue(rs.getString("name").equals("dave3")); assertEquals("dave3", rs.getString("name"));
rs.moveToInsertRow(); rs.moveToInsertRow();
rs.updateInt( "id", 4 ); rs.updateInt( "id", 4 );
@ -157,33 +184,19 @@ public class UpdateableResultTest extends TestCase
rs.insertRow(); rs.insertRow();
rs.moveToCurrentRow(); rs.moveToCurrentRow();
assertTrue(rs.getInt("id") == 3 ); assertEquals(3, rs.getInt("id"));
assertTrue(rs.getString("name").equals("dave3")); assertEquals("dave3", rs.getString("name"));
assertTrue( rs.next() ); assertTrue( rs.next() );
assertTrue(rs.getInt("id") == 4 ); assertEquals(4, rs.getInt("id"));
assertTrue(rs.getString("name").equals("dave4")); assertEquals("dave4", rs.getString("name"));
assertTrue( rs.next() ); assertTrue( rs.next() );
assertTrue(rs.getInt("id") == 5 ); assertEquals(5, rs.getInt("id"));
assertTrue(rs.getString("name").equals("dave5")); assertEquals("dave5", rs.getString("name"));
}
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
rs.close();
st.close(); st.close();
} }
catch (Exception ex)
{
ex.printStackTrace();
fail(ex.getMessage());
}
}
} }