// Check that the cursor is not positioned on insertRow
checkNotOnInsertRow();
setupContextStack();
LanguageConnectionContext lcc = getEmbedConnection().getLanguageConnection();
StatementContext statementContext = null;
try {
if (currentRowHasBeenUpdated == false) //nothing got updated on this row
return; //nothing to do since no updates were made to this row
//now construct the update where current of sql
boolean foundOneColumnAlready = false;
StringBuffer updateWhereCurrentOfSQL = new StringBuffer("UPDATE ");
CursorActivation activation = lcc.lookupCursorActivation(getCursorName());
ExecCursorTableReference targetTable = activation.getPreparedStatement().getTargetTable();
updateWhereCurrentOfSQL.append(getFullBaseTableName(targetTable));//got the underlying (schema.)table name
updateWhereCurrentOfSQL.append(" SET ");
ResultDescription rd = theResults.getResultDescription();
for (int i=1; i<=rd.getColumnCount(); i++) { //in this for loop we are constructing columnname=?,... part of the update sql
if (columnGotUpdated[i-1]) { //if the column got updated, do following
if (foundOneColumnAlready)
updateWhereCurrentOfSQL.append(",");
//using quotes around the column name to preserve case sensitivity
updateWhereCurrentOfSQL.append(IdUtil.normalToDelimited(
rd.getColumnDescriptor(i).getName()) + "=?");
foundOneColumnAlready = true;
}
}
//using quotes around the cursor name to preserve case sensitivity
updateWhereCurrentOfSQL.append(" WHERE CURRENT OF " +
IdUtil.normalToDelimited(getCursorName()));
StatementContext currSC = lcc.getStatementContext();
Activation parentAct = null;
if (currSC != null) {
parentAct = currSC.getActivation();
}
// Context used for preparing, don't set any timeout (use 0)
statementContext = lcc.pushStatementContext(isAtomic, false, updateWhereCurrentOfSQL.toString(), null, false, 0L);
// A priori, the new statement context inherits the activation of
// the existing statementContext, so that that activation ends up
// as parent of the new activation 'act' created below, which will
// be the activation of the pushed statement context.
statementContext.setActivation(parentAct);
org.apache.derby.iapi.sql.PreparedStatement ps = lcc.prepareInternalStatement(updateWhereCurrentOfSQL.toString());
Activation act = ps.getActivation(lcc, false);
statementContext.setActivation(act);
//in this for loop we are assigning values for parameters in sql constructed earlier with columnname=?,...
for (int i=1, paramPosition=0; i<=rd.getColumnCount(); i++) {
if (columnGotUpdated[i-1]) //if the column got updated, do following
act.getParameterValueSet().getParameterForSet(paramPosition++).setValue(updateRow.getColumn(i));
}
// Don't set any timeout when updating rows (use 0)
// Execute the update where current of sql.
org.apache.derby.iapi.sql.ResultSet rs =
ps.executeSubStatement(activation, act, true, 0L);
SQLWarning w = act.getWarnings();
if (w != null) {
addWarning(w);
}
act.close();
//For forward only resultsets, after a update, the ResultSet will be positioned right before the next row.
if (getType() == TYPE_FORWARD_ONLY) {
currentRow = null;
} else {
movePosition(RELATIVE, 0, "relative");
}
lcc.popStatementContext(statementContext, null);
InterruptStatus.restoreIntrFlagIfSeen(lcc);
} catch (Throwable t) {
throw closeOnTransactionError(t);
} finally {
if (statementContext != null)
lcc.popStatementContext(statementContext, null);
restoreContextStack();
initializeUpdateRowModifiers();
}
}
}