use of org.apache.derby.iapi.sql.PreparedStatement in project derby by apache.
the class EmbedStatement method executeStatement.
// ///////////////////////////////////////////////////////////////////////
//
// Implementation specific methods
//
// ///////////////////////////////////////////////////////////////////////
/**
* Execute the current statement.
* @exception SQLException thrown on failure.
*/
boolean executeStatement(Activation a, boolean executeQuery, boolean executeUpdate) throws SQLException {
synchronized (getConnectionSynchronization()) {
if (SanityManager.DEBUG) {
// Ensure that clearResultSets has been called
// to fulfill [JDBC4: section 15.2.5 ]
// A ResultSet object is implicitly closed when:
// The associated Statement object is re-executed
SanityManager.ASSERT(results == null);
SanityManager.ASSERT(dynamicResults == null);
SanityManager.ASSERT(autoGeneratedKeysResultSet == null);
}
// make sure there's context
setupContextStack();
boolean retval;
pvs = a.getParameterValueSet();
try {
clearWarnings();
if (!forMetaData) {
// commit the last statement if needed
commitIfNeeded();
needCommit();
} else {
if (lcc.getActivationCount() > 1) {
// we do not want to commit here as there seems to be other
// statements/resultSets currently opened for this connection.
} else {
// we can legitimately commit
commitIfNeeded();
needCommit();
}
}
// Get the statement. We don't care if it's invalid, because it
// will be recompiled when we execute it if needed (DERBY-3024).
PreparedStatement ps = a.getPreparedStatement();
if (cursorName != null) {
a.setCursorName(cursorName);
}
boolean executeHoldable = getExecuteHoldable();
a.setResultSetHoldability(executeHoldable);
// reset the activation to clear warnings
// and clear existing result sets in case this has been cached
a.reset();
a.setMaxRows(maxRows);
ResultSet resultsToWrap = ps.execute(a, forMetaData, timeoutMillis);
addWarning(ps.getCompileTimeWarnings());
addWarning(a.getWarnings());
if (resultsToWrap.returnsRows()) {
// executeUpdate() is not allowed.
if (executeUpdate) {
throw StandardException.newException(SQLState.LANG_INVALID_CALL_TO_EXECUTE_UPDATE);
}
EmbedResultSet lresults = factory.newEmbedResultSet(getEmbedConnection(), resultsToWrap, forMetaData, this, ps.isAtomic());
results = lresults;
// outside of finalization.
if (a.isSingleExecution())
lresults.singleUseActivation = a;
updateCount = -1L;
retval = true;
} else {
// the auto-generated keys resultset will be null if used for other statement
if (a.getAutoGeneratedKeysResultsetMode() && (resultsToWrap.getAutoGeneratedKeysResultset() != null)) {
resultsToWrap.getAutoGeneratedKeysResultset().open();
autoGeneratedKeysResultSet = factory.newEmbedResultSet(getEmbedConnection(), resultsToWrap.getAutoGeneratedKeysResultset(), false, this, ps.isAtomic());
}
updateCount = resultsToWrap.modifiedRowCount();
// note that we have none.
results = null;
int dynamicResultCount = 0;
if (a.getDynamicResults() != null) {
dynamicResultCount = processDynamicResults(a.getDynamicResults(), a.getMaxDynamicResults());
}
// Don't need the result set any more
resultsToWrap.close();
// doesn't return exactly one ResultSet.
if (executeQuery && dynamicResultCount != 1) {
throw StandardException.newException(SQLState.LANG_INVALID_CALL_TO_EXECUTE_QUERY);
}
// returns ResultSets.
if (executeUpdate && dynamicResultCount > 0) {
throw StandardException.newException(SQLState.LANG_INVALID_CALL_TO_EXECUTE_UPDATE);
}
if (dynamicResultCount == 0) {
if (a.isSingleExecution()) {
a.close();
}
if (!forMetaData)
commitIfNeeded();
else {
if (lcc.getActivationCount() > 1) {
// we do not want to commit here as there seems to be other
// statements/resultSets currently opened for this connection.
} else {
// we can legitimately commit
commitIfNeeded();
}
}
}
retval = (dynamicResultCount > 0);
}
InterruptStatus.restoreIntrFlagIfSeen(lcc);
} catch (Throwable t) {
if (a.isSingleExecution()) {
try {
a.close();
} catch (Throwable tt) {
;
}
}
throw handleException(t);
} finally {
restoreContextStack();
}
return retval;
}
}
Aggregations