Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add | Add net8 compatibility #1934

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,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
Original file line number Diff line number Diff line change
Expand Up @@ -6085,7 +6085,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 @@ -6378,7 +6382,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 @@ -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<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 @@ -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<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
Original file line number Diff line number Diff line change
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
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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