diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 4a1e9a7742..f3f7a58789 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -202,6 +202,9 @@ Microsoft\Data\SqlClient\SqlBulkCopyColumnOrderHintCollection.cs + + Microsoft\Data\SqlClient\SqlCachedBuffer.cs + Microsoft\Data\SqlClient\SqlClientEncryptionAlgorithm.cs @@ -496,7 +499,6 @@ - diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index d1443d0754..cf1ac8b7d2 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -273,6 +273,9 @@ Microsoft\Data\SqlClient\SqlBulkCopyColumnOrderHintCollection.cs + + Microsoft\Data\SqlClient\SqlCachedBuffer.cs + Microsoft\Data\SqlClient\SqlClientEncryptionAlgorithm.cs @@ -456,7 +459,6 @@ - diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs deleted file mode 100644 index 000d2647cc..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs +++ /dev/null @@ -1,148 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Collections.Generic; -using System.Data.SqlTypes; -using System.Diagnostics; -using System.IO; -using System.Runtime.CompilerServices; -using System.Xml; -using Microsoft.Data.SqlTypes; - -namespace Microsoft.Data.SqlClient -{ - // Caches the bytes returned from partial length prefixed datatypes, like XML - sealed internal class SqlCachedBuffer : System.Data.SqlTypes.INullable - { - public static readonly SqlCachedBuffer Null = new SqlCachedBuffer(); - private const int _maxChunkSize = 2048; // Arbitrary value for chunk size. Revisit this later for better perf - - private List _cachedBytes; - - private SqlCachedBuffer() - { - // For constructing Null - } - - private SqlCachedBuffer(List cachedBytes) - { - _cachedBytes = cachedBytes; - } - - internal List CachedBytes - { - get { return _cachedBytes; } - } - - // Reads off from the network buffer and caches bytes. Only reads one column value in the current row. - static internal bool TryCreate(SqlMetaDataPriv metadata, TdsParser parser, TdsParserStateObject stateObj, out SqlCachedBuffer buffer) - { - int cb = 0; - ulong plplength; - byte[] byteArr; - - List cachedBytes = new List(); - buffer = null; - - // the very first length is already read. - if (!parser.TryPlpBytesLeft(stateObj, out plplength)) - { - return false; - } - // For now we only handle Plp data from the parser directly. - Debug.Assert(metadata.metaType.IsPlp, "SqlCachedBuffer call on a non-plp data"); - do - { - if (plplength == 0) - break; - do - { - cb = (plplength > (ulong)_maxChunkSize) ? _maxChunkSize : (int)plplength; - byteArr = new byte[cb]; - if (!stateObj.TryReadPlpBytes(ref byteArr, 0, cb, out cb)) - { - return false; - } - Debug.Assert(cb == byteArr.Length); - if (cachedBytes.Count == 0) - { - // Add the Byte order mark if needed if we read the first array - AddByteOrderMark(byteArr, cachedBytes); - } - cachedBytes.Add(byteArr); - plplength -= (ulong)cb; - } while (plplength > 0); - if (!parser.TryPlpBytesLeft(stateObj, out plplength)) - { - return false; - } - } while (plplength > 0); - Debug.Assert(stateObj._longlen == 0 && stateObj._longlenleft == 0); - - buffer = new SqlCachedBuffer(cachedBytes); - return true; - } - - private static void AddByteOrderMark(byte[] byteArr, List cachedBytes) - { - // Need to find out if we should add byte order mark or not. - // We need to add this if we are getting ntext xml, not if we are getting binary xml - // Binary Xml always begins with the bytes 0xDF and 0xFF - // If we aren't getting these, then we are getting unicode xml - if ((byteArr.Length < 2) || (byteArr[0] != 0xDF) || (byteArr[1] != 0xFF)) - { - Debug.Assert(cachedBytes.Count == 0); - cachedBytes.Add(TdsEnums.XMLUNICODEBOMBYTES); - } - } - - internal Stream ToStream() - { - return new SqlCachedStream(this); - } - - override public string ToString() - { - if (IsNull) - throw new SqlNullValueException(); - - if (_cachedBytes.Count == 0) - { - return String.Empty; - } - SqlXml sxml = new SqlXml(ToStream()); - return sxml.Value; - } - - internal SqlString ToSqlString() - { - if (IsNull) - return SqlString.Null; - string str = ToString(); - return new SqlString(str); - } - - internal SqlXml ToSqlXml() - { - SqlXml sx = new SqlXml(ToStream()); - return sx; - } - - // Prevent inlining so that reflection calls are not moved to caller that may be in a different assembly that may have a different grant set. - [MethodImpl(MethodImplOptions.NoInlining)] - internal XmlReader ToXmlReader() - { - return SqlTypeWorkarounds.SqlXmlCreateSqlXmlReader(ToStream(), closeInput: false, async: false); - } - - public bool IsNull - { - get - { - return (_cachedBytes == null) ? true : false; - } - } - } -} diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs similarity index 79% rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs index be5e1e330c..5a4de1e191 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs @@ -13,12 +13,12 @@ namespace Microsoft.Data.SqlClient { // Caches the bytes returned from partial length prefixed datatypes, like XML - sealed internal class SqlCachedBuffer : System.Data.SqlTypes.INullable + internal sealed class SqlCachedBuffer : INullable { - public static readonly SqlCachedBuffer Null = new SqlCachedBuffer(); - private const int _maxChunkSize = 2048; // Arbitrary value for chunk size. Revisit this later for better perf + public static readonly SqlCachedBuffer Null = new(); + private const int MaxChunkSize = 2048; // Arbitrary value for chunk size. Revisit this later for better perf - private List _cachedBytes; + private readonly List _cachedBytes; private SqlCachedBuffer() { @@ -30,23 +30,20 @@ private SqlCachedBuffer(List cachedBytes) _cachedBytes = cachedBytes; } - internal List CachedBytes - { - get { return _cachedBytes; } - } + internal List CachedBytes =>_cachedBytes; - // Reads off from the network buffer and caches bytes. Only reads one column value in the current row. + /// + /// Reads off from the network buffer and caches bytes. Only reads one column value in the current row. + /// internal static bool TryCreate(SqlMetaDataPriv metadata, TdsParser parser, TdsParserStateObject stateObj, out SqlCachedBuffer buffer) { - int cb = 0; - ulong plplength; byte[] byteArr; - List cachedBytes = new List(); + List cachedBytes = new(); buffer = null; // the very first length is already read. - if (!parser.TryPlpBytesLeft(stateObj, out plplength)) + if (!parser.TryPlpBytesLeft(stateObj, out ulong plplength)) { return false; } @@ -55,10 +52,12 @@ internal static bool TryCreate(SqlMetaDataPriv metadata, TdsParser parser, TdsPa do { if (plplength == 0) + { break; + } do { - cb = (plplength > (ulong)_maxChunkSize) ? _maxChunkSize : (int)plplength; + int cb = (plplength > (ulong)MaxChunkSize) ? MaxChunkSize : (int)plplength; byteArr = new byte[cb]; if (!stateObj.TryReadPlpBytes(ref byteArr, 0, cb, out cb)) { @@ -105,27 +104,32 @@ internal Stream ToStream() override public string ToString() { if (IsNull) + { throw new SqlNullValueException(); + } if (_cachedBytes.Count == 0) { return string.Empty; } - SqlXml sxml = new SqlXml(ToStream()); + SqlXml sxml = new(ToStream()); return sxml.Value; } internal SqlString ToSqlString() { if (IsNull) + { return SqlString.Null; + } + string str = ToString(); return new SqlString(str); } internal SqlXml ToSqlXml() { - SqlXml sx = new SqlXml(ToStream()); + SqlXml sx = new(ToStream()); return sx; } @@ -136,14 +140,6 @@ internal XmlReader ToXmlReader() return SqlTypeWorkarounds.SqlXmlCreateSqlXmlReader(ToStream(), closeInput: false, async: false); } - public bool IsNull - { - get - { - return (_cachedBytes == null) ? true : false; - } - } - - + public bool IsNull => _cachedBytes == null; } }