Skip to content

Commit

Permalink
UDF fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyc committed Sep 17, 2023
1 parent 8a376c2 commit e93eac7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6988,6 +6988,35 @@ final short peekStatusFlag() {
return 0;
}

final int peekReturnValueStatus() throws SQLServerException {
// Ensure that we have a packet to read from.
if (!ensurePayload()) {
throwInvalidTDS();
}

// In order to parse the 'status' value, we need to skip over the following properties in the TDS packet
// payload: TDS token type (1 byte value), ordinal/length (2 byte value), parameter name length value (1 byte value) and
// the number of bytes that make the parameter name (need to be calculated).
//
// 'offset' starts at 4 because tdsTokenType + ordinal/length + parameter name length value is 4 bytes. So, we
// skip 4 bytes immediateley.
int offset = 4;
int paramNameLength = currentPacket.payload[payloadOffset + 3];

// Check if parameter name is set. If it's set, it should be > 0. In which case, we add the
// additional bytes to skip.
if (paramNameLength > 0) {
// Each character in unicode is 2 bytes
offset += 2 * paramNameLength;
}

if (payloadOffset + offset <= currentPacket.payloadLength) {
return currentPacket.payload[payloadOffset + offset] & 0xFF;
}

return -1;
}

final int readUnsignedByte() throws SQLServerException {
// Ensure that we have a packet to read from.
if (!ensurePayload())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private Parameter getOutParameter(int i) throws SQLServerException {
return inOutParam[i - 1];
}

if (inOutParam[i - 1].isReturnValue() && bReturnValueSyntax && !isCursorable(executeMethod) && !isTVPType) {
if (inOutParam[i - 1].isReturnValue() && bReturnValueSyntax && !isCursorable(executeMethod) && !isTVPType && returnValueStatus != 2) {
return inOutParam[i - 1];
}

Expand Down Expand Up @@ -341,7 +341,7 @@ boolean onRetValue(TDSReader tdsReader) throws SQLServerException {
OutParamHandler outParamHandler = new OutParamHandler();

if (bReturnValueSyntax && (nOutParamsAssigned == 0) && !isCursorable(executeMethod) && !isTVPType
&& SQLServerConnection.isCallRemoteProcDirectValid(userSQL, inOutParam.length, bReturnValueSyntax)) {
&& SQLServerConnection.isCallRemoteProcDirectValid(userSQL, inOutParam.length, bReturnValueSyntax) && returnValueStatus != 2) {
nOutParamsAssigned++;
}

Expand Down Expand Up @@ -389,7 +389,7 @@ boolean onRetValue(TDSReader tdsReader) throws SQLServerException {
outParamIndex = outParamHandler.srv.getOrdinalOrLength();

if (bReturnValueSyntax && !isCursorable(executeMethod) && !isTVPType && SQLServerConnection
.isCallRemoteProcDirectValid(userSQL, inOutParam.length, bReturnValueSyntax)) {
.isCallRemoteProcDirectValid(userSQL, inOutParam.length, bReturnValueSyntax) && returnValueStatus != 2) {
outParamIndex++;
} else {
// Statements need to have their out param indices adjusted by the number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class SQLServerStatement implements ISQLServerStatement {
/**
* Always update serialVersionUID when prompted.
*/

private static final long serialVersionUID = -4421134713913331507L;

final static char LEFT_CURLY_BRACKET = 123;
Expand All @@ -66,6 +67,9 @@ public class SQLServerStatement implements ISQLServerStatement {
/** response buffer adaptive flag */
boolean isResponseBufferingAdaptive = false;

/** TDS token return value status **/
int returnValueStatus;

final boolean getIsResponseBufferingAdaptive() {
return isResponseBufferingAdaptive;
}
Expand Down Expand Up @@ -1627,11 +1631,21 @@ boolean onRetStatus(TDSReader tdsReader) throws SQLServerException {

@Override
boolean onRetValue(TDSReader tdsReader) throws SQLServerException {
// Status: A value of 0x01 means the return value corresponds to an output parameter from
// a stored procedure. If it's 0x02 then the value corresponds to a return value from a
// user defined function.
//
// If it's a return value from a user defined function, we need to return false from this method
// so that the return value is not skipped.
int status = tdsReader.peekReturnValueStatus();

SQLServerStatement.this.returnValueStatus = status;

// We are only interested in return values that are statement OUT parameters,
// in which case we need to stop parsing and let CallableStatement take over.
// A RETVALUE token appearing in the execution results, but before any RETSTATUS
// token, is a TEXTPTR return value that should be ignored.
if (moreResults && null == procedureRetStatToken) {
if (moreResults && null == procedureRetStatToken && status != 2) {
Parameter p = new Parameter(
Util.shouldHonorAEForParameters(stmtColumnEncriptionSetting, connection));
p.skipRetValStatus(tdsReader);
Expand Down

0 comments on commit e93eac7

Please sign in to comment.