From e58febc07a0b92c69abf458653e8ccf3f4077454 Mon Sep 17 00:00:00 2001 From: Wraith2 Date: Sun, 26 Feb 2023 00:50:41 +0000 Subject: [PATCH 1/2] add net8 ifdef protected use of net8 apis and remove unused SqlGuid workaround. --- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 41 +++++++++++++------ .../SqlTypes/SqlTypeWorkarounds.netcore.cs | 28 +------------ .../Data/SqlClient/Server/ValueUtilsSmi.cs | 8 ++++ .../src/Microsoft/Data/SqlClient/SqlBuffer.cs | 4 ++ 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index 49327a4cdd..4268524c75 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -6085,7 +6085,11 @@ internal bool TryReadSqlValue(SqlBuffer value, SqlMetaDataPriv md, int length, T } else { +#if NET8_0_OR_GREATER + value.SqlBinary = SqlBinary.WrapBytes(b); +#else value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); // doesn't copy the byte array +#endif } break; @@ -6378,7 +6382,11 @@ internal bool TryReadSqlValueInternal(SqlBuffer value, byte tdsType, int length, { return false; } +#if NET8_0_OR_GREATER + value.SqlBinary = SqlBinary.WrapBytes(b); +#else value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); +#endif break; } @@ -7244,18 +7252,23 @@ internal byte[] SerializeSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj) else bytes[current++] = 0; - uint data1, data2, data3, data4; - SqlTypeWorkarounds.SqlDecimalExtractData(d, out data1, out data2, out data3, out data4); - byte[] bytesPart = SerializeUnsignedInt(data1, stateObj); + + Span data = stackalloc uint[4]; +#if NET8_0_OR_GREATER + d.WriteTdsValue(data); +#else + SqlTypeWorkarounds.SqlDecimalExtractData(d, out data[0], out data[1], out data[2], out data[3]); +#endif + byte[] bytesPart = SerializeUnsignedInt(data[0], stateObj); Buffer.BlockCopy(bytesPart, 0, bytes, current, 4); current += 4; - bytesPart = SerializeUnsignedInt(data2, stateObj); + bytesPart = SerializeUnsignedInt(data[1], stateObj); Buffer.BlockCopy(bytesPart, 0, bytes, current, 4); current += 4; - bytesPart = SerializeUnsignedInt(data3, stateObj); + bytesPart = SerializeUnsignedInt(data[2], stateObj); Buffer.BlockCopy(bytesPart, 0, bytes, current, 4); current += 4; - bytesPart = SerializeUnsignedInt(data4, stateObj); + bytesPart = SerializeUnsignedInt(data[3], stateObj); Buffer.BlockCopy(bytesPart, 0, bytes, current, 4); return bytes; @@ -7269,12 +7282,16 @@ internal void WriteSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj) else stateObj.WriteByte(0); - uint data1, data2, data3, data4; - SqlTypeWorkarounds.SqlDecimalExtractData(d, out data1, out data2, out data3, out data4); - WriteUnsignedInt(data1, stateObj); - WriteUnsignedInt(data2, stateObj); - WriteUnsignedInt(data3, stateObj); - WriteUnsignedInt(data4, stateObj); + Span data = stackalloc uint[4]; +#if NET8_0_OR_GREATER + d.WriteTdsValue(data); +#else + SqlTypeWorkarounds.SqlDecimalExtractData(d, out data[0], out data[1], out data[2], out data[3]); +#endif + WriteUnsignedInt(data[0], stateObj); + WriteUnsignedInt(data[1], stateObj); + WriteUnsignedInt(data[2], stateObj); + WriteUnsignedInt(data[3], stateObj); } private byte[] SerializeDecimal(decimal value, TdsParserStateObject stateObj) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs index 43ed8bbee4..91aff4ebdf 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs @@ -7,6 +7,7 @@ namespace Microsoft.Data.SqlTypes { +#if !NET8_0_OR_GREATER /// /// This type provides workarounds for the separation between System.Data.Common /// and Microsoft.Data.SqlClient. The latter wants to access internal members of the former, and @@ -122,31 +123,6 @@ private struct SqlBinaryCaster internal SqlBinaryLookalike Fake; } #endregion - - #region Work around inability to access SqlGuid.ctor(byte[], bool) - internal static SqlGuid SqlGuidCtor(byte[] value, bool ignored) - { - // Construct a SqlGuid without allocating/copying the byte[]. This provides - // the same behavior as SqlGuid.ctor(byte[], bool). - var c = default(SqlGuidCaster); - c.Fake._value = value; - return c.Real; - } - - [StructLayout(LayoutKind.Sequential)] - private struct SqlGuidLookalike - { - internal byte[] _value; - } - - [StructLayout(LayoutKind.Explicit)] - private struct SqlGuidCaster - { - [FieldOffset(0)] - internal SqlGuid Real; - [FieldOffset(0)] - internal SqlGuidLookalike Fake; - } - #endregion } +#endif } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs index 0548bbf126..d24b756dbe 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs @@ -3158,7 +3158,11 @@ private static SqlMoney GetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedG long temp = getters.GetInt64(sink, ordinal); sink.ProcessMessagesAndThrow(); +#if NETCOREAPP && NET8_0_OR_GREATER + return SqlMoney.FromTdsValue(temp); +#else return SqlTypeWorkarounds.SqlMoneyCtor(temp, 1 /* ignored */ ); +#endif } private static SqlXml GetSqlXml_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiContext context) @@ -3638,7 +3642,11 @@ private static void SetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedSette sink.ProcessMessagesAndThrow(); } +#if NET8_0_OR_GREATER + setters.SetInt64(sink, ordinal, value.GetTdsValue()); +#else setters.SetInt64(sink, ordinal, SqlTypeWorkarounds.SqlMoneyToSqlInternalRepresentation(value)); +#endif } sink.ProcessMessagesAndThrow(); } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs index 686c5157ef..544b45fb9a 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs @@ -886,7 +886,11 @@ internal SqlMoney SqlMoney { return SqlMoney.Null; } +#if NETCOREAPP && NET8_0_OR_GREATER + return SqlMoney.FromTdsValue(_value._int64); +#else return SqlTypeWorkarounds.SqlMoneyCtor(_value._int64, 1/*ignored*/); +#endif } return (SqlMoney)SqlValue; // anything else we haven't thought of goes through boxing. } From 37fb9a0e55779e9af66af463004a42cbc679b093 Mon Sep 17 00:00:00 2001 From: JRahnama Date: Mon, 8 May 2023 11:00:47 -0700 Subject: [PATCH 2/2] changing Net8 to Net7 and adjusting csproj to exclude 'SqlTypeWorkarounds.netcore.cs' --- .../netcore/src/Microsoft.Data.SqlClient.csproj | 3 +++ .../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs | 8 ++++---- .../Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs | 2 -- .../src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs | 4 ++-- .../src/Microsoft/Data/SqlClient/SqlBuffer.cs | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) 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 bc755d3486..adcd941afb 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -648,6 +648,9 @@ + + + diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index 4268524c75..d255f7ba74 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -6085,7 +6085,7 @@ internal bool TryReadSqlValue(SqlBuffer value, SqlMetaDataPriv md, int length, T } else { -#if NET8_0_OR_GREATER +#if NET7_0_OR_GREATER value.SqlBinary = SqlBinary.WrapBytes(b); #else value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); // doesn't copy the byte array @@ -6382,7 +6382,7 @@ internal bool TryReadSqlValueInternal(SqlBuffer value, byte tdsType, int length, { return false; } -#if NET8_0_OR_GREATER +#if NET7_0_OR_GREATER value.SqlBinary = SqlBinary.WrapBytes(b); #else value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); @@ -7254,7 +7254,7 @@ internal byte[] SerializeSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj) Span data = stackalloc uint[4]; -#if NET8_0_OR_GREATER +#if NET7_0_OR_GREATER d.WriteTdsValue(data); #else SqlTypeWorkarounds.SqlDecimalExtractData(d, out data[0], out data[1], out data[2], out data[3]); @@ -7283,7 +7283,7 @@ internal void WriteSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj) stateObj.WriteByte(0); Span data = stackalloc uint[4]; -#if NET8_0_OR_GREATER +#if NET7_0_OR_GREATER d.WriteTdsValue(data); #else SqlTypeWorkarounds.SqlDecimalExtractData(d, out data[0], out data[1], out data[2], out data[3]); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs index 91aff4ebdf..7ce991530a 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs @@ -7,7 +7,6 @@ namespace Microsoft.Data.SqlTypes { -#if !NET8_0_OR_GREATER /// /// This type provides workarounds for the separation between System.Data.Common /// and Microsoft.Data.SqlClient. The latter wants to access internal members of the former, and @@ -124,5 +123,4 @@ private struct SqlBinaryCaster } #endregion } -#endif } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs index d24b756dbe..61bcec417f 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs @@ -3158,7 +3158,7 @@ private static SqlMoney GetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedG long temp = getters.GetInt64(sink, ordinal); sink.ProcessMessagesAndThrow(); -#if NETCOREAPP && NET8_0_OR_GREATER +#if NETCOREAPP && NET7_0_OR_GREATER return SqlMoney.FromTdsValue(temp); #else return SqlTypeWorkarounds.SqlMoneyCtor(temp, 1 /* ignored */ ); @@ -3642,7 +3642,7 @@ private static void SetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedSette sink.ProcessMessagesAndThrow(); } -#if NET8_0_OR_GREATER +#if NET7_0_OR_GREATER setters.SetInt64(sink, ordinal, value.GetTdsValue()); #else setters.SetInt64(sink, ordinal, SqlTypeWorkarounds.SqlMoneyToSqlInternalRepresentation(value)); diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs index 544b45fb9a..9cfc077da4 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs @@ -886,7 +886,7 @@ internal SqlMoney SqlMoney { return SqlMoney.Null; } -#if NETCOREAPP && NET8_0_OR_GREATER +#if NETCOREAPP && NET7_0_OR_GREATER return SqlMoney.FromTdsValue(_value._int64); #else return SqlTypeWorkarounds.SqlMoneyCtor(_value._int64, 1/*ignored*/);