Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Marusyk committed Mar 24, 2020
1 parent cd4b3f2 commit 6a2e2e3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ public static TimeSpan TimeFromParts(
public static int? DataLength(
[CanBeNull] this DbFunctions _,
[CanBeNull] string arg)
=> throw new InvalidOperationException(SqlServerStrings.FunctionOnClient(nameof(DataLength)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DataLength)));

/// <summary>
/// Returns the number of bytes used to represent any expression.
Expand All @@ -987,7 +987,7 @@ public static TimeSpan TimeFromParts(
public static int? DataLength(
[CanBeNull] this DbFunctions _,
bool? arg)
=> throw new InvalidOperationException(SqlServerStrings.FunctionOnClient(nameof(DataLength)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DataLength)));

/// <summary>
/// Returns the number of bytes used to represent any expression.
Expand All @@ -998,7 +998,7 @@ public static TimeSpan TimeFromParts(
public static int? DataLength(
[CanBeNull] this DbFunctions _,
double? arg)
=> throw new InvalidOperationException(SqlServerStrings.FunctionOnClient(nameof(DataLength)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DataLength)));

/// <summary>
/// Returns the number of bytes used to represent any expression.
Expand All @@ -1009,7 +1009,7 @@ public static TimeSpan TimeFromParts(
public static int? DataLength(
[CanBeNull] this DbFunctions _,
decimal? arg)
=> throw new InvalidOperationException(SqlServerStrings.FunctionOnClient(nameof(DataLength)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DataLength)));

/// <summary>
/// Returns the number of bytes used to represent any expression.
Expand All @@ -1020,7 +1020,7 @@ public static TimeSpan TimeFromParts(
public static int? DataLength(
[CanBeNull] this DbFunctions _,
DateTime? arg)
=> throw new InvalidOperationException(SqlServerStrings.FunctionOnClient(nameof(DataLength)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DataLength)));

/// <summary>
/// Returns the number of bytes used to represent any expression.
Expand All @@ -1031,7 +1031,7 @@ public static TimeSpan TimeFromParts(
public static int? DataLength(
[CanBeNull] this DbFunctions _,
TimeSpan? arg)
=> throw new InvalidOperationException(SqlServerStrings.FunctionOnClient(nameof(DataLength)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DataLength)));

/// <summary>
/// Returns the number of bytes used to represent any expression.
Expand All @@ -1042,7 +1042,7 @@ public static TimeSpan TimeFromParts(
public static int? DataLength(
[CanBeNull] this DbFunctions _,
DateTimeOffset? arg)
=> throw new InvalidOperationException(SqlServerStrings.FunctionOnClient(nameof(DataLength)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DataLength)));

/// <summary>
/// Returns the number of bytes used to represent any expression.
Expand All @@ -1053,7 +1053,7 @@ public static TimeSpan TimeFromParts(
public static int? DataLength(
[CanBeNull] this DbFunctions _,
[CanBeNull] byte[] arg)
=> throw new InvalidOperationException(SqlServerStrings.FunctionOnClient(nameof(DataLength)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DataLength)));

/// <summary>
/// Returns the number of bytes used to represent any expression.
Expand All @@ -1064,6 +1064,6 @@ public static TimeSpan TimeFromParts(
public static int? DataLength(
[CanBeNull] this DbFunctions _,
Guid? arg)
=> throw new InvalidOperationException(SqlServerStrings.FunctionOnClient(nameof(DataLength)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DataLength)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal
{
public class SqlServerDataLengthFunctionTranslator : IMethodCallTranslator
{
private static readonly List<string> _longReturningTypes = new List<string> { "nvarchar(max)", "varchar(max)", "varbinary(max)" };

private static readonly HashSet<MethodInfo> _methodInfoDataLengthMapping
= new HashSet<MethodInfo>
{
Expand Down Expand Up @@ -68,6 +70,24 @@ public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method

if (_methodInfoDataLengthMapping.Contains(method))
{
var argument = arguments[1];
if (argument.TypeMapping == null)
{
argument = _sqlExpressionFactory.ApplyDefaultTypeMapping(argument);
}

if (_longReturningTypes.Contains(argument.TypeMapping.StoreType))
{
var result = _sqlExpressionFactory.Function(
"DATALENGTH",
arguments.Skip(1),
nullable: true,
argumentsPropagateNullability: new[] { true },
typeof(long));

return _sqlExpressionFactory.Convert(result, method.ReturnType);
}

return _sqlExpressionFactory.Function(
"DATALENGTH",
arguments.Skip(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestModels.GearsOfWarModel;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.EntityFrameworkCore.Query
Expand Down Expand Up @@ -6976,6 +6979,20 @@ FROM [Gears] AS [g]
ORDER BY [g].[Nickname]");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task DataLength_function_for_string_parameter(bool async)
{
await AssertQueryScalar(
async,
ss => ss.Set<Mission>().Select(m => EF.Functions.DataLength(m.CodeName)),
ss => ss.Set<Mission>().Select(m => (int?)(m.CodeName.Length * 2)));

AssertSql(
@"SELECT CAST(DATALENGTH([m].[CodeName]) AS int)
FROM [Missions] AS [m]");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
Expand Down

0 comments on commit 6a2e2e3

Please sign in to comment.