-
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.
- Loading branch information
Showing
7 changed files
with
200 additions
and
1 deletion.
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
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
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
66 changes: 66 additions & 0 deletions
66
src/EFCore.Sqlite.Core/Query/Internal/SqliteRegexTranslator.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,66 @@ | ||
// 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.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
using System.Text.RegularExpressions; | ||
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 SqliteRegexTranslator : IMethodCallTranslator | ||
{ | ||
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 SqliteRegexTranslator([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 SqlExpression Translate(SqlExpression instance, | ||
MethodInfo method, | ||
IReadOnlyList<SqlExpression> arguments, | ||
IDiagnosticsLogger<DbLoggerCategory.Query> logger) | ||
{ | ||
Check.NotNull(method, nameof(method)); | ||
Check.NotNull(arguments, nameof(arguments)); | ||
Check.NotNull(logger, nameof(logger)); | ||
|
||
var regexIsMatchMethod = typeof(Regex).GetMethod(nameof(Regex.IsMatch), new Type[] { typeof(string), typeof(string) }); | ||
|
||
if (method == regexIsMatchMethod) | ||
{ | ||
return _sqlExpressionFactory.Function("regexp", | ||
arguments, | ||
false, | ||
new[] { false, false }, | ||
typeof(bool), | ||
arguments[0].TypeMapping); | ||
} | ||
|
||
return 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
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,70 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.TestUtilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public class RegexIsMatchTest : IClassFixture<RegexIsMatchFixture> | ||
{ | ||
public RegexIsMatchTest(RegexIsMatchFixture fixture) | ||
=> Fixture = fixture; | ||
protected RegexIsMatchFixture Fixture { get; } | ||
|
||
[ConditionalFact] | ||
public void Can_use_Regex_IsMatch() | ||
{ | ||
using (var context = CreateContext()) | ||
{ | ||
var honeyDijon = context.Add(new User { Name = "Test1" }).Entity; | ||
var buffaloBleu = context.Add(new User { Name = "NTest2" }).Entity; | ||
var buffaloBleuNull = context.Add(new User { Name = null }).Entity; | ||
|
||
context.SaveChanges(); | ||
|
||
var startingWithT = context.Users.Where(f => Regex.IsMatch(f.Name, @"^T")); | ||
|
||
var nullPattern = context.Users.Where(f => Regex.IsMatch(f.Name, null)); | ||
|
||
Assert.Single(startingWithT); | ||
Assert.Empty(nullPattern); | ||
} | ||
} | ||
|
||
private RegexIsMatchContext CreateContext() | ||
=> Fixture.CreateContext(); | ||
} | ||
public class RegexIsMatchFixture : SharedStoreFixtureBase<RegexIsMatchContext> | ||
{ | ||
protected override string StoreName { get; } = "RegexIsMatchTest"; | ||
|
||
protected override ITestStoreFactory TestStoreFactory | ||
=> SqliteTestStoreFactory.Instance; | ||
|
||
protected override Type ContextType | ||
=> typeof(RegexIsMatchContext); | ||
|
||
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder) | ||
{ | ||
return base.AddOptions(builder).UseSqlite(f => f.EnableRegex()); | ||
} | ||
} | ||
|
||
public class RegexIsMatchContext : PoolableDbContext | ||
{ | ||
public RegexIsMatchContext(DbContextOptions options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public DbSet<User> Users { get; set; } | ||
} | ||
|
||
public class User | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
} |
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