下面列出了java.sql.ResultSet#deleteRow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected boolean deleteRowSFU_URSTx(ResultSet updatableRs) {
try {
int cid = updatableRs.getInt("CID");
updatableRs.deleteRow();
Log.getLogWriter().info("delete from trade.networth for cid: " + cid);
} catch (SQLException se) {
if (isHATest && SQLHelper.gotTXNodeFailureException(se)) {
Log.getLogWriter().info(
"got node failure exception, needs to retry the op");
return false;
}
SQLHelper.handleSQLException(se); // should not see exception as the row
// should be locked
}
return true;
}
/**
* Test that rowUpdated() and rowDeleted() methods both return true when
* the row has first been updated and then deleted using the updateRow()
* and deleteRow() methods.
*/
public void testRowUpdatedAndRowDeleted() throws SQLException {
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select a,b from t1");
rs.next();
rs.updateInt(1, rs.getInt(1) + 2 * recordCount);
rs.updateRow();
assertTrue("Expected rowUpdated() to return true", rs.rowUpdated());
rs.deleteRow();
rs.next();
rs.previous();
assertTrue("Expected rowUpdated() to return true", rs.rowUpdated());
assertTrue("Expected rowDeleted() to return true", rs.rowDeleted());
rs.next();
assertFalse("Expected rowUpdated() to return false", rs.rowUpdated());
assertFalse("Expected rowDeleted() to return false", rs.rowDeleted());
rs.previous();
assertTrue("Expected rowUpdated() to return true", rs.rowUpdated());
assertTrue("Expected rowDeleted() to return true", rs.rowDeleted());
rs.close();
s.close();
}
protected boolean deleteRowSFU_URSTx(ResultSet updatableRs) {
try {
int cid = updatableRs.getInt("CID");
updatableRs.deleteRow();
Log.getLogWriter().info("delete from trade.networth for cid: " + cid);
} catch (SQLException se) {
if (isHATest && SQLHelper.gotTXNodeFailureException(se)) {
Log.getLogWriter().info(
"got node failure exception, needs to retry the op");
return false;
}
SQLHelper.handleSQLException(se); // should not see exception as the row
// should be locked
}
return true;
}
/**
* Test that rowUpdated() and rowDeleted() methods both return true when
* the row has first been updated and then deleted using the updateRow()
* and deleteRow() methods.
*/
public void testRowUpdatedAndRowDeleted() throws SQLException {
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select a,b from t1");
rs.next();
rs.updateInt(1, rs.getInt(1) + 2 * recordCount);
rs.updateRow();
assertTrue("Expected rowUpdated() to return true", rs.rowUpdated());
rs.deleteRow();
rs.next();
rs.previous();
assertTrue("Expected rowUpdated() to return true", rs.rowUpdated());
assertTrue("Expected rowDeleted() to return true", rs.rowDeleted());
rs.next();
assertFalse("Expected rowUpdated() to return false", rs.rowUpdated());
assertFalse("Expected rowDeleted() to return false", rs.rowDeleted());
rs.previous();
assertTrue("Expected rowUpdated() to return true", rs.rowUpdated());
assertTrue("Expected rowDeleted() to return true", rs.rowDeleted());
rs.close();
s.close();
}
/**
* Test that PHB can actually update using {@code ResultSet.insertRow},
* {@code ResultSet.updateRow} and {@code ResultSet.deleteRow}.
* <p/>
* Aside: This test is somewhat artificial here, since the middle manager
* would not be allowed to do this, presumably; just added here to test
* this functionality (which was initially broken by the first patch for
* DERBY-4551).
* <p/>
* The problem was that the nested statement contexts used for SQL
* substatements generated for these ResultSet operations were not
* correctly set up, so the effective user id would not be the DEFINER
* (DBO), and authorization would fail. Cf DERBY-4551 and DERBY-3327
* for more detail.
*/
public static void updateWage()
throws SQLException
{
Connection c = null;
c = DriverManager.getConnection("jdbc:default:connection");
Statement cStmt = c.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// Try nested statements by inserting, updating and deleting a bogus
// row
ResultSet rs = cStmt.executeQuery(
"select * from s1.wages");
assertTrue(rs.isBeforeFirst());
rs.moveToInsertRow();
rs.updateInt("EMPLOYEEID", 666);
rs.updateInt("CATEGORY", 667);
rs.updateDouble("SALARY", 666.0);
rs.updateString("NAME", "N.N.");
rs.insertRow();
rs.close();
rs = cStmt.executeQuery(
"select * from s1.wages where name = 'N.N.'");
rs.next();
rs.updateDouble("SALARY", 666.1);
rs.updateRow();
rs.close();
rs = cStmt.executeQuery(
"select * from s1.wages where name = 'N.N.'");
rs.next();
rs.deleteRow();
rs.close();
cStmt.close();
c.close();
}
@After
public void closeConnection() throws SQLException {
Statement updatableStmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet updatableResultSet = updatableStmt.executeQuery("SELECT * FROM employees");
while (updatableResultSet.next()) {
updatableResultSet.deleteRow();
}
con.close();
}
protected boolean deleteURSRowTx(ResultSet updatableRs) {
try {
int cid = updatableRs.getInt("CID");
updatableRs.deleteRow();
Log.getLogWriter().info("delete from trade.networth for cid: " + cid);
} catch (SQLException se) {
if (se.getSQLState().equals("X0Z02")) {
SQLHelper.printSQLException(se);
return false; // expected updatable result set
} else
SQLHelper.handleSQLException(se);
}
return true;
}
/**
* Delete a random sample of n records in the resultset
* @param rs result set to be updated
* @param rows map of rows, will also be updated
* @param deletedRows set of rows being deleted (position in RS)
* @param k number of records to be deleted
*/
private void deleteRandomSampleOfNRecords(final ResultSet rs,
final Map rows,
final Set deletedRows,
final int k)
throws SQLException
{
List sampledKeys = createRandomSample(rows, k);
println("Sampled keys:" + sampledKeys);
ResultSetMetaData meta = rs.getMetaData();
for (Iterator i = sampledKeys.iterator(); i.hasNext();) {
Integer key = (Integer) i.next();
rs.absolute(key.intValue());
if (rs.rowDeleted()) continue; // skip deleting row if already deleted
if (positioned) {
createStatement().executeUpdate
("DELETE FROM T1 WHERE CURRENT OF \"" + cursorName +
"\"");
} else {
rs.deleteRow();
}
rs.relative(0);
println("Deleted row " + key);
// Update the rows table
rows.put(key, getRowString(rs));
// Update the updatedRows set
deletedRows.add(key);
}
}
protected boolean deleteURSRowTx(ResultSet updatableRs) {
try {
int cid = updatableRs.getInt("CID");
updatableRs.deleteRow();
Log.getLogWriter().info("delete from trade.networth for cid: " + cid);
} catch (SQLException se) {
if (se.getSQLState().equals("X0Z02")) {
SQLHelper.printSQLException(se);
return false; // expected updatable result set
} else
SQLHelper.handleSQLException(se);
}
return true;
}
/**
* Tests for remove.
*
* @throws SQLException in case of an unexpected problem
*/
@Test
public void testRemove() throws SQLException {
ResultSet resultsetdelteThrowsException = createMock(ResultSet.class);
resultsetdelteThrowsException.deleteRow();
expectLastCall().andThrow(new SQLException());
replay(resultsetdelteThrowsException);
final ResultSetIterator<String> iter = new ResultSetIterImplementation(new StatementImplementation(),
resultsetdelteThrowsException);
iter.remove();
assertTrue("no exception thrown", true);
verify(resultsetdelteThrowsException);
}
/**
* Test that you get cursor operation conflict warning if updating
* a row which has been deleted from the table.
*/
public void testCursorOperationConflictWarning1()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select * from t1");
rs.next();
createStatement().executeUpdate("delete from t1 where id=" +
rs.getString("ID"));
final int newValue = -3333;
final int oldValue = rs.getInt(2);
rs.updateInt(2, newValue);
rs.updateRow();
SQLWarning warn = rs.getWarnings();
assertWarning(warn, CURSOR_OPERATION_CONFLICT);
assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
rs.clearWarnings();
rs.deleteRow();
warn = rs.getWarnings();
assertWarning(warn, CURSOR_OPERATION_CONFLICT);
rs.relative(0);
assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
rs.close();
s.close();
}
/**
* Delete a random sample of n records in the resultset
* @param rs result set to be updated
* @param rows map of rows, will also be updated
* @param deletedRows set of rows being deleted (position in RS)
* @param k number of records to be deleted
*/
private void deleteRandomSampleOfNRecords(final ResultSet rs,
final Map rows,
final Set deletedRows,
final int k)
throws SQLException
{
List sampledKeys = createRandomSample(rows, k);
println("Sampled keys:" + sampledKeys);
ResultSetMetaData meta = rs.getMetaData();
for (Iterator i = sampledKeys.iterator(); i.hasNext();) {
Integer key = (Integer) i.next();
rs.absolute(key.intValue());
if (rs.rowDeleted()) continue; // skip deleting row if already deleted
if (positioned) {
createStatement().executeUpdate
("DELETE FROM T1 WHERE CURRENT OF \"" + cursorName +
"\"");
} else {
rs.deleteRow();
}
rs.relative(0);
println("Deleted row " + key);
// Update the rows table
rows.put(key, getRowString(rs));
// Update the updatedRows set
deletedRows.add(key);
}
}
/**
* Delete a random sample of n records in the resultset
* @param rs result set to be updated
* @param rows map of rows, will also be updated
* @param deletedRows set of rows being deleted (position in RS)
* @param k number of records to be deleted
*/
private void deleteRandomSampleOfNRecords(final ResultSet rs,
final Map rows,
final Set deletedRows,
final int k)
throws SQLException
{
List sampledKeys = createRandomSample(rows, k);
println("Sampled keys:" + sampledKeys);
ResultSetMetaData meta = rs.getMetaData();
for (Iterator i = sampledKeys.iterator(); i.hasNext();) {
Integer key = (Integer) i.next();
rs.absolute(key.intValue());
if (rs.rowDeleted()) continue; // skip deleting row if already deleted
if (positioned) {
createStatement().executeUpdate
("DELETE FROM T1 WHERE CURRENT OF \"" + cursorName +
"\"");
} else {
rs.deleteRow();
}
rs.relative(0);
println("Deleted row " + key);
// Update the rows table
rows.put(key, getRowString(rs));
// Update the updatedRows set
deletedRows.add(key);
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertDeleteRow() throws SQLException {
for (ResultSet each : resultSets) {
each.deleteRow();
}
}
public void testSelectForUpdate_directDeleteNoPK() throws Exception {
startVMs(2, 1);
Connection conn = TestUtil.getConnection();
conn.setTransactionIsolation(Connection.TRANSACTION_NONE);
conn.setAutoCommit(false);
Statement stmtForTableAndInsert = conn.createStatement();
stmtForTableAndInsert.execute("create table Employee"
+ "(firstname varchar(50) not null, lastname varchar(50) not null, "
+ "workdept varchar(50), bonus decimal(10,4))");
stmtForTableAndInsert
.execute("insert into employee values('neeraj', 'kumar', 'rnd', 0.0), "
+ "('asif', 'shahid', 'rnd', 1.0), "
+ "('dada', 'ji', 'rnd', 2.0), ('sum', 'wale', 'rnd', 3.0)");
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
ResultSet uprs = stmt.executeQuery("SELECT workdept, bonus "
+ "FROM EMPLOYEE FOR UPDATE of BONUS");
while (uprs.next()) {
uprs.deleteRow();
}
SQLWarning w = uprs.getWarnings();
assertTrue("0A000".equals(w.getSQLState()));
uprs.close();
Statement stmt2 = conn.createStatement();
ResultSet rs = stmt2.executeQuery("select * from employee");
w = rs.getWarnings();
assertNull(w);
rs = stmt2.executeQuery("select * from employee");
assertFalse(rs.next());
conn.setTransactionIsolation(getIsolationLevel());
conn.setAutoCommit(false);
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
stmt.execute("insert into employee values('asif', 'shahid', 'rnd', 1.0), ('dada', 'ji', 'rnd', 2.0), ('sum', 'wale', 'rnd', 3.0)");
uprs = stmt.executeQuery("SELECT * "
+ "FROM EMPLOYEE FOR UPDATE of BONUS");
while (uprs.next()) {
if (uprs.getString(1).equalsIgnoreCase("asif")) {
uprs.deleteRow();
}
}
w = uprs.getWarnings();
assertNull(w);
uprs.close();
conn.commit();
stmt2 = conn.createStatement();
rs = stmt2.executeQuery("select * from employee");
w = rs.getWarnings();
assertNull(w);
assertTrue(rs.next());
assertTrue((rs.getString(1).equalsIgnoreCase("sum") || rs.getString(1)
.equalsIgnoreCase("dada")));
assertTrue(rs.next());
assertTrue((rs.getString(1).equalsIgnoreCase("sum") || rs.getString(1)
.equalsIgnoreCase("dada")));
assertFalse(rs.next());
}
/**
* Test that the JDBC detectability calls throw correct exceptions when
* called in in wrong row states.
* This is done for both supported updatable result set types.
*/
public void testDetectabilityExceptions() throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery("select * from t1");
checkDetectabilityCallsOutsideRow(rs, "before positioning");
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs,
"on insertRow before positioning");
rs.next();
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs, "on insertRow");
rs.moveToCurrentRow(); // needed until to DERBY-1322 is fixed
rs.beforeFirst();
checkDetectabilityCallsOutsideRow(rs, "on beforeFirst row");
rs.afterLast();
checkDetectabilityCallsOutsideRow(rs, "on afterLast row");
rs.first();
rs.deleteRow();
checkDetectabilityCallsOutsideRow(rs, "after deleteRow");
rs.last();
rs.deleteRow();
checkDetectabilityCallsOutsideRow(rs, "after deleteRow of last row");
rs.close();
s.close();
// Not strictly SUR, but fixed in same patch, so we test it here.
s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
rs = s.executeQuery("select * from t1");
checkDetectabilityCallsOutsideRow(rs, "before FO positioning");
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs,
"on insertRow before FO positioning");
rs.next();
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs, "on FO insertRow");
rs.next();
rs.updateInt(2, 666);
rs.updateRow();
checkDetectabilityCallsOutsideRow(rs, "after FO updateRow");
rs.next();
rs.deleteRow();
checkDetectabilityCallsOutsideRow(rs, "after FO deleteRow");
while (rs.next()) {};
checkDetectabilityCallsOutsideRow(rs, "after FO emptied out");
rs.close();
s.close();
}
@Override
public Object map(final ResultSet resultSet) throws SQLException {
final String name = resultSet.getString("name");
resultSet.deleteRow();
return name;
}
public void testSelectForUpdate_directDeleteNoPK() throws Exception {
startVMs(2, 1);
Connection conn = TestUtil.getConnection();
conn.setTransactionIsolation(Connection.TRANSACTION_NONE);
conn.setAutoCommit(false);
Statement stmtForTableAndInsert = conn.createStatement();
stmtForTableAndInsert.execute("create table Employee"
+ "(firstname varchar(50) not null, lastname varchar(50) not null, "
+ "workdept varchar(50), bonus decimal(10,4))");
stmtForTableAndInsert
.execute("insert into employee values('neeraj', 'kumar', 'rnd', 0.0), "
+ "('asif', 'shahid', 'rnd', 1.0), "
+ "('dada', 'ji', 'rnd', 2.0), ('sum', 'wale', 'rnd', 3.0)");
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
ResultSet uprs = stmt.executeQuery("SELECT workdept, bonus "
+ "FROM EMPLOYEE FOR UPDATE of BONUS");
while (uprs.next()) {
uprs.deleteRow();
}
SQLWarning w = uprs.getWarnings();
assertTrue("0A000".equals(w.getSQLState()));
uprs.close();
Statement stmt2 = conn.createStatement();
ResultSet rs = stmt2.executeQuery("select * from employee");
w = rs.getWarnings();
assertNull(w);
rs = stmt2.executeQuery("select * from employee");
assertFalse(rs.next());
conn.setTransactionIsolation(getIsolationLevel());
conn.setAutoCommit(false);
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
stmt.execute("insert into employee values('asif', 'shahid', 'rnd', 1.0), ('dada', 'ji', 'rnd', 2.0), ('sum', 'wale', 'rnd', 3.0)");
uprs = stmt.executeQuery("SELECT * "
+ "FROM EMPLOYEE FOR UPDATE of BONUS");
while (uprs.next()) {
if (uprs.getString(1).equalsIgnoreCase("asif")) {
uprs.deleteRow();
}
}
w = uprs.getWarnings();
assertNull(w);
uprs.close();
conn.commit();
stmt2 = conn.createStatement();
rs = stmt2.executeQuery("select * from employee");
w = rs.getWarnings();
assertNull(w);
assertTrue(rs.next());
assertTrue((rs.getString(1).equalsIgnoreCase("sum") || rs.getString(1)
.equalsIgnoreCase("dada")));
assertTrue(rs.next());
assertTrue((rs.getString(1).equalsIgnoreCase("sum") || rs.getString(1)
.equalsIgnoreCase("dada")));
assertFalse(rs.next());
}
/**
* Test that the JDBC detectability calls throw correct exceptions when
* called in in wrong row states.
* This is done for both supported updatable result set types.
*/
public void testDetectabilityExceptions() throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery("select * from t1");
checkDetectabilityCallsOutsideRow(rs, "before positioning");
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs,
"on insertRow before positioning");
rs.next();
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs, "on insertRow");
rs.moveToCurrentRow(); // needed until to DERBY-1322 is fixed
rs.beforeFirst();
checkDetectabilityCallsOutsideRow(rs, "on beforeFirst row");
rs.afterLast();
checkDetectabilityCallsOutsideRow(rs, "on afterLast row");
rs.first();
rs.deleteRow();
checkDetectabilityCallsOutsideRow(rs, "after deleteRow");
rs.last();
rs.deleteRow();
checkDetectabilityCallsOutsideRow(rs, "after deleteRow of last row");
rs.close();
s.close();
// Not strictly SUR, but fixed in same patch, so we test it here.
s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
rs = s.executeQuery("select * from t1");
checkDetectabilityCallsOutsideRow(rs, "before FO positioning");
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs,
"on insertRow before FO positioning");
rs.next();
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs, "on FO insertRow");
rs.next();
rs.updateInt(2, 666);
rs.updateRow();
checkDetectabilityCallsOutsideRow(rs, "after FO updateRow");
rs.next();
rs.deleteRow();
checkDetectabilityCallsOutsideRow(rs, "after FO deleteRow");
while (rs.next()) {};
checkDetectabilityCallsOutsideRow(rs, "after FO emptied out");
rs.close();
s.close();
}
/**
* Test that the JDBC detectability calls throw correct exceptions when
* called in in wrong row states.
* This is done for both supported updatable result set types.
*/
public void testDetectabilityExceptions() throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery("select * from t1");
checkDetectabilityCallsOutsideRow(rs, "before positioning");
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs,
"on insertRow before positioning");
rs.next();
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs, "on insertRow");
rs.moveToCurrentRow(); // needed until to DERBY-1322 is fixed
rs.beforeFirst();
checkDetectabilityCallsOutsideRow(rs, "on beforeFirst row");
rs.afterLast();
checkDetectabilityCallsOutsideRow(rs, "on afterLast row");
rs.first();
rs.deleteRow();
checkDetectabilityCallsOutsideRow(rs, "after deleteRow");
rs.last();
rs.deleteRow();
checkDetectabilityCallsOutsideRow(rs, "after deleteRow of last row");
rs.close();
s.close();
// Not strictly SUR, but fixed in same patch, so we test it here.
s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
rs = s.executeQuery("select * from t1");
checkDetectabilityCallsOutsideRow(rs, "before FO positioning");
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs,
"on insertRow before FO positioning");
rs.next();
rs.moveToInsertRow();
checkDetectabilityCallsOutsideRow(rs, "on FO insertRow");
rs.next();
rs.updateInt(2, 666);
rs.updateRow();
checkDetectabilityCallsOutsideRow(rs, "after FO updateRow");
rs.next();
rs.deleteRow();
checkDetectabilityCallsOutsideRow(rs, "after FO deleteRow");
while (rs.next()) {};
checkDetectabilityCallsOutsideRow(rs, "after FO emptied out");
rs.close();
s.close();
}