Skip to content

Commit

Permalink
Get and set expected value for DdType.Date and DbType.Time (#269)
Browse files Browse the repository at this point in the history
* Get and set expected value for DdType.Date and DbType.Time

Breaks support for SQL Server 2008 (I believe)

fixes #5

Extend DateTimeTest.ReaderParameterTest to cover DbType parameters
  • Loading branch information
ErikEJ authored and David-Engel committed Nov 4, 2019
1 parent 486908b commit b1d4b96
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ internal static MetaType GetMetaTypeFromDbType(DbType target)
case DbType.Currency:
return s_metaMoney;
case DbType.Date:
return s_metaDate;
case DbType.DateTime:
return s_metaDateTime;
case DbType.Decimal:
Expand All @@ -301,7 +302,7 @@ internal static MetaType GetMetaTypeFromDbType(DbType target)
case DbType.StringFixedLength:
return s_metaNChar;
case DbType.Time:
return s_metaDateTime;
return MetaTime;
case DbType.Xml:
return MetaXml;
case DbType.DateTime2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,7 @@ public override DbType DbType
set
{
MetaType metatype = _metaType;
if ((null == metatype) || (metatype.DbType != value) ||
// Two special datetime cases for backward compat
// DbType.Date and DbType.Time should always be treated as setting DbType.DateTime instead
value == DbType.Date ||
value == DbType.Time)
if ((null == metatype) || (metatype.DbType != value))
{
PropertyTypeChanging();
_metaType = MetaType.GetMetaTypeFromDbType(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ internal static MetaType GetMetaTypeFromDbType(DbType target)
case DbType.Currency:
return MetaMoney;
case DbType.Date:
return MetaDate;
case DbType.DateTime:
return MetaDateTime;
case DbType.Decimal:
Expand All @@ -302,7 +303,7 @@ internal static MetaType GetMetaTypeFromDbType(DbType target)
case DbType.StringFixedLength:
return MetaNChar;
case DbType.Time:
return MetaDateTime;
return MetaTime;
case DbType.Xml:
return MetaXml;
case DbType.DateTime2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,7 @@ override public DbType DbType
set
{
MetaType metatype = _metaType;
if ((null == metatype) || (metatype.DbType != value) ||
// SQLBU 504029: Two special datetime cases for backward compat
// DbType.Date and DbType.Time should always be treated as setting DbType.DateTime instead
value == DbType.Date ||
value == DbType.Time)
if ((null == metatype) || (metatype.DbType != value))
{
PropertyTypeChanging();
_metaType = MetaType.GetMetaTypeFromDbType(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,17 @@ public void InferType_TimeSpan()
Assert.Equal(DbType.Time, param.DbType);
}

[Fact]
public void InferType_DateTimeOffset()
{
DateTimeOffset value = new DateTimeOffset(new DateTime(2019, 10, 15), new TimeSpan(1, 0, 0));

SqlParameter param = new SqlParameter();
param.Value = value;
Assert.Equal(SqlDbType.DateTimeOffset, param.SqlDbType);
Assert.Equal(DbType.DateTimeOffset, param.DbType);
}

[Fact]
public void LocaleId()
{
Expand Down Expand Up @@ -834,6 +845,48 @@ public void ResetDbType()
Assert.Null(p.Value);
}

[Theory]
[InlineData(DbType.AnsiString, SqlDbType.VarChar)]
[InlineData(DbType.AnsiStringFixedLength, SqlDbType.Char)]
[InlineData(DbType.Binary, SqlDbType.VarBinary)]
[InlineData(DbType.Boolean, SqlDbType.Bit)]
[InlineData(DbType.Byte, SqlDbType.TinyInt)]
[InlineData(DbType.Currency, SqlDbType.Money)]
[InlineData(DbType.Date, SqlDbType.Date)]
[InlineData(DbType.DateTime, SqlDbType.DateTime)]
[InlineData(DbType.DateTime2, SqlDbType.DateTime2)]
[InlineData(DbType.DateTimeOffset, SqlDbType.DateTimeOffset)]
[InlineData(DbType.Decimal, SqlDbType.Decimal)]
[InlineData(DbType.Double, SqlDbType.Float)]
[InlineData(DbType.Guid, SqlDbType.UniqueIdentifier)]
[InlineData(DbType.Int16, SqlDbType.SmallInt)]
[InlineData(DbType.Int32, SqlDbType.Int)]
[InlineData(DbType.Int64, SqlDbType.BigInt)]
[InlineData(DbType.Object, SqlDbType.Variant)]
[InlineData(DbType.Single, SqlDbType.Real)]
[InlineData(DbType.String, SqlDbType.NVarChar)]
[InlineData(DbType.Time, SqlDbType.Time)]
[InlineData(DbType.Xml, SqlDbType.Xml)]
public void Parameter_Supported(DbType dbType, SqlDbType sqlDbType)
{
var parameter = new SqlParameter();
parameter.DbType = dbType;
Assert.Equal(dbType, parameter.DbType);
Assert.Equal(sqlDbType, parameter.SqlDbType);
}

[Theory]
[InlineData(DbType.SByte)]
[InlineData(DbType.UInt16)]
[InlineData(DbType.UInt32)]
[InlineData(DbType.UInt64)]
[InlineData(DbType.VarNumeric)]
public void Parameter_NotSupported(DbType dbType)
{
var parameter = new SqlParameter();
Assert.Throws<ArgumentException>(() => parameter.DbType = dbType);
}

[Fact]
public void ResetSqlDbType()
{
Expand Down
Loading

0 comments on commit b1d4b96

Please sign in to comment.