Skip to content

Merge | Netfx SqlTypeWorkarounds #3448

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="System.Data.Common" Version="4.3.0" />
<PackageVersion Include="System.Text.Encodings.Web" Version="8.0.0" />
<PackageVersion Include="System.ValueTuple" Version="4.6.1" />
</ItemGroup>
<!-- NetFx and NetCore project dependencies -->
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,6 @@
<Compile Include="Microsoft\Data\SqlClient\TdsParser.netfx.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.netfx.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
<Compile Include="Microsoft\Data\SqlTypes\SqlTypeWorkarounds.netfx.cs" />
</ItemGroup>
<!-- Resources -->
<ItemGroup>
Expand Down Expand Up @@ -971,9 +970,11 @@
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" />
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" />
<PackageReference Include="System.Buffers" />
<PackageReference Include="System.Memory" />
<PackageReference Include="System.Security.Cryptography.Pkcs" />
<PackageReference Include="System.Text.Encodings.Web" />
<PackageReference Include="System.Text.Json" />
<PackageReference Include="System.ValueTuple" />
</ItemGroup>
<Import Project="$(CommonSourceRoot)tools\targets\GenerateResourceStringsSource.targets" />
<Import Project="$(NetFxSource)tools\targets\GenerateThisAssemblyCs.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6416,7 +6416,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
case TdsEnums.SQLUNIQUEID:
{
Debug.Assert(length == 16, "invalid length for SqlGuid type!");
value.SqlGuid = SqlTypeWorkarounds.SqlGuidCtor(unencryptedBytes, true); // doesn't copy the byte array
value.SqlGuid = SqlTypeWorkarounds.ByteArrayToSqlGuid(unencryptedBytes);
break;
}

Expand All @@ -6437,7 +6437,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
unencryptedBytes = bytes;
}

value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(unencryptedBytes, true); // doesn't copy the byte array
value.SqlBinary = SqlTypeWorkarounds.ByteArrayToSqlBinary(unencryptedBytes);
break;
}

Expand Down Expand Up @@ -6662,7 +6662,7 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
}
else
{
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); // doesn't copy the byte array
value.SqlBinary = SqlTypeWorkarounds.ByteArrayToSqlBinary(b);
}
break;

Expand All @@ -6677,7 +6677,7 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,

// Internally, we use Sqlbinary to deal with varbinary data and store it in
// SqlBuffer as SqlBinary value.
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true);
value.SqlBinary = SqlTypeWorkarounds.ByteArrayToSqlBinary(b);

// Extract the metadata from the payload and set it as the vector attributes
// in the SqlBuffer. This metadata is further used when constructing a SqlVector
Expand Down Expand Up @@ -7006,8 +7006,8 @@ internal TdsOperationStatus TryReadSqlValueInternal(SqlBuffer value, byte tdsTyp
{
return result;
}
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); // doesn't copy the byte array


value.SqlBinary = SqlTypeWorkarounds.ByteArrayToSqlBinary(b);
break;
}

Expand Down Expand Up @@ -7887,22 +7887,27 @@ internal byte[] SerializeSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj)

// sign
if (d.IsPositive)
{
bytes[current++] = 1;
}
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];
SqlTypeWorkarounds.SqlDecimalWriteTdsValue(d, data);

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 @@ -7912,16 +7917,21 @@ internal void WriteSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj)
{
// sign
if (d.IsPositive)
{
stateObj.WriteByte(1);
}
else
{
stateObj.WriteByte(0);
}

Span<uint> data = stackalloc uint[4];
SqlTypeWorkarounds.SqlDecimalWriteTdsValue(d, data);

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);
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
Loading
Loading