-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of DateTime2 + accompanying GetSqlDateTime2 method (#846)
- Loading branch information
1 parent
699ae37
commit b0b5515
Showing
16 changed files
with
513 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"/> & <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"/> & <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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlTypes/SqlDateTime2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System; | ||
using System.Data.SqlTypes; | ||
using System.Runtime.InteropServices; | ||
using System.Xml; | ||
using System.Xml.Schema; | ||
using System.Xml.Serialization; | ||
using Microsoft.Data.Common; | ||
|
||
namespace Microsoft.Data.SqlTypes | ||
{ | ||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml' path='docs/members[@name="SqlDateTime2"]/SqlDateTime2/*' /> | ||
[XmlSchemaProvider("GetXsdType")] | ||
[Serializable] | ||
[StructLayout(LayoutKind.Sequential)] | ||
public struct SqlDateTime2 : INullable, IComparable<SqlDateTime2>, IEquatable<SqlDateTime2>, IComparable, IXmlSerializable | ||
{ | ||
private const long _minTicks = 0; // DateTime.MinValue.Ticks | ||
private const long _maxTicks = 3155378975999999999; // DateTime.MaxValue.Ticks | ||
|
||
private bool _notNull; // false if null - has to be _notNull and not _isNull - otherwise default(SqlDateTime2) will return a SqlDateTime2 with _isNull=false and that would make XmlSerialization fail | ||
private long _ticks; // Ticks representation similar to DateTime.Tics | ||
|
||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml' path='docs/members[@name="SqlDateTime2"]/ctor1/*' /> | ||
private SqlDateTime2(bool isNull) | ||
{ | ||
_notNull = !isNull; | ||
_ticks = 0; | ||
} | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml' path='docs/members[@name="SqlDateTime2"]/ctor2/*' /> | ||
public SqlDateTime2(long ticks) | ||
{ | ||
if (ticks < _minTicks || ticks > _maxTicks) | ||
{ | ||
throw ADP.ArgumentOutOfRange(nameof(ticks)); | ||
} | ||
_notNull = true; | ||
_ticks = ticks; | ||
|
||
} | ||
|
||
/// <inheritdoc /> | ||
public bool IsNull => !_notNull; | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml' path='docs/members[@name="SqlDateTime2"]/Value/*' /> | ||
public DateTime Value => _notNull ? new DateTime(_ticks) : throw new SqlNullValueException(); | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml' path='docs/members[@name="SqlDateTime2"]/Null/*' /> | ||
public static readonly SqlDateTime2 Null = new SqlDateTime2(true); | ||
|
||
/// <inheritdoc /> | ||
public bool Equals(SqlDateTime2 other) | ||
{ | ||
return _notNull == other._notNull && _ticks == other._ticks; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public int CompareTo(SqlDateTime2 other) | ||
{ | ||
var result = _notNull.CompareTo(other._notNull); | ||
|
||
if (result != 0) | ||
{ | ||
return result; | ||
} | ||
|
||
return _ticks.CompareTo(other._ticks); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public int CompareTo(object obj) | ||
{ | ||
if (obj is SqlDateTime2 sqlDateTime2) | ||
{ | ||
return CompareTo(sqlDateTime2); | ||
} | ||
|
||
throw ADP.WrongType(obj.GetType(), typeof(SqlDateTime2)); | ||
} | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml' path='docs/members[@name="SqlDateTime2"]/OperatorDateTime/*' /> | ||
public static explicit operator SqlDateTime2(DateTime dateTime) | ||
{ | ||
return new SqlDateTime2(dateTime.Ticks); | ||
} | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml' path='docs/members[@name="SqlDateTime2"]/OperatorDBNull/*' /> | ||
public static explicit operator SqlDateTime2(DBNull _) | ||
{ | ||
return Null; | ||
} | ||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml' path='docs/members[@name="SqlDateTime2"]/OperatorSqlDateTime/*' /> | ||
public static explicit operator DateTime(SqlDateTime2 sqlDateTime2) | ||
{ | ||
return sqlDateTime2.Value; | ||
} | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlDateTime2.xml' path='docs/members[@name="SqlDateTime2"]/GetXsdType/*' /> | ||
public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) | ||
{ | ||
return new XmlQualifiedName("dateTime2", "http://www.w3.org/2001/XMLSchema"); | ||
} | ||
|
||
XmlSchema IXmlSerializable.GetSchema() | ||
{ | ||
return null; | ||
} | ||
|
||
void IXmlSerializable.ReadXml(XmlReader reader) | ||
{ | ||
string attribute = reader.GetAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"); | ||
if (attribute != null && XmlConvert.ToBoolean(attribute)) | ||
{ | ||
reader.ReadElementString(); | ||
_notNull = false; | ||
} | ||
else | ||
{ | ||
DateTime dateTime = XmlConvert.ToDateTime(reader.ReadElementString(), XmlDateTimeSerializationMode.RoundtripKind); | ||
if (dateTime.Kind != DateTimeKind.Unspecified) | ||
throw new SqlTypeException(SQLResource.TimeZoneSpecifiedMessage); | ||
_ticks = dateTime.Ticks; | ||
_notNull = true; | ||
} | ||
} | ||
|
||
void IXmlSerializable.WriteXml(XmlWriter writer) | ||
{ | ||
if (IsNull) | ||
writer.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true"); | ||
else | ||
writer.WriteString(XmlConvert.ToString(Value, "yyyy-MM-ddTHH:mm:ss.fffffff")); | ||
} | ||
} | ||
} |
Oops, something went wrong.