Skip to content

Commit

Permalink
Query: Small cleanup in #19400
Browse files Browse the repository at this point in the history
  • Loading branch information
smitpatel committed Feb 7, 2020
1 parent 8fb06e1 commit 6548767
Showing 1 changed file with 32 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,46 @@ namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal
{
public class SqlServerFromPartsFunctionTranslator : IMethodCallTranslator
{
private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly IRelationalTypeMappingSource _typeMappingSource;

private static readonly MethodInfo _dateFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions)
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int) });
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateFromParts),
new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int) });

private static readonly MethodInfo _dateTimeFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions)
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTimeFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTimeFromParts),
new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });

private static readonly MethodInfo _dateTime2FromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions)
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTime2FromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTime2FromParts),
new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int),
typeof(int), typeof(int), typeof(int) });

private static readonly MethodInfo _dateTimeOffsetFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions)
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTimeOffsetFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTimeOffsetFromParts),
new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int),
typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });

private static readonly MethodInfo _smallDateTimeFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions)
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.SmallDateTimeFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.SmallDateTimeFromParts),
new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });

private static readonly MethodInfo _timeFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions)
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.TimeFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.TimeFromParts),
new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });

private static readonly IDictionary<MethodInfo, (string FunctionName, string ReturnType)> _methodFunctionMapping
= new Dictionary<MethodInfo, (string, string)>
{
{ _dateFromPartsMethodInfo, ("DATEFROMPARTS", "date") },
{ _dateTimeFromPartsMethodInfo, ("DATETIMEFROMPARTS", "datetime") },
{ _dateTime2FromPartsMethodInfo, ("DATETIME2FROMPARTS", "datetime2") },
{ _dateTimeOffsetFromPartsMethodInfo, ("DATETIMEOFFSETFROMPARTS", "datetimeoffset") },
{ _smallDateTimeFromPartsMethodInfo, ("SMALLDATETIMEFROMPARTS", "smalldatetime") },
{ _timeFromPartsMethodInfo, ("TIMEFROMPARTS", "time") },
};

private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly IRelationalTypeMappingSource _typeMappingSource;


public SqlServerFromPartsFunctionTranslator(
[NotNull] ISqlExpressionFactory sqlExpressionFactory,
Expand All @@ -45,70 +65,15 @@ public SqlServerFromPartsFunctionTranslator(

public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments)
{
if (_dateFromPartsMethodInfo.Equals(method))
if (_methodFunctionMapping.TryGetValue(method, out var value))
{
return _sqlExpressionFactory.Function(
"DATEFROMPARTS",
value.FunctionName,
arguments.Skip(1),
nullResultAllowed: true,
argumentsPropagateNullability: arguments.Skip(1).Select(a => true),
_dateFromPartsMethodInfo.ReturnType,
_typeMappingSource.FindMapping(typeof(DateTime), "date"));
}

if (_dateTimeFromPartsMethodInfo.Equals(method))
{
return _sqlExpressionFactory.Function(
"DATETIMEFROMPARTS",
arguments.Skip(1),
nullResultAllowed: true,
argumentsPropagateNullability: arguments.Skip(1).Select(a => true),
_dateTimeFromPartsMethodInfo.ReturnType,
_typeMappingSource.FindMapping(typeof(DateTime), "datetime"));
}

if (_dateTime2FromPartsMethodInfo.Equals(method))
{
return _sqlExpressionFactory.Function(
"DATETIME2FROMPARTS",
arguments.Skip(1),
nullResultAllowed: true,
argumentsPropagateNullability: arguments.Skip(1).Select(a => true),
_dateTime2FromPartsMethodInfo.ReturnType,
_typeMappingSource.FindMapping(typeof(DateTime), "datetime2"));
}

if (_dateTimeOffsetFromPartsMethodInfo.Equals(method))
{
return _sqlExpressionFactory.Function(
"DATETIMEOFFSETFROMPARTS",
arguments.Skip(1),
nullResultAllowed: true,
argumentsPropagateNullability: arguments.Skip(1).Select(a => true),
_dateTimeOffsetFromPartsMethodInfo.ReturnType,
_typeMappingSource.FindMapping(typeof(DateTimeOffset), "datetimeoffset"));
}

if (_smallDateTimeFromPartsMethodInfo.Equals(method))
{
return _sqlExpressionFactory.Function(
"SMALLDATETIMEFROMPARTS",
arguments.Skip(1),
nullResultAllowed: true,
argumentsPropagateNullability: arguments.Skip(1).Select(a => true),
_smallDateTimeFromPartsMethodInfo.ReturnType,
_typeMappingSource.FindMapping(typeof(DateTime), "smalldatetime"));
}

if (_timeFromPartsMethodInfo.Equals(method))
{
return _sqlExpressionFactory.Function(
"TIMEFROMPARTS",
arguments.Skip(1),
nullResultAllowed: true,
argumentsPropagateNullability: arguments.Skip(1).Select(a => true),
_timeFromPartsMethodInfo.ReturnType,
_typeMappingSource.FindMapping(typeof(TimeSpan), "time"));
_typeMappingSource.FindMapping(_dateFromPartsMethodInfo.ReturnType, value.ReturnType));
}

return null;
Expand Down

0 comments on commit 6548767

Please sign in to comment.