Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/EFCore.Ydb/src/Extensions/YdbDbFunctionsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Microsoft.EntityFrameworkCore;

namespace EntityFrameworkCore.Ydb.Extensions;

public static class YdbDbFunctionsExtension
{
public const string InvalidCallMessage = "This function is designed only for LINQ queries";
public static bool ILike(this DbFunctions dbFunctions, string match, string pattern)
=> throw new NotSupportedException(InvalidCallMessage);
}
60 changes: 60 additions & 0 deletions src/EFCore.Ydb/src/Query/Expressions/YdbILikeExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;

namespace EntityFrameworkCore.Ydb.Query.Expressions;

public class YdbILikeExpression: SqlExpression
{
public const string ILikeConst = "ILIKE";
public const string ILikeWithSpacesConst = " " + ILikeConst + " ";

public virtual SqlExpression Match { get; }

public virtual SqlExpression Pattern { get; }

public YdbILikeExpression(SqlExpression match, SqlExpression pattern, RelationalTypeMapping? typeMapping)
: base(typeof(bool), typeMapping)
{
Match = match;
Pattern = pattern;
}

public override Expression Quote() => new YdbILikeExpression((SqlExpression)Match.Quote(), (SqlExpression)Pattern.Quote(), TypeMapping);

protected override Expression VisitChildren(ExpressionVisitor visitor)
=> Update(
(SqlExpression)visitor.Visit(Match),
(SqlExpression)visitor.Visit(Pattern));

public YdbILikeExpression Update(
SqlExpression match,
SqlExpression pattern)
=> match == Match && pattern == Pattern

Check warning on line 35 in src/EFCore.Ydb/src/Query/Expressions/YdbILikeExpression.cs

View workflow job for this annotation

GitHub Actions / Inspection (./src/YdbSdk.sln)

"[PossibleUnintendedReferenceComparison] Possible unintended reference comparison. To get a value comparison, use 'Equals' method." on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/EFCore.Ydb/src/Query/Expressions/YdbILikeExpression.cs(35,32)

Check warning on line 35 in src/EFCore.Ydb/src/Query/Expressions/YdbILikeExpression.cs

View workflow job for this annotation

GitHub Actions / Inspection (./src/YdbSdk.sln)

"[PossibleUnintendedReferenceComparison] Possible unintended reference comparison. To get a value comparison, use 'Equals' method." on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/EFCore.Ydb/src/Query/Expressions/YdbILikeExpression.cs(35,12)
? this
: new YdbILikeExpression(match, pattern, TypeMapping);

public override bool Equals(object? obj)
=> obj is YdbILikeExpression other && Equals(other);

public virtual bool Equals(YdbILikeExpression? other)
=> ReferenceEquals(this, other)
|| other is not null
&& base.Equals(other)
&& Equals(Match, other.Match)
&& Equals(Pattern, other.Pattern);

public override int GetHashCode()
=> HashCode.Combine(base.GetHashCode(), Match, Pattern);

protected override void Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.Visit(Match);
expressionPrinter.Append(ILikeWithSpacesConst);
expressionPrinter.Visit(Pattern);
}

public override string ToString() => $"{Match} ILIKE {Pattern}";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Reflection;
using EntityFrameworkCore.Ydb.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

namespace EntityFrameworkCore.Ydb.Query.Internal.Translators;

public class YdbILikeMethodTranslator: IMethodCallTranslator
{
private static readonly MethodInfo ILike =
typeof(YdbDbFunctionsExtension).GetRuntimeMethod(

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Possible null reference assignment.

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Possible null reference assignment.

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Possible null reference assignment.

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Possible null reference assignment.

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / efcore-tests (trunk, 9.0.x)

Possible null reference assignment.

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / efcore-tests (latest, 9.0.x)

Possible null reference assignment.

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / efcore-tests (trunk, 8.0.x)

Possible null reference assignment.

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / efcore-tests (latest, 8.0.x)

Possible null reference assignment.

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / Inspection (./src/YdbSdk.sln)

"[CSharpWarnings::CS8601] Possible null reference assignment" on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs(14,3)

Check warning on line 14 in src/EFCore.Ydb/src/Query/Internal/Translators/YdbILikeMethodTranslator.cs

View workflow job for this annotation

GitHub Actions / SLO test (EF)

Possible null reference assignment.
nameof(YdbDbFunctionsExtension.ILike),
[typeof(DbFunctions), typeof(string), typeof(string)]);

private readonly YdbSqlExpressionFactory _sqlExpressionFactory;

public YdbILikeMethodTranslator(YdbSqlExpressionFactory sqlExpressionFactory)
{
_sqlExpressionFactory = sqlExpressionFactory;
}

public SqlExpression? Translate(SqlExpression? instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (method == ILike)
{
return _sqlExpressionFactory.ILike(arguments[1], arguments[2]);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

namespace EntityFrameworkCore.Ydb.Query.Internal;

public sealed class YdbMethodCallTranslatorProvider : RelationalMethodCallTranslatorProvider
public class YdbMethodCallTranslatorProvider : RelationalMethodCallTranslatorProvider
{
public YdbMethodCallTranslatorProvider(RelationalMethodCallTranslatorProviderDependencies dependencies) :
base(dependencies)
{
var sqlExpressionFactory = (YdbSqlExpressionFactory)dependencies.SqlExpressionFactory;

AddTranslators(

Check warning on line 13 in src/EFCore.Ydb/src/Query/Internal/YdbMethodCallTranslatorProvider.cs

View workflow job for this annotation

GitHub Actions / Inspection (./src/YdbSdk.sln)

"[VirtualMemberCallInConstructor] Virtual member call in constructor" on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/EFCore.Ydb/src/Query/Internal/YdbMethodCallTranslatorProvider.cs(13,9)
[
new YdbDateTimeMethodTranslator(sqlExpressionFactory),
new YdbMathTranslator(sqlExpressionFactory),
new YdbByteArrayMethodTranslator(sqlExpressionFactory)
new YdbByteArrayMethodTranslator(sqlExpressionFactory),
new YdbILikeMethodTranslator(sqlExpressionFactory)
]
);
}
Expand Down
18 changes: 14 additions & 4 deletions src/EFCore.Ydb/src/Query/Internal/YdbParameterBasedSqlProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query;

namespace EntityFrameworkCore.Ydb.Query.Internal;

public class YdbParameterBasedSqlProcessor(
RelationalParameterBasedSqlProcessorDependencies dependencies,
RelationalParameterBasedSqlProcessorParameters parameters
) : RelationalParameterBasedSqlProcessor(dependencies, parameters);
public class YdbParameterBasedSqlProcessor : RelationalParameterBasedSqlProcessor
{
public YdbParameterBasedSqlProcessor(RelationalParameterBasedSqlProcessorDependencies dependencies,
RelationalParameterBasedSqlProcessorParameters parameters) : base(dependencies, parameters)
{
}

protected override Expression ProcessSqlNullability(Expression queryExpression,
IReadOnlyDictionary<string, object?> parametersValues, out bool canCache) =>
new YdbSqlNullabilityProcessor(Dependencies, Parameters).Process(queryExpression, parametersValues,
out canCache);
}
36 changes: 36 additions & 0 deletions src/EFCore.Ydb/src/Query/Internal/YdbQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using EntityFrameworkCore.Ydb.Query.Expressions;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -320,4 +321,39 @@ protected override Expression VisitCase(CaseExpression caseExpression)

return caseExpression;
}


protected override Expression VisitSqlUnary(SqlUnaryExpression sqlUnaryExpression)
{
if (sqlUnaryExpression.OperatorType == ExpressionType.Not && sqlUnaryExpression.Type == typeof(bool)
&&
sqlUnaryExpression.Operand is YdbILikeExpression iLikeExpression)
{
VisitILike(iLikeExpression, true);
return sqlUnaryExpression;
}

return base.VisitSqlUnary(sqlUnaryExpression);
}

protected override Expression VisitExtension(Expression extensionExpression) =>
extensionExpression switch
{
YdbILikeExpression ilikeExpression => VisitILike(ilikeExpression, false),
_ => base.VisitExtension(extensionExpression)
};



private Expression VisitILike(YdbILikeExpression ilikeExpression, bool not)
{
Visit(ilikeExpression.Match);
if (not)
{
Sql.Append(" NOT");
}
Sql.Append(" ILIKE ");
Visit(ilikeExpression.Pattern);
return ilikeExpression;
}
}
26 changes: 23 additions & 3 deletions src/EFCore.Ydb/src/Query/Internal/YdbSqlExpressionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;

Check warning on line 1 in src/EFCore.Ydb/src/Query/Internal/YdbSqlExpressionFactory.cs

View workflow job for this annotation

GitHub Actions / Inspection (./src/YdbSdk.sln)

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/EFCore.Ydb/src/Query/Internal/YdbSqlExpressionFactory.cs(1,1)
using System.Diagnostics.CodeAnalysis;
using EntityFrameworkCore.Ydb.Query.Expressions;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
Expand All @@ -7,7 +9,25 @@

public class YdbSqlExpressionFactory(SqlExpressionFactoryDependencies dependencies) : SqlExpressionFactory(dependencies)
{
[return: NotNullIfNotNull("sqlExpression")]
public override SqlExpression? ApplyTypeMapping(SqlExpression? sqlExpression, RelationalTypeMapping? typeMapping) =>
base.ApplyTypeMapping(sqlExpression, typeMapping);
[return: NotNullIfNotNull("sqlExpression")]
public override SqlExpression? ApplyTypeMapping(SqlExpression? sqlExpression, RelationalTypeMapping? typeMapping) =>
sqlExpression switch
{
YdbILikeExpression ilikeExpression => ApplyILikeExpression(ilikeExpression, typeMapping),
_ => base.ApplyTypeMapping(sqlExpression, typeMapping)
};


protected SqlExpression ApplyILikeExpression(YdbILikeExpression ilikeExpression, RelationalTypeMapping? typeMapping)
{
var inferredTypeMapping = ExpressionExtensions.InferTypeMapping(ilikeExpression.Match, ilikeExpression.Pattern);

return new YdbILikeExpression(ApplyTypeMapping(ilikeExpression.Match, inferredTypeMapping),
ApplyTypeMapping(ilikeExpression.Pattern, inferredTypeMapping), typeMapping);
}

public YdbILikeExpression ILike(
SqlExpression match,
SqlExpression pattern)
=> (YdbILikeExpression)ApplyDefaultTypeMapping(new YdbILikeExpression(match, pattern, null));
}
33 changes: 33 additions & 0 deletions src/EFCore.Ydb/src/Query/Internal/YdbSqlNullabilityProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using EntityFrameworkCore.Ydb.Query.Expressions;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

namespace EntityFrameworkCore.Ydb.Query.Internal;

public class YdbSqlNullabilityProcessor: SqlNullabilityProcessor
{
private readonly ISqlExpressionFactory _sqlExpressionFactory;

Check warning on line 9 in src/EFCore.Ydb/src/Query/Internal/YdbSqlNullabilityProcessor.cs

View workflow job for this annotation

GitHub Actions / Inspection (./src/YdbSdk.sln)

"[NotAccessedField.Local] Field '_sqlExpressionFactory' is assigned but its value is never used" on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/EFCore.Ydb/src/Query/Internal/YdbSqlNullabilityProcessor.cs(9,41)
public YdbSqlNullabilityProcessor(RelationalParameterBasedSqlProcessorDependencies dependencies, RelationalParameterBasedSqlProcessorParameters parameters) : base(dependencies, parameters)
{
_sqlExpressionFactory = dependencies.SqlExpressionFactory;
}

protected override SqlExpression VisitCustomSqlExpression(SqlExpression sqlExpression, bool allowOptimizedExpansion,
out bool nullable) => sqlExpression switch
{
YdbILikeExpression ilikeExpression => VisitILikeExpression(ilikeExpression, allowOptimizedExpansion,
out nullable),
_ => base.VisitCustomSqlExpression(sqlExpression, allowOptimizedExpansion, out nullable)
};

protected SqlExpression VisitILikeExpression(YdbILikeExpression ilikeExpression, bool allowOptimizedExpansion, out bool nullable)
{
var match = Visit(ilikeExpression.Match, out var matchNullable);
var pattern = Visit(ilikeExpression.Pattern, out var patternNullable);

nullable = matchNullable || patternNullable;
SqlExpression result = ilikeExpression.Update(match, pattern);

return result;
}
}
Loading