X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E7ED73A3C5D for ; Wed, 17 Nov 2004 15:32:13 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 09137-01 for ; Wed, 17 Nov 2004 15:32:03 +0000 (GMT) Received: from main.gmane.org (main.gmane.org [80.91.229.2]) by svr1.postgresql.org (Postfix) with ESMTP id 509653A4030 for ; Wed, 17 Nov 2004 15:32:06 +0000 (GMT) Received: from root by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1CURmw-0000Kw-00 for ; Wed, 17 Nov 2004 16:32:06 +0100 Received: from secure.openpricer.com ([80.65.224.34]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 17 Nov 2004 16:32:06 +0100 Received: from xpoinsard by secure.openpricer.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 17 Nov 2004 16:32:06 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-jdbc@postgresql.org From: Xavier Poinsard Subject: Tests for prepared insert statements Date: Wed, 17 Nov 2004 16:28:37 +0100 Lines: 193 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------090408000205090108060306" X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: secure.openpricer.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041007 Debian/1.7.3-5 X-Accept-Language: fr-fr, en-us, en X-Archive: Encrypt X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/127 X-Sequence-Number: 11549 This is a multi-part message in MIME format. --------------090408000205090108060306 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Hi, Since I was suspecting the driver, I wrote a two tests regarding prepared insert statements. May be they could be included. Xavier. --------------090408000205090108060306 Content-Type: text/plain; name="PreparedInsertTest.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="PreparedInsertTest.diff" Index: Jdbc2TestSuite.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java,v retrieving revision 1.18 diff -u -r1.18 Jdbc2TestSuite.java --- Jdbc2TestSuite.java 9 Nov 2004 08:54:37 -0000 1.18 +++ Jdbc2TestSuite.java 17 Nov 2004 15:16:40 -0000 @@ -55,6 +55,7 @@ // PreparedStatement suite.addTestSuite(PreparedStatementTest.class); suite.addTestSuite(StatementTest.class); + suite.addTestSuite(BatchPreparedInsertTest.class); // ServerSide Prepared Statements suite.addTestSuite(ServerPreparedStmtTest.class); Index: BatchPreparedInsertTest.java =================================================================== RCS file: BatchPreparedInsertTest.java diff -N BatchPreparedInsertTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ BatchPreparedInsertTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,151 @@ +/*------------------------------------------------------------------------- +* +* Copyright (c) 2004, PostgreSQL Global Development Group +* +* IDENTIFICATION +* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java,v 1.11 2004/11/09 08:54:00 jurka Exp $ +* +*------------------------------------------------------------------------- +*/ +package org.postgresql.test.jdbc2; + +import org.postgresql.test.TestUtil; +import junit.framework.TestCase; +import java.sql.*; + + +/* + * Test case for prepared insert statements + */ +public class BatchPreparedInsertTest extends TestCase +{ + + private Connection con; + + public BatchPreparedInsertTest(String name) + { + super(name); + } + + // Set up the fixture for this testcase: a connection to a database with + // a table for this test. + protected void setUp() throws Exception + { + con = TestUtil.openDB(); + Statement stmt = con.createStatement(); + + // Drop the test table if it already exists for some reason. It is + // not an error if it doesn't exist. + TestUtil.createTable(con, "testbatchinsert", "pk INTEGER, colInt INTEGER,colReal REAL, colVarchar VARCHAR(10)"); + + stmt.executeUpdate("INSERT INTO testbatchinsert VALUES (1, 0,0,'')"); + + // Generally recommended with batch updates. By default we run all + // tests in this test case with autoCommit disabled. + con.setAutoCommit(false); + } + + // Tear down the fixture for this test case. + protected void tearDown() throws Exception + { + con.setAutoCommit(true); + + TestUtil.dropTable(con, "testbatchinsert"); + TestUtil.closeDB(con); + } + + private void assertColIntHasValue(int expected) throws Exception + { + Statement getCol1 = con.createStatement(); + + ResultSet rs = + getCol1.executeQuery("SELECT colInt FROM testbatchinsert WHERE pk = 1"); + assertTrue(rs.next()); + + int actual = rs.getInt("colInt"); + + assertEquals(expected, actual); + + assertEquals(false, rs.next()); + + rs.close(); + getCol1.close(); + } + + private void assertColRealHasValue(float expected) throws Exception + { + Statement getCol1 = con.createStatement(); + + ResultSet rs = + getCol1.executeQuery("SELECT colReal FROM testbatchinsert WHERE pk = 1"); + assertTrue(rs.next()); + + float actual = rs.getFloat("colReal"); + + assertEquals(expected, actual,0f); + + assertEquals(false, rs.next()); + + rs.close(); + getCol1.close(); + } + + private void assertColVarcharHasValue(String expected) throws Exception + { + Statement getCol1 = con.createStatement(); + + ResultSet rs = + getCol1.executeQuery("SELECT colVarchar FROM testbatchinsert WHERE pk = 1"); + assertTrue(rs.next()); + + String actual = rs.getString("colVarchar"); + + assertEquals(expected, actual); + + assertEquals(false, rs.next()); + + rs.close(); + getCol1.close(); + } + + + /** + * test basic insert prepared statement + * @throws Exception + */ + public void testPreparedStatement() throws Exception + { + PreparedStatement pstmt = con.prepareStatement( + "INSERT INTO testbatchinsert (pk,colInt,colReal,colVarchar)" + + "VALUES (1,?,?,?)" ); + + pstmt.setInt(1, 1); + pstmt.setFloat(2, 0.3f); + pstmt.setString(3,"test"); + pstmt.execute(); + con.commit(); + pstmt.close(); + } + /** + * test a batch of prepared insert statements + * @throws Exception + */ + public void testBatchPreparedStatement() throws Exception + { + PreparedStatement pstmt = con.prepareStatement( + "INSERT INTO testbatchinsert (pk,colInt,colReal,colVarchar)" + + "VALUES (?,?,?,?)" ); + + for (int i=1;i<=TestUtil.getPrepareThreshold()+3;i++){ + pstmt.setInt(1, i+1); + pstmt.setInt(2, i+1); + pstmt.setFloat(3, i*0.3f); + pstmt.setString(4,"test"+i); + pstmt.addBatch(); + } + pstmt.executeBatch(); + con.commit(); + pstmt.close(); + } + +} --------------090408000205090108060306--