Skip to content

Commit 3d66569

Browse files
Wraith2kant2002
authored andcommitted
Add net8 compatibility (dotnet#1934)
1 parent d463683 commit 3d66569

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,9 @@
646646
<Compile Include="Microsoft\Data\SqlClient\TdsParserHelperClasses.cs" />
647647
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.cs" />
648648
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectManaged.cs" />
649+
</ItemGroup>
650+
<!--This will exclude SqlTypeWorkarounds.netcore.cs from Net7 and greater versions-->
651+
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
649652
<Compile Include="Microsoft\Data\SqlTypes\SqlTypeWorkarounds.netcore.cs" />
650653
</ItemGroup>
651654
<!-- Windows only -->

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,7 +6092,11 @@ internal bool TryReadSqlValue(SqlBuffer value, SqlMetaDataPriv md, int length, T
60926092
}
60936093
else
60946094
{
6095+
#if NET7_0_OR_GREATER
6096+
value.SqlBinary = SqlBinary.WrapBytes(b);
6097+
#else
60956098
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); // doesn't copy the byte array
6099+
#endif
60966100
}
60976101
break;
60986102

@@ -6385,7 +6389,11 @@ internal bool TryReadSqlValueInternal(SqlBuffer value, byte tdsType, int length,
63856389
{
63866390
return false;
63876391
}
6392+
#if NET7_0_OR_GREATER
6393+
value.SqlBinary = SqlBinary.WrapBytes(b);
6394+
#else
63886395
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true);
6396+
#endif
63896397

63906398
break;
63916399
}
@@ -7251,18 +7259,23 @@ internal byte[] SerializeSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj)
72517259
else
72527260
bytes[current++] = 0;
72537261

7254-
uint data1, data2, data3, data4;
7255-
SqlTypeWorkarounds.SqlDecimalExtractData(d, out data1, out data2, out data3, out data4);
7256-
byte[] bytesPart = SerializeUnsignedInt(data1, stateObj);
7262+
7263+
Span<uint> data = stackalloc uint[4];
7264+
#if NET7_0_OR_GREATER
7265+
d.WriteTdsValue(data);
7266+
#else
7267+
SqlTypeWorkarounds.SqlDecimalExtractData(d, out data[0], out data[1], out data[2], out data[3]);
7268+
#endif
7269+
byte[] bytesPart = SerializeUnsignedInt(data[0], stateObj);
72577270
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
72587271
current += 4;
7259-
bytesPart = SerializeUnsignedInt(data2, stateObj);
7272+
bytesPart = SerializeUnsignedInt(data[1], stateObj);
72607273
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
72617274
current += 4;
7262-
bytesPart = SerializeUnsignedInt(data3, stateObj);
7275+
bytesPart = SerializeUnsignedInt(data[2], stateObj);
72637276
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
72647277
current += 4;
7265-
bytesPart = SerializeUnsignedInt(data4, stateObj);
7278+
bytesPart = SerializeUnsignedInt(data[3], stateObj);
72667279
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
72677280

72687281
return bytes;
@@ -7276,12 +7289,16 @@ internal void WriteSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj)
72767289
else
72777290
stateObj.WriteByte(0);
72787291

7279-
uint data1, data2, data3, data4;
7280-
SqlTypeWorkarounds.SqlDecimalExtractData(d, out data1, out data2, out data3, out data4);
7281-
WriteUnsignedInt(data1, stateObj);
7282-
WriteUnsignedInt(data2, stateObj);
7283-
WriteUnsignedInt(data3, stateObj);
7284-
WriteUnsignedInt(data4, stateObj);
7292+
Span<uint> data = stackalloc uint[4];
7293+
#if NET7_0_OR_GREATER
7294+
d.WriteTdsValue(data);
7295+
#else
7296+
SqlTypeWorkarounds.SqlDecimalExtractData(d, out data[0], out data[1], out data[2], out data[3]);
7297+
#endif
7298+
WriteUnsignedInt(data[0], stateObj);
7299+
WriteUnsignedInt(data[1], stateObj);
7300+
WriteUnsignedInt(data[2], stateObj);
7301+
WriteUnsignedInt(data[3], stateObj);
72857302
}
72867303

72877304
private byte[] SerializeDecimal(decimal value, TdsParserStateObject stateObj)

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.netcore.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,31 +122,5 @@ private struct SqlBinaryCaster
122122
internal SqlBinaryLookalike Fake;
123123
}
124124
#endregion
125-
126-
#region Work around inability to access SqlGuid.ctor(byte[], bool)
127-
internal static SqlGuid SqlGuidCtor(byte[] value, bool ignored)
128-
{
129-
// Construct a SqlGuid without allocating/copying the byte[]. This provides
130-
// the same behavior as SqlGuid.ctor(byte[], bool).
131-
var c = default(SqlGuidCaster);
132-
c.Fake._value = value;
133-
return c.Real;
134-
}
135-
136-
[StructLayout(LayoutKind.Sequential)]
137-
private struct SqlGuidLookalike
138-
{
139-
internal byte[] _value;
140-
}
141-
142-
[StructLayout(LayoutKind.Explicit)]
143-
private struct SqlGuidCaster
144-
{
145-
[FieldOffset(0)]
146-
internal SqlGuid Real;
147-
[FieldOffset(0)]
148-
internal SqlGuidLookalike Fake;
149-
}
150-
#endregion
151125
}
152126
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,7 +3158,11 @@ private static SqlMoney GetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedG
31583158

31593159
long temp = getters.GetInt64(sink, ordinal);
31603160
sink.ProcessMessagesAndThrow();
3161+
#if NETCOREAPP && NET7_0_OR_GREATER
3162+
return SqlMoney.FromTdsValue(temp);
3163+
#else
31613164
return SqlTypeWorkarounds.SqlMoneyCtor(temp, 1 /* ignored */ );
3165+
#endif
31623166
}
31633167

31643168
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
36383642
sink.ProcessMessagesAndThrow();
36393643
}
36403644

3645+
#if NET7_0_OR_GREATER
3646+
setters.SetInt64(sink, ordinal, value.GetTdsValue());
3647+
#else
36413648
setters.SetInt64(sink, ordinal, SqlTypeWorkarounds.SqlMoneyToSqlInternalRepresentation(value));
3649+
#endif
36423650
}
36433651
sink.ProcessMessagesAndThrow();
36443652
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,11 @@ internal SqlMoney SqlMoney
886886
{
887887
return SqlMoney.Null;
888888
}
889+
#if NETCOREAPP && NET7_0_OR_GREATER
890+
return SqlMoney.FromTdsValue(_value._int64);
891+
#else
889892
return SqlTypeWorkarounds.SqlMoneyCtor(_value._int64, 1/*ignored*/);
893+
#endif
890894
}
891895
return (SqlMoney)SqlValue; // anything else we haven't thought of goes through boxing.
892896
}

0 commit comments

Comments
 (0)