Skip to content

Commit

Permalink
Merge pull request #73 from v-suhame/dev
Browse files Browse the repository at this point in the history
Read SQL Server error message if status flag has DONE_ERROR set.
  • Loading branch information
xiangyushawn authored Dec 9, 2016
2 parents af0ac45 + 18d3cf2 commit 13fe3e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
14 changes: 14 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,20 @@ 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) {
short value = Util.readShort(currentPacket.payload, payloadOffset + 1);
return value;
}

// as per TDS protocol, TDS_DONE packet should always be followed by status flag
// throw exception if status packet is not available
throwInvalidTDS();
return 0;
}

final int readUnsignedByte() throws SQLServerException
{
// Ensure that we have a packet to read from.
Expand Down
27 changes: 20 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,26 @@ 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();
// check if status flag has DONE_ERROR set i.e., 0x2
if ((status & 0x0002) != 0) {
// Consume the DONE packet if there is error
StreamDone doneToken = new StreamDone();
doneToken.setFromTDS(tdsReader);
return true;
}
}

return false;
}
}

this.stmt = stmtIn;
Expand Down

0 comments on commit 13fe3e8

Please sign in to comment.