-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sqlite: Support translation of ToString
* Resolve: #17223
- Loading branch information
1 parent
bfd0d0e
commit dc559cf
Showing
3 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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.Query; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal | ||
{ | ||
public class SqliteObjectToStringTranslator : IMethodCallTranslator | ||
{ | ||
private static readonly HashSet<Type> _typeMapping = new HashSet<Type> | ||
{ | ||
typeof(int), | ||
typeof(long), | ||
typeof(DateTime), | ||
typeof(Guid), | ||
typeof(byte), | ||
typeof(byte[]), | ||
typeof(double), | ||
typeof(DateTimeOffset), | ||
typeof(char), | ||
typeof(short), | ||
typeof(float), | ||
typeof(decimal), | ||
typeof(TimeSpan), | ||
typeof(uint), | ||
typeof(ushort), | ||
typeof(ulong), | ||
typeof(sbyte), | ||
}; | ||
|
||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
public SqliteObjectToStringTranslator([NotNull] ISqlExpressionFactory sqlExpressionFactory) | ||
=> _sqlExpressionFactory = sqlExpressionFactory; | ||
|
||
public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments) | ||
{ | ||
Check.NotNull(method, nameof(method)); | ||
Check.NotNull(arguments, nameof(arguments)); | ||
|
||
return method.Name == nameof(ToString) | ||
&& arguments.Count == 0 | ||
&& instance != null | ||
&& _typeMapping.Contains(instance.Type.UnwrapNullableType()) | ||
? _sqlExpressionFactory.Convert(instance, typeof(string)) | ||
: null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters