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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2001-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java,v 1.30 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2;
import java.sql.*;
import java.util.Arrays;
import junit.framework.TestCase;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.TimeZone;
import org.postgresql.test.TestUtil;
public class UpdateableResultTest extends TestCase
{
private Connection con;
public UpdateableResultTest( String name )
{
super( name );
try
{
Class.forName("org.postgresql.Driver");
}
catch( Exception ex ){}
}
protected void setUp() throws Exception
{
con = TestUtil.openDB();
TestUtil.createTable(con, "updateable", "id int primary key, name text, notselected text, ts timestamp with time zone, intarr int[]", true);
TestUtil.createTable(con, "second", "id1 int primary key, name1 text");
TestUtil.createTable(con, "stream", "id int primary key, asi text, chr text, bin bytea");
// put some dummy data into second
Statement st2 = con.createStatement();
st2.execute( "insert into second values (1,'anyvalue' )");
st2.close();
}
protected void tearDown() throws Exception
{
TestUtil.dropTable(con, "updateable");
TestUtil.dropTable(con, "second");
TestUtil.dropTable(con, "stream");
TestUtil.closeDB(con);
}
public void testDeleteRows() throws SQLException
{
Statement st = con.createStatement();
st.executeUpdate("INSERT INTO second values (2,'two')");
st.executeUpdate("INSERT INTO second values (3,'three')");
st.executeUpdate("INSERT INTO second values (4,'four')");
st.close();
st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select id1,name1 from second order by id1");
assertTrue(rs.next());
assertEquals(1, rs.getInt("id1"));
rs.deleteRow();
assertTrue(rs.isBeforeFirst());
assertTrue(rs.next());
assertTrue(rs.next());
assertEquals(3, rs.getInt("id1"));
rs.deleteRow();
assertEquals(2, rs.getInt("id1"));
rs.close();
st.close();
}
public void testCancelRowUpdates() throws Exception
{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select * from second");
// make sure we're dealing with the correct row.
rs.first();
assertEquals(1, rs.getInt(1));
assertEquals("anyvalue", rs.getString(2));
// update, cancel and make sure nothings changed.
rs.updateInt(1, 99);
rs.cancelRowUpdates();
assertEquals(1, rs.getInt(1));
assertEquals("anyvalue", rs.getString(2));
// real update
rs.updateInt(1, 999);
rs.updateRow();
assertEquals(999, rs.getInt(1));
assertEquals("anyvalue", rs.getString(2));
// scroll some and make sure the update is still there
rs.beforeFirst();
rs.next();
assertEquals(999, rs.getInt(1));
assertEquals("anyvalue", rs.getString(2));
// make sure the update got to the db and the driver isn't lying to us.
rs.close();
rs = st.executeQuery( "select * from second");
rs.first();
assertEquals(999, rs.getInt(1));
assertEquals("anyvalue", rs.getString(2));
rs.close();
st.close();
}
private void checkPositioning(ResultSet rs) throws SQLException
{
try
{
rs.getInt(1);
fail("Can't use an incorrectly positioned result set.");
}
catch (SQLException sqle)
{
}
try
{
rs.updateInt(1, 2);
fail("Can't use an incorrectly positioned result set.");
}
catch (SQLException sqle)
{
}
try
{
rs.updateRow();
fail("Can't use an incorrectly positioned result set.");
}
catch (SQLException sqle)
{
}
try
{
rs.deleteRow();
fail("Can't use an incorrectly positioned result set.");
}
catch (SQLException sqle)
{
}
}
public void testPositioning() throws SQLException
{
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT id1,name1 FROM second");
checkPositioning(rs);
assertTrue(rs.next());
rs.beforeFirst();
checkPositioning(rs);
rs.afterLast();
checkPositioning(rs);
rs.beforeFirst();
assertTrue(rs.next());
assertTrue(!rs.next());
checkPositioning(rs);
rs.afterLast();
assertTrue(rs.previous());
assertTrue(!rs.previous());
checkPositioning(rs);
rs.close();
stmt.close();
}
public void testUpdateTimestamp() throws SQLException
{
TimeZone origTZ = TimeZone.getDefault();
try {
// We choose a timezone which has a partial hour portion
// Asia/Tehran is +3:30
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tehran"));
Timestamp ts = Timestamp.valueOf("2006-11-20 16:17:18");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT id, ts FROM updateable");
rs.moveToInsertRow();
rs.updateInt(1,1);
rs.updateTimestamp(2,ts);
rs.insertRow();
rs.first();
assertEquals(ts, rs.getTimestamp(2));
} finally {
TimeZone.setDefault(origTZ);
}
}
public void testUpdateStreams() throws SQLException, UnsupportedEncodingException
{
String string = "Hello";
byte[] bytes = new byte[]{0,'\\',(byte) 128,(byte) 255};
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT id, asi, chr, bin FROM stream");
rs.moveToInsertRow();
rs.updateInt(1, 1);
rs.updateAsciiStream("asi", null, 17);
rs.updateCharacterStream("chr", null, 81);
rs.updateBinaryStream("bin", null, 0);
rs.insertRow();
rs.moveToInsertRow();
rs.updateInt(1, 3);
rs.updateAsciiStream("asi", new ByteArrayInputStream(string.getBytes("US-ASCII")), 5);
rs.updateCharacterStream("chr", new StringReader(string), 5);
rs.updateBinaryStream("bin", new ByteArrayInputStream(bytes), bytes.length);
rs.insertRow();
rs.beforeFirst();
rs.next();
assertEquals(1, rs.getInt(1));
assertNull(rs.getString(2));
assertNull(rs.getString(3));
assertNull(rs.getBytes(4));
rs.updateInt("id", 2);
rs.updateAsciiStream("asi", new ByteArrayInputStream(string.getBytes("US-ASCII")), 5);
rs.updateCharacterStream("chr", new StringReader(string), 5);
rs.updateBinaryStream("bin", new ByteArrayInputStream(bytes), bytes.length);
rs.updateRow();
assertEquals(2, rs.getInt(1));
assertEquals(string, rs.getString(2));
assertEquals(string, rs.getString(3));
assertTrue(Arrays.equals(bytes, rs.getBytes(4)));
rs.refreshRow();
assertEquals(2, rs.getInt(1));
assertEquals(string, rs.getString(2));
assertEquals(string, rs.getString(3));
assertTrue(Arrays.equals(bytes, rs.getBytes(4)));
rs.next();
assertEquals(3, rs.getInt(1));
assertEquals(string, rs.getString(2));
assertEquals(string, rs.getString(3));
assertTrue(Arrays.equals(bytes, rs.getBytes(4)));
rs.close();
stmt.close();
}
public void testZeroRowResult() throws SQLException
{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select * from updateable WHERE 0 > 1");
assertTrue(!rs.next());
rs.moveToInsertRow();
rs.moveToCurrentRow();
}
public void testUpdateable() throws SQLException
{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select * from updateable");
assertNotNull( rs );
rs.moveToInsertRow();
rs.updateInt( 1, 1 );
rs.updateString( 2, "jake" );
rs.updateString( 3, "avalue" );
rs.insertRow();
rs.first();
rs.updateInt( "id", 2 );
rs.updateString( "name", "dave" );
rs.updateRow();
assertEquals(2, rs.getInt("id"));
assertEquals("dave", rs.getString("name"));
assertEquals("avalue", rs.getString("notselected"));
rs.deleteRow();
rs.moveToInsertRow();
rs.updateInt("id", 3);
rs.updateString("name", "paul");
rs.insertRow();
try
{
rs.refreshRow();
fail("Can't refresh when on the insert row.");
}
catch (SQLException sqle)
{
}
assertEquals(3, rs.getInt("id"));
assertEquals("paul", rs.getString("name"));
assertNull(rs.getString("notselected"));
rs.close();
rs = st.executeQuery("select id1, id, name, name1 from updateable, second" );
try
{
while ( rs.next() )
{
rs.updateInt( "id", 2 );
rs.updateString( "name", "dave" );
rs.updateRow();
}
fail("should not get here, update should fail");
}
catch (SQLException ex)
{
}
rs = st.executeQuery("select oid,* from updateable");
assertTrue(rs.first());
rs.updateInt( "id", 3 );
rs.updateString( "name", "dave3");
rs.updateRow();
assertEquals(3, rs.getInt("id"));
assertEquals("dave3", rs.getString("name"));
rs.moveToInsertRow();
rs.updateInt( "id", 4 );
rs.updateString( "name", "dave4" );
rs.insertRow();
rs.updateInt("id", 5 );
rs.updateString( "name", "dave5" );
rs.insertRow();
rs.moveToCurrentRow();
assertEquals(3, rs.getInt("id"));
assertEquals("dave3", rs.getString("name"));
assertTrue( rs.next() );
assertEquals(4, rs.getInt("id"));
assertEquals("dave4", rs.getString("name"));
assertTrue( rs.next() );
assertEquals(5, rs.getInt("id"));
assertEquals("dave5", rs.getString("name"));
rs.close();
st.close();
}
public void testInsertRowIllegalMethods() throws Exception
{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select * from updateable");
assertNotNull(rs);
rs.moveToInsertRow();
try {
rs.cancelRowUpdates();
fail("expected an exception when calling cancelRowUpdates() on the insert row");
} catch (SQLException e) {}
try {
rs.updateRow();
fail("expected an exception when calling updateRow() on the insert row");
} catch (SQLException e) {}
try {
rs.deleteRow();
fail("expected an exception when calling deleteRow() on the insert row");
} catch (SQLException e) {}
try {
rs.refreshRow();
fail("expected an exception when calling refreshRow() on the insert row");
} catch (SQLException e) {}
rs.close();
st.close();
}
public void testUpdateablePreparedStatement() throws Exception
{
// No args.
PreparedStatement st = con.prepareStatement("select * from updateable",
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery();
rs.moveToInsertRow();
rs.close();
st.close();
// With args.
st = con.prepareStatement("select * from updateable where id = ?",
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
st.setInt(1, 1);
rs = st.executeQuery();
rs.moveToInsertRow();
rs.close();
st.close();
}
public void testUpdateSelectOnly() throws Exception
{
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery( "select * from only second");
assertTrue(rs.next());
rs.updateInt(1, 2);
rs.updateRow();
}
public void testUpdateReadOnlyResultSet() throws Exception
{
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = st.executeQuery( "select * from updateable");
try {
rs.moveToInsertRow();
fail("expected an exception when calling moveToInsertRow() on a read-only resultset");
} catch (SQLException e) {}
}
public void testBadColumnIndexes() throws Exception
{
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery( "select * from updateable");
rs.moveToInsertRow();
try {
rs.updateInt(0,1);
fail("Should have thrown an exception on bad column index.");
} catch (SQLException sqle) { }
try {
rs.updateString(1000,"hi");
fail("Should have thrown an exception on bad column index.");
} catch (SQLException sqle) { }
try {
rs.updateNull(1000);
fail("Should have thrown an exception on bad column index.");
} catch (SQLException sqle) { }
}
public void testArray() throws SQLException {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate("INSERT INTO updateable (id, intarr) VALUES (1, '{1,2,3}'::int4[])");
ResultSet rs = stmt.executeQuery("SELECT id, intarr FROM updateable");
assertTrue(rs.next());
rs.updateObject(2, rs.getArray(2));
rs.updateRow();
Array arr = rs.getArray(2);
assertEquals(Types.INTEGER, arr.getBaseType());
Integer intarr[] = (Integer[])arr.getArray();
assertEquals(3, intarr.length);
assertEquals(1, intarr[0].intValue());
assertEquals(2, intarr[1].intValue());
assertEquals(3, intarr[2].intValue());
rs.close();
rs = stmt.executeQuery("SELECT id,intarr FROM updateable");
assertTrue(rs.next());
arr = rs.getArray(2);
assertEquals(Types.INTEGER, arr.getBaseType());
intarr = (Integer[])arr.getArray();
assertEquals(3, intarr.length);
assertEquals(1, intarr[0].intValue());
assertEquals(2, intarr[1].intValue());
assertEquals(3, intarr[2].intValue());
rs.close();
stmt.close();
}
}
|