From 2712da556ac1ad372b0662f9eb19e1c9f97779a7 Mon Sep 17 00:00:00 2001 From: Dave Cramer Date: Fri, 12 Dec 2003 18:38:19 +0000 Subject: [PATCH] cancel row updates sets values to null by Kris Jurka --- .../jdbc2/AbstractJdbc2ResultSet.java | 15 ++-- .../test/jdbc2/UpdateableResultTest.java | 76 ++++++++++++++++--- 2 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java b/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java index b8590dff84..8cd30f50f1 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java @@ -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 2003/10/29 02:39:09 davec Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.25.2.1 2003/12/12 18:38:19 davec Exp $ * *------------------------------------------------------------------------- */ @@ -518,7 +518,7 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra { doingUpdates = false; - clearRowBuffer(); + clearRowBuffer(true); } } @@ -661,7 +661,7 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra this_row = rowBuffer; // need to clear this in case of another insert - clearRowBuffer(); + clearRowBuffer(false); } @@ -706,7 +706,7 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra // make sure the underlying data is null - clearRowBuffer(); + clearRowBuffer(false); onInsertRow = true; doingUpdates = false; @@ -714,12 +714,17 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra } - private synchronized void clearRowBuffer() + private synchronized void clearRowBuffer(boolean copyCurrentRow) throws SQLException { // rowBuffer is the temporary storage for the row rowBuffer = new byte[fields.length][]; + // inserts want an empty array while updates want a copy of the current row + if (copyCurrentRow) { + System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length); + } + // clear the updateValues hashTable for the next set of updates updateValues.clear(); diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java index fcd6321803..362f3ace63 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java @@ -15,25 +15,79 @@ import org.postgresql.test.TestUtil; public class UpdateableResultTest extends TestCase { + private Connection con; public UpdateableResultTest( String name ) { super( name ); } + protected void setUp() throws Exception + { + con = TestUtil.openDB(); + TestUtil.createTable(con, "updateable", "id int primary key, name text, notselected text"); + TestUtil.createTable(con, "second", "id1 int primary key, name1 text"); + + // put some dummy data into second + Statement st2 = con.createStatement(); + st2.execute( "insert into second values (1,'anyvalue' )"); + st2.close(); + + } + + protected void tearDown() throws Exception + { + TestUtil.dropTable(con, "updateable"); + TestUtil.dropTable(con, "second"); + TestUtil.closeDB(con); + } + + public void testCancelRowUpdates() throws Exception + { + Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); + ResultSet rs = st.executeQuery( "select * from second"); + + // make sure we're dealing with the correct row. + rs.first(); + assertEquals(1,rs.getInt(1)); + assertEquals("anyvalue",rs.getString(2)); + + // update, cancel and make sure nothings changed. + rs.updateInt(1,99); + rs.cancelRowUpdates(); + assertEquals(1,rs.getInt(1)); + assertEquals("anyvalue",rs.getString(2)); + + // real update + rs.updateInt(1,999); + rs.updateRow(); + assertEquals(999,rs.getInt(1)); + assertEquals("anyvalue",rs.getString(2)); + + // scroll some and make sure the update is still there + rs.beforeFirst(); + rs.next(); + assertEquals(999,rs.getInt(1)); + assertEquals("anyvalue",rs.getString(2)); + + + // make sure the update got to the db and the driver isn't lying to us. + rs.close(); + rs = st.executeQuery( "select * from second"); + rs.first(); + assertEquals(999,rs.getInt(1)); + assertEquals("anyvalue",rs.getString(2)); + + rs.close(); + st.close(); + } + + + public void testUpdateable() { try { - Connection con = TestUtil.openDB(); - TestUtil.createTable(con, "updateable", "id int primary key, name text, notselected text"); - TestUtil.createTable(con, "second", "id1 int primary key, name1 text"); - - // put some dummy data into second - Statement st2 = con.createStatement(); - st2.execute( "insert into second values (1,'anyvalue' )"); - st2.close(); - Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); ResultSet rs = st.executeQuery( "select * from updateable"); assertNotNull( rs ); @@ -123,12 +177,10 @@ public class UpdateableResultTest extends TestCase st.close(); - TestUtil.dropTable( con, "updateable" ); - TestUtil.dropTable( con, "second" ); - TestUtil.closeDB( con ); } catch (Exception ex) { + ex.printStackTrace(); fail(ex.getMessage()); } } -- 2.50.1