diff --git a/src/libraries/System.Data.Common/ref/System.Data.Common.cs b/src/libraries/System.Data.Common/ref/System.Data.Common.cs index 7f8e500b823aa..e46b17a11d302 100644 --- a/src/libraries/System.Data.Common/ref/System.Data.Common.cs +++ b/src/libraries/System.Data.Common/ref/System.Data.Common.cs @@ -2796,6 +2796,7 @@ void System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader read void System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) { } public System.Data.SqlTypes.SqlGuid ToSqlGuid() { throw null; } public override string ToString() { throw null; } + public static SqlBinary WrapBytes(byte[] bytes) { throw null; } } [System.Xml.Serialization.XmlSchemaProviderAttribute("GetXsdType")] public partial struct SqlBoolean : System.Data.SqlTypes.INullable, System.IComparable, System.Xml.Serialization.IXmlSerializable, System.IEquatable @@ -3147,6 +3148,8 @@ void System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter wri public System.Data.SqlTypes.SqlString ToSqlString() { throw null; } public override string ToString() { throw null; } public static System.Data.SqlTypes.SqlDecimal Truncate(System.Data.SqlTypes.SqlDecimal n, int position) { throw null; } + [System.CLSCompliantAttribute(false)] + public int WriteTdsValue(System.Span destination) { throw null; } } [System.Xml.Serialization.XmlSchemaProviderAttribute("GetXsdType")] public partial struct SqlDouble : System.Data.SqlTypes.INullable, System.IComparable, System.Xml.Serialization.IXmlSerializable, System.IEquatable @@ -3501,7 +3504,9 @@ public partial struct SqlMoney : System.Data.SqlTypes.INullable, System.ICompara public static System.Data.SqlTypes.SqlBoolean Equals(System.Data.SqlTypes.SqlMoney x, System.Data.SqlTypes.SqlMoney y) { throw null; } public bool Equals(System.Data.SqlTypes.SqlMoney other) { throw null; } public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? value) { throw null; } + public static System.Data.SqlTypes.SqlMoney FromTdsValue(long value) { throw null; } public override int GetHashCode() { throw null; } + public long GetTdsValue() { throw null; } public static System.Xml.XmlQualifiedName GetXsdType(System.Xml.Schema.XmlSchemaSet schemaSet) { throw null; } public static System.Data.SqlTypes.SqlBoolean GreaterThan(System.Data.SqlTypes.SqlMoney x, System.Data.SqlTypes.SqlMoney y) { throw null; } public static System.Data.SqlTypes.SqlBoolean GreaterThanOrEqual(System.Data.SqlTypes.SqlMoney x, System.Data.SqlTypes.SqlMoney y) { throw null; } diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLBinary.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLBinary.cs index d684a8a410b72..3da0280967889 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLBinary.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLBinary.cs @@ -464,6 +464,11 @@ public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) return new XmlQualifiedName("base64Binary", XmlSchema.Namespace); } + public static SqlBinary WrapBytes(byte[] bytes) + { + return new SqlBinary(bytes, ignored: true); + } + /// /// Represents a null value that can be assigned to the property of an /// instance of the class. diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs index db617033a8f76..29f2b8eb5f3d5 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs @@ -1152,6 +1152,26 @@ private decimal ToDecimal() } } + [CLSCompliant(false)] + public int WriteTdsValue(Span destination) + { + if (IsNull) + { + throw new SqlNullValueException(); + } + + if (destination.Length < 4) + { + throw new ArgumentOutOfRangeException(nameof(destination)); + } + + destination[0] = _data1; + destination[1] = _data2; + destination[2] = _data3; + destination[3] = _data4; + return 4; + } + // Implicit conversion from Decimal to SqlDecimal public static implicit operator SqlDecimal(decimal x) { diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLMoney.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLMoney.cs index 50b9403146e3c..2e3dd347a22c4 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLMoney.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLMoney.cs @@ -163,14 +163,6 @@ public long ToInt64() return ret; } - internal long ToSqlInternalRepresentation() - { - if (IsNull) - throw new SqlNullValueException(); - - return _value; - } - public int ToInt32() { return checked((int)(ToInt64())); @@ -216,6 +208,14 @@ public override string ToString() return money.ToString("#0.00##", null); } + public long GetTdsValue() + { + if (IsNull) + throw new SqlNullValueException(); + + return _value; + } + public static SqlMoney Parse(string s) { // Try parsing the format of '#0.00##' generated by ToString() by using the @@ -249,6 +249,11 @@ public static SqlMoney Parse(string s) return money; } + public static SqlMoney FromTdsValue(long value) + { + return new SqlMoney(value, 0); + } + // Unary operators public static SqlMoney operator -(SqlMoney x) { diff --git a/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlBinaryTest.cs b/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlBinaryTest.cs index 8655d3feea003..f072e09a93096 100644 --- a/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlBinaryTest.cs +++ b/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlBinaryTest.cs @@ -250,5 +250,23 @@ public void GetXsdTypeTest() XmlQualifiedName qualifiedName = SqlBinary.GetXsdType(null); Assert.Equal("base64Binary", qualifiedName.Name); } + + [Fact] + public void WrapBytes() + { + byte[] bytes = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; + + SqlBinary binary = SqlBinary.WrapBytes(bytes); + + Assert.Equal(bytes[5], binary[5]); + + // verify that changing the byte[] that was passed in also changes the value in the SqlBinary instance + bytes[5] = 0xFF; + Assert.Equal(bytes[5], binary[5]); + + SqlBinary nullBinary = SqlBinary.WrapBytes(null); + + Assert.True(nullBinary.IsNull); + } } } diff --git a/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlDecimalTest.cs b/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlDecimalTest.cs index b9807ebcc102e..7513730ee0416 100644 --- a/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlDecimalTest.cs +++ b/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlDecimalTest.cs @@ -582,5 +582,26 @@ public void ReadWriteXmlTest() Assert.Throws(() => ReadWriteXmlTestInternal(xml3, test3, "BA03")); Assert.Equal(typeof(FormatException), ex.InnerException.GetType()); } + + [Fact] + public void WriteTdsValue() + { + uint[] array = new uint[5]; + Span span = array; + Array.Clear(array, 0, array.Length); + + int count = SqlDecimal.MaxValue.WriteTdsValue(span); + Assert.Equal(4, count); + Assert.Equal(0xFFFFFFFFu, array[0]); + Assert.Equal(0x098a223fu, array[1]); + Assert.Equal(0x5a86c47au, array[2]); + Assert.Equal(0x4b3b4ca8u, array[3]); + + Assert.Equal(0u, array[4]); + + array = new uint[3]; + Assert.Throws( () => { _ = SqlDecimal.MaxValue.WriteTdsValue(array); } ); + + } } } diff --git a/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlMoneyTest.cs b/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlMoneyTest.cs index db99ba4de6691..d067e9ce32899 100644 --- a/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlMoneyTest.cs +++ b/src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlMoneyTest.cs @@ -428,5 +428,22 @@ public void GetXsdTypeTest() XmlQualifiedName qualifiedName = SqlMoney.GetXsdType(null); Assert.Equal("decimal", qualifiedName.Name); } + + [Fact] + public void GetTdsValue() + { + Assert.Equal(long.MaxValue,SqlMoney.MaxValue.GetTdsValue()); + Assert.Equal(long.MinValue, SqlMoney.MinValue.GetTdsValue()); + Assert.Equal((long)0, new SqlMoney(0).GetTdsValue()); + Assert.Throws(() => SqlMoney.Null.GetTdsValue()); + } + + [Fact] + public void FromTdsValue() + { + Assert.Equal(SqlMoney.FromTdsValue(long.MaxValue), SqlMoney.MaxValue); + Assert.Equal(SqlMoney.FromTdsValue(long.MinValue), SqlMoney.MinValue); + Assert.Equal(SqlMoney.FromTdsValue(0), new SqlMoney(0)); + } } }