Skip to content

Commit 7584037

Browse files
authored
Merge | Netfx SqlTypeWorkarounds (#3448)
* Merge SqlBinary and SqlMoney helpers into SqlTypeWorkarounds * Merge SqlGuid conversion * Merge (/rewrite) SqlDecimal conversion * Remove netfx file * Fix bug in decimal conversion * Rewrite SqlMoney to internal implementation to use member rather than method call * UNIT TESTS :explosion: 🎉 🆒 * Remove member variable access for SqlDecimal * Add notes for documentation of internal methods Replace SqlMoneyToLong code to use ToInternalRepresentation
1 parent 06f7eca commit 7584037

File tree

9 files changed

+461
-393
lines changed

9 files changed

+461
-393
lines changed

src/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageVersion Include="System.Memory" Version="4.5.5" />
1111
<PackageVersion Include="System.Data.Common" Version="4.3.0" />
1212
<PackageVersion Include="System.Text.Encodings.Web" Version="8.0.0" />
13+
<PackageVersion Include="System.ValueTuple" Version="4.6.1" />
1314
</ItemGroup>
1415
<!-- NetFx and NetCore project dependencies -->
1516
<ItemGroup>

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,6 @@
930930
<Compile Include="Microsoft\Data\SqlClient\TdsParser.netfx.cs" />
931931
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.netfx.cs" />
932932
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
933-
<Compile Include="Microsoft\Data\SqlTypes\SqlTypeWorkarounds.netfx.cs" />
934933
</ItemGroup>
935934
<!-- Resources -->
936935
<ItemGroup>
@@ -971,9 +970,11 @@
971970
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" />
972971
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" />
973972
<PackageReference Include="System.Buffers" />
973+
<PackageReference Include="System.Memory" />
974974
<PackageReference Include="System.Security.Cryptography.Pkcs" />
975975
<PackageReference Include="System.Text.Encodings.Web" />
976976
<PackageReference Include="System.Text.Json" />
977+
<PackageReference Include="System.ValueTuple" />
977978
</ItemGroup>
978979
<Import Project="$(CommonSourceRoot)tools\targets\GenerateResourceStringsSource.targets" />
979980
<Import Project="$(NetFxSource)tools\targets\GenerateThisAssemblyCs.targets" />

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6415,7 +6415,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
64156415
case TdsEnums.SQLUNIQUEID:
64166416
{
64176417
Debug.Assert(length == 16, "invalid length for SqlGuid type!");
6418-
value.SqlGuid = SqlTypeWorkarounds.SqlGuidCtor(unencryptedBytes, true); // doesn't copy the byte array
6418+
value.SqlGuid = SqlTypeWorkarounds.ByteArrayToSqlGuid(unencryptedBytes);
64196419
break;
64206420
}
64216421

@@ -6436,7 +6436,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
64366436
unencryptedBytes = bytes;
64376437
}
64386438

6439-
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(unencryptedBytes, true); // doesn't copy the byte array
6439+
value.SqlBinary = SqlTypeWorkarounds.ByteArrayToSqlBinary(unencryptedBytes);
64406440
break;
64416441
}
64426442

@@ -6661,7 +6661,7 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
66616661
}
66626662
else
66636663
{
6664-
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); // doesn't copy the byte array
6664+
value.SqlBinary = SqlTypeWorkarounds.ByteArrayToSqlBinary(b);
66656665
}
66666666
break;
66676667

@@ -6676,7 +6676,7 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
66766676

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

66816681
// Extract the metadata from the payload and set it as the vector attributes
66826682
// in the SqlBuffer. This metadata is further used when constructing a SqlVector
@@ -7005,8 +7005,8 @@ internal TdsOperationStatus TryReadSqlValueInternal(SqlBuffer value, byte tdsTyp
70057005
{
70067006
return result;
70077007
}
7008-
value.SqlBinary = SqlTypeWorkarounds.SqlBinaryCtor(b, true); // doesn't copy the byte array
7009-
7008+
7009+
value.SqlBinary = SqlTypeWorkarounds.ByteArrayToSqlBinary(b);
70107010
break;
70117011
}
70127012

@@ -7886,22 +7886,27 @@ internal byte[] SerializeSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj)
78867886

78877887
// sign
78887888
if (d.IsPositive)
7889+
{
78897890
bytes[current++] = 1;
7891+
}
78907892
else
7893+
{
78917894
bytes[current++] = 0;
7895+
}
78927896

7893-
uint data1, data2, data3, data4;
7894-
SqlTypeWorkarounds.SqlDecimalExtractData(d, out data1, out data2, out data3, out data4);
7895-
byte[] bytesPart = SerializeUnsignedInt(data1, stateObj);
7897+
Span<uint> data = stackalloc uint[4];
7898+
SqlTypeWorkarounds.SqlDecimalWriteTdsValue(d, data);
7899+
7900+
byte[] bytesPart = SerializeUnsignedInt(data[0], stateObj);
78967901
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
78977902
current += 4;
7898-
bytesPart = SerializeUnsignedInt(data2, stateObj);
7903+
bytesPart = SerializeUnsignedInt(data[1], stateObj);
78997904
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
79007905
current += 4;
7901-
bytesPart = SerializeUnsignedInt(data3, stateObj);
7906+
bytesPart = SerializeUnsignedInt(data[2], stateObj);
79027907
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
79037908
current += 4;
7904-
bytesPart = SerializeUnsignedInt(data4, stateObj);
7909+
bytesPart = SerializeUnsignedInt(data[3], stateObj);
79057910
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
79067911

79077912
return bytes;
@@ -7911,16 +7916,21 @@ internal void WriteSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj)
79117916
{
79127917
// sign
79137918
if (d.IsPositive)
7919+
{
79147920
stateObj.WriteByte(1);
7921+
}
79157922
else
7923+
{
79167924
stateObj.WriteByte(0);
7925+
}
7926+
7927+
Span<uint> data = stackalloc uint[4];
7928+
SqlTypeWorkarounds.SqlDecimalWriteTdsValue(d, data);
79177929

7918-
uint data1, data2, data3, data4;
7919-
SqlTypeWorkarounds.SqlDecimalExtractData(d, out data1, out data2, out data3, out data4);
7920-
WriteUnsignedInt(data1, stateObj);
7921-
WriteUnsignedInt(data2, stateObj);
7922-
WriteUnsignedInt(data3, stateObj);
7923-
WriteUnsignedInt(data4, stateObj);
7930+
WriteUnsignedInt(data[0], stateObj);
7931+
WriteUnsignedInt(data[1], stateObj);
7932+
WriteUnsignedInt(data[2], stateObj);
7933+
WriteUnsignedInt(data[3], stateObj);
79247934
}
79257935

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

0 commit comments

Comments
 (0)