Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to read SQL Warnings after ResultSet is read completely #785

Merged
merged 4 commits into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE

ensureExecuteResultsReader(command.startResponse(getIsResponseBufferingAdaptive()));
startResults();
getNextResult();
getNextResult(true);
} catch (SQLException e) {
if (retryBasedOnFailedReuseOfCachedHandle(e, attempt, needsPrepare, false))
continue;
Expand Down Expand Up @@ -2763,7 +2763,7 @@ final void doExecutePreparedStatementBatch(PrepStmtBatchExecCmd batchCommand) th
// Get the first result from the batch. If there is no result for this batch
// then bail, leaving EXECUTE_FAILED in the current and remaining slots of
// the update count array.
if (!getNextResult())
if (!getNextResult(true))
return;

// If the result is a ResultSet (rather than an update count) then throw an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,12 @@ public boolean next() throws SQLServerException {
if (UNKNOWN_ROW_COUNT == rowCount)
rowCount = currentRow;

// Read SQL Warnings at the end of ResultSet
if (stmt.resultsReader().peekTokenType() == TDS.TDS_MSG) {
stmt.startResults();
stmt.getNextResult(false);
}

currentRow = AFTER_LAST_ROW;
loggerExternal.exiting(getClassNameLogging(), "next", false);
return false;
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ final void doExecuteStatement(StmtExecCmd execCmd) throws SQLServerException {
// Start the response
ensureExecuteResultsReader(execCmd.startResponse(isResponseBufferingAdaptive));
startResults();
getNextResult();
getNextResult(true);
}

// If execution produced no result set, then throw an exception if executeQuery() was used.
Expand Down Expand Up @@ -923,7 +923,7 @@ private void doExecuteStatementBatch(StmtBatchExecCmd execCmd) throws SQLServerE
// Start the response
ensureExecuteResultsReader(execCmd.startResponse(isResponseBufferingAdaptive));
startResults();
getNextResult();
getNextResult(true);

// If execution produced a result set, then throw an exception
if (null != resultSet) {
Expand Down Expand Up @@ -1277,7 +1277,7 @@ final void processResults() throws SQLServerException {
while (moreResults) {
// Get the next result
try {
getNextResult();
getNextResult(true);
} catch (SQLServerException e) {
// If an exception is thrown while processing the results
// then decide what to do with it:
Expand Down Expand Up @@ -1327,7 +1327,7 @@ public final boolean getMoreResults() throws SQLServerException {
// Get the next result, whatever it is (ResultSet or update count).
// Don't just return the value from the getNextResult() call, however.
// The getMoreResults method has a subtle spec for its return value (see above).
getNextResult();
getNextResult(true);
loggerExternal.exiting(getClassNameLogging(), "getMoreResults", null != resultSet);
return null != resultSet;
}
Expand Down Expand Up @@ -1368,7 +1368,7 @@ final void clearLastResult() {
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modify javadoc now that clearFlag is a parameter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* @return true if another result (ResultSet or update count) was available; false if there were no more results.
*/
final boolean getNextResult() throws SQLServerException {
final boolean getNextResult(boolean clearFlag) throws SQLServerException {
/**
* TDS response token stream handler used to locate the next result in the TDS response token stream.
*/
Expand Down Expand Up @@ -1591,8 +1591,9 @@ boolean onInfo(TDSReader tdsReader) throws SQLServerException {
return false;
}

// Clear out previous results
clearLastResult();
// Clear out previous results only when clearFlag = true
if (clearFlag)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add brackets even if there's only one line in the if statement

clearLastResult();

// If there are no more results, then we're done.
// All we had to do was to close out the previous results.
Expand Down Expand Up @@ -1786,7 +1787,7 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
// If there are not enough results (update counts) to satisfy the number of batches,
// then bail, leaving EXECUTE_FAILED in the remaining slots of the update count array.
startResults();
if (!getNextResult())
if (!getNextResult(true))
break;
}

Expand Down Expand Up @@ -1863,7 +1864,7 @@ public long[] executeLargeBatch() throws SQLServerException, BatchUpdateExceptio
// If there are not enough results (update counts) to satisfy the number of batches,
// then bail, leaving EXECUTE_FAILED in the remaining slots of the update count array.
startResults();
if (!getNextResult())
if (!getNextResult(true))
break;
}

Expand Down Expand Up @@ -1997,7 +1998,7 @@ private void doExecuteCursored(StmtExecCmd execCmd, String sql) throws SQLServer

ensureExecuteResultsReader(execCmd.startResponse(isResponseBufferingAdaptive));
startResults();
getNextResult();
getNextResult(true);
}

/* JDBC 3.0 */
Expand Down Expand Up @@ -2182,7 +2183,7 @@ public final ResultSet getGeneratedKeys() throws SQLServerException {
// Generated keys are returned in a ResultSet result right after the update count.
// Try to get that ResultSet. If there are no more results after the update count,
// or if the next result isn't a ResultSet, then something is wrong.
if (!getNextResult() || null == resultSet) {
if (!getNextResult(true) || null == resultSet) {
SQLServerException.makeFromDriverError(connection, this,
SQLServerException.getErrString("R_statementMustBeExecuted"), null, false);
}
Expand Down