prepareStatement("insert into t2017_id values (?, ?)");
// Insert the 3 first rows (id is 1-based).
for (int i=0; i < 3; i++) {
ps.setInt(1, i+1);
int length = INSERT[i];
ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
assertEquals(1, ps.executeUpdate());
}
// Insert the 4th row with a stream that's longer than the specified
// length, then the 5th row that's shorter. Both should fail, and the
// data shouldn't be inserted into the database.
Reader r4 = new LoopingAlphabetReader(INSERT[3]);
ps.setInt(1, 4);
ps.setCharacterStream(2, r4, INSERT[3] - 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too long");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN015", sqle);
if (rollbackOnError) {
rollback();
}
}
Reader r5 = new LoopingAlphabetReader(INSERT[4]);
ps.setInt(1, 5);
ps.setCharacterStream(2, r5, INSERT[4] + 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too short");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN017", sqle);
if (rollbackOnError) {
rollback();
}
}
// The errors above should have statement severity. Insert the last
// two rows and make sure the transaction commits.
for (int i=5; i < INSERT.length; i++) {
ps.setInt(1, i+1);
int length = INSERT[i];
ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
assertEquals(1, ps.executeUpdate());
}
if (!autoCommit) {
commit();
}
// Make sure we have the expected number of rows.
ResultSet rs = stmt.executeQuery("select count(*) from t2017_id");
rs.next();
assertEquals((rollbackOnError && !autoCommit ? 2 : 5), rs.getInt(1));
// Select data in the table, compare to what we expect.
rs = stmt.executeQuery( "select * from t2017_id order by id asc");
// Check rows 1-4 if rollback on error is false.
if (autoCommit || !rollbackOnError) {
for (int i=0; i < 3; i++) {
rs.next();
int id = rs.getInt(1);
assertTrue(id - 1 == i);
assertEquals(new LoopingAlphabetReader(INSERT[i]),
rs.getCharacterStream(2));
}
}
// Check rows 6 and 7.
for (int i=5; i < 7; i++) {
rs.next();
int id = rs.getInt(1);
assertTrue(id - 1 == i);
assertEquals(new LoopingAlphabetReader(INSERT[i]),
rs.getCharacterStream(2));
}
assertFalse(rs.next());
rs.close();
}