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
*
* 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();
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
{
// only sub-classes implement CONCURuPDATEABLE
throw Driver.notImplemented();
return false;
}
public boolean rowInserted() throws SQLException
{
// only sub-classes implement CONCURuPDATEABLE
throw Driver.notImplemented();
return false;
}
public boolean rowUpdated() throws SQLException
{
// only sub-classes implement CONCURuPDATEABLE
throw Driver.notImplemented();
return false;
}
@ -938,9 +937,12 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
{
throw new PSQLException( "postgresql.updateable.notupdateable" );
}
if (onInsertRow)
throw new PSQLException("postgresql.res.oninsertrow");
if (isBeforeFirst() || isAfterLast())
return;
try
{
StringBuffer selectSQL = new StringBuffer( "select ");
final int numColumns = java.lang.reflect.Array.getLength(fields);
@ -999,14 +1001,6 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
selectStatement = null;
}
catch (Exception e)
{
if ( Driver.logDebug )
Driver.debug(e.getClass().getName() + e);
throw new SQLException( e.getMessage() );
}
}
public synchronized void updateRow()

View File

@ -42,6 +42,34 @@ public class UpdateableResultTest extends TestCase
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
{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
@ -84,9 +112,7 @@ public class UpdateableResultTest extends TestCase
public void testUpdateable()
{
try
public void testUpdateable() throws SQLException
{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select * from updateable");
@ -102,9 +128,9 @@ public class UpdateableResultTest extends TestCase
rs.updateString( "name", "dave" );
rs.updateRow();
assertTrue( rs.getInt("id") == 2 );
assertTrue( rs.getString("name").equals("dave"));
assertTrue( rs.getString("notselected").equals("avalue") );
assertEquals(2, rs.getInt("id"));
assertEquals("dave", rs.getString("name"));
assertEquals("avalue", rs.getString("notselected"));
rs.deleteRow();
rs.moveToInsertRow();
@ -112,11 +138,15 @@ public class UpdateableResultTest extends TestCase
rs.updateString("name", "paul");
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();
@ -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)
{}
try
{
rs = st.executeQuery("select oid,* from updateable");
if ( rs.first() )
{
assertTrue(rs.first());
rs.updateInt( "id", 3 );
rs.updateString( "name", "dave3");
rs.updateRow();
assertTrue(rs.getInt("id") == 3 );
assertTrue(rs.getString("name").equals("dave3"));
assertEquals(3, rs.getInt("id"));
assertEquals("dave3", rs.getString("name"));
rs.moveToInsertRow();
rs.updateInt( "id", 4 );
@ -157,33 +184,19 @@ public class UpdateableResultTest extends TestCase
rs.insertRow();
rs.moveToCurrentRow();
assertTrue(rs.getInt("id") == 3 );
assertTrue(rs.getString("name").equals("dave3"));
assertEquals(3, rs.getInt("id"));
assertEquals("dave3", rs.getString("name"));
assertTrue( rs.next() );
assertTrue(rs.getInt("id") == 4 );
assertTrue(rs.getString("name").equals("dave4"));
assertEquals(4, rs.getInt("id"));
assertEquals("dave4", rs.getString("name"));
assertTrue( rs.next() );
assertTrue(rs.getInt("id") == 5 );
assertTrue(rs.getString("name").equals("dave5"));
}
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
assertEquals(5, rs.getInt("id"));
assertEquals("dave5", rs.getString("name"));
rs.close();
st.close();
}
catch (Exception ex)
{
ex.printStackTrace();
fail(ex.getMessage());
}
}
}