Skip to content

Commit

Permalink
Add SqlTypes apis for SqlClient (#72724)
Browse files Browse the repository at this point in the history
* SqlMoney changes

* SqlDecimal changes

* SqlBinary changes

* add CLS compliance attributes
  • Loading branch information
Wraith2 committed Jul 27, 2022
1 parent d944592 commit ada5503
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/libraries/System.Data.Common/ref/System.Data.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<System.Data.SqlTypes.SqlBoolean>
Expand Down Expand Up @@ -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<uint> destination) { throw null; }
}
[System.Xml.Serialization.XmlSchemaProviderAttribute("GetXsdType")]
public partial struct SqlDouble : System.Data.SqlTypes.INullable, System.IComparable, System.Xml.Serialization.IXmlSerializable, System.IEquatable<System.Data.SqlTypes.SqlDouble>
Expand Down Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Represents a null value that can be assigned to the <see cref='Value'/> property of an
/// instance of the <see cref='SqlBinary'/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,26 @@ private decimal ToDecimal()
}
}

[CLSCompliant(false)]
public int WriteTdsValue(Span<uint> 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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -582,5 +582,26 @@ public void ReadWriteXmlTest()
Assert.Throws<InvalidOperationException>(() => ReadWriteXmlTestInternal(xml3, test3, "BA03"));
Assert.Equal(typeof(FormatException), ex.InnerException.GetType());
}

[Fact]
public void WriteTdsValue()
{
uint[] array = new uint[5];
Span<uint> 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<ArgumentOutOfRangeException>( () => { _ = SqlDecimal.MaxValue.WriteTdsValue(array); } );

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SqlNullValueException>(() => 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));
}
}
}

0 comments on commit ada5503

Please sign in to comment.