1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java,v 1.17 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2;
import org.postgresql.PGStatement;
import org.postgresql.test.TestUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import junit.framework.TestCase;
/*
* 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, value boolean");
stmt.executeUpdate("INSERT INTO testsps VALUES (1,'t')");
stmt.executeUpdate("INSERT INTO testsps VALUES (2,'t')");
stmt.executeUpdate("INSERT INTO testsps VALUES (3,'t')");
stmt.executeUpdate("INSERT INTO testsps VALUES (4,'t')");
stmt.executeUpdate("INSERT INTO testsps VALUES (6,'t')");
stmt.executeUpdate("INSERT INTO testsps VALUES (9,'f')");
stmt.close();
}
protected void tearDown() throws Exception
{
TestUtil.dropTable(con, "testsps");
TestUtil.closeDB(con);
}
public void testPreparedExecuteCount() throws Exception
{
PreparedStatement pstmt = con.prepareStatement("UPDATE testsps SET id = id + 44");
((PGStatement)pstmt).setUseServerPrepare(true);
int count = pstmt.executeUpdate();
assertEquals(6, count);
pstmt.close();
}
public void testPreparedStatementsNoBinds() throws Exception
{
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = 2");
((PGStatement)pstmt).setUseServerPrepare(true);
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 still works after turning off prepares
((PGStatement)pstmt).setUseServerPrepare(false);
assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(2, 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);
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 still works after turning off prepares
((PGStatement)pstmt).setUseServerPrepare(false);
assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
pstmt.setInt(1, 9);
rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(9, rs.getInt(1));
rs.close();
pstmt.close();
}
// Verify we can bind booleans-as-objects ok.
public void testBooleanObjectBind() throws Exception
{
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE value = ?");
((PGStatement)pstmt).setUseServerPrepare(true);
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
pstmt.setObject(1, new Boolean(false), java.sql.Types.BIT);
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(9, rs.getInt(1));
rs.close();
}
// Verify we can bind booleans-as-integers ok.
public void testBooleanIntegerBind() throws Exception
{
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = ?");
((PGStatement)pstmt).setUseServerPrepare(true);
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
pstmt.setObject(1, new Boolean(true), java.sql.Types.INTEGER);
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
rs.close();
}
// Verify we can bind booleans-as-native-types ok.
public void testBooleanBind() throws Exception
{
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE value = ?");
((PGStatement)pstmt).setUseServerPrepare(true);
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
pstmt.setBoolean(1, false);
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(9, rs.getInt(1));
rs.close();
}
public void testPreparedStatementsWithBinds() throws Exception
{
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = ? or id = ?");
((PGStatement)pstmt).setUseServerPrepare(true);
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
//Test that basic functionality works
//bind different datatypes
pstmt.setInt(1, 2);
pstmt.setLong(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();
}
public void testSPSToggle() throws Exception
{
// Verify we can toggle UseServerPrepare safely before a query is executed.
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = 2");
((PGStatement)pstmt).setUseServerPrepare(true);
((PGStatement)pstmt).setUseServerPrepare(false);
}
public void testBytea() throws Exception
{
// Verify we can use setBytes() with a server-prepared update.
try
{
TestUtil.createTable(con, "testsps_bytea", "data bytea");
PreparedStatement pstmt = con.prepareStatement("INSERT INTO testsps_bytea(data) VALUES (?)");
((PGStatement)pstmt).setUseServerPrepare(true);
pstmt.setBytes(1, new byte[100]);
pstmt.executeUpdate();
}
finally
{
TestUtil.dropTable(con, "testsps_bytea");
}
}
// Check statements are not transformed when they shouldn't be.
public void testCreateTable() throws Exception {
// CREATE TABLE isn't supported by PREPARE; the driver should realize this and
// still complete without error.
PreparedStatement pstmt = con.prepareStatement("CREATE TABLE testsps_bad(data int)");
((PGStatement)pstmt).setUseServerPrepare(true);
pstmt.executeUpdate();
TestUtil.dropTable(con, "testsps_bad");
}
public void testMultistatement() throws Exception {
// Shouldn't try to PREPARE this one, if we do we get:
// PREPARE x(int,int) AS INSERT .... $1 ; INSERT ... $2 -- syntax error
try
{
TestUtil.createTable(con, "testsps_multiple", "data int");
PreparedStatement pstmt = con.prepareStatement("INSERT INTO testsps_multiple(data) VALUES (?); INSERT INTO testsps_multiple(data) VALUES (?)");
((PGStatement)pstmt).setUseServerPrepare(true);
pstmt.setInt(1, 1);
pstmt.setInt(2, 2);
pstmt.executeUpdate(); // Two inserts.
pstmt.setInt(1, 3);
pstmt.setInt(2, 4);
pstmt.executeUpdate(); // Two more inserts.
ResultSet check = con.createStatement().executeQuery("SELECT COUNT(*) FROM testsps_multiple");
assertTrue(check.next());
assertEquals(4, check.getInt(1));
}
finally
{
TestUtil.dropTable(con, "testsps_multiple");
}
}
public void testTypeChange() throws Exception {
PreparedStatement pstmt = con.prepareStatement("SELECT CAST (? AS TEXT)");
((PGStatement)pstmt).setUseServerPrepare(true);
// Prepare with int parameter.
pstmt.setInt(1, 1);
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertTrue(!rs.next());
// Change to text parameter, check it still works.
pstmt.setString(1, "test string");
rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals("test string", rs.getString(1));
assertTrue(!rs.next());
}
}
|