forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 2
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: dotnet#17223
- Loading branch information
1 parent
f6c782d
commit 25bf352
Showing
3 changed files
with
97 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
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
// 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.Linq; | ||
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 Type[] _typeMapping = | ||
{ | ||
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