[libpostgresql-jdbc-java] 04/09: Backport bug with executeBatch. This bug was a regression from 7.2 and has been reported by Marko Strukelj and Keith Wannamaker

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:19:00 UTC 2017


This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to tag REL7_3_2
in repository libpostgresql-jdbc-java.

commit 27daf5ae6065e56cc8e2d58d76987b543988508e
Author: Barry Lind <barry at xythos.com>
Date:   Wed Nov 20 07:54:27 2002 +0000

    Backport bug with executeBatch.  This bug was a regression from 7.2 and has
    been reported by Marko Strukelj and Keith Wannamaker
    
     Modified Files:
      Tag: REL7_3_STABLE
     	jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
     	jdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java
     	jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java
---
 org/postgresql/jdbc1/AbstractJdbc1Statement.java |  2 +-
 org/postgresql/jdbc2/AbstractJdbc2Statement.java | 40 +++++++++++++++++++++---
 org/postgresql/test/jdbc2/BatchExecuteTest.java  | 12 +++++--
 3 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/org/postgresql/jdbc1/AbstractJdbc1Statement.java b/org/postgresql/jdbc1/AbstractJdbc1Statement.java
index 6f08ebd..c107a8c 100644
--- a/org/postgresql/jdbc1/AbstractJdbc1Statement.java
+++ b/org/postgresql/jdbc1/AbstractJdbc1Statement.java
@@ -47,7 +47,7 @@ public abstract class AbstractJdbc1Statement implements org.postgresql.PGStateme
 	private String[] m_origSqlFragments;
 	private String[] m_executeSqlFragments;
 	protected Object[] m_binds = new Object[0];
-	private String[] m_bindTypes = new String[0];
+	protected String[] m_bindTypes = new String[0];
 	private String m_statementName = null;
 	private boolean m_useServerPrepare = false;
 	private static int m_preparedCount = 1;
diff --git a/org/postgresql/jdbc2/AbstractJdbc2Statement.java b/org/postgresql/jdbc2/AbstractJdbc2Statement.java
index 4815950..a77fc50 100644
--- a/org/postgresql/jdbc2/AbstractJdbc2Statement.java
+++ b/org/postgresql/jdbc2/AbstractJdbc2Statement.java
@@ -59,7 +59,8 @@ public abstract class AbstractJdbc2Statement extends org.postgresql.jdbc1.Abstra
 	{
 		if (batch == null)
 			batch = new Vector();
-		batch.addElement(p_sql);
+        Object[] l_statement = new Object[] {new String[] {p_sql}, new Object[0], new String[0]};
+		batch.addElement(l_statement);
 	}
 
 	public void clearBatch() throws SQLException
@@ -76,8 +77,25 @@ public abstract class AbstractJdbc2Statement extends org.postgresql.jdbc1.Abstra
 		int i = 0;
 		try
 		{
-			for (i = 0;i < size;i++)
-				result[i] = this.executeUpdate((String)batch.elementAt(i));
+            //copy current state of statement
+			String[] l_origSqlFragments = m_sqlFragments;
+			Object[] l_origBinds = m_binds;
+			String[] l_origBindTypes = m_bindTypes;
+
+			for (i = 0;i < size;i++) {
+                //set state from batch
+                Object[] l_statement = (Object[])batch.elementAt(i);
+				this.m_sqlFragments = (String[])l_statement[0];
+				this.m_binds = (Object[])l_statement[1];
+				this.m_bindTypes = (String[])l_statement[2];
+				result[i] = this.executeUpdate();
+			}
+
+            //restore state of statement
+            String[] m_sqlFragments = l_origSqlFragments;
+			Object[] m_binds = l_origBinds;
+			String[] m_bindTypes = l_origBindTypes;
+
 		}
 		catch (SQLException e)
 		{
@@ -150,7 +168,21 @@ public abstract class AbstractJdbc2Statement extends org.postgresql.jdbc1.Abstra
 
 	public void addBatch() throws SQLException
 	{
-		addBatch(this.toString());
+		if (batch == null)
+			batch = new Vector();
+
+		//we need to create copies, otherwise the values can be changed
+		Object[] l_newSqlFragments = null;
+		if (m_sqlFragments != null) { 
+			l_newSqlFragments = new String[m_sqlFragments.length];
+			System.arraycopy(m_sqlFragments,0,l_newSqlFragments,0,m_sqlFragments.length);
+		}
+		Object[] l_newBinds = new String[m_binds.length];
+        System.arraycopy(m_binds,0,l_newBinds,0,m_binds.length);
+		String[] l_newBindTypes = new String[m_bindTypes.length];
+        System.arraycopy(m_bindTypes,0,l_newBindTypes,0,m_bindTypes.length);
+        Object[] l_statement = new Object[] {l_newSqlFragments, l_newBinds, l_newBindTypes};
+		batch.addElement(l_statement);
 	}
 
 	public java.sql.ResultSetMetaData getMetaData() throws SQLException
diff --git a/org/postgresql/test/jdbc2/BatchExecuteTest.java b/org/postgresql/test/jdbc2/BatchExecuteTest.java
index 1c25692..e7f27ed 100644
--- a/org/postgresql/test/jdbc2/BatchExecuteTest.java
+++ b/org/postgresql/test/jdbc2/BatchExecuteTest.java
@@ -157,11 +157,19 @@ public class BatchExecuteTest extends TestCase
 		pstmt.executeBatch();
 		assertCol1HasValue(7);
 
-		con.commit();
+		//now test to see that we can still use the statement after the execute
+		pstmt.setInt(1, 3);
+		pstmt.addBatch();
 		assertCol1HasValue(7);
 
+		pstmt.executeBatch();
+		assertCol1HasValue(10);
+
+		con.commit();
+		assertCol1HasValue(10);
+
 		con.rollback();
-		assertCol1HasValue(7);
+		assertCol1HasValue(10);
 
 		pstmt.close();
 	}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git



More information about the pkg-java-commits mailing list