Skip to content

Commit

Permalink
Requested changes WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Danevandy99 committed Jun 3, 2024
1 parent 55762db commit 126c618
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -882,13 +882,10 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
}

// if object is nullable, add null safeguard before calling the function
// we special-case Nullable<>.GetValueOrDefault, which doesn't need the safeguard,
// and Nullable<>.ToString when the object is a nullable value type.
// we special-case Nullable<>.GetValueOrDefault, which doesn't need the safeguard
if (methodCallExpression.Object != null
&& @object!.Type.IsNullableType()
&& methodCallExpression.Method.Name != nameof(Nullable<int>.GetValueOrDefault)
&& (!@object!.Type.IsNullableValueType()
|| methodCallExpression.Method.Name != nameof(Nullable<int>.ToString)))
&& methodCallExpression.Method.Name != nameof(Nullable<int>.GetValueOrDefault))
{
var result = (Expression)methodCallExpression.Update(
Expression.Convert(@object, methodCallExpression.Object.Type),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,30 @@ public EnumMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
if (converterType is not null
&& converterType.IsGenericType)
{
if (converterType.GetGenericTypeDefinition() == typeof(EnumToNumberConverter<,>)
&& converterType.GetGenericArguments().Length == 2
&& converterType.GetGenericArguments()[1] == typeof(int)
&& (instance is SqlParameterExpression || instance is ColumnExpression))
if (converterType.GetGenericTypeDefinition() == typeof(EnumToNumberConverter<,>))
{
var cases = Enum.GetValues(instance.Type)
var whenClauses = Enum.GetValues(instance.Type)
.Cast<object>()
.Select(value => new CaseWhenClause(
_sqlExpressionFactory.Equal(instance, _sqlExpressionFactory.Constant(value)),
_sqlExpressionFactory.Constant(value),
_sqlExpressionFactory.Constant(value.ToString(), typeof(string))))
.ToArray();

return _sqlExpressionFactory.Case(cases, _sqlExpressionFactory.Constant(string.Empty, typeof(string)));
SqlExpression elseResult = _sqlExpressionFactory.Convert(instance, typeof(string));

if (instance is ColumnExpression { IsNullable: true })
{
elseResult = _sqlExpressionFactory.Coalesce(
elseResult,
_sqlExpressionFactory.Constant(string.Empty, typeof(string)));
}

return _sqlExpressionFactory.Case(instance, whenClauses, elseResult);
}
else if (converterType.GetGenericTypeDefinition() == typeof(EnumToStringConverter<>))
{
// TODO: Unnecessary cast to string, #33733
return _sqlExpressionFactory.MakeUnary(ExpressionType.Convert, instance, typeof(string));
return _sqlExpressionFactory.Convert(instance, typeof(string));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4023,17 +4023,17 @@ public override async Task ToString_enum_property_projection(bool async)

AssertSql(
"""
SELECT CASE
WHEN [g].[Rank] = 0 THEN N'None'
WHEN [g].[Rank] = 1 THEN N'Private'
WHEN [g].[Rank] = 2 THEN N'Corporal'
WHEN [g].[Rank] = 4 THEN N'Sergeant'
WHEN [g].[Rank] = 8 THEN N'Lieutenant'
WHEN [g].[Rank] = 16 THEN N'Captain'
WHEN [g].[Rank] = 32 THEN N'Major'
WHEN [g].[Rank] = 64 THEN N'Colonel'
WHEN [g].[Rank] = 128 THEN N'General'
ELSE N''
SELECT CASE [g].[Rank]
WHEN 0 THEN N'None'
WHEN 1 THEN N'Private'
WHEN 2 THEN N'Corporal'
WHEN 4 THEN N'Sergeant'
WHEN 8 THEN N'Lieutenant'
WHEN 16 THEN N'Captain'
WHEN 32 THEN N'Major'
WHEN 64 THEN N'Colonel'
WHEN 128 THEN N'General'
ELSE CAST([g].[Rank] AS nvarchar(max))
END
FROM [Gears] AS [g]
""");
Expand All @@ -4045,10 +4045,10 @@ public override async Task ToString_nullable_enum_property_projection(bool async

AssertSql(
"""
SELECT CASE
WHEN [w].[AmmunitionType] = 1 THEN N'Cartridge'
WHEN [w].[AmmunitionType] = 2 THEN N'Shell'
ELSE N''
SELECT CASE [w].[AmmunitionType]
WHEN 1 THEN N'Cartridge'
WHEN 2 THEN N'Shell'
ELSE COALESCE(CAST([w].[AmmunitionType] AS nvarchar(max)), N'')
END
FROM [Weapons] AS [w]
""");
Expand All @@ -4074,10 +4074,10 @@ public override async Task ToString_nullable_enum_contains(bool async)
"""
SELECT [w].[Name]
FROM [Weapons] AS [w]
WHERE CASE
WHEN [w].[AmmunitionType] = 1 THEN N'Cartridge'
WHEN [w].[AmmunitionType] = 2 THEN N'Shell'
ELSE N''
WHERE CASE [w].[AmmunitionType]
WHEN 1 THEN N'Cartridge'
WHEN 2 THEN N'Shell'
ELSE CAST([w].[AmmunitionType] AS nvarchar(max))
END LIKE N'%Cart%'
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5386,17 +5386,17 @@ public override async Task ToString_enum_property_projection(bool async)

AssertSql(
"""
SELECT CASE
WHEN [u].[Rank] = 0 THEN N'None'
WHEN [u].[Rank] = 1 THEN N'Private'
WHEN [u].[Rank] = 2 THEN N'Corporal'
WHEN [u].[Rank] = 4 THEN N'Sergeant'
WHEN [u].[Rank] = 8 THEN N'Lieutenant'
WHEN [u].[Rank] = 16 THEN N'Captain'
WHEN [u].[Rank] = 32 THEN N'Major'
WHEN [u].[Rank] = 64 THEN N'Colonel'
WHEN [u].[Rank] = 128 THEN N'General'
ELSE N''
SELECT CASE [u].[Rank]
WHEN 0 THEN N'None'
WHEN 1 THEN N'Private'
WHEN 2 THEN N'Corporal'
WHEN 4 THEN N'Sergeant'
WHEN 8 THEN N'Lieutenant'
WHEN 16 THEN N'Captain'
WHEN 32 THEN N'Major'
WHEN 64 THEN N'Colonel'
WHEN 128 THEN N'General'
ELSE CAST([u].[Rank] AS nvarchar(max))
END
FROM (
SELECT [g].[Rank]
Expand All @@ -5414,10 +5414,10 @@ public override async Task ToString_nullable_enum_property_projection(bool async

AssertSql(
"""
SELECT CASE
WHEN [w].[AmmunitionType] = 1 THEN N'Cartridge'
WHEN [w].[AmmunitionType] = 2 THEN N'Shell'
ELSE N''
SELECT CASE [w].[AmmunitionType]
WHEN 1 THEN N'Cartridge'
WHEN 2 THEN N'Shell'
ELSE COALESCE(CAST([w].[AmmunitionType] AS nvarchar(max)), N'')
END
FROM [Weapons] AS [w]
""");
Expand All @@ -5443,10 +5443,10 @@ public override async Task ToString_nullable_enum_contains(bool async)
"""
SELECT [w].[Name]
FROM [Weapons] AS [w]
WHERE CASE
WHEN [w].[AmmunitionType] = 1 THEN N'Cartridge'
WHEN [w].[AmmunitionType] = 2 THEN N'Shell'
ELSE N''
WHERE CASE [w].[AmmunitionType]
WHEN 1 THEN N'Cartridge'
WHEN 2 THEN N'Shell'
ELSE CAST([w].[AmmunitionType] AS nvarchar(max))
END LIKE N'%Cart%'
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4672,17 +4672,17 @@ public override async Task ToString_enum_property_projection(bool async)

AssertSql(
"""
SELECT CASE
WHEN [g].[Rank] = 0 THEN N'None'
WHEN [g].[Rank] = 1 THEN N'Private'
WHEN [g].[Rank] = 2 THEN N'Corporal'
WHEN [g].[Rank] = 4 THEN N'Sergeant'
WHEN [g].[Rank] = 8 THEN N'Lieutenant'
WHEN [g].[Rank] = 16 THEN N'Captain'
WHEN [g].[Rank] = 32 THEN N'Major'
WHEN [g].[Rank] = 64 THEN N'Colonel'
WHEN [g].[Rank] = 128 THEN N'General'
ELSE N''
SELECT CASE [g].[Rank]
WHEN 0 THEN N'None'
WHEN 1 THEN N'Private'
WHEN 2 THEN N'Corporal'
WHEN 4 THEN N'Sergeant'
WHEN 8 THEN N'Lieutenant'
WHEN 16 THEN N'Captain'
WHEN 32 THEN N'Major'
WHEN 64 THEN N'Colonel'
WHEN 128 THEN N'General'
ELSE CAST([g].[Rank] AS nvarchar(max))
END
FROM [Gears] AS [g]
""");
Expand All @@ -4694,10 +4694,10 @@ public override async Task ToString_nullable_enum_property_projection(bool async

AssertSql(
"""
SELECT CASE
WHEN [w].[AmmunitionType] = 1 THEN N'Cartridge'
WHEN [w].[AmmunitionType] = 2 THEN N'Shell'
ELSE N''
SELECT CASE [w].[AmmunitionType]
WHEN 1 THEN N'Cartridge'
WHEN 2 THEN N'Shell'
ELSE COALESCE(CAST([w].[AmmunitionType] AS nvarchar(max)), N'')
END
FROM [Weapons] AS [w]
""");
Expand All @@ -4723,10 +4723,10 @@ public override async Task ToString_nullable_enum_contains(bool async)
"""
SELECT [w].[Name]
FROM [Weapons] AS [w]
WHERE CASE
WHEN [w].[AmmunitionType] = 1 THEN N'Cartridge'
WHEN [w].[AmmunitionType] = 2 THEN N'Shell'
ELSE N''
WHERE CASE [w].[AmmunitionType]
WHEN 1 THEN N'Cartridge'
WHEN 2 THEN N'Shell'
ELSE CAST([w].[AmmunitionType] AS nvarchar(max))
END LIKE N'%Cart%'
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4799,17 +4799,17 @@ public override async Task ToString_enum_property_projection(bool async)

AssertSql(
"""
SELECT CASE
WHEN [g].[Rank] = 0 THEN N'None'
WHEN [g].[Rank] = 1 THEN N'Private'
WHEN [g].[Rank] = 2 THEN N'Corporal'
WHEN [g].[Rank] = 4 THEN N'Sergeant'
WHEN [g].[Rank] = 8 THEN N'Lieutenant'
WHEN [g].[Rank] = 16 THEN N'Captain'
WHEN [g].[Rank] = 32 THEN N'Major'
WHEN [g].[Rank] = 64 THEN N'Colonel'
WHEN [g].[Rank] = 128 THEN N'General'
ELSE N''
SELECT CASE [g].[Rank]
WHEN 0 THEN N'None'
WHEN 1 THEN N'Private'
WHEN 2 THEN N'Corporal'
WHEN 4 THEN N'Sergeant'
WHEN 8 THEN N'Lieutenant'
WHEN 16 THEN N'Captain'
WHEN 32 THEN N'Major'
WHEN 64 THEN N'Colonel'
WHEN 128 THEN N'General'
ELSE CAST([g].[Rank] AS nvarchar(max))
END
FROM [Gears] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [g]
""");
Expand All @@ -4821,10 +4821,10 @@ public override async Task ToString_nullable_enum_property_projection(bool async

AssertSql(
"""
SELECT CASE
WHEN [w].[AmmunitionType] = 1 THEN N'Cartridge'
WHEN [w].[AmmunitionType] = 2 THEN N'Shell'
ELSE N''
SELECT CASE [w].[AmmunitionType]
WHEN 1 THEN N'Cartridge'
WHEN 2 THEN N'Shell'
ELSE COALESCE(CAST([w].[AmmunitionType] AS nvarchar(max)), N'')
END
FROM [Weapons] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [w]
""");
Expand All @@ -4850,10 +4850,10 @@ public override async Task ToString_nullable_enum_contains(bool async)
"""
SELECT [w].[Name]
FROM [Weapons] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [w]
WHERE CASE
WHEN [w].[AmmunitionType] = 1 THEN N'Cartridge'
WHEN [w].[AmmunitionType] = 2 THEN N'Shell'
ELSE N''
WHERE CASE [w].[AmmunitionType]
WHEN 1 THEN N'Cartridge'
WHEN 2 THEN N'Shell'
ELSE CAST([w].[AmmunitionType] AS nvarchar(max))
END LIKE N'%Cart%'
""");
}
Expand Down

0 comments on commit 126c618

Please sign in to comment.