From 7d6a055a7f8a3fef6cdf1d23d738c84663fc1161 Mon Sep 17 00:00:00 2001
From: Barry Lind <barry@xythos.com>
Date: Sat, 14 Sep 2002 03:52:56 +0000
Subject: [PATCH] Added regression test for using server side prepared
 statements in jdbc and fixed a bug found by the regression test

 Modified Files:
 	jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
 	jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java
 Added Files:
 	jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java
---
 .../jdbc1/AbstractJdbc1Statement.java         |   3 +-
 .../postgresql/test/jdbc2/Jdbc2TestSuite.java |   7 +-
 .../test/jdbc2/ServerPreparedStmtTest.java    | 153 ++++++++++++++++++
 3 files changed, 160 insertions(+), 3 deletions(-)
 create mode 100644 src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java

diff --git a/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java b/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
index e7e5d85d226..dc9bafd2bbe 100644
--- a/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
+++ b/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
@@ -8,7 +8,7 @@ import java.util.Vector;
 import org.postgresql.largeobject.*;
 import org.postgresql.util.*;
 
-/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.9 2002/09/11 05:38:44 barry Exp $
+/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.10 2002/09/14 03:52:56 barry Exp $
  * This class defines methods of the jdbc1 specification.  This class is
  * extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
  * methods.  The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
@@ -129,6 +129,7 @@ public abstract class AbstractJdbc1Statement implements org.postgresql.PGStateme
 	{
 		String l_sql = replaceProcessing(p_sql);
 		m_sqlFragments = new String[] {l_sql};
+		m_binds = new Object[0];
 		//If we have already created a server prepared statement, we need
 		//to deallocate the existing one
 		if (m_statementName != null) {
diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java
index 3dbc1f8d321..2a117cecf03 100644
--- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java
+++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java
@@ -48,6 +48,9 @@ public class Jdbc2TestSuite extends TestSuite
 
 		// PreparedStatement
 
+        // ServerSide Prepared Statements
+        suite.addTestSuite(ServerPreparedStmtTest.class);
+
 		// BatchExecute
 		suite.addTestSuite(BatchExecuteTest.class);
 
@@ -60,9 +63,9 @@ public class Jdbc2TestSuite extends TestSuite
 
 		// Fastpath/LargeObject
 		suite.addTestSuite(BlobTest.class);
-		suite.addTestSuite( UpdateableResultTest.class );
+		suite.addTestSuite(UpdateableResultTest.class );
 
-		suite.addTestSuite( CallableStmtTest.class );
+		suite.addTestSuite(CallableStmtTest.class );
 
 		// That's all folks
 		return suite;
diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java
new file mode 100644
index 00000000000..7617603857e
--- /dev/null
+++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java
@@ -0,0 +1,153 @@
+package org.postgresql.test.jdbc2;
+
+import org.postgresql.test.TestUtil;
+import org.postgresql.PGStatement;
+import junit.framework.TestCase;
+import java.io.*;
+import java.sql.*;
+
+/*
+ *  Tests for using server side prepared statements
+ */
+public class ServerPreparedStmtTest extends TestCase
+{
+	private Connection con;
+
+	public ServerPreparedStmtTest(String name)
+	{
+		super(name);
+	}
+
+	protected void setUp() throws Exception
+	{
+		con = TestUtil.openDB();
+		Statement stmt = con.createStatement();
+
+		TestUtil.createTable(con, "testsps", "id integer");
+
+		stmt.executeUpdate("INSERT INTO testsps VALUES (1)");
+		stmt.executeUpdate("INSERT INTO testsps VALUES (2)");
+		stmt.executeUpdate("INSERT INTO testsps VALUES (3)");
+		stmt.executeUpdate("INSERT INTO testsps VALUES (4)");
+		stmt.executeUpdate("INSERT INTO testsps VALUES (6)");
+		stmt.executeUpdate("INSERT INTO testsps VALUES (9)");
+
+		stmt.close();
+	}
+
+	protected void tearDown() throws Exception
+	{
+		TestUtil.dropTable(con, "testsps");
+		TestUtil.closeDB(con);
+	}
+
+	public void testPreparedStatementsNoBinds() throws Exception
+	{
+		PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = 2");
+        ((PGStatement)pstmt).setUseServerPrepare(true);
+        if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) {
+			assertTrue(((PGStatement)pstmt).isUseServerPrepare());
+		} else {
+			assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
+		}
+
+        //Test that basic functionality works
+		ResultSet rs = pstmt.executeQuery();
+		assertTrue(rs.next());
+        assertEquals(2, rs.getInt(1));
+        rs.close();
+
+        //Verify that subsequent calls still work
+		rs = pstmt.executeQuery();
+		assertTrue(rs.next());
+        assertEquals(2, rs.getInt(1));
+        rs.close();
+
+        //Verify that using the statement to execute a different query works
+		rs = pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9");
+		assertTrue(rs.next());
+        assertEquals(9, rs.getInt(1));
+        rs.close();
+
+        ((PGStatement)pstmt).setUseServerPrepare(false);
+        assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
+
+        //Verify that using the statement still works after turning off prepares
+		rs = pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9");
+		assertTrue(rs.next());
+        assertEquals(9, rs.getInt(1));
+        rs.close();
+
+		pstmt.close();
+	}
+
+	public void testPreparedStatementsWithOneBind() throws Exception
+	{
+		PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = ?");
+        ((PGStatement)pstmt).setUseServerPrepare(true);
+        if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) {
+			assertTrue(((PGStatement)pstmt).isUseServerPrepare());
+		} else {
+			assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
+		}
+
+        //Test that basic functionality works
+        pstmt.setInt(1,2);
+		ResultSet rs = pstmt.executeQuery();
+		assertTrue(rs.next());
+        assertEquals(2, rs.getInt(1));
+        rs.close();
+
+        //Verify that subsequent calls still work
+		rs = pstmt.executeQuery();
+		assertTrue(rs.next());
+        assertEquals(2, rs.getInt(1));
+        rs.close();
+
+        //Verify that using the statement to execute a different query works
+		rs = pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9");
+		assertTrue(rs.next());
+        assertEquals(9, rs.getInt(1));
+        rs.close();
+
+        ((PGStatement)pstmt).setUseServerPrepare(false);
+        assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
+
+        //Verify that using the statement still works after turning off prepares
+		rs = pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9");
+		assertTrue(rs.next());
+        assertEquals(9, rs.getInt(1));
+        rs.close();
+
+		pstmt.close();
+	}
+
+	public void testPreparedStatementsWithBinds() throws Exception
+	{
+		PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = ? or id = ?");
+        ((PGStatement)pstmt).setUseServerPrepare(true);
+        if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) {
+			assertTrue(((PGStatement)pstmt).isUseServerPrepare());
+		} else {
+			assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
+		}
+
+        //Test that basic functionality works
+        //bind different datatypes
+        pstmt.setInt(1,2);
+        pstmt.setString(2,"2");
+		ResultSet rs = pstmt.executeQuery();
+		assertTrue(rs.next());
+        assertEquals(2, rs.getInt(1));
+        rs.close();
+
+        //Verify that subsequent calls still work
+		rs = pstmt.executeQuery();
+		assertTrue(rs.next());
+        assertEquals(2, rs.getInt(1));
+        rs.close();
+
+		pstmt.close();
+	}
+
+}
-- 
GitLab