Skip to content

Commit

Permalink
Merge pull request #1104 from mikependon/repodb-issue-1100
Browse files Browse the repository at this point in the history
#1100 Fixes to the problem.
  • Loading branch information
mikependon authored Oct 22, 2022
2 parents 4c677ca + 076f07d commit 47cdb11
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,43 +1147,43 @@ internal static Expression GetClassPropertyParameterInfoIsDbNullFalseValueExpres
// get handler on class property or type level
var handlerInstance = GetHandlerInstance(classPropertyParameterInfo, readerField) ?? PropertyHandlerCache.Get<object>(classPropertyParameterInfo.GetTargetType());

// Auto-conversion
if (isAutomaticConversion == true)
// Enumerations
if (targetTypeUnderlyingType.IsEnum)
{
try
// If it has a PropertyHandler and the parameter type is matching, then, skip the auto conversion.
var autoConvertEnum = true;
if (handlerInstance != null)
{
valueExpression = ConvertExpressionWithAutomaticConversion(valueExpression, targetTypeUnderlyingType);
var getParameter = GetPropertyHandlerGetParameter(GetPropertyHandlerGetMethod(handlerInstance));
autoConvertEnum = !(getParameter.ParameterType.GetUnderlyingType() == readerField.Type);
}
catch (Exception ex)
if (autoConvertEnum)
{
throw new InvalidOperationException($"Compiler.DataReader.IsDbNull.FalseExpression: Failed to automatically convert the value expression. " +
$"{classPropertyParameterInfo.GetDescriptiveContextString()}", ex);
try
{
valueExpression = ConvertExpressionToEnumExpression(valueExpression, readerField.Type, targetTypeUnderlyingType);
}
catch (Exception ex)
{
throw new InvalidOperationException($"Compiler.DataReader.IsDbNull.FalseExpression: Failed to convert the value expression into enum type '{targetType.GetUnderlyingType()}'. " +
$"{classPropertyParameterInfo.GetDescriptiveContextString()}", ex);
}

}
}
else
{
// Enumerations
if (targetTypeUnderlyingType.IsEnum)
// Auto-conversion
if (isAutomaticConversion == true)
{
// If it has a PropertyHandler and the parameter type is matching, then, skip the auto conversion.
var autoConvertEnum = true;
if (handlerInstance != null)
try
{
var getParameter = GetPropertyHandlerGetParameter(GetPropertyHandlerGetMethod(handlerInstance));
autoConvertEnum = !(getParameter.ParameterType.GetUnderlyingType() == readerField.Type);
valueExpression = ConvertExpressionWithAutomaticConversion(valueExpression, targetTypeUnderlyingType);
}
if (autoConvertEnum)
catch (Exception ex)
{
try
{
valueExpression = ConvertExpressionToEnumExpression(valueExpression, readerField.Type, targetTypeUnderlyingType);
}
catch (Exception ex)
{
throw new InvalidOperationException($"Compiler.DataReader.IsDbNull.FalseExpression: Failed to convert the value expression into enum type '{targetType.GetUnderlyingType()}'. " +
$"{classPropertyParameterInfo.GetDescriptiveContextString()}", ex);
}

throw new InvalidOperationException($"Compiler.DataReader.IsDbNull.FalseExpression: Failed to automatically convert the value expression. " +
$"{classPropertyParameterInfo.GetDescriptiveContextString()}", ex);
}
}
}
Expand Down Expand Up @@ -1459,20 +1459,6 @@ internal static Expression GetEntityInstancePropertyValueExpression(Expression e
var handlerInstance = classProperty.GetPropertyHandler() ?? PropertyHandlerCache.Get<object>(dbField.Type.GetUnderlyingType());
var targetType = GetPropertyHandlerSetParameter(handlerInstance)?.ParameterType ?? dbField.Type;

// Auto-conversion Handling
if (GlobalConfiguration.Options.ConversionType == ConversionType.Automatic || dbField?.IsPrimary == true || dbField?.IsIdentity == true)
{
try
{
expression = ConvertExpressionWithAutomaticConversion(expression, targetType?.GetUnderlyingType());
}
catch (Exception ex)
{
throw new InvalidOperationException($"Compiler.Entity/Object.Property: Failed to automatically convert the value expression for " +
$"property '{classProperty.GetMappedName()} ({classProperty.PropertyInfo.PropertyType.FullName})'. {classProperty}", ex);
}
}

/*
* Note: The other data provider can coerce the Enum into its destination data type in the DB by default,
* except for PostgreSQL. The code written below is only to address the issue for this specific provider.
Expand All @@ -1497,6 +1483,20 @@ internal static Expression GetEntityInstancePropertyValueExpression(Expression e
}
}

// Auto-conversion Handling
if (GlobalConfiguration.Options.ConversionType == ConversionType.Automatic || dbField?.IsPrimary == true || dbField?.IsIdentity == true)
{
try
{
expression = ConvertExpressionWithAutomaticConversion(expression, targetType?.GetUnderlyingType());
}
catch (Exception ex)
{
throw new InvalidOperationException($"Compiler.Entity/Object.Property: Failed to automatically convert the value expression for " +
$"property '{classProperty.GetMappedName()} ({classProperty.PropertyInfo.PropertyType.FullName})'. {classProperty}", ex);
}
}

// Property Handler
try
{
Expand Down

0 comments on commit 47cdb11

Please sign in to comment.