Skip to content

Commit

Permalink
Merge pull request #690 from mikependon/fix/set_parameter_DbType
Browse files Browse the repository at this point in the history
fix null parameter being mistaken for the string type
  • Loading branch information
mikependon authored Jan 2, 2021
2 parents fd0ac1a + aea430a commit 8ea31c0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ public void TestSqlConnectionExecuteQueryWithParameterAsQueryGroup()
}
}

[TestMethod]
public void TestSqlConnectionExecuteQueryWithParameterAsNullDecimal()
{
using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb).EnsureOpen())
{
// Act Query
var data = connection.ExecuteQuery<decimal?>("select @value", new { value = (decimal?)null }).First();

// Assert
Assert.IsNull(data);
}
}

#endregion

#region ExecuteQueryAsync
Expand Down
18 changes: 18 additions & 0 deletions RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ private static void CreateParametersInternal(IDbCommand command,
classProperty.GetDbType() ??
value?.GetType()?.GetDbType();

// Try get fallback dbType by classProperty to avoid being mistaken as string when value is null.
if (dbType == null && classProperty != null)
{
dbType = clientTypeToDbTypeResolver.Resolve(classProperty.PropertyInfo.PropertyType);
}

// Specialized enum
if (dbType == null && isEnum.HasValue && isEnum.Value == true)
{
Expand Down Expand Up @@ -334,6 +340,12 @@ private static void CreateParameters(IDbCommand command,
classProperty?.GetDbType() ??
value?.GetType()?.GetDbType();

// Try get fallback dbType by classProperty to avoid being mistaken as string when value is null.
if (dbType == null && classProperty != null)
{
dbType = clientTypeToDbTypeResolver.Resolve(classProperty.PropertyInfo.PropertyType);
}

// Specialized enum
if (dbType == null && isEnum.HasValue && isEnum.Value == true)
{
Expand Down Expand Up @@ -463,6 +475,12 @@ private static void CreateParameters(this IDbCommand command,
classProperty?.GetDbType() ??
value?.GetType()?.GetDbType();

// Try get fallback dbType by classProperty to avoid being mistaken as string when value is null.
if (dbType == null && classProperty != null)
{
dbType = clientTypeToDbTypeResolver.Resolve(classProperty.PropertyInfo.PropertyType);
}

// Specialized enum
if (dbType == null && isEnum.HasValue && isEnum == true)
{
Expand Down
3 changes: 3 additions & 0 deletions RepoDb.Core/RepoDb/Mappers/TypeMapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using RepoDb.Exceptions;
Expand Down
28 changes: 13 additions & 15 deletions RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,17 @@ internal static Expression GetDictionaryStringObjectDbParameterValueAssignmentEx
return Expression.Call(parameterVariableExpression, GetDbParameterValueSetMethod(), expression);
}

private static DbType? GetDbType(ClassProperty classProperty, Type dbFieldType)
{
var dbType = classProperty?.GetDbType();
if (dbType == null)
{
var underlyingType = dbFieldType?.GetUnderlyingType();
dbType = TypeMapper.Get(underlyingType) ?? new ClientTypeToDbTypeResolver().Resolve(underlyingType);
}
return dbType;
}

/// <summary>
///
/// </summary>
Expand All @@ -1557,14 +1568,7 @@ internal static Expression GetDictionaryStringObjectDbParameterValueAssignmentEx
internal static MethodCallExpression GetDbParameterDbTypeAssignmentExpression(ParameterExpression parameterVariableExpression,
ClassProperty classProperty,
DbField dbField)
{
var underlyingType = dbField.Type?.GetUnderlyingType();
var dbType = classProperty?.GetDbType() ?? TypeMapper.Get(underlyingType) ??
new ClientTypeToDbTypeResolver().Resolve(underlyingType);

// Return the expression
return GetDbParameterDbTypeAssignmentExpression(parameterVariableExpression, dbType);
}
=> GetDbParameterDbTypeAssignmentExpression(parameterVariableExpression, GetDbType(classProperty, dbField.Type));

/// <summary>
///
Expand All @@ -1574,13 +1578,7 @@ internal static MethodCallExpression GetDbParameterDbTypeAssignmentExpression(Pa
/// <returns></returns>
internal static MethodCallExpression GetDbParameterDbTypeAssignmentExpression(ParameterExpression parameterVariableExpression,
DbField dbField)
{
var underlyingType = dbField.Type?.GetUnderlyingType();
var dbType = TypeMapper.Get(underlyingType) ?? new ClientTypeToDbTypeResolver().Resolve(underlyingType);

// Return the expression
return GetDbParameterDbTypeAssignmentExpression(parameterVariableExpression, dbType);
}
=> GetDbParameterDbTypeAssignmentExpression(parameterVariableExpression, GetDbType(null, dbField.Type));

/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ internal static Action<DbCommand, object> GetPlainTypeToDbParametersCompiledFunc
dbField.Type.GetUnderlyingType());
}

// DbType
var dbType = (DbType?)null;
if (handlerSetType == null)
// DbType. PropertyHandler first
var dbType = handlerSetType != null ?
GetDbType(null, handlerSetType) :
GetDbType(entityProperty ?? paramProperty, (entityProperty ?? paramProperty).PropertyInfo.PropertyType);
if (dbType == null && paramProperty.PropertyInfo.PropertyType.IsEnum)
{
dbType = (entityProperty ?? paramProperty).GetDbType();
if (dbType == null && paramProperty.PropertyInfo.PropertyType.IsEnum)
{
dbType = Converter.EnumDefaultDatabaseType;
}
dbType = Converter.EnumDefaultDatabaseType;
}

var dbTypeExpression = dbType == null ? GetNullableTypeExpression(StaticType.DbType) :
Expand Down

0 comments on commit 8ea31c0

Please sign in to comment.