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

Issue#2550 - Fixed getGeneratedKeys functionality for execute API #2554

Merged
merged 12 commits into from
Jan 30, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE
if (EXECUTE_QUERY == executeMethod && null == resultSet) {
SQLServerException.makeFromDriverError(connection, this, SQLServerException.getErrString("R_noResultset"),
null, true);
} else if (EXECUTE_UPDATE == executeMethod && null != resultSet) {
} else if ((EXECUTE_UPDATE == executeMethod) && (null != resultSet) && !bRequestedGeneratedKeys) {
SQLServerException.makeFromDriverError(connection, this,
SQLServerException.getErrString("R_resultsetGeneratedForUpdate"), null, false);
}
Expand Down
41 changes: 31 additions & 10 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -1601,9 +1601,15 @@
if (null != procedureName)
return false;

//For Insert, we must fetch additional TDS_DONE token that comes with the actual update count
if ((StreamDone.CMD_INSERT == doneToken.getCurCmd()) && (-1 != doneToken.getUpdateCount()) && EXECUTE == executeMethod) {
return true;
}

// Always return all update counts from statements executed through Statement.execute()
if (EXECUTE == executeMethod)
return false;
if (EXECUTE == executeMethod) {
return false;
}

// Statement.executeUpdate() may or may not return this update count depending on the
// setting of the lastUpdateCount connection property:
Expand Down Expand Up @@ -2357,17 +2363,27 @@

if (null == autoGeneratedKeys) {
long orgUpd = updateCount;
//
//A case of SET NOCOUNT ON and GENERATED KEYS requested
//where we may not have received update count but would have already read the resultset
//so directly consume it.
//
if ((executeMethod != EXECUTE_QUERY) && bRequestedGeneratedKeys && (resultSet != null)) {
autoGeneratedKeys = resultSet;
updateCount = orgUpd;
} else {

// 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(true) || null == resultSet) {
SQLServerException.makeFromDriverError(connection, this,
SQLServerException.getErrString("R_statementMustBeExecuted"), null, false);
// 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(true) || null == resultSet) {
SQLServerException.makeFromDriverError(connection, this,
SQLServerException.getErrString("R_statementMustBeExecuted"), null, false);

Check warning on line 2381 in src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java#L2380-L2381

Added lines #L2380 - L2381 were not covered by tests
}
autoGeneratedKeys = resultSet;
updateCount = orgUpd;
}

autoGeneratedKeys = resultSet;
updateCount = orgUpd;
}
loggerExternal.exiting(getClassNameLogging(), "getGeneratedKeys", autoGeneratedKeys);
return autoGeneratedKeys;
Expand Down Expand Up @@ -2616,6 +2632,11 @@
lock.unlock();
}
}

protected void setAutoGeneratedKey(SQLServerResultSet rs) {
autoGeneratedKeys = rs;
}

Check warning on line 2638 in src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java#L2637-L2638

Added lines #L2637 - L2638 were not covered by tests

}


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/StreamDone.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ final long getUpdateCount() {
}

final boolean cmdIsDMLOrDDL() {
switch (curCmd) {
switch (curCmd) {
case CMD_INSERT:
case CMD_BULKINSERT:
case CMD_DELETE:
Expand Down
Loading
Loading