Skip to content

Commit 44762d9

Browse files
Change TryReadString to use TryReadBytesWithContinue (#3422)
* change TryReadString to use TryReadBytesWithContinue * review feedback * tidy up sql * Update src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataReaderTest/DataReaderTest.cs --------- Co-authored-by: Cheena Malhotra <13396919+cheenamalhotra@users.noreply.github.com>
1 parent 1fadbd9 commit 44762d9

File tree

4 files changed

+117
-95
lines changed

4 files changed

+117
-95
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6374,7 +6374,7 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
63746374
}
63756375
else
63766376
{
6377-
result = TryReadByteArrayWithContinue(stateObj, length, out b);
6377+
result = stateObj.TryReadByteArrayWithContinue(length, out b);
63786378
if (result != TdsOperationStatus.Done)
63796379
{
63806380
return result;
@@ -6495,48 +6495,6 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
64956495
return TdsOperationStatus.Done;
64966496
}
64976497

6498-
private TdsOperationStatus TryReadByteArrayWithContinue(TdsParserStateObject stateObj, int length, out byte[] bytes)
6499-
{
6500-
bytes = null;
6501-
int offset = 0;
6502-
byte[] temp = null;
6503-
(bool canContinue, bool isStarting, bool isContinuing) = stateObj.GetSnapshotStatuses();
6504-
if (canContinue)
6505-
{
6506-
if (isContinuing || isStarting)
6507-
{
6508-
temp = stateObj.TryTakeSnapshotStorage() as byte[];
6509-
Debug.Assert(bytes == null || bytes.Length == length, "stored buffer length must be null or must have been created with the correct length");
6510-
}
6511-
if (temp != null)
6512-
{
6513-
offset = stateObj.GetSnapshotTotalSize();
6514-
}
6515-
}
6516-
6517-
6518-
if (temp == null)
6519-
{
6520-
temp = new byte[length];
6521-
}
6522-
6523-
TdsOperationStatus result = stateObj.TryReadByteArray(temp, length, out _, offset, isStarting || isContinuing);
6524-
6525-
if (result == TdsOperationStatus.Done)
6526-
{
6527-
bytes = temp;
6528-
}
6529-
else if (result == TdsOperationStatus.NeedMoreData)
6530-
{
6531-
if (isStarting || isContinuing)
6532-
{
6533-
stateObj.SetSnapshotStorage(temp);
6534-
}
6535-
}
6536-
6537-
return result;
6538-
}
6539-
65406498
private TdsOperationStatus TryReadSqlDateTime(SqlBuffer value, byte tdsType, int length, byte scale, TdsParserStateObject stateObj)
65416499
{
65426500
Span<byte> datetimeBuffer = ((uint)length <= 16) ? stackalloc byte[16] : new byte[length];

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6570,7 +6570,7 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
65706570
}
65716571
else
65726572
{
6573-
result = TryReadByteArrayWithContinue(stateObj, length, out b);
6573+
result = stateObj.TryReadByteArrayWithContinue(length, out b);
65746574
if (result != TdsOperationStatus.Done)
65756575
{
65766576
return result;
@@ -6691,48 +6691,6 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
66916691
return TdsOperationStatus.Done;
66926692
}
66936693

6694-
private TdsOperationStatus TryReadByteArrayWithContinue(TdsParserStateObject stateObj, int length, out byte[] bytes)
6695-
{
6696-
bytes = null;
6697-
int offset = 0;
6698-
byte[] temp = null;
6699-
(bool canContinue, bool isStarting, bool isContinuing) = stateObj.GetSnapshotStatuses();
6700-
if (canContinue)
6701-
{
6702-
if (isContinuing || isStarting)
6703-
{
6704-
temp = stateObj.TryTakeSnapshotStorage() as byte[];
6705-
Debug.Assert(bytes == null || bytes.Length == length, "stored buffer length must be null or must have been created with the correct length");
6706-
}
6707-
if (temp != null)
6708-
{
6709-
offset = stateObj.GetSnapshotTotalSize();
6710-
}
6711-
}
6712-
6713-
6714-
if (temp == null)
6715-
{
6716-
temp = new byte[length];
6717-
}
6718-
6719-
TdsOperationStatus result = stateObj.TryReadByteArray(temp, length, out _, offset, isStarting || isContinuing);
6720-
6721-
if (result == TdsOperationStatus.Done)
6722-
{
6723-
bytes = temp;
6724-
}
6725-
else if (result == TdsOperationStatus.NeedMoreData)
6726-
{
6727-
if (isStarting || isContinuing)
6728-
{
6729-
stateObj.SetSnapshotStorage(temp);
6730-
}
6731-
}
6732-
6733-
return result;
6734-
}
6735-
67366694
private TdsOperationStatus TryReadSqlDateTime(SqlBuffer value, byte tdsType, int length, byte scale, TdsParserStateObject stateObj)
67376695
{
67386696
Span<byte> datetimeBuffer = ((uint)length <= 16) ? stackalloc byte[16] : new byte[length];

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,48 @@ public TdsOperationStatus TryReadByteArray(Span<byte> buff, int len, out int tot
14971497
return TdsOperationStatus.Done;
14981498
}
14991499

1500+
public TdsOperationStatus TryReadByteArrayWithContinue(int length, out byte[] bytes)
1501+
{
1502+
bytes = null;
1503+
int offset = 0;
1504+
byte[] temp = null;
1505+
(bool canContinue, bool isStarting, bool isContinuing) = GetSnapshotStatuses();
1506+
if (canContinue)
1507+
{
1508+
if (isContinuing || isStarting)
1509+
{
1510+
temp = TryTakeSnapshotStorage() as byte[];
1511+
Debug.Assert(bytes == null || bytes.Length == length, "stored buffer length must be null or must have been created with the correct length");
1512+
}
1513+
if (temp != null)
1514+
{
1515+
offset = GetSnapshotTotalSize();
1516+
}
1517+
}
1518+
1519+
1520+
if (temp == null)
1521+
{
1522+
temp = new byte[length];
1523+
}
1524+
1525+
TdsOperationStatus result = TryReadByteArray(temp, length, out _, offset, isStarting || isContinuing);
1526+
1527+
if (result == TdsOperationStatus.Done)
1528+
{
1529+
bytes = temp;
1530+
}
1531+
else if (result == TdsOperationStatus.NeedMoreData)
1532+
{
1533+
if (isStarting || isContinuing)
1534+
{
1535+
SetSnapshotStorage(temp);
1536+
}
1537+
}
1538+
1539+
return result;
1540+
}
1541+
15001542
// Takes no arguments and returns a byte from the buffer. If the buffer is empty, it is filled
15011543
// before the byte is returned.
15021544
internal TdsOperationStatus TryReadByte(out byte value)
@@ -1843,21 +1885,13 @@ internal TdsOperationStatus TryReadString(int length, out string value)
18431885

18441886
if (((_inBytesUsed + cBytes) > _inBytesRead) || (_inBytesPacket < cBytes))
18451887
{
1846-
if (_bTmp == null || _bTmp.Length < cBytes)
1847-
{
1848-
_bTmp = new byte[cBytes];
1849-
}
1850-
1851-
TdsOperationStatus result = TryReadByteArray(_bTmp, cBytes);
1888+
TdsOperationStatus result = TryReadByteArrayWithContinue(cBytes, out buf);
18521889
if (result != TdsOperationStatus.Done)
18531890
{
18541891
value = null;
18551892
return result;
18561893
}
18571894

1858-
// assign local to point to parser scratch buffer
1859-
buf = _bTmp;
1860-
18611895
AssertValidState();
18621896
}
18631897
else

0 commit comments

Comments
 (0)