Skip to content

Commit ada5503

Browse files
authored
Add SqlTypes apis for SqlClient (#72724)
* SqlMoney changes * SqlDecimal changes * SqlBinary changes * add CLS compliance attributes
1 parent d944592 commit ada5503

File tree

7 files changed

+99
-8
lines changed

7 files changed

+99
-8
lines changed

src/libraries/System.Data.Common/ref/System.Data.Common.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,7 @@ void System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader read
27962796
void System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) { }
27972797
public System.Data.SqlTypes.SqlGuid ToSqlGuid() { throw null; }
27982798
public override string ToString() { throw null; }
2799+
public static SqlBinary WrapBytes(byte[] bytes) { throw null; }
27992800
}
28002801
[System.Xml.Serialization.XmlSchemaProviderAttribute("GetXsdType")]
28012802
public partial struct SqlBoolean : System.Data.SqlTypes.INullable, System.IComparable, System.Xml.Serialization.IXmlSerializable, System.IEquatable<System.Data.SqlTypes.SqlBoolean>
@@ -3147,6 +3148,8 @@ void System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter wri
31473148
public System.Data.SqlTypes.SqlString ToSqlString() { throw null; }
31483149
public override string ToString() { throw null; }
31493150
public static System.Data.SqlTypes.SqlDecimal Truncate(System.Data.SqlTypes.SqlDecimal n, int position) { throw null; }
3151+
[System.CLSCompliantAttribute(false)]
3152+
public int WriteTdsValue(System.Span<uint> destination) { throw null; }
31503153
}
31513154
[System.Xml.Serialization.XmlSchemaProviderAttribute("GetXsdType")]
31523155
public partial struct SqlDouble : System.Data.SqlTypes.INullable, System.IComparable, System.Xml.Serialization.IXmlSerializable, System.IEquatable<System.Data.SqlTypes.SqlDouble>
@@ -3501,7 +3504,9 @@ public partial struct SqlMoney : System.Data.SqlTypes.INullable, System.ICompara
35013504
public static System.Data.SqlTypes.SqlBoolean Equals(System.Data.SqlTypes.SqlMoney x, System.Data.SqlTypes.SqlMoney y) { throw null; }
35023505
public bool Equals(System.Data.SqlTypes.SqlMoney other) { throw null; }
35033506
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? value) { throw null; }
3507+
public static System.Data.SqlTypes.SqlMoney FromTdsValue(long value) { throw null; }
35043508
public override int GetHashCode() { throw null; }
3509+
public long GetTdsValue() { throw null; }
35053510
public static System.Xml.XmlQualifiedName GetXsdType(System.Xml.Schema.XmlSchemaSet schemaSet) { throw null; }
35063511
public static System.Data.SqlTypes.SqlBoolean GreaterThan(System.Data.SqlTypes.SqlMoney x, System.Data.SqlTypes.SqlMoney y) { throw null; }
35073512
public static System.Data.SqlTypes.SqlBoolean GreaterThanOrEqual(System.Data.SqlTypes.SqlMoney x, System.Data.SqlTypes.SqlMoney y) { throw null; }

src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLBinary.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,11 @@ public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet)
464464
return new XmlQualifiedName("base64Binary", XmlSchema.Namespace);
465465
}
466466

467+
public static SqlBinary WrapBytes(byte[] bytes)
468+
{
469+
return new SqlBinary(bytes, ignored: true);
470+
}
471+
467472
/// <summary>
468473
/// Represents a null value that can be assigned to the <see cref='Value'/> property of an
469474
/// instance of the <see cref='SqlBinary'/> class.

src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,26 @@ private decimal ToDecimal()
11521152
}
11531153
}
11541154

1155+
[CLSCompliant(false)]
1156+
public int WriteTdsValue(Span<uint> destination)
1157+
{
1158+
if (IsNull)
1159+
{
1160+
throw new SqlNullValueException();
1161+
}
1162+
1163+
if (destination.Length < 4)
1164+
{
1165+
throw new ArgumentOutOfRangeException(nameof(destination));
1166+
}
1167+
1168+
destination[0] = _data1;
1169+
destination[1] = _data2;
1170+
destination[2] = _data3;
1171+
destination[3] = _data4;
1172+
return 4;
1173+
}
1174+
11551175
// Implicit conversion from Decimal to SqlDecimal
11561176
public static implicit operator SqlDecimal(decimal x)
11571177
{

src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLMoney.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,6 @@ public long ToInt64()
163163
return ret;
164164
}
165165

166-
internal long ToSqlInternalRepresentation()
167-
{
168-
if (IsNull)
169-
throw new SqlNullValueException();
170-
171-
return _value;
172-
}
173-
174166
public int ToInt32()
175167
{
176168
return checked((int)(ToInt64()));
@@ -216,6 +208,14 @@ public override string ToString()
216208
return money.ToString("#0.00##", null);
217209
}
218210

211+
public long GetTdsValue()
212+
{
213+
if (IsNull)
214+
throw new SqlNullValueException();
215+
216+
return _value;
217+
}
218+
219219
public static SqlMoney Parse(string s)
220220
{
221221
// Try parsing the format of '#0.00##' generated by ToString() by using the
@@ -249,6 +249,11 @@ public static SqlMoney Parse(string s)
249249
return money;
250250
}
251251

252+
public static SqlMoney FromTdsValue(long value)
253+
{
254+
return new SqlMoney(value, 0);
255+
}
256+
252257
// Unary operators
253258
public static SqlMoney operator -(SqlMoney x)
254259
{

src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlBinaryTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,5 +250,23 @@ public void GetXsdTypeTest()
250250
XmlQualifiedName qualifiedName = SqlBinary.GetXsdType(null);
251251
Assert.Equal("base64Binary", qualifiedName.Name);
252252
}
253+
254+
[Fact]
255+
public void WrapBytes()
256+
{
257+
byte[] bytes = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
258+
259+
SqlBinary binary = SqlBinary.WrapBytes(bytes);
260+
261+
Assert.Equal(bytes[5], binary[5]);
262+
263+
// verify that changing the byte[] that was passed in also changes the value in the SqlBinary instance
264+
bytes[5] = 0xFF;
265+
Assert.Equal(bytes[5], binary[5]);
266+
267+
SqlBinary nullBinary = SqlBinary.WrapBytes(null);
268+
269+
Assert.True(nullBinary.IsNull);
270+
}
253271
}
254272
}

src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlDecimalTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,5 +582,26 @@ public void ReadWriteXmlTest()
582582
Assert.Throws<InvalidOperationException>(() => ReadWriteXmlTestInternal(xml3, test3, "BA03"));
583583
Assert.Equal(typeof(FormatException), ex.InnerException.GetType());
584584
}
585+
586+
[Fact]
587+
public void WriteTdsValue()
588+
{
589+
uint[] array = new uint[5];
590+
Span<uint> span = array;
591+
Array.Clear(array, 0, array.Length);
592+
593+
int count = SqlDecimal.MaxValue.WriteTdsValue(span);
594+
Assert.Equal(4, count);
595+
Assert.Equal(0xFFFFFFFFu, array[0]);
596+
Assert.Equal(0x098a223fu, array[1]);
597+
Assert.Equal(0x5a86c47au, array[2]);
598+
Assert.Equal(0x4b3b4ca8u, array[3]);
599+
600+
Assert.Equal(0u, array[4]);
601+
602+
array = new uint[3];
603+
Assert.Throws<ArgumentOutOfRangeException>( () => { _ = SqlDecimal.MaxValue.WriteTdsValue(array); } );
604+
605+
}
585606
}
586607
}

src/libraries/System.Data.Common/tests/System/Data/SqlTypes/SqlMoneyTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,5 +428,22 @@ public void GetXsdTypeTest()
428428
XmlQualifiedName qualifiedName = SqlMoney.GetXsdType(null);
429429
Assert.Equal("decimal", qualifiedName.Name);
430430
}
431+
432+
[Fact]
433+
public void GetTdsValue()
434+
{
435+
Assert.Equal(long.MaxValue,SqlMoney.MaxValue.GetTdsValue());
436+
Assert.Equal(long.MinValue, SqlMoney.MinValue.GetTdsValue());
437+
Assert.Equal((long)0, new SqlMoney(0).GetTdsValue());
438+
Assert.Throws<SqlNullValueException>(() => SqlMoney.Null.GetTdsValue());
439+
}
440+
441+
[Fact]
442+
public void FromTdsValue()
443+
{
444+
Assert.Equal(SqlMoney.FromTdsValue(long.MaxValue), SqlMoney.MaxValue);
445+
Assert.Equal(SqlMoney.FromTdsValue(long.MinValue), SqlMoney.MinValue);
446+
Assert.Equal(SqlMoney.FromTdsValue(0), new SqlMoney(0));
447+
}
431448
}
432449
}

0 commit comments

Comments
 (0)