Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix null parameter being mistaken for the string type #690

Merged
merged 3 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is correct now, so I will accept this. Though, you can also remove the additional checking and make it single line like below.

// DbType
var dbType = (returnType != null ? clientTypeToDbTypeResolver.Resolve(returnType) : null) ??
	classProperty.GetDbType() ??
	value?.GetType()?.GetDbType() ??
        classProperty != null ? clientTypeToDbTypeResolver.Resolve(classProperty.PropertyInfo.PropertyType) : null;


// 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of our coding standard, you might notice that we are always doing a "new line" per argument.

private static DbType? GetDbType(ClassProperty classProperty,
      Type dbFieldType)
{
     ...
}

But I am happy that you are very engage here, I know that next time, you will be one of my biggest contributor and/or active collaborator. Thanks as always 😺

{
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