Skip to content

Commit

Permalink
Implementation of DateTime2 + accompanying GetSqlDateTime2 method (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbouetsmith committed Jan 12, 2021
1 parent 661b640 commit df12b41
Show file tree
Hide file tree
Showing 16 changed files with 528 additions and 7 deletions.
19 changes: 19 additions & 0 deletions doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,25 @@
]]></format>
</remarks>
</GetSqlDateTime>
<GetSqlDateTime2>
<param name="i">The zero-based column ordinal.</param>
<summary>
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlDateTime2" />.
</summary>
<returns>
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlDateTime2" />.
</returns>
<remarks>
<format type="text/markdown">
<![CDATA[
## Remarks
No conversions are performed; therefore, the data retrieved must already be a date/time value or Null, otherwise an exception is thrown.
]]>
</format>
</remarks>
</GetSqlDateTime2>
<GetSqlDecimal>
<param name="i">The zero-based column ordinal.</param>
<summary>Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlDecimal" />.</summary>
Expand Down
72 changes: 72 additions & 0 deletions doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<docs>
<members name="SqlDateTime2">
<SqlFileStream>
<summary>Exposes SQL Server data that is stored as a DATETIME2.</summary>
<remarks>
<format type="text/markdown">
<![CDATA[
## Remarks
The <xref:Microsoft.Data.SqlTypes.SqlDateTime2> class is used to work with `DATETIME2` data.
`DATETIME2`has a wider range than DATETIME and thus needs its own type.
]]>
</format>
</remarks>
</SqlFileStream>
<ctor1>
<param name="isNull">Whether or not this instance should be considered null - true means null, false not null</param>
<summary>
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlTypes.SqlDateTime2" /> struct.
Will initialize the datetime value to 0.
</summary>
</ctor1>
<ctor2>
<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>

<summary>
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlTypes.SqlDateTime2" /> class.
</summary>
<exception cref="T:System.ArgumentOutOfRangeException">
<paramref name="ticks" /> is not in the range of <see cref="T:Microsoft.DateTime.MinValue.Ticks"/> &amp; <see cref="T:Microsoft.DateTime.MaxValue.Ticks"/>
</exception>
</ctor2>
<Value>
<summary>
Gets the <see cref="T:System.DateTime"/> representation of this instance.
</summary>
<exception cref="T:Microsoft.Data.SqlTypes.SqlNullValueException">
If this instance is null
</exception>
</Value>
<Null>
<summary>
Gets an instance representing the value NULL from the database.
</summary>
</Null>
<OperatorDateTime>
<summary>
Converts a DateTime into a SqlDateTime2
</summary>
</OperatorDateTime>
<OperatorDBNull>
<summary>
Converts a DBNull instance into a Null SqlDateTime2
</summary>
</OperatorDBNull>
<OperatorSqlDateTime>
<summary>
Converts a SqlDateTime2 into DateTime
</summary>
<exception cref="T:Microsoft.Data.SqlTypes.SqlNullValueException">
If the SqlDateTime2 instance has a null value
</exception>
</OperatorSqlDateTime>
<GetXsdType>
<summary>
returns a <see cref="T:System.Xml.XmlQualifiedName"/> for serialization purposes
</summary>
<param name="schemaSet">unused parameter</param>
</GetXsdType>
</members>
</docs>
1 change: 1 addition & 0 deletions src/Microsoft.Data.SqlClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Data.SqlClient.Se
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Data.SqlTypes", "Microsoft.Data.SqlTypes", "{5A7600BD-AED8-44AB-8F2A-7CB33A8D9C02}"
ProjectSection(SolutionItems) = preProject
..\doc\snippets\Microsoft.Data.SqlTypes\SqlDateTime2.xml = ..\doc\snippets\Microsoft.Data.SqlTypes\SqlDateTime2.xml
..\doc\snippets\Microsoft.Data.SqlTypes\SqlFileStream.xml = ..\doc\snippets\Microsoft.Data.SqlTypes\SqlFileStream.xml
EndProjectSection
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,5 +527,10 @@ internal static void SetCurrentTransaction(Transaction transaction)
{
Transaction.Current = transaction;
}

static internal Exception WrongType(Type got, Type expected)
{
return Argument(StringsHelper.GetString(Strings.SQL_WrongType, got.ToString(), expected.ToString()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionEnclaveProvider.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlEnclaveAttestationParameters.cs" />
<Compile Include="Microsoft\Data\SqlClient\EnclaveDelegate.cs" />
<Compile Include="..\..\src\Microsoft\Data\SqlTypes\SqlDateTime2.cs" Link="Microsoft\Data\SQLTypes\SqlDateTime2.cs" />
</ItemGroup>
<!-- Windows only -->
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,40 @@ internal SqlDateTime SqlDateTime
}
}

internal SqlDateTime2 SqlDateTime2
{
get
{
if (IsNull)
{
return SqlDateTime2.Null;
}

if (StorageType.DateTime2 == _type)
{
return new SqlDateTime2(GetTicksFromDateTime2Info(_value._dateTime2Info));
}

if (StorageType.DateTime == _type)
{
// also handle DATETIME without boxing to object first
var dateTime = SqlTypeWorkarounds.SqlDateTimeToDateTime(_value._dateTimeInfo.daypart, _value._dateTimeInfo.timepart);

return new SqlDateTime2(dateTime.Ticks);
}

if (StorageType.Date == _type)
{
return (SqlDateTime2)DateTime.MinValue.AddDays(_value._int32);
}

// cannot use SqlValue, since that causes invalid cast exception since object cannot be dynamic cast to SqlDateTime2 - only explicit cast
// (SqlDateTime2)(object)dateTimeValue;
// So assume its called on some kind of DATE type, so DateTime property can handle it
return (SqlDateTime2)DateTime;
}
}

internal SqlDecimal SqlDecimal
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,13 @@ virtual public SqlDateTime GetSqlDateTime(int i)
return _data[i].SqlDateTime;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDateTime2/*' />
virtual public SqlDateTime2 GetSqlDateTime2(int i)
{
ReadColumn(i);
return _data[i].SqlDateTime2;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDecimal/*' />
virtual public SqlDecimal GetSqlDecimal(int i)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
<Compile Include="..\..\src\Microsoft\Data\Common\ActivityCorrelator.cs">
<Link>Microsoft\Data\Common\ActivityCorrelator.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlTypes\SqlDateTime2.cs">
<Link>Microsoft\Data\SqlTypes\SqlDateTime2.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\Common\DbConnectionPoolKey.cs">
<Link>Microsoft\Data\Common\DbConnectionPoolKey.cs</Link>
</Compile>
Expand Down Expand Up @@ -368,18 +371,26 @@
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCngProvider.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCspProvider.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionEnclaveProvider.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlCommand.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlCommandBuilder.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlCommand.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Microsoft\Data\SqlClient\SqlCommandBuilder.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Microsoft\Data\SqlClient\SqlCommandSet.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnection.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnection.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionFactory.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionPoolGroupProviderInfo.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionPoolKey.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionString.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionStringBuilder.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionTimeoutErrorInternal.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlCredential.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDataAdapter.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDataAdapter.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Microsoft\Data\SqlClient\SqlDataReader.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDataReaderSmi.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs" />
Expand Down Expand Up @@ -443,7 +454,9 @@
<Compile Include="Microsoft\Data\ProviderBase\DbBuffer.cs" />
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionClosed.cs" />
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionFactory.cs" />
<Compile Include="Microsoft\Data\ProviderBase\SqlConnectionHelper.cs" />
<Compile Include="Microsoft\Data\ProviderBase\SqlConnectionHelper.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionInternal.cs" />
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPool.cs" />
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolGroup.cs" />
Expand Down Expand Up @@ -545,4 +558,4 @@
<Import Project="$(NetFxSource)tools\targets\GenerateThisAssemblyCs.targets" />
<Import Project="$(NetFxSource)tools\targets\GenerateAssemblyRef.targets" />
<Import Project="$(NetFxSource)tools\targets\GenerateAssemblyInfo.targets" />
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,40 @@ internal SqlDateTime SqlDateTime
}
}

internal SqlDateTime2 SqlDateTime2
{
get
{
if (IsNull)
{
return SqlDateTime2.Null;
}

if (StorageType.DateTime2 == _type)
{
return new SqlDateTime2(GetTicksFromDateTime2Info(_value._dateTime2Info));
}

if (StorageType.DateTime == _type)
{
// also handle DATETIME without boxing to object first
var dateTime = SqlTypeWorkarounds.SqlDateTimeToDateTime(_value._dateTimeInfo._daypart, _value._dateTimeInfo._timepart);

return new SqlDateTime2(dateTime.Ticks);
}

if (StorageType.Date == _type)
{
return (SqlDateTime2)DateTime.MinValue.AddDays(_value._int32);
}

// cannot use SqlValue, since that causes invalid cast exception since object cannot be dynamic cast to SqlDateTime2 - only explicit cast
// (SqlDateTime2)(object)dateTimeValue;
// So assume its called on some kind of DATE type, so DateTime property can handle it
return (SqlDateTime2)DateTime;
}
}

internal SqlDecimal SqlDecimal
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2777,6 +2777,13 @@ virtual public SqlDateTime GetSqlDateTime(int i)
return _data[i].SqlDateTime;
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDateTime2/*' />
virtual public SqlDateTime2 GetSqlDateTime2(int i)
{
ReadColumn(i);
return _data[i].SqlDateTime2;
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDecimal/*' />
virtual public SqlDecimal GetSqlDecimal(int i)
{
Expand Down
Loading

0 comments on commit df12b41

Please sign in to comment.