pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Xavier Poinsard <xpoinsard@openpricer.com>
To: pgsql-jdbc@postgresql.org
Subject: Tests for prepared insert statements
Date: Wed, 17 Nov 2004 16:28:37 +0100
Message-ID: <cnfqn2$99h$2@sea.gmane.org> (raw )
Hi,
Since I was suspecting the driver, I wrote a two tests regarding
prepared insert statements. May be they could be included.
Xavier.
Attachments:
PreparedInsertTest.diff (text/plain, 5.3K) download
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();
+ }
+
+}
view thread (2+ messages) latest in thread
Message-ID: <cnfqn2$99h$2@sea.gmane.org>
Permalink: ../cnfqn2$99h$2@sea.gmane.org/
Also on: postgresql.org/message-id/cnfqn2$99h$2@sea.gmane.org
copy link · copy postgr.es · copy git am
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: xpoinsard@openpricer.com
Subject: Re: Tests for prepared insert statements
In-Reply-To: <cnfqn2$99h$2@sea.gmane.org>
References: <cnfqn2$99h$2@sea.gmane.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox