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

Sqlite: Support translation of ToString #19473

Merged
merged 2 commits into from
Oct 31, 2020
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 @@ -31,6 +31,7 @@ public SqliteMethodCallTranslatorProvider([NotNull] RelationalMethodCallTranslat
new SqliteByteArrayMethodTranslator(sqlExpressionFactory),
new SqliteDateTimeAddTranslator(sqlExpressionFactory),
new SqliteMathTranslator(sqlExpressionFactory),
new SqliteObjectToStringTranslator(sqlExpressionFactory),
new SqliteRegexMethodTranslator(sqlExpressionFactory),
new SqliteStringMethodTranslator(sqlExpressionFactory)
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class SqliteObjectToStringTranslator : IMethodCallTranslator
{
private static readonly HashSet<Type> _typeMapping = new HashSet<Type>
{
typeof(bool),
typeof(byte),
typeof(byte[]),
bricelam marked this conversation as resolved.
Show resolved Hide resolved
typeof(char),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(decimal),
typeof(double),
typeof(float),
typeof(Guid),
typeof(int),
typeof(long),
typeof(sbyte),
typeof(short),
typeof(TimeSpan),
typeof(uint),
typeof(ushort),
};

private readonly ISqlExpressionFactory _sqlExpressionFactory;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqliteObjectToStringTranslator([NotNull] ISqlExpressionFactory sqlExpressionFactory)
=> _sqlExpressionFactory = sqlExpressionFactory;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual SqlExpression Translate(
SqlExpression instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));

return method.Name == nameof(ToString)
&& arguments.Count == 0
&& instance != null
&& _typeMapping.Contains(instance.Type)
? _sqlExpressionFactory.Convert(instance, typeof(string))
: null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ public override void Object_to_string_conversion()
base.Object_to_string_conversion();

AssertSql(
@"SELECT ""b"".""TestSignedByte"", ""b"".""TestByte"", ""b"".""TestInt16"", ""b"".""TestUnsignedInt16"", ""b"".""TestInt32"", ""b"".""TestUnsignedInt32"", ""b"".""TestInt64"", ""b"".""TestUnsignedInt64"", ""b"".""TestSingle"", ""b"".""TestDouble"", ""b"".""TestDecimal"", ""b"".""TestCharacter"", ""b"".""TestDateTime"", ""b"".""TestDateTimeOffset"", ""b"".""TestTimeSpan""
@"SELECT CAST(""b"".""TestSignedByte"" AS TEXT), CAST(""b"".""TestByte"" AS TEXT), CAST(""b"".""TestInt16"" AS TEXT), CAST(""b"".""TestUnsignedInt16"" AS TEXT), CAST(""b"".""TestInt32"" AS TEXT), CAST(""b"".""TestUnsignedInt32"" AS TEXT), CAST(""b"".""TestInt64"" AS TEXT), ""b"".""TestUnsignedInt64"", CAST(""b"".""TestSingle"" AS TEXT), CAST(""b"".""TestDouble"" AS TEXT), CAST(""b"".""TestDecimal"" AS TEXT), CAST(""b"".""TestCharacter"" AS TEXT), CAST(""b"".""TestDateTime"" AS TEXT), CAST(""b"".""TestDateTimeOffset"" AS TEXT), CAST(""b"".""TestTimeSpan"" AS TEXT)
FROM ""BuiltInDataTypes"" AS ""b""
WHERE ""b"".""Id"" = 13");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ public NorthwindMiscellaneousQuerySqliteTest(
//Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
}

public override Task Query_expression_with_to_string_and_contains(bool async)
=> AssertTranslationFailed(() => base.Query_expression_with_to_string_and_contains(async));
public override async Task Query_expression_with_to_string_and_contains(bool async)
{
await base.Query_expression_with_to_string_and_contains(async);

AssertSql(
@"SELECT ""o"".""CustomerID""
FROM ""Orders"" AS ""o""
WHERE ""o"".""OrderDate"" IS NOT NULL AND (('10' = '') OR (instr(CAST(""o"".""EmployeeID"" AS TEXT), '10') > 0))");
}

public override async Task Take_Skip(bool async)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,15 @@ public override async Task Decimal_cast_to_double_works(bool async)
WHERE CAST(""p"".""UnitPrice"" AS REAL) > 100.0");
}

[ConditionalTheory(Skip = "Issue#17223")]
public override Task Like_with_non_string_column_using_ToString(bool async)
=> base.Like_with_non_string_column_using_ToString(async);
public override async Task Like_with_non_string_column_using_ToString(bool async)
{
await base.Like_with_non_string_column_using_ToString(async);

AssertSql(
@"SELECT ""o"".""OrderID"", ""o"".""CustomerID"", ""o"".""EmployeeID"", ""o"".""OrderDate""
FROM ""Orders"" AS ""o""
WHERE CAST(""o"".""OrderID"" AS TEXT) LIKE '%20%'");
}

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