Skip to content

Commit

Permalink
Add net8 compatibility (dotnet#1934)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 authored and kant2002 committed Jun 29, 2023
1 parent d463683 commit 3d66569
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
Expand Up @@ -646,6 +646,9 @@
<Compile Include="Microsoft\Data\SqlClient\TdsParserHelperClasses.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectManaged.cs" />
</ItemGroup>
<!--This will exclude SqlTypeWorkarounds.netcore.cs from Net7 and greater versions-->
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
<Compile Include="Microsoft\Data\SqlTypes\SqlTypeWorkarounds.netcore.cs" />
</ItemGroup>
<!-- Windows only -->
Expand Down
Expand Up @@ -6092,7 +6092,11 @@ internal bool TryReadSqlValue(SqlBuffer value, SqlMetaDataPriv md, int length, T
}
else
{
#if NET7_0_OR_GREATER
value.SqlBinary = SqlBinary.WrapBytes(b);
#else
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); // doesn't copy the byte array
#endif
}
break;

Expand Down Expand Up @@ -6385,7 +6389,11 @@ internal bool TryReadSqlValueInternal(SqlBuffer value, byte tdsType, int length,
{
return false;
}
#if NET7_0_OR_GREATER
value.SqlBinary = SqlBinary.WrapBytes(b);
#else
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true);
#endif

break;
}
Expand Down Expand Up @@ -7251,18 +7259,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<uint> data = stackalloc uint[4];
#if NET7_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;
Expand All @@ -7276,12 +7289,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<uint> data = stackalloc uint[4];
#if NET7_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)
Expand Down
Expand Up @@ -122,31 +122,5 @@ 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
}
}
Expand Up @@ -3158,7 +3158,11 @@ private static SqlMoney GetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedG

long temp = getters.GetInt64(sink, ordinal);
sink.ProcessMessagesAndThrow();
#if NETCOREAPP && NET7_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)
Expand Down Expand Up @@ -3638,7 +3642,11 @@ private static void SetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedSette
sink.ProcessMessagesAndThrow();
}

#if NET7_0_OR_GREATER
setters.SetInt64(sink, ordinal, value.GetTdsValue());
#else
setters.SetInt64(sink, ordinal, SqlTypeWorkarounds.SqlMoneyToSqlInternalRepresentation(value));
#endif
}
sink.ProcessMessagesAndThrow();
}
Expand Down
Expand Up @@ -886,7 +886,11 @@ internal SqlMoney SqlMoney
{
return SqlMoney.Null;
}
#if NETCOREAPP && NET7_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.
}
Expand Down

0 comments on commit 3d66569

Please sign in to comment.