Skip to content

Commit

Permalink
Allow enum parameters to be create as int32
Browse files Browse the repository at this point in the history
  • Loading branch information
busterwood committed Mar 16, 2016
1 parent af97765 commit af5cb5b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
11 changes: 11 additions & 0 deletions Mapper.UnitTests/CommandExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ public void can_add_null_DateTime()
Assert.AreEqual(DBNull.Value, cmd.Parameters[0].Value);
}

[Test]
public void can_add_enum_as_int()
{
SqlCommand cmd = new SqlCommand();
cmd.AddParameters(new { SomeId = TestEnum.Something });
Assert.AreEqual(1, cmd.Parameters.Count);
Assert.AreEqual("@SomeId", cmd.Parameters[0].ParameterName);
Assert.AreEqual(DbType.Int32, cmd.Parameters[0].DbType);
Assert.AreEqual(1, cmd.Parameters[0].Value);
}

[Test]
public void can_add_null_TableType()
{
Expand Down
27 changes: 21 additions & 6 deletions Mapper/CommandExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,14 @@ private static Action<IDbCommand, object> CreateAddParametersAction(Type type)
var createParameter = typeof(IDbCommand).GetMethod("CreateParameter", Type.EmptyTypes);
foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!Types.IsStructured(prop.PropertyType) && !Types.TypeToDbType.ContainsKey(prop.PropertyType)) continue;
var propertyType = prop.PropertyType;
if (!IsSupportedType(propertyType)) continue;
lines.Add(Expression.Assign(dataParam, Expression.Call(cmd, createParameter)));
lines.Add(Expression.Assign(Expression.Property(dataParam, "ParameterName"), Expression.Constant("@" + prop.Name)));

if (Types.IsStructured(prop.PropertyType))
if (Types.IsStructured(propertyType))
{
if (prop.PropertyType != typeof(TableType))
if (propertyType != typeof(TableType))
throw new NotSupportedException($"Parameter {dataParam.Name} implements {nameof(IEnumerable<SqlDataRecord>)} but type name is unknown. Please wrap parameter by calling {nameof(SqlDataRecordExtensions.WithTypeName)}");

lines.Add(Expression.IfThen(
Expand All @@ -171,12 +172,16 @@ private static Action<IDbCommand, object> CreateAddParametersAction(Type type)
lines.Add(Expression.Assign(Expression.Property(Expression.Convert(dataParam, typeof (SqlParameter)), "SqlDbType"), Expression.Constant(SqlDbType.Structured)));
lines.Add(Expression.Assign(Expression.Property(Expression.Convert(dataParam, typeof (SqlParameter)), "TypeName"), Expression.Property(Expression.Property(parameters, prop.Name), "TypeName")));
}
else if (propertyType.IsEnum)
{
lines.Add(Expression.Assign(Expression.Property(dataParam, "DbType"), Expression.Constant(DbType.Int32)));
}
else
{
lines.Add(Expression.Assign(Expression.Property(dataParam, "DbType"), Expression.Constant(Types.TypeToDbType[prop.PropertyType])));
lines.Add(Expression.Assign(Expression.Property(dataParam, "DbType"), Expression.Constant(Types.TypeToDbType[propertyType])));
}

if (Types.CanBeNull(prop.PropertyType))
if (Types.CanBeNull(propertyType))
{
lines.Add(Expression.IfThenElse(
Expression.Equal(Expression.Property(parameters, prop.Name), Expression.Constant(null)),
Expand All @@ -195,9 +200,19 @@ private static Action<IDbCommand, object> CreateAddParametersAction(Type type)
return Expression.Lambda<Action<IDbCommand, object>>(block, cmd, obj).Compile();
}

private static bool IsSupportedType(Type propertyType)
{
return Types.IsStructured(propertyType) || Types.TypeToDbType.ContainsKey(propertyType) || propertyType.IsEnum;
}

private static UnaryExpression PropertyValue(ParameterExpression obj, PropertyInfo prop)
{
return Expression.Convert(Expression.Property(obj, prop.Name), typeof(object));
Expression value = Expression.Property(obj, prop.Name);
if (prop.PropertyType.IsEnum)
{
value = Expression.Convert(value, typeof(int));
}
return Expression.Convert(value, typeof(object));
}

}
Expand Down
2 changes: 1 addition & 1 deletion Mapper/Mapper.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Mapper</id>
<version>1.0.1.1</version>
<version>1.0.1.2</version>
<authors>BusterWood</authors>
<description>A convention-based object cloner, object-object mapper (like AutoMapper), IDataReader to object mapper, object to IDbDataParameter mapper, etc.</description>
<projectUrl>https://github.com/busterwood/mapper</projectUrl>
Expand Down
2 changes: 1 addition & 1 deletion Mapper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.1.1")]
[assembly: AssemblyFileVersion("1.0.1.2")]

[assembly: InternalsVisibleTo("Mapper.UnitTests")]

0 comments on commit af5cb5b

Please sign in to comment.