diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs
index f0ae13e6cd..986e464acb 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs
@@ -257,33 +257,14 @@ internal _SqlMetaDataSet MetaData
{
throw SQL.PendingBeginXXXExists();
}
-
-#if NETFRAMEWORK
- RuntimeHelpers.PrepareConstrainedRegions();
-#endif
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
+
+ Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
+ if (TryConsumeMetaData() != TdsOperationStatus.Done)
{
-#endif
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- if (TryConsumeMetaData() != TdsOperationStatus.Done)
- {
- throw SQL.SynchronousCallMayNotPend();
- }
- }
-#if NETFRAMEWORK && DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
+ throw SQL.SynchronousCallMayNotPend();
}
-#endif
}
+
return _metaData;
}
}
@@ -856,30 +837,10 @@ private TdsOperationStatus TryCleanPartialRead()
private void CleanPartialReadReliable()
{
AssertReaderState(requireData: true, permitAsync: false);
-
-#if NETFRAMEWORK
- RuntimeHelpers.PrepareConstrainedRegions();
-#endif
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
- {
-#endif
- TdsOperationStatus result = TryCleanPartialRead();
- Debug.Assert(result == TdsOperationStatus.Done, "Should not pend on sync call");
- Debug.Assert(!_sharedState._dataReady, "_dataReady should be cleared");
- }
-#if NETFRAMEWORK && DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif
+
+ TdsOperationStatus result = TryCleanPartialRead();
+ Debug.Assert(result == TdsOperationStatus.Done, "Should not pend on sync call");
+ Debug.Assert(!_sharedState._dataReady, "_dataReady should be cleared");
}
///
@@ -1004,77 +965,60 @@ private TdsOperationStatus TryCloseInternal(bool closeReader)
#endif
try
{
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
+ if ((!_isClosed) && (parser != null) && (stateObj != null) && (stateObj.HasPendingData))
{
-#endif
- if ((!_isClosed) && (parser != null) && (stateObj != null) && (stateObj.HasPendingData))
+ // It is possible for this to be called during connection close on a
+ // broken connection, so check state first.
+ if (parser.State == TdsParserState.OpenLoggedIn)
{
- // It is possible for this to be called during connection close on a
- // broken connection, so check state first.
- if (parser.State == TdsParserState.OpenLoggedIn)
- {
- // if user called read but didn't fetch any values, skip the row
- // same applies after NextResult on ALTROW because NextResult starts rowconsumption in that case ...
+ // if user called read but didn't fetch any values, skip the row
+ // same applies after NextResult on ALTROW because NextResult starts rowconsumption in that case ...
- Debug.Assert(SniContext.Snix_Read == stateObj.SniContext, $"The SniContext should be Snix_Read but it actually is {stateObj.SniContext}");
+ Debug.Assert(SniContext.Snix_Read == stateObj.SniContext, $"The SniContext should be Snix_Read but it actually is {stateObj.SniContext}");
- if (_altRowStatus == ALTROWSTATUS.AltRow)
+ if (_altRowStatus == ALTROWSTATUS.AltRow)
+ {
+ _sharedState._dataReady = true; // set _sharedState._dataReady to not confuse CleanPartialRead
+ }
+ _stateObj.SetTimeoutStateStopped();
+ if (_sharedState._dataReady)
+ {
+ cleanDataFailed = true;
+ result = TryCleanPartialRead();
+ if (result == TdsOperationStatus.Done)
{
- _sharedState._dataReady = true; // set _sharedState._dataReady to not confuse CleanPartialRead
+ cleanDataFailed = false;
}
- _stateObj.SetTimeoutStateStopped();
- if (_sharedState._dataReady)
+ else
{
- cleanDataFailed = true;
- result = TryCleanPartialRead();
- if (result == TdsOperationStatus.Done)
- {
- cleanDataFailed = false;
- }
- else
- {
- return result;
- }
+ return result;
}
+ }
#if DEBUG
- else
+ else
+ {
+ byte token;
+ result = _stateObj.TryPeekByte(out token);
+ if (result != TdsOperationStatus.Done)
{
- byte token;
- result = _stateObj.TryPeekByte(out token);
- if (result != TdsOperationStatus.Done)
- {
- return result;
- }
-
- Debug.Assert(TdsParser.IsValidTdsToken(token), $"DataReady is false, but next token is invalid: {token,-2:X2}");
+ return result;
}
+
+ Debug.Assert(TdsParser.IsValidTdsToken(token), $"DataReady is false, but next token is invalid: {token,-2:X2}");
+ }
#endif
- result = parser.TryRun(RunBehavior.Clean, _command, this, null, stateObj, out _);
- if (result != TdsOperationStatus.Done)
- {
- return result;
- }
+ result = parser.TryRun(RunBehavior.Clean, _command, this, null, stateObj, out _);
+ if (result != TdsOperationStatus.Done)
+ {
+ return result;
}
}
-
- RestoreServerSettings(parser, stateObj);
- return TdsOperationStatus.Done;
}
-#if NETFRAMEWORK && DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif
+
+ RestoreServerSettings(parser, stateObj);
+ return TdsOperationStatus.Done;
}
finally
{
@@ -1113,45 +1057,25 @@ private TdsOperationStatus TryCloseInternal(bool closeReader)
{
Connection.RemoveWeakReference(this); // This doesn't catch everything -- the connection may be closed, but it prevents dead readers from clogging the collection
}
-
-#if NETFRAMEWORK
- RuntimeHelpers.PrepareConstrainedRegions();
-#endif
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
+
+ // IsClosed may be true if CloseReaderFromConnection was called - in which case, the session has already been closed
+ if (!wasClosed && stateObj != null)
{
-#endif
- // IsClosed may be true if CloseReaderFromConnection was called - in which case, the session has already been closed
- if (!wasClosed && stateObj != null)
+ if (!cleanDataFailed)
{
- if (!cleanDataFailed)
+ stateObj.CloseSession();
+ }
+ else
+ {
+ if (parser != null)
{
- stateObj.CloseSession();
- }
- else
- {
- if (parser != null)
- {
- parser.State = TdsParserState.Broken; // We failed while draining data, so TDS pointer can be between tokens - cannot recover
- parser.PutSession(stateObj);
- parser.Connection.BreakConnection();
- }
+ parser.State = TdsParserState.Broken; // We failed while draining data, so TDS pointer can be between tokens - cannot recover
+ parser.PutSession(stateObj);
+ parser.Connection.BreakConnection();
}
}
- // DO NOT USE stateObj after this point - it has been returned to the TdsParser's session pool and potentially handed out to another thread
- }
-#if NETFRAMEWORK && DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
}
-#endif
+ // DO NOT USE stateObj after this point - it has been returned to the TdsParser's session pool and potentially handed out to another thread
// do not retry here
result = TrySetMetaData(null, false);
@@ -1731,251 +1655,232 @@ private TdsOperationStatus TryGetBytesInternal(int i, long dataIndex, byte[] buf
{
remaining = 0;
TdsOperationStatus result;
-#if NETFRAMEWORK
- RuntimeHelpers.PrepareConstrainedRegions();
-#endif
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
+ int cbytes = 0;
+ AssertReaderState(requireData: true, permitAsync: true, columnIndex: i, enforceSequentialAccess: true);
+
+ // sequential reading
+ if (IsCommandBehavior(CommandBehavior.SequentialAccess))
{
-#endif
- int cbytes = 0;
- AssertReaderState(requireData: true, permitAsync: true, columnIndex: i, enforceSequentialAccess: true);
+ Debug.Assert(!HasActiveStreamOrTextReaderOnColumn(i), "Column has an active Stream or TextReader");
- // sequential reading
- if (IsCommandBehavior(CommandBehavior.SequentialAccess))
+ if (_metaData[i] != null && _metaData[i].cipherMD != null)
{
- Debug.Assert(!HasActiveStreamOrTextReaderOnColumn(i), "Column has an active Stream or TextReader");
-
- if (_metaData[i] != null && _metaData[i].cipherMD != null)
- {
- throw SQL.SequentialAccessNotSupportedOnEncryptedColumn(_metaData[i].column);
- }
+ throw SQL.SequentialAccessNotSupportedOnEncryptedColumn(_metaData[i].column);
+ }
- if (_sharedState._nextColumnHeaderToRead <= i)
+ if (_sharedState._nextColumnHeaderToRead <= i)
+ {
+ result = TryReadColumnHeader(i);
+ if (result != TdsOperationStatus.Done)
{
- result = TryReadColumnHeader(i);
- if (result != TdsOperationStatus.Done)
- {
- return result;
- }
+ return result;
}
+ }
- // If data is null, ReadColumnHeader sets the data.IsNull bit.
- if (_data[i] != null && _data[i].IsNull)
- {
- throw new SqlNullValueException();
- }
+ // If data is null, ReadColumnHeader sets the data.IsNull bit.
+ if (_data[i] != null && _data[i].IsNull)
+ {
+ throw new SqlNullValueException();
+ }
- // If there are an unknown (-1) number of bytes left for a PLP, read its size
- if ((-1 == _sharedState._columnDataBytesRemaining) && (_metaData[i].metaType.IsPlp))
+ // If there are an unknown (-1) number of bytes left for a PLP, read its size
+ if ((-1 == _sharedState._columnDataBytesRemaining) && (_metaData[i].metaType.IsPlp))
+ {
+ ulong left;
+ result = _parser.TryPlpBytesLeft(_stateObj, out left);
+ if (result != TdsOperationStatus.Done)
{
- ulong left;
- result = _parser.TryPlpBytesLeft(_stateObj, out left);
- if (result != TdsOperationStatus.Done)
- {
- return result;
- }
- _sharedState._columnDataBytesRemaining = (long)left;
+ return result;
}
+ _sharedState._columnDataBytesRemaining = (long)left;
+ }
- if (0 == _sharedState._columnDataBytesRemaining)
- {
- return TdsOperationStatus.Done; // We've read this column to the end
- }
+ if (0 == _sharedState._columnDataBytesRemaining)
+ {
+ return TdsOperationStatus.Done; // We've read this column to the end
+ }
- // if no buffer is passed in, return the number total of bytes, or -1
- if (buffer == null)
+ // if no buffer is passed in, return the number total of bytes, or -1
+ if (buffer == null)
+ {
+ if (_metaData[i].metaType.IsPlp)
{
- if (_metaData[i].metaType.IsPlp)
- {
- remaining = (long)_parser.PlpBytesTotalLength(_stateObj);
- return TdsOperationStatus.Done;
- }
- remaining = _sharedState._columnDataBytesRemaining;
+ remaining = (long)_parser.PlpBytesTotalLength(_stateObj);
return TdsOperationStatus.Done;
}
+ remaining = _sharedState._columnDataBytesRemaining;
+ return TdsOperationStatus.Done;
+ }
- if (dataIndex < 0)
- {
- throw ADP.NegativeParameter(nameof(dataIndex));
- }
+ if (dataIndex < 0)
+ {
+ throw ADP.NegativeParameter(nameof(dataIndex));
+ }
- if (dataIndex < _columnDataBytesRead)
- {
- throw ADP.NonSeqByteAccess(dataIndex, _columnDataBytesRead, nameof(GetBytes));
- }
+ if (dataIndex < _columnDataBytesRead)
+ {
+ throw ADP.NonSeqByteAccess(dataIndex, _columnDataBytesRead, nameof(GetBytes));
+ }
- // if the dataIndex is not equal to bytes read, then we have to skip bytes
- long cb = dataIndex - _columnDataBytesRead;
+ // if the dataIndex is not equal to bytes read, then we have to skip bytes
+ long cb = dataIndex - _columnDataBytesRead;
- // if dataIndex is outside of the data range, return 0
- if ((cb > _sharedState._columnDataBytesRemaining) && !_metaData[i].metaType.IsPlp)
- {
- return TdsOperationStatus.Done;
- }
+ // if dataIndex is outside of the data range, return 0
+ if ((cb > _sharedState._columnDataBytesRemaining) && !_metaData[i].metaType.IsPlp)
+ {
+ return TdsOperationStatus.Done;
+ }
- // if bad buffer index, throw
- if (bufferIndex < 0 || bufferIndex >= buffer.Length)
- {
- throw ADP.InvalidDestinationBufferIndex(buffer.Length, bufferIndex, nameof(bufferIndex));
- }
+ // if bad buffer index, throw
+ if (bufferIndex < 0 || bufferIndex >= buffer.Length)
+ {
+ throw ADP.InvalidDestinationBufferIndex(buffer.Length, bufferIndex, nameof(bufferIndex));
+ }
- // if there is not enough room in the buffer for data
- if (length + bufferIndex > buffer.Length)
- {
- throw ADP.InvalidBufferSizeOrIndex(length, bufferIndex);
- }
+ // if there is not enough room in the buffer for data
+ if (length + bufferIndex > buffer.Length)
+ {
+ throw ADP.InvalidBufferSizeOrIndex(length, bufferIndex);
+ }
- if (length < 0)
- {
- throw ADP.InvalidDataLength(length);
- }
+ if (length < 0)
+ {
+ throw ADP.InvalidDataLength(length);
+ }
- // Skip if needed
- if (cb > 0)
+ // Skip if needed
+ if (cb > 0)
+ {
+ if (_metaData[i].metaType.IsPlp)
{
- if (_metaData[i].metaType.IsPlp)
+ ulong skipped;
+ result = _parser.TrySkipPlpValue((ulong)cb, _stateObj, out skipped);
+ if (result != TdsOperationStatus.Done)
{
- ulong skipped;
- result = _parser.TrySkipPlpValue((ulong)cb, _stateObj, out skipped);
- if (result != TdsOperationStatus.Done)
- {
- return result;
- }
- _columnDataBytesRead += (long)skipped;
+ return result;
}
- else
+ _columnDataBytesRead += (long)skipped;
+ }
+ else
+ {
+ result = _stateObj.TrySkipLongBytes(cb);
+ if (result != TdsOperationStatus.Done)
{
- result = _stateObj.TrySkipLongBytes(cb);
- if (result != TdsOperationStatus.Done)
- {
- return result;
- }
- _columnDataBytesRead += cb;
- _sharedState._columnDataBytesRemaining -= cb;
+ return result;
}
+ _columnDataBytesRead += cb;
+ _sharedState._columnDataBytesRemaining -= cb;
}
-
- int bytesRead;
- result = TryGetBytesInternalSequential(i, buffer, bufferIndex, length, out bytesRead);
- remaining = (int)bytesRead;
- return result;
}
- // random access now!
- // note that since we are caching in an array, and arrays aren't 64 bit ready yet,
- // we need can cast to int if the dataIndex is in range
- if (dataIndex < 0)
- {
- throw ADP.NegativeParameter(nameof(dataIndex));
- }
+ int bytesRead;
+ result = TryGetBytesInternalSequential(i, buffer, bufferIndex, length, out bytesRead);
+ remaining = (int)bytesRead;
+ return result;
+ }
- if (dataIndex > int.MaxValue)
- {
- throw ADP.InvalidSourceBufferIndex(cbytes, dataIndex, nameof(dataIndex));
- }
+ // random access now!
+ // note that since we are caching in an array, and arrays aren't 64 bit ready yet,
+ // we need can cast to int if the dataIndex is in range
+ if (dataIndex < 0)
+ {
+ throw ADP.NegativeParameter(nameof(dataIndex));
+ }
- int ndataIndex = (int)dataIndex;
- byte[] data;
+ if (dataIndex > int.MaxValue)
+ {
+ throw ADP.InvalidSourceBufferIndex(cbytes, dataIndex, nameof(dataIndex));
+ }
+
+ int ndataIndex = (int)dataIndex;
+ byte[] data;
- // WebData 99342 - in the non-sequential case, we need to support
- // the use of GetBytes on string data columns, but
- // GetSqlBinary isn't supposed to. What we end up
- // doing isn't exactly pretty, but it does work.
- if (_metaData[i].metaType.IsBinType)
+ // WebData 99342 - in the non-sequential case, we need to support
+ // the use of GetBytes on string data columns, but
+ // GetSqlBinary isn't supposed to. What we end up
+ // doing isn't exactly pretty, but it does work.
+ if (_metaData[i].metaType.IsBinType)
+ {
+ data = GetSqlBinary(i).Value;
+ }
+ else
+ {
+ Debug.Assert(_metaData[i].metaType.IsLong, "non long type?");
+ Debug.Assert(_metaData[i].metaType.IsCharType, "non-char type?");
+
+ SqlString temp = GetSqlString(i);
+ if (_metaData[i].metaType.IsNCharType)
{
- data = GetSqlBinary(i).Value;
+ data = temp.GetUnicodeBytes();
}
else
{
- Debug.Assert(_metaData[i].metaType.IsLong, "non long type?");
- Debug.Assert(_metaData[i].metaType.IsCharType, "non-char type?");
+ data = temp.GetNonUnicodeBytes();
+ }
+ }
+
+ cbytes = data.Length;
+
+ // if no buffer is passed in, return the number of characters we have
+ if (buffer == null)
+ {
+ remaining = cbytes;
+ return TdsOperationStatus.Done;
+ }
- SqlString temp = GetSqlString(i);
- if (_metaData[i].metaType.IsNCharType)
+ // if dataIndex is outside of data range, return 0
+ if (ndataIndex < 0 || ndataIndex >= cbytes)
+ {
+ return TdsOperationStatus.Done;
+ }
+ try
+ {
+ if (ndataIndex < cbytes)
+ {
+ // help the user out in the case where there's less data than requested
+ if ((ndataIndex + length) > cbytes)
{
- data = temp.GetUnicodeBytes();
+ cbytes = cbytes - ndataIndex;
}
else
{
- data = temp.GetNonUnicodeBytes();
+ cbytes = length;
}
}
+ Buffer.BlockCopy(data, ndataIndex, buffer, bufferIndex, cbytes);
+ }
+ catch (Exception e)
+ {
+ if (!ADP.IsCatchableExceptionType(e))
+ {
+ throw;
+ }
cbytes = data.Length;
- // if no buffer is passed in, return the number of characters we have
- if (buffer == null)
+ if (length < 0)
{
- remaining = cbytes;
- return TdsOperationStatus.Done;
+ throw ADP.InvalidDataLength(length);
}
- // if dataIndex is outside of data range, return 0
- if (ndataIndex < 0 || ndataIndex >= cbytes)
+ // if bad buffer index, throw
+ if (bufferIndex < 0 || bufferIndex >= buffer.Length)
{
- return TdsOperationStatus.Done;
+ throw ADP.InvalidDestinationBufferIndex(buffer.Length, bufferIndex, nameof(bufferIndex));
}
- try
- {
- if (ndataIndex < cbytes)
- {
- // help the user out in the case where there's less data than requested
- if ((ndataIndex + length) > cbytes)
- {
- cbytes = cbytes - ndataIndex;
- }
- else
- {
- cbytes = length;
- }
- }
- Buffer.BlockCopy(data, ndataIndex, buffer, bufferIndex, cbytes);
- }
- catch (Exception e)
+ // if there is not enough room in the buffer for data
+ if (cbytes + bufferIndex > buffer.Length)
{
- if (!ADP.IsCatchableExceptionType(e))
- {
- throw;
- }
- cbytes = data.Length;
-
- if (length < 0)
- {
- throw ADP.InvalidDataLength(length);
- }
-
- // if bad buffer index, throw
- if (bufferIndex < 0 || bufferIndex >= buffer.Length)
- {
- throw ADP.InvalidDestinationBufferIndex(buffer.Length, bufferIndex, nameof(bufferIndex));
- }
-
- // if there is not enough room in the buffer for data
- if (cbytes + bufferIndex > buffer.Length)
- {
- throw ADP.InvalidBufferSizeOrIndex(cbytes, bufferIndex);
- }
-
- throw;
+ throw ADP.InvalidBufferSizeOrIndex(cbytes, bufferIndex);
}
- remaining = cbytes;
- return TdsOperationStatus.Done;
+ throw;
}
-#if NETFRAMEWORK && DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif //DEBUG
+
+ remaining = cbytes;
+ return TdsOperationStatus.Done;
}
internal int GetBytesInternalSequential(int i, byte[] buffer, int index, int length, long? timeoutMilliseconds = null)
@@ -2027,66 +1932,47 @@ internal TdsOperationStatus TryGetBytesInternalSequential(int i, byte[] buffer,
bytesRead = 0;
TdsOperationStatus result;
-#if NETFRAMEWORK
- RuntimeHelpers.PrepareConstrainedRegions();
-#endif
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try
+ if ((_sharedState._columnDataBytesRemaining == 0) || (length == 0))
{
- tdsReliabilitySection.Start();
-#else
+ // No data left or nothing requested, return 0
+ bytesRead = 0;
+ return TdsOperationStatus.Done;
+ }
+ else
{
-#endif
- if ((_sharedState._columnDataBytesRemaining == 0) || (length == 0))
- {
- // No data left or nothing requested, return 0
- bytesRead = 0;
- return TdsOperationStatus.Done;
- }
- else
+ // if plp columns, do partial reads. Don't read the entire value in one shot.
+ if (_metaData[i].metaType.IsPlp)
{
- // if plp columns, do partial reads. Don't read the entire value in one shot.
- if (_metaData[i].metaType.IsPlp)
+ // Read in data
+ result = _stateObj.TryReadPlpBytes(ref buffer, index, length, out bytesRead);
+ _columnDataBytesRead += bytesRead;
+ if (result != TdsOperationStatus.Done)
{
- // Read in data
- result = _stateObj.TryReadPlpBytes(ref buffer, index, length, out bytesRead);
- _columnDataBytesRead += bytesRead;
- if (result != TdsOperationStatus.Done)
- {
- return result;
- }
-
- // Query for number of bytes left
- ulong left;
- result = _parser.TryPlpBytesLeft(_stateObj, out left);
- if (result != TdsOperationStatus.Done)
- {
- _sharedState._columnDataBytesRemaining = -1;
- return result;
- }
- _sharedState._columnDataBytesRemaining = (long)left;
- return TdsOperationStatus.Done;
+ return result;
}
- else
+
+ // Query for number of bytes left
+ ulong left;
+ result = _parser.TryPlpBytesLeft(_stateObj, out left);
+ if (result != TdsOperationStatus.Done)
{
- // Read data (not exceeding the total amount of data available)
- int bytesToRead = (int)Math.Min((long)length, _sharedState._columnDataBytesRemaining);
- result = _stateObj.TryReadByteArray(buffer.AsSpan(index), bytesToRead, out bytesRead);
- _columnDataBytesRead += bytesRead;
- _sharedState._columnDataBytesRemaining -= bytesRead;
+ _sharedState._columnDataBytesRemaining = -1;
return result;
}
+ _sharedState._columnDataBytesRemaining = (long)left;
+ return TdsOperationStatus.Done;
+ }
+ else
+ {
+ // Read data (not exceeding the total amount of data available)
+ int bytesToRead = (int)Math.Min((long)length, _sharedState._columnDataBytesRemaining);
+ result = _stateObj.TryReadByteArray(buffer.AsSpan(index), bytesToRead, out bytesRead);
+ _columnDataBytesRead += bytesRead;
+ _sharedState._columnDataBytesRemaining -= bytesRead;
+ return result;
}
}
-#if NETFRAMEWORK && DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif
}
///
@@ -2358,113 +2244,93 @@ override public long GetChars(int i, long dataIndex, char[] buffer, int bufferIn
private long GetCharsFromPlpData(int i, long dataIndex, char[] buffer, int bufferIndex, int length)
{
-#if NETFRAMEWORK
- RuntimeHelpers.PrepareConstrainedRegions();
-#endif
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
+ long cch;
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
- {
-#endif
- long cch;
-
- AssertReaderState(requireData: true, permitAsync: false, columnIndex: i, enforceSequentialAccess: true);
- Debug.Assert(!HasActiveStreamOrTextReaderOnColumn(i), "Column has active Stream or TextReader");
- // don't allow get bytes on non-long or non-binary columns
- Debug.Assert(_metaData[i].metaType.IsPlp, "GetCharsFromPlpData called on a non-plp column!");
- // Must be sequential reading
- Debug.Assert(IsCommandBehavior(CommandBehavior.SequentialAccess), "GetCharsFromPlpData called for non-Sequential access");
+ AssertReaderState(requireData: true, permitAsync: false, columnIndex: i, enforceSequentialAccess: true);
+ Debug.Assert(!HasActiveStreamOrTextReaderOnColumn(i), "Column has active Stream or TextReader");
+ // don't allow get bytes on non-long or non-binary columns
+ Debug.Assert(_metaData[i].metaType.IsPlp, "GetCharsFromPlpData called on a non-plp column!");
+ // Must be sequential reading
+ Debug.Assert(IsCommandBehavior(CommandBehavior.SequentialAccess), "GetCharsFromPlpData called for non-Sequential access");
- if (!_metaData[i].metaType.IsCharType)
- {
- throw SQL.NonCharColumn(_metaData[i].column);
- }
+ if (!_metaData[i].metaType.IsCharType)
+ {
+ throw SQL.NonCharColumn(_metaData[i].column);
+ }
- if (_sharedState._nextColumnHeaderToRead <= i)
- {
- ReadColumnHeader(i);
- }
+ if (_sharedState._nextColumnHeaderToRead <= i)
+ {
+ ReadColumnHeader(i);
+ }
- // If data is null, ReadColumnHeader sets the data.IsNull bit.
- if (_data[i] != null && _data[i].IsNull)
- {
- throw new SqlNullValueException();
- }
+ // If data is null, ReadColumnHeader sets the data.IsNull bit.
+ if (_data[i] != null && _data[i].IsNull)
+ {
+ throw new SqlNullValueException();
+ }
- if (dataIndex < _columnDataCharsRead)
- {
- // Don't allow re-read of same chars in sequential access mode
- throw ADP.NonSeqByteAccess(dataIndex, _columnDataCharsRead, nameof(GetChars));
- }
+ if (dataIndex < _columnDataCharsRead)
+ {
+ // Don't allow re-read of same chars in sequential access mode
+ throw ADP.NonSeqByteAccess(dataIndex, _columnDataCharsRead, nameof(GetChars));
+ }
- // If we start reading the new column, either dataIndex is 0 or
- // _columnDataCharsRead is 0 and dataIndex > _columnDataCharsRead is true below.
- // In both cases we will clean decoder
- if (dataIndex == 0)
- {
- _stateObj._plpdecoder = null;
- }
+ // If we start reading the new column, either dataIndex is 0 or
+ // _columnDataCharsRead is 0 and dataIndex > _columnDataCharsRead is true below.
+ // In both cases we will clean decoder
+ if (dataIndex == 0)
+ {
+ _stateObj._plpdecoder = null;
+ }
- bool isUnicode = _metaData[i].metaType.IsNCharType;
+ bool isUnicode = _metaData[i].metaType.IsNCharType;
- // If there are an unknown (-1) number of bytes left for a PLP, read its size
- if (-1 == _sharedState._columnDataBytesRemaining)
- {
- _sharedState._columnDataBytesRemaining = (long)_parser.PlpBytesLeft(_stateObj);
- }
+ // If there are an unknown (-1) number of bytes left for a PLP, read its size
+ if (-1 == _sharedState._columnDataBytesRemaining)
+ {
+ _sharedState._columnDataBytesRemaining = (long)_parser.PlpBytesLeft(_stateObj);
+ }
- if (0 == _sharedState._columnDataBytesRemaining)
- {
- _stateObj._plpdecoder = null;
- return 0; // We've read this column to the end
- }
+ if (0 == _sharedState._columnDataBytesRemaining)
+ {
+ _stateObj._plpdecoder = null;
+ return 0; // We've read this column to the end
+ }
- // if no buffer is passed in, return the total number of characters or -1
- if (buffer == null)
- {
- cch = (long)_parser.PlpBytesTotalLength(_stateObj);
- return (isUnicode && (cch > 0)) ? cch >> 1 : cch;
- }
- if (dataIndex > _columnDataCharsRead)
- {
- // Skip chars
+ // if no buffer is passed in, return the total number of characters or -1
+ if (buffer == null)
+ {
+ cch = (long)_parser.PlpBytesTotalLength(_stateObj);
+ return (isUnicode && (cch > 0)) ? cch >> 1 : cch;
+ }
+ if (dataIndex > _columnDataCharsRead)
+ {
+ // Skip chars
- // Clean decoder state: we do not reset it, but destroy to ensure
- // that we do not start decoding the column with decoder from the old one
- _stateObj._plpdecoder = null;
- cch = dataIndex - _columnDataCharsRead;
- cch = isUnicode ? (cch << 1) : cch;
- cch = (long)_parser.SkipPlpValue((ulong)(cch), _stateObj);
- _columnDataBytesRead += cch;
- _columnDataCharsRead += (isUnicode && (cch > 0)) ? cch >> 1 : cch;
- }
- cch = length;
+ // Clean decoder state: we do not reset it, but destroy to ensure
+ // that we do not start decoding the column with decoder from the old one
+ _stateObj._plpdecoder = null;
+ cch = dataIndex - _columnDataCharsRead;
+ cch = isUnicode ? (cch << 1) : cch;
+ cch = (long)_parser.SkipPlpValue((ulong)(cch), _stateObj);
+ _columnDataBytesRead += cch;
+ _columnDataCharsRead += (isUnicode && (cch > 0)) ? cch >> 1 : cch;
+ }
+ cch = length;
- if (isUnicode)
- {
- cch = (long)_parser.ReadPlpUnicodeChars(ref buffer, bufferIndex, length, _stateObj);
- _columnDataBytesRead += (cch << 1);
- }
- else
- {
- cch = (long)_parser.ReadPlpAnsiChars(ref buffer, bufferIndex, length, _metaData[i], _stateObj);
- _columnDataBytesRead += cch << 1;
- }
- _columnDataCharsRead += cch;
- _sharedState._columnDataBytesRemaining = (long)_parser.PlpBytesLeft(_stateObj);
- return cch;
+ if (isUnicode)
+ {
+ cch = (long)_parser.ReadPlpUnicodeChars(ref buffer, bufferIndex, length, _stateObj);
+ _columnDataBytesRead += (cch << 1);
}
-#if NETFRAMEWORK && DEBUG
- finally
+ else
{
- tdsReliabilitySection.Stop();
+ cch = (long)_parser.ReadPlpAnsiChars(ref buffer, bufferIndex, length, _metaData[i], _stateObj);
+ _columnDataBytesRead += cch << 1;
}
-#endif
+ _columnDataCharsRead += cch;
+ _sharedState._columnDataBytesRemaining = (long)_parser.PlpBytesLeft(_stateObj);
+ return cch;
}
internal long GetStreamingXmlChars(int i, long dataIndex, char[] buffer, int bufferIndex, int length)
@@ -3565,155 +3431,138 @@ private TdsOperationStatus TryNextResult(out bool more)
try
{
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
+ statistics = SqlStatistics.StartTimer(Statistics);
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
+ SetTimeout(_defaultTimeoutMilliseconds);
+
+ if (IsClosed)
{
-#endif
- statistics = SqlStatistics.StartTimer(Statistics);
+ throw ADP.DataReaderClosed(nameof(NextResult));
+ }
+ _fieldNameLookup = null;
- SetTimeout(_defaultTimeoutMilliseconds);
+ bool success = false; // WebData 100390
+ _hasRows = false; // reset HasRows
- if (IsClosed)
+ // if we are specifically only processing a single result, then read all the results off the wire and detach
+ if (IsCommandBehavior(CommandBehavior.SingleResult))
+ {
+ result = TryCloseInternal(closeReader: false);
+ if (result != TdsOperationStatus.Done)
{
- throw ADP.DataReaderClosed(nameof(NextResult));
+ more = false;
+ return result;
}
- _fieldNameLookup = null;
- bool success = false; // WebData 100390
- _hasRows = false; // reset HasRows
+ // In the case of not closing the reader, null out the metadata AFTER
+ // CloseInternal finishes - since CloseInternal may go to the wire
+ // and use the metadata.
+ ClearMetaData();
+ more = success;
+ return TdsOperationStatus.Done;
+ }
- // if we are specifically only processing a single result, then read all the results off the wire and detach
- if (IsCommandBehavior(CommandBehavior.SingleResult))
+ if (_parser != null)
+ {
+ // if there are more rows, then skip them, the user wants the next result
+ bool moreRows = true;
+ while (moreRows)
{
- result = TryCloseInternal(closeReader: false);
+ result = TryReadInternal(false, out moreRows);
if (result != TdsOperationStatus.Done)
{
+ // don't reset set the timeout value
more = false;
return result;
}
-
- // In the case of not closing the reader, null out the metadata AFTER
- // CloseInternal finishes - since CloseInternal may go to the wire
- // and use the metadata.
- ClearMetaData();
- more = success;
- return TdsOperationStatus.Done;
}
+ }
- if (_parser != null)
+ // we may be done, so continue only if we have not detached ourselves from the parser
+ if (_parser != null)
+ {
+ bool moreResults;
+ result = TryHasMoreResults(out moreResults);
+ if (result != TdsOperationStatus.Done)
{
- // if there are more rows, then skip them, the user wants the next result
- bool moreRows = true;
- while (moreRows)
+ more = false;
+ return result;
+ }
+ if (moreResults)
+ {
+ _metaDataConsumed = false;
+ _browseModeInfoConsumed = false;
+
+ switch (_altRowStatus)
{
- result = TryReadInternal(false, out moreRows);
- if (result != TdsOperationStatus.Done)
- {
- // don't reset set the timeout value
- more = false;
- return result;
- }
+ case ALTROWSTATUS.AltRow:
+ int altRowId;
+ result = _parser.TryGetAltRowId(_stateObj, out altRowId);
+ if (result != TdsOperationStatus.Done)
+ {
+ more = false;
+ return result;
+ }
+ _SqlMetaDataSet altMetaDataSet = _altMetaDataSetCollection.GetAltMetaData(altRowId);
+ if (altMetaDataSet != null)
+ {
+ _metaData = altMetaDataSet;
+ }
+ Debug.Assert((_metaData != null), "Can't match up altrowmetadata");
+ break;
+ case ALTROWSTATUS.Done:
+ // restore the row-metaData
+ _metaData = _altMetaDataSetCollection.metaDataSet;
+ Debug.Assert(_altRowStatus == ALTROWSTATUS.Done, "invalid AltRowStatus");
+ _altRowStatus = ALTROWSTATUS.Null;
+ break;
+ default:
+ result = TryConsumeMetaData();
+ if (result != TdsOperationStatus.Done)
+ {
+ more = false;
+ return result;
+ }
+ if (_metaData == null)
+ {
+ more = false;
+ return TdsOperationStatus.Done;
+ }
+ break;
}
- }
- // we may be done, so continue only if we have not detached ourselves from the parser
- if (_parser != null)
+ success = true;
+ }
+ else
{
- bool moreResults;
- result = TryHasMoreResults(out moreResults);
+ // detach the parser from this reader now
+ result = TryCloseInternal(closeReader: false);
if (result != TdsOperationStatus.Done)
{
more = false;
- return result;
+ return TdsOperationStatus.Done;
}
- if (moreResults)
- {
- _metaDataConsumed = false;
- _browseModeInfoConsumed = false;
-
- switch (_altRowStatus)
- {
- case ALTROWSTATUS.AltRow:
- int altRowId;
- result = _parser.TryGetAltRowId(_stateObj, out altRowId);
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return result;
- }
- _SqlMetaDataSet altMetaDataSet = _altMetaDataSetCollection.GetAltMetaData(altRowId);
- if (altMetaDataSet != null)
- {
- _metaData = altMetaDataSet;
- }
- Debug.Assert((_metaData != null), "Can't match up altrowmetadata");
- break;
- case ALTROWSTATUS.Done:
- // restore the row-metaData
- _metaData = _altMetaDataSetCollection.metaDataSet;
- Debug.Assert(_altRowStatus == ALTROWSTATUS.Done, "invalid AltRowStatus");
- _altRowStatus = ALTROWSTATUS.Null;
- break;
- default:
- result = TryConsumeMetaData();
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return result;
- }
- if (_metaData == null)
- {
- more = false;
- return TdsOperationStatus.Done;
- }
- break;
- }
- success = true;
- }
- else
+ // In the case of not closing the reader, null out the metadata AFTER
+ // CloseInternal finishes - since CloseInternal may go to the wire
+ // and use the metadata.
+ result = TrySetMetaData(null, false);
+ if (result != TdsOperationStatus.Done)
{
- // detach the parser from this reader now
- result = TryCloseInternal(closeReader: false);
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return TdsOperationStatus.Done;
- }
-
- // In the case of not closing the reader, null out the metadata AFTER
- // CloseInternal finishes - since CloseInternal may go to the wire
- // and use the metadata.
- result = TrySetMetaData(null, false);
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return result;
- }
+ more = false;
+ return result;
}
}
- else
- {
- // Clear state in case of Read calling CloseInternal() then user calls NextResult()
- // and the case where the Read() above will do essentially the same thing.
- ClearMetaData();
- }
-
- more = success;
- return TdsOperationStatus.Done;
}
-#if NETFRAMEWORK && DEBUG
- finally
+ else
{
- tdsReliabilitySection.Stop();
+ // Clear state in case of Read calling CloseInternal() then user calls NextResult()
+ // and the case where the Read() above will do essentially the same thing.
+ ClearMetaData();
}
-#endif
+
+ more = success;
+ return TdsOperationStatus.Done;
}
finally
{
@@ -3755,140 +3604,117 @@ private TdsOperationStatus TryReadInternal(bool setTimeout, out bool more)
try
{
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
+ TdsOperationStatus result;
+ statistics = SqlStatistics.StartTimer(Statistics);
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
+ if (_parser != null)
{
-#endif
- TdsOperationStatus result;
- statistics = SqlStatistics.StartTimer(Statistics);
-
- if (_parser != null)
+ if (setTimeout)
{
- if (setTimeout)
- {
- SetTimeout(_defaultTimeoutMilliseconds);
- }
- if (_sharedState._dataReady)
+ SetTimeout(_defaultTimeoutMilliseconds);
+ }
+ if (_sharedState._dataReady)
+ {
+ result = TryCleanPartialRead();
+ if (result != TdsOperationStatus.Done)
{
- result = TryCleanPartialRead();
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return result;
- }
+ more = false;
+ return result;
}
+ }
- // clear out our buffers
- SqlBuffer.Clear(_data);
+ // clear out our buffers
+ SqlBuffer.Clear(_data);
- _sharedState._nextColumnHeaderToRead = 0;
- _sharedState._nextColumnDataToRead = 0;
- _sharedState._columnDataBytesRemaining = -1; // unknown
- _lastColumnWithDataChunkRead = -1;
+ _sharedState._nextColumnHeaderToRead = 0;
+ _sharedState._nextColumnDataToRead = 0;
+ _sharedState._columnDataBytesRemaining = -1; // unknown
+ _lastColumnWithDataChunkRead = -1;
- if (!_haltRead)
+ if (!_haltRead)
+ {
+ bool moreRows;
+ result = TryHasMoreRows(out moreRows);
+ if (result != TdsOperationStatus.Done)
{
- bool moreRows;
- result = TryHasMoreRows(out moreRows);
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return result;
- }
- if (moreRows)
+ more = false;
+ return result;
+ }
+ if (moreRows)
+ {
+ // read the row from the backend (unless it's an altrow were the marker is already inside the altrow ...)
+ while (_stateObj.HasPendingData)
{
- // read the row from the backend (unless it's an altrow were the marker is already inside the altrow ...)
- while (_stateObj.HasPendingData)
+ if (_altRowStatus != ALTROWSTATUS.AltRow)
{
- if (_altRowStatus != ALTROWSTATUS.AltRow)
+ // if this is an ordinary row we let the run method consume the ROW token
+ result = _parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out _sharedState._dataReady);
+ if (result != TdsOperationStatus.Done)
{
- // if this is an ordinary row we let the run method consume the ROW token
- result = _parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out _sharedState._dataReady);
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return result;
- }
- if (_sharedState._dataReady)
- {
- break;
- }
+ more = false;
+ return result;
}
- else
+ if (_sharedState._dataReady)
{
- // ALTROW token and AltrowId are already consumed ...
- Debug.Assert(_altRowStatus == ALTROWSTATUS.AltRow, "invalid AltRowStatus");
- _altRowStatus = ALTROWSTATUS.Done;
- _sharedState._dataReady = true;
break;
}
}
- if (_sharedState._dataReady)
+ else
{
- _haltRead = IsCommandBehavior(CommandBehavior.SingleRow);
- more = true;
- return TdsOperationStatus.Done;
+ // ALTROW token and AltrowId are already consumed ...
+ Debug.Assert(_altRowStatus == ALTROWSTATUS.AltRow, "invalid AltRowStatus");
+ _altRowStatus = ALTROWSTATUS.Done;
+ _sharedState._dataReady = true;
+ break;
}
}
-
- if (!_stateObj.HasPendingData)
+ if (_sharedState._dataReady)
{
- result = TryCloseInternal(closeReader: false);
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return result;
- }
+ _haltRead = IsCommandBehavior(CommandBehavior.SingleRow);
+ more = true;
+ return TdsOperationStatus.Done;
}
}
- else
+
+ if (!_stateObj.HasPendingData)
{
- // if we did not get a row and halt is true, clean off rows of result
- // success must be false - or else we could have just read off row and set
- // halt to true
- bool moreRows;
- result = TryHasMoreRows(out moreRows);
+ result = TryCloseInternal(closeReader: false);
if (result != TdsOperationStatus.Done)
{
more = false;
return result;
}
- while (moreRows)
+ }
+ }
+ else
+ {
+ // if we did not get a row and halt is true, clean off rows of result
+ // success must be false - or else we could have just read off row and set
+ // halt to true
+ bool moreRows;
+ result = TryHasMoreRows(out moreRows);
+ if (result != TdsOperationStatus.Done)
+ {
+ more = false;
+ return result;
+ }
+ while (moreRows)
+ {
+ // if we are in SingleRow mode, and we've read the first row,
+ // read the rest of the rows, if any
+ while (_stateObj.HasPendingData && !_sharedState._dataReady)
{
- // if we are in SingleRow mode, and we've read the first row,
- // read the rest of the rows, if any
- while (_stateObj.HasPendingData && !_sharedState._dataReady)
- {
- result = _parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out _sharedState._dataReady);
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return result;
- }
- }
-
- if (_sharedState._dataReady)
+ result = _parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out _sharedState._dataReady);
+ if (result != TdsOperationStatus.Done)
{
- result = TryCleanPartialRead();
- if (result != TdsOperationStatus.Done)
- {
- more = false;
- return result;
- }
+ more = false;
+ return result;
}
+ }
- // clear out our buffers
- SqlBuffer.Clear(_data);
-
- _sharedState._nextColumnHeaderToRead = 0;
-
- result = TryHasMoreRows(out moreRows);
+ if (_sharedState._dataReady)
+ {
+ result = TryCleanPartialRead();
if (result != TdsOperationStatus.Done)
{
more = false;
@@ -3896,38 +3722,44 @@ private TdsOperationStatus TryReadInternal(bool setTimeout, out bool more)
}
}
- // reset haltRead
- _haltRead = false;
+ // clear out our buffers
+ SqlBuffer.Clear(_data);
+
+ _sharedState._nextColumnHeaderToRead = 0;
+
+ result = TryHasMoreRows(out moreRows);
+ if (result != TdsOperationStatus.Done)
+ {
+ more = false;
+ return result;
+ }
}
+
+ // reset haltRead
+ _haltRead = false;
}
- else if (IsClosed)
- {
- throw ADP.DataReaderClosed(nameof(Read));
- }
- more = false;
+ }
+ else if (IsClosed)
+ {
+ throw ADP.DataReaderClosed(nameof(Read));
+ }
+ more = false;
#if DEBUG
- if ((!_sharedState._dataReady) && (_stateObj.HasPendingData))
+ if ((!_sharedState._dataReady) && (_stateObj.HasPendingData))
+ {
+ byte token;
+ result = _stateObj.TryPeekByte(out token);
+ if (result != TdsOperationStatus.Done)
{
- byte token;
- result = _stateObj.TryPeekByte(out token);
- if (result != TdsOperationStatus.Done)
- {
- return result;
- }
-
- Debug.Assert(TdsParser.IsValidTdsToken(token), $"DataReady is false, but next token is invalid: {token,-2:X2}");
+ return result;
}
-#endif
- return TdsOperationStatus.Done;
- }
-#if NETFRAMEWORK && DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
+ Debug.Assert(TdsParser.IsValidTdsToken(token), $"DataReady is false, but next token is invalid: {token,-2:X2}");
}
-#endif //DEBUG
+#endif
+
+ return TdsOperationStatus.Done;
}
catch (OutOfMemoryException e)
{
@@ -3987,41 +3819,22 @@ private void ReadColumn(int i, bool setTimeout = true, bool allowPartiallyReadCo
private TdsOperationStatus TryReadColumn(int i, bool setTimeout, bool allowPartiallyReadColumn = false, bool forStreaming = false)
{
CheckDataIsReady(columnIndex: i, permitAsync: true, allowPartiallyReadColumn: allowPartiallyReadColumn, methodName: null);
-#if NETFRAMEWORK
- RuntimeHelpers.PrepareConstrainedRegions();
-#endif
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
- {
-#endif
- Debug.Assert(_sharedState._nextColumnHeaderToRead <= _metaData.Length, "_sharedState._nextColumnHeaderToRead too large");
- Debug.Assert(_sharedState._nextColumnDataToRead <= _metaData.Length, "_sharedState._nextColumnDataToRead too large");
-
- if (setTimeout)
- {
- SetTimeout(_defaultTimeoutMilliseconds);
- }
-
- TdsOperationStatus result = TryReadColumnInternal(i, readHeaderOnly: false, forStreaming: forStreaming);
- if (result != TdsOperationStatus.Done)
- {
- return result;
- }
-
- Debug.Assert(_data[i] != null, " data buffer is null?");
- }
-#if NETFRAMEWORK && DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif
+ Debug.Assert(_sharedState._nextColumnHeaderToRead <= _metaData.Length, "_sharedState._nextColumnHeaderToRead too large");
+ Debug.Assert(_sharedState._nextColumnDataToRead <= _metaData.Length, "_sharedState._nextColumnDataToRead too large");
+
+ if (setTimeout)
+ {
+ SetTimeout(_defaultTimeoutMilliseconds);
+ }
+
+ TdsOperationStatus result = TryReadColumnInternal(i, readHeaderOnly: false, forStreaming: forStreaming);
+ if (result != TdsOperationStatus.Done)
+ {
+ return result;
+ }
+
+ Debug.Assert(_data[i] != null, " data buffer is null?");
return TdsOperationStatus.Done;
}
@@ -4065,27 +3878,7 @@ private TdsOperationStatus TryReadColumnHeader(int i)
throw SQL.InvalidRead();
}
-#if NETFRAMEWORK
- RuntimeHelpers.PrepareConstrainedRegions();
-#endif
-#if NETFRAMEWORK && DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
- {
-#endif
- return TryReadColumnInternal(i, readHeaderOnly: true);
- }
-#if NETFRAMEWORK && DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif
+ return TryReadColumnInternal(i, readHeaderOnly: true);
}
internal TdsOperationStatus TryReadColumnInternal(int i, bool readHeaderOnly = false, bool forStreaming = false)
@@ -5556,25 +5349,7 @@ private static Task GetFieldValueAsyncExecute(Task task, object state)
{
if (reader.IsCommandBehavior(CommandBehavior.SequentialAccess) && reader._sharedState._dataReady)
{
- bool internalReadSuccess = false;
-#if NETFRAMEWORK
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
- {
-#endif
- internalReadSuccess = reader.TryReadColumnInternal(context._columnIndex, readHeaderOnly: true) == TdsOperationStatus.Done;
- }
-#if NETFRAMEWORK
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif
-
+ bool internalReadSuccess = reader.TryReadColumnInternal(context._columnIndex, readHeaderOnly: true) == TdsOperationStatus.Done;
if (internalReadSuccess)
{
return Task.FromResult(reader.GetFieldValueFromSqlBufferInternal(reader._data[columnIndex], reader._metaData[columnIndex], isAsync: true));
diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.netcore.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.netcore.cs
index f6e639c5f9..71ef5e9381 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.netcore.cs
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.netcore.cs
@@ -21,27 +21,6 @@ internal struct SNIErrorDetails
public Exception exception;
}
- internal struct ReliabilitySection
- {
- ///
- /// This is a no-op in netcore version. Only needed for merging with netfx codebase.
- ///
- [Conditional("NETFRAMEWORK")]
- internal static void Assert(string message)
- {
- }
-
- [Conditional("NETFRAMEWORK")]
- internal void Start()
- {
- }
-
- [Conditional("NETFRAMEWORK")]
- internal void Stop()
- {
- }
- }
-
internal static void FillGuidBytes(Guid guid, Span buffer) => guid.TryWriteBytes(buffer);
internal static void FillDoubleBytes(double value, Span buffer) => BinaryPrimitives.TryWriteInt64LittleEndian(buffer, BitConverter.DoubleToInt64Bits(value));
diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs
index 4f655dc403..3f32dbeebd 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs
@@ -298,8 +298,6 @@ internal bool ValidateSNIConnection()
// This method should only be called by ReadSni! If not - it may have problems with timeouts!
private void ReadSniError(TdsParserStateObject stateObj, uint error)
{
- TdsParser.ReliabilitySection.Assert("unreliable call to ReadSniSyncError"); // you need to setup for a thread abort somewhere before you call this method
-
if (TdsEnums.SNI_WAIT_TIMEOUT == error)
{
Debug.Assert(_syncOverAsync, "Should never reach here with async on!");
@@ -753,8 +751,6 @@ public void WriteAsyncCallback(IntPtr key, PacketHandle packet, uint sniError)
//
internal void WriteSecureString(SecureString secureString)
{
- TdsParser.ReliabilitySection.Assert("unreliable call to WriteSecureString"); // you need to setup for a thread abort somewhere before you call this method
-
Debug.Assert(_securePasswords[0] == null || _securePasswords[1] == null, "There are more than two secure passwords");
int index = _securePasswords[0] != null ? 1 : 0;
@@ -829,8 +825,6 @@ internal Task WaitForAccumulatedWrites()
// and then the buffer is re-initialized in flush() and then the byte is put in the buffer.
internal void WriteByte(byte b)
{
- TdsParser.ReliabilitySection.Assert("unreliable call to WriteByte"); // you need to setup for a thread abort somewhere before you call this method
-
Debug.Assert(_outBytesUsed <= _outBuff.Length, "ERROR - TDSParser: _outBytesUsed > _outBuff.Length");
// check to make sure we haven't used the full amount of space available in the buffer, if so, flush it
@@ -866,8 +860,6 @@ private Task WriteBytes(ReadOnlySpan b, int len, int offsetBuffer, bool ca
}
try
{
- TdsParser.ReliabilitySection.Assert("unreliable call to WriteByteArray"); // you need to setup for a thread abort somewhere before you call this method
-
bool async = _parser._asyncWrite; // NOTE: We are capturing this now for the assert after the Task is returned, since WritePacket will turn off async if there is an exception
Debug.Assert(async || _asyncWriteCount == 0);
// Do we have to send out in packet size chunks, or can we rely on netlib layer to break it up?
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs
index ac9e1a79c7..040ef93723 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs
@@ -2059,58 +2059,40 @@ private Task WriteRowSourceToServerAsync(int columnCount, CancellationToken ctok
RuntimeHelpers.PrepareConstrainedRegions();
try
{
-#if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else // !DEBUG
- {
-#endif //DEBUG
- bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_connection);
- WriteRowSourceToServerCommon(columnCount); //this is common in both sync and async
- Task resultTask = WriteToServerInternalAsync(ctoken); // resultTask is null for sync, but Task for async.
- if (resultTask != null)
- {
- finishedSynchronously = false;
- return resultTask.ContinueWith(
- static (Task task, object state) =>
+ bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_connection);
+ WriteRowSourceToServerCommon(columnCount); //this is common in both sync and async
+ Task resultTask = WriteToServerInternalAsync(ctoken); // resultTask is null for sync, but Task for async.
+ if (resultTask != null)
+ {
+ finishedSynchronously = false;
+ return resultTask.ContinueWith(
+ static (Task task, object state) =>
+ {
+ SqlBulkCopy sqlBulkCopy = (SqlBulkCopy)state;
+ try
{
- SqlBulkCopy sqlBulkCopy = (SqlBulkCopy)state;
- try
+ sqlBulkCopy.AbortTransaction(); // if there is one, on success transactions will be commited
+ }
+ finally
+ {
+ sqlBulkCopy._isBulkCopyingInProgress = false;
+ if (sqlBulkCopy._parser != null)
{
- sqlBulkCopy.AbortTransaction(); // if there is one, on success transactions will be commited
+ sqlBulkCopy._parser._asyncWrite = false;
}
- finally
+ if (sqlBulkCopy._parserLock != null)
{
- sqlBulkCopy._isBulkCopyingInProgress = false;
- if (sqlBulkCopy._parser != null)
- {
- sqlBulkCopy._parser._asyncWrite = false;
- }
- if (sqlBulkCopy._parserLock != null)
- {
- sqlBulkCopy._parserLock.Release();
- sqlBulkCopy._parserLock = null;
- }
+ sqlBulkCopy._parserLock.Release();
+ sqlBulkCopy._parserLock = null;
}
- return task;
- },
- state: this,
- scheduler: TaskScheduler.Default
- ).Unwrap();
- }
- return null;
- }
-
-#if DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
+ }
+ return task;
+ },
+ state: this,
+ scheduler: TaskScheduler.Default
+ ).Unwrap();
}
-#endif //DEBUG
+ return null;
}
catch (System.OutOfMemoryException e)
{
@@ -2796,13 +2778,6 @@ private void CopyBatchesAsyncContinuedOnError(bool cleanupParser)
RuntimeHelpers.PrepareConstrainedRegions();
try
{
-#if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#endif //DEBUG
if ((cleanupParser) && (_parser != null) && (_stateObj != null))
{
_parser._asyncWrite = false;
@@ -2815,13 +2790,6 @@ private void CopyBatchesAsyncContinuedOnError(bool cleanupParser)
{
CleanUpStateObject();
}
-#if DEBUG
- }
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif //DEBUG
}
catch (OutOfMemoryException)
{
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs
index f66d9a811d..579c64181a 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs
@@ -534,22 +534,8 @@ private SqlCommand(SqlCommand from) : this()
RuntimeHelpers.PrepareConstrainedRegions();
try
{
-#if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#endif //DEBUG
- // cleanup
- Unprepare();
-#if DEBUG
- }
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif //DEBUG
+ // cleanup
+ Unprepare();
}
catch (System.OutOfMemoryException)
{
@@ -1077,23 +1063,7 @@ public override void Prepare()
}
}
-#if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
- {
-#endif //DEBUG
- InternalPrepare();
- }
-#if DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif //DEBUG
+ InternalPrepare();
}
catch (System.OutOfMemoryException e)
{
@@ -1261,48 +1231,31 @@ public override void Cancel()
RuntimeHelpers.PrepareConstrainedRegions();
try
{
-#if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- tdsReliabilitySection.Start();
-#else
- {
-#endif //DEBUG
- bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
-
- if (!_pendingCancel)
- { // Do nothing if aleady pending.
- // Before attempting actual cancel, set the _pendingCancel flag to false.
- // This denotes to other thread before obtaining stateObject from the
- // session pool that there is another thread wishing to cancel.
- // The period in question is between entering the ExecuteAPI and obtaining
- // a stateObject.
- _pendingCancel = true;
-
- TdsParserStateObject stateObj = _stateObj;
- if (stateObj != null)
- {
- stateObj.Cancel(ObjectID);
- }
- else
+ bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(_activeConnection);
+
+ if (!_pendingCancel)
+ { // Do nothing if aleady pending.
+ // Before attempting actual cancel, set the _pendingCancel flag to false.
+ // This denotes to other thread before obtaining stateObject from the
+ // session pool that there is another thread wishing to cancel.
+ // The period in question is between entering the ExecuteAPI and obtaining
+ // a stateObject.
+ _pendingCancel = true;
+
+ TdsParserStateObject stateObj = _stateObj;
+ if (stateObj != null)
+ {
+ stateObj.Cancel(ObjectID);
+ }
+ else
+ {
+ SqlDataReader reader = connection.FindLiveReader(this);
+ if (reader != null)
{
- SqlDataReader reader = connection.FindLiveReader(this);
- if (reader != null)
- {
- reader.Cancel(ObjectID);
- }
+ reader.Cancel(ObjectID);
}
}
}
-#if DEBUG
- finally
- {
- tdsReliabilitySection.Stop();
- }
-#endif //DEBUG
}
catch (System.OutOfMemoryException e)
{
@@ -1685,27 +1638,10 @@ private void BeginExecuteNonQueryInternalReadStage(TaskCompletionSource