Skip to content

Commit 31bfb32

Browse files
Implementation of DateTime2 + accompanying GetSqlDateTime2 method (#846)
1 parent 661b640 commit 31bfb32

File tree

16 files changed

+509
-549
lines changed

16 files changed

+509
-549
lines changed

doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,25 @@
634634
]]></format>
635635
</remarks>
636636
</GetSqlDateTime>
637+
<GetSqlDateTime2>
638+
<param name="i">The zero-based column ordinal.</param>
639+
<summary>
640+
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlDateTime2" />.
641+
</summary>
642+
<returns>
643+
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlDateTime2" />.
644+
</returns>
645+
<remarks>
646+
<format type="text/markdown">
647+
<![CDATA[
648+
649+
## Remarks
650+
No conversions are performed; therefore, the data retrieved must already be a date/time value or Null, otherwise an exception is thrown.
651+
652+
]]>
653+
</format>
654+
</remarks>
655+
</GetSqlDateTime2>
637656
<GetSqlDecimal>
638657
<param name="i">The zero-based column ordinal.</param>
639658
<summary>Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlDecimal" />.</summary>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<docs>
2+
<members name="SqlDateTime2">
3+
<SqlFileStream>
4+
<summary>Exposes SQL Server data that is stored as a DATETIME2.</summary>
5+
<remarks>
6+
<format type="text/markdown">
7+
<![CDATA[
8+
9+
## Remarks
10+
The <xref:Microsoft.Data.SqlTypes.SqlDateTime2> class is used to work with `DATETIME2` data.
11+
`DATETIME2`has a wider range than DATETIME and thus needs its own type.
12+
13+
]]>
14+
</format>
15+
</remarks>
16+
</SqlFileStream>
17+
<ctor1>
18+
<param name="isNull">Whether or not this instance should be considered null - true means null, false not null</param>
19+
<summary>
20+
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlTypes.SqlDateTime2" /> struct.
21+
Will initialize the datetime value to 0.
22+
</summary>
23+
</ctor1>
24+
<ctor2>
25+
<param name="ticks">Number of ticks to initialize this instance with - in the range of <see cref="T:Microsoft.DateTime.MinValue.Ticks"/> &amp; <see cref="T:Microsoft.DateTime.MaxValue.Ticks"/></param>
26+
27+
<summary>
28+
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlTypes.SqlDateTime2" /> class.
29+
</summary>
30+
<exception cref="T:System.ArgumentOutOfRangeException">
31+
<paramref name="ticks" /> is not in the range of <see cref="T:Microsoft.DateTime.MinValue.Ticks"/> &amp; <see cref="T:Microsoft.DateTime.MaxValue.Ticks"/>
32+
</exception>
33+
</ctor2>
34+
<Value>
35+
<summary>
36+
Gets the <see cref="T:System.DateTime"/> representation of this instance.
37+
</summary>
38+
<exception cref="T:Microsoft.Data.SqlTypes.SqlNullValueException">
39+
If this instance is null
40+
</exception>
41+
</Value>
42+
<Null>
43+
<summary>
44+
Gets an instance representing the value NULL from the database.
45+
</summary>
46+
</Null>
47+
<OperatorDateTime>
48+
<summary>
49+
Converts a DateTime into a SqlDateTime2
50+
</summary>
51+
</OperatorDateTime>
52+
<OperatorDBNull>
53+
<summary>
54+
Converts a DBNull instance into a Null SqlDateTime2
55+
</summary>
56+
</OperatorDBNull>
57+
<OperatorSqlDateTime>
58+
<summary>
59+
Converts a SqlDateTime2 into DateTime
60+
</summary>
61+
<exception cref="T:Microsoft.Data.SqlTypes.SqlNullValueException">
62+
If the SqlDateTime2 instance has a null value
63+
</exception>
64+
</OperatorSqlDateTime>
65+
<GetXsdType>
66+
<summary>
67+
returns a <see cref="T:System.Xml.XmlQualifiedName"/> for serialization purposes
68+
</summary>
69+
<param name="schemaSet">unused parameter</param>
70+
</GetXsdType>
71+
</members>
72+
</docs>

src/Microsoft.Data.SqlClient.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Data.SqlClient.Se
159159
EndProject
160160
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Data.SqlTypes", "Microsoft.Data.SqlTypes", "{5A7600BD-AED8-44AB-8F2A-7CB33A8D9C02}"
161161
ProjectSection(SolutionItems) = preProject
162+
..\doc\snippets\Microsoft.Data.SqlTypes\SqlDateTime2.xml = ..\doc\snippets\Microsoft.Data.SqlTypes\SqlDateTime2.xml
162163
..\doc\snippets\Microsoft.Data.SqlTypes\SqlFileStream.xml = ..\doc\snippets\Microsoft.Data.SqlTypes\SqlFileStream.xml
163164
EndProjectSection
164165
EndProject

src/Microsoft.Data.SqlClient/netcore/src/Common/src/Microsoft/Data/Common/AdapterUtil.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,5 +527,10 @@ internal static void SetCurrentTransaction(Transaction transaction)
527527
{
528528
Transaction.Current = transaction;
529529
}
530+
531+
static internal Exception WrongType(Type got, Type expected)
532+
{
533+
return Argument(StringsHelper.GetString(Strings.SQL_WrongType, got.ToString(), expected.ToString()));
534+
}
530535
}
531536
}

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@
464464
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionEnclaveProvider.cs" />
465465
<Compile Include="Microsoft\Data\SqlClient\SqlEnclaveAttestationParameters.cs" />
466466
<Compile Include="Microsoft\Data\SqlClient\EnclaveDelegate.cs" />
467+
<Compile Include="..\..\src\Microsoft\Data\SqlTypes\SqlDateTime2.cs" Link="Microsoft\Data\SQLTypes\SqlDateTime2.cs" />
467468
</ItemGroup>
468469
<!-- Windows only -->
469470
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBuffer.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,40 @@ internal SqlDateTime SqlDateTime
615615
}
616616
}
617617

618+
internal SqlDateTime2 SqlDateTime2
619+
{
620+
get
621+
{
622+
if (IsNull)
623+
{
624+
return SqlDateTime2.Null;
625+
}
626+
627+
if (StorageType.DateTime2 == _type)
628+
{
629+
return new SqlDateTime2(GetTicksFromDateTime2Info(_value._dateTime2Info));
630+
}
631+
632+
if (StorageType.DateTime == _type)
633+
{
634+
// also handle DATETIME without boxing to object first
635+
var dateTime = SqlTypeWorkarounds.SqlDateTimeToDateTime(_value._dateTimeInfo.daypart, _value._dateTimeInfo.timepart);
636+
637+
return new SqlDateTime2(dateTime.Ticks);
638+
}
639+
640+
if (StorageType.Date == _type)
641+
{
642+
return (SqlDateTime2)DateTime.MinValue.AddDays(_value._int32);
643+
}
644+
645+
// cannot use SqlValue, since that causes invalid cast exception since object cannot be dynamic cast to SqlDateTime2 - only explicit cast
646+
// (SqlDateTime2)(object)dateTimeValue;
647+
// So assume its called on some kind of DATE type, so DateTime property can handle it
648+
return (SqlDateTime2)DateTime;
649+
}
650+
}
651+
618652
internal SqlDecimal SqlDecimal
619653
{
620654
get

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,6 +2397,13 @@ virtual public SqlDateTime GetSqlDateTime(int i)
23972397
return _data[i].SqlDateTime;
23982398
}
23992399

2400+
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDateTime2/*' />
2401+
virtual public SqlDateTime2 GetSqlDateTime2(int i)
2402+
{
2403+
ReadColumn(i);
2404+
return _data[i].SqlDateTime2;
2405+
}
2406+
24002407
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDecimal/*' />
24012408
virtual public SqlDecimal GetSqlDecimal(int i)
24022409
{

0 commit comments

Comments
 (0)