Skip to content

Commit

Permalink
Fixes #76 - DateTimeOffset should now be usable everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
abe545 committed Aug 5, 2016
1 parent 1661a00 commit 81e27d9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
75 changes: 44 additions & 31 deletions CodeOnlyStoredProcedure/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ internal static DbType InferDbType(this Type type)
return DbType.String;
else if (type == typeof(DateTime))
return DbType.DateTime;
else if (type == typeof(DateTimeOffset))
return DbType.DateTimeOffset;
else if (type == typeof(Int64))
return DbType.Int64;
else if (type == typeof(Int16))
Expand All @@ -133,6 +135,42 @@ internal static DbType InferDbType(this Type type)
return DbType.Object;
}

internal static SqlDbType? TryInferSqlDbType(this Type type)
{
Contract.Requires(type != null);

UnwrapNullable(ref type);

if (type == typeof(Int32) || type == typeof(UInt32))
return SqlDbType.Int;
else if (type == typeof(Double))
return SqlDbType.Float;
else if (type == typeof(Decimal))
return SqlDbType.Decimal;
else if (type == typeof(Boolean))
return SqlDbType.Bit;
else if (type == typeof(String) || type.IsEnum)
return SqlDbType.NVarChar;
else if (type == typeof(DateTime))
return SqlDbType.DateTime;
else if (type == typeof(DateTimeOffset))
return SqlDbType.DateTimeOffset;
else if (type == typeof(Int64) || type == typeof(UInt64))
return SqlDbType.BigInt;
else if (type == typeof(Int16) || type == typeof(UInt16))
return SqlDbType.SmallInt;
else if (type == typeof(Byte) || type == typeof(SByte))
return SqlDbType.TinyInt;
else if (type == typeof(Guid))
return SqlDbType.UniqueIdentifier;
else if (type == typeof(Char))
return SqlDbType.NChar;
else if (type == typeof(Single))
return SqlDbType.Real;

return null;
}

internal static void SetTypePrecisionAndScale(this Type type,
IDbDataParameter parameter,
DbType? specifiedType,
Expand Down Expand Up @@ -186,39 +224,14 @@ internal static SqlMetaData CreateSqlMetaData(this Type type,

if (!specifiedSqlDbType.HasValue)
{
UnwrapNullable(ref type);

if (type == typeof(string))
specifiedSqlDbType = SqlDbType.NVarChar;
else if (type == typeof(char))
{
specifiedSqlDbType = SqlDbType.NChar;
specifiedSize = specifiedSize ?? 1;
}
else if (type == typeof(Decimal))
specifiedSqlDbType = SqlDbType.Decimal;
else if (type == typeof(Int32))
specifiedSqlDbType = SqlDbType.Int;
else if (type == typeof(Double))
specifiedSqlDbType = SqlDbType.Float;
else if (type == typeof(Boolean))
specifiedSqlDbType = SqlDbType.Bit;
else if (type == typeof(DateTime))
specifiedSqlDbType = SqlDbType.DateTime;
else if (type == typeof(Int64))
specifiedSqlDbType = SqlDbType.BigInt;
else if (type == typeof(Int16))
specifiedSqlDbType = SqlDbType.SmallInt;
else if (type == typeof(Byte))
specifiedSqlDbType = SqlDbType.TinyInt;
else if (type == typeof(Single))
specifiedSqlDbType = SqlDbType.Real;
else if (type == typeof(Guid))
specifiedSqlDbType = SqlDbType.UniqueIdentifier;
else
throw new NotSupportedException("Could not determine the type of " + columnName + " for the Table Valued Parameter. You can specify the desired type by decorating the property of your model with a SqlServerStoredProcedureParameter attribute.");
specifiedSqlDbType = type.TryInferSqlDbType();
if (specifiedSize == null && type == typeof(Char))
specifiedSize = 1;
}

if (!specifiedSqlDbType.HasValue)
throw new NotSupportedException("Could not determine the type of " + columnName + " for the Table Valued Parameter. You can specify the desired type by decorating the property of your model with a SqlServerStoredProcedureParameter attribute.");

switch (specifiedSqlDbType.Value)
{
case SqlDbType.Binary:
Expand Down
15 changes: 15 additions & 0 deletions CodeOnlyTests/TypeExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,21 @@ public void InferDbType_ReturnsString_ForEnum()
var result = typeof(EnumToTest).InferDbType();
result.Should().Be(DbType.String, "because an enum should be passed as a string");
}

[TestMethod]
public void InferDbType_ReturnsDateTimeOffset()
{
typeof(DateTimeOffset).InferDbType().Should().Be(DbType.DateTimeOffset);
}
#endregion

#region CreateSqlMetaData
[TestMethod]
public void CreateSqlMetaData_ReturnsSqlMetaData_ThatRepresents_DateTimeOffset()
{
var result = typeof(DateTimeOffset).CreateSqlMetaData("Foo", null, null, null, null);
result.SqlDbType.Should().Be(SqlDbType.DateTimeOffset);
}
#endregion

#region Types To Test With
Expand Down

0 comments on commit 81e27d9

Please sign in to comment.