|
| 1 | +packageorg.postgresql.test.jdbc2; |
| 2 | + |
| 3 | +importorg.postgresql.test.TestUtil; |
| 4 | +importorg.postgresql.PGStatement; |
| 5 | +importjunit.framework.TestCase; |
| 6 | +importjava.io.*; |
| 7 | +importjava.sql.*; |
| 8 | + |
| 9 | +/* |
| 10 | + * Tests for using server side prepared statements |
| 11 | + */ |
| 12 | +publicclassServerPreparedStmtTestextendsTestCase |
| 13 | +{ |
| 14 | +privateConnectioncon; |
| 15 | + |
| 16 | +publicServerPreparedStmtTest(Stringname) |
| 17 | +{ |
| 18 | +super(name); |
| 19 | +} |
| 20 | + |
| 21 | +protectedvoidsetUp()throwsException |
| 22 | +{ |
| 23 | +con =TestUtil.openDB(); |
| 24 | +Statementstmt =con.createStatement(); |
| 25 | + |
| 26 | +TestUtil.createTable(con,"testsps","id integer"); |
| 27 | + |
| 28 | +stmt.executeUpdate("INSERT INTO testsps VALUES (1)"); |
| 29 | +stmt.executeUpdate("INSERT INTO testsps VALUES (2)"); |
| 30 | +stmt.executeUpdate("INSERT INTO testsps VALUES (3)"); |
| 31 | +stmt.executeUpdate("INSERT INTO testsps VALUES (4)"); |
| 32 | +stmt.executeUpdate("INSERT INTO testsps VALUES (6)"); |
| 33 | +stmt.executeUpdate("INSERT INTO testsps VALUES (9)"); |
| 34 | + |
| 35 | +stmt.close(); |
| 36 | +} |
| 37 | + |
| 38 | +protectedvoidtearDown()throwsException |
| 39 | +{ |
| 40 | +TestUtil.dropTable(con,"testsps"); |
| 41 | +TestUtil.closeDB(con); |
| 42 | +} |
| 43 | + |
| 44 | +publicvoidtestPreparedStatementsNoBinds()throwsException |
| 45 | +{ |
| 46 | +PreparedStatementpstmt =con.prepareStatement("SELECT * FROM testsps WHERE id = 2"); |
| 47 | + ((PGStatement)pstmt).setUseServerPrepare(true); |
| 48 | +if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) { |
| 49 | +assertTrue(((PGStatement)pstmt).isUseServerPrepare()); |
| 50 | +}else { |
| 51 | +assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); |
| 52 | +} |
| 53 | + |
| 54 | +//Test that basic functionality works |
| 55 | +ResultSetrs =pstmt.executeQuery(); |
| 56 | +assertTrue(rs.next()); |
| 57 | +assertEquals(2,rs.getInt(1)); |
| 58 | +rs.close(); |
| 59 | + |
| 60 | +//Verify that subsequent calls still work |
| 61 | +rs =pstmt.executeQuery(); |
| 62 | +assertTrue(rs.next()); |
| 63 | +assertEquals(2,rs.getInt(1)); |
| 64 | +rs.close(); |
| 65 | + |
| 66 | +//Verify that using the statement to execute a different query works |
| 67 | +rs =pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9"); |
| 68 | +assertTrue(rs.next()); |
| 69 | +assertEquals(9,rs.getInt(1)); |
| 70 | +rs.close(); |
| 71 | + |
| 72 | + ((PGStatement)pstmt).setUseServerPrepare(false); |
| 73 | +assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); |
| 74 | + |
| 75 | +//Verify that using the statement still works after turning off prepares |
| 76 | +rs =pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9"); |
| 77 | +assertTrue(rs.next()); |
| 78 | +assertEquals(9,rs.getInt(1)); |
| 79 | +rs.close(); |
| 80 | + |
| 81 | +pstmt.close(); |
| 82 | +} |
| 83 | + |
| 84 | +publicvoidtestPreparedStatementsWithOneBind()throwsException |
| 85 | +{ |
| 86 | +PreparedStatementpstmt =con.prepareStatement("SELECT * FROM testsps WHERE id = ?"); |
| 87 | + ((PGStatement)pstmt).setUseServerPrepare(true); |
| 88 | +if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) { |
| 89 | +assertTrue(((PGStatement)pstmt).isUseServerPrepare()); |
| 90 | +}else { |
| 91 | +assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); |
| 92 | +} |
| 93 | + |
| 94 | +//Test that basic functionality works |
| 95 | +pstmt.setInt(1,2); |
| 96 | +ResultSetrs =pstmt.executeQuery(); |
| 97 | +assertTrue(rs.next()); |
| 98 | +assertEquals(2,rs.getInt(1)); |
| 99 | +rs.close(); |
| 100 | + |
| 101 | +//Verify that subsequent calls still work |
| 102 | +rs =pstmt.executeQuery(); |
| 103 | +assertTrue(rs.next()); |
| 104 | +assertEquals(2,rs.getInt(1)); |
| 105 | +rs.close(); |
| 106 | + |
| 107 | +//Verify that using the statement to execute a different query works |
| 108 | +rs =pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9"); |
| 109 | +assertTrue(rs.next()); |
| 110 | +assertEquals(9,rs.getInt(1)); |
| 111 | +rs.close(); |
| 112 | + |
| 113 | + ((PGStatement)pstmt).setUseServerPrepare(false); |
| 114 | +assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); |
| 115 | + |
| 116 | +//Verify that using the statement still works after turning off prepares |
| 117 | +rs =pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9"); |
| 118 | +assertTrue(rs.next()); |
| 119 | +assertEquals(9,rs.getInt(1)); |
| 120 | +rs.close(); |
| 121 | + |
| 122 | +pstmt.close(); |
| 123 | +} |
| 124 | + |
| 125 | +publicvoidtestPreparedStatementsWithBinds()throwsException |
| 126 | +{ |
| 127 | +PreparedStatementpstmt =con.prepareStatement("SELECT * FROM testsps WHERE id = ? or id = ?"); |
| 128 | + ((PGStatement)pstmt).setUseServerPrepare(true); |
| 129 | +if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) { |
| 130 | +assertTrue(((PGStatement)pstmt).isUseServerPrepare()); |
| 131 | +}else { |
| 132 | +assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); |
| 133 | +} |
| 134 | + |
| 135 | +//Test that basic functionality works |
| 136 | +//bind different datatypes |
| 137 | +pstmt.setInt(1,2); |
| 138 | +pstmt.setString(2,"2"); |
| 139 | +ResultSetrs =pstmt.executeQuery(); |
| 140 | +assertTrue(rs.next()); |
| 141 | +assertEquals(2,rs.getInt(1)); |
| 142 | +rs.close(); |
| 143 | + |
| 144 | +//Verify that subsequent calls still work |
| 145 | +rs =pstmt.executeQuery(); |
| 146 | +assertTrue(rs.next()); |
| 147 | +assertEquals(2,rs.getInt(1)); |
| 148 | +rs.close(); |
| 149 | + |
| 150 | +pstmt.close(); |
| 151 | +} |
| 152 | + |
| 153 | +} |