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

Read SQL Server error message if status flag has DONE_ERROR set. #73

Merged
merged 3 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local.properties
.classpath
.settings/
.loadpath
*.class
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we can create a separate PR for this


# External tool builders
.externalToolBuilders/
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6844,6 +6844,17 @@ final int peekTokenType() throws SQLServerException
return currentPacket.payload[payloadOffset] & 0xFF;
}


final short peekStatusFlag() throws SQLServerException {
// skip the current packet(i.e, TDS packet type) and peek into the status flag (USHORT)
if (payloadOffset + 3 <= currentPacket.payloadLength) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we check it is not the end of payload using ensurePayload() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ensurePayload() makes sense only for packet type. Since we already have determined the packet type to be TDS_DONE, there must be a status flag that follows as per TDS standards.

short value = Util.readShort(currentPacket.payload, payloadOffset + 1);
return value;
}

return Util.readShort(readWrappedBytes(3), 1);
}

final int readUnsignedByte() throws SQLServerException
{
// Ensure that we have a packet to read from.
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,25 @@ boolean onError(TDSReader tdsReader) throws SQLServerException
return false;
}

boolean onDone(TDSReader tdsReader) throws SQLServerException
{
// When initializing client-cursored ResultSets, a DONE token
// following the column metadata indicates an empty result set.
rowCount = 0;
return false;
}
boolean onDone(TDSReader tdsReader) throws SQLServerException {
// When initializing client-cursored ResultSets, a DONE token
// following the column metadata indicates an empty result set.
rowCount = 0;

// Continue to read the error message if DONE packet has error flag
int packetType = tdsReader.peekTokenType();
if (TDS.TDS_DONE == packetType) {
short status = tdsReader.peekStatusFlag();
Copy link
Contributor

Choose a reason for hiding this comment

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

SUGGESTION: You can add one method in TDSReader.

boolean isError() throws SQLServerException {
     //Get Status.
     short status = peekStatusFlag();
     return (status & 0x0002) != 0;
}

Usage might be...

Boolean onDone(TDSReader tdsReader) throws SQLServerException {
    ...
    ...
    if(TDS.TDS_DONE == packetType) {
           if(tdsReader.isError()) {
                 StreamDone doneTaken = ...
                 .....
           } 
   }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Function of methods in TDSReader are restricted to read operation, it doesn't do any data validation, it's good to keep it that way.

if ((status & 0x0002) != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add a comment to explain why we need this & 0x0002 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, will do that.

// Consume the DONE packet if there is error
StreamDone doneToken = new StreamDone();
doneToken.setFromTDS(tdsReader);
return true;
}
}

return false;
}
}

this.stmt = stmtIn;
Expand Down