|
| 1 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions; |
| 2 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal; |
| 3 | +using static Npgsql.EntityFrameworkCore.PostgreSQL.Utilities.Statics; |
| 4 | + |
| 5 | +namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 9 | +/// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 10 | +/// any release. You should only use it directly in your code with extreme caution and knowing that |
| 11 | +/// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 12 | +/// </summary> |
| 13 | +public class NpgsqlCubeTranslator( |
| 14 | + NpgsqlSqlExpressionFactory sqlExpressionFactory, |
| 15 | + IRelationalTypeMappingSource typeMappingSource) : IMethodCallTranslator, IMemberTranslator |
| 16 | +{ |
| 17 | + private readonly RelationalTypeMapping _cubeTypeMapping = typeMappingSource.FindMapping(typeof(NpgsqlCube))!; |
| 18 | + private readonly RelationalTypeMapping _doubleTypeMapping = typeMappingSource.FindMapping(typeof(double))!; |
| 19 | + |
| 20 | + /// <inheritdoc /> |
| 21 | + public virtual SqlExpression? Translate( |
| 22 | + SqlExpression? instance, |
| 23 | + MethodInfo method, |
| 24 | + IReadOnlyList<SqlExpression> arguments, |
| 25 | + IDiagnosticsLogger<DbLoggerCategory.Query> logger) |
| 26 | + { |
| 27 | + // Handle NpgsqlCubeDbFunctionsExtensions methods |
| 28 | + if (method.DeclaringType != typeof(NpgsqlCubeDbFunctionsExtensions)) |
| 29 | + { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + return method.Name switch |
| 34 | + { |
| 35 | + nameof(NpgsqlCubeDbFunctionsExtensions.Overlaps) when arguments is [var cube1, var cube2] |
| 36 | + => sqlExpressionFactory.Overlaps(cube1, cube2), |
| 37 | + |
| 38 | + nameof(NpgsqlCubeDbFunctionsExtensions.Contains) when arguments is [var cube1, var cube2] |
| 39 | + => sqlExpressionFactory.Contains(cube1, cube2), |
| 40 | + |
| 41 | + nameof(NpgsqlCubeDbFunctionsExtensions.ContainedBy) when arguments is [var cube1, var cube2] |
| 42 | + => sqlExpressionFactory.ContainedBy(cube1, cube2), |
| 43 | + |
| 44 | + nameof(NpgsqlCubeDbFunctionsExtensions.Distance) when arguments is [var cube1, var cube2] |
| 45 | + => new PgBinaryExpression( |
| 46 | + PgExpressionType.Distance, |
| 47 | + sqlExpressionFactory.ApplyTypeMapping(cube1, _cubeTypeMapping), |
| 48 | + sqlExpressionFactory.ApplyTypeMapping(cube2, _cubeTypeMapping), |
| 49 | + typeof(double), |
| 50 | + _doubleTypeMapping), |
| 51 | + |
| 52 | + nameof(NpgsqlCubeDbFunctionsExtensions.DistanceTaxicab) when arguments is [var cube1, var cube2] |
| 53 | + => new PgBinaryExpression( |
| 54 | + PgExpressionType.CubeDistanceTaxicab, |
| 55 | + sqlExpressionFactory.ApplyTypeMapping(cube1, _cubeTypeMapping), |
| 56 | + sqlExpressionFactory.ApplyTypeMapping(cube2, _cubeTypeMapping), |
| 57 | + typeof(double), |
| 58 | + _doubleTypeMapping), |
| 59 | + |
| 60 | + nameof(NpgsqlCubeDbFunctionsExtensions.DistanceChebyshev) when arguments is [var cube1, var cube2] |
| 61 | + => new PgBinaryExpression( |
| 62 | + PgExpressionType.CubeDistanceChebyshev, |
| 63 | + sqlExpressionFactory.ApplyTypeMapping(cube1, _cubeTypeMapping), |
| 64 | + sqlExpressionFactory.ApplyTypeMapping(cube2, _cubeTypeMapping), |
| 65 | + typeof(double), |
| 66 | + _doubleTypeMapping), |
| 67 | + |
| 68 | + nameof(NpgsqlCubeDbFunctionsExtensions.NthCoordinate) when arguments is [var cube, var index] |
| 69 | + => new PgBinaryExpression( |
| 70 | + PgExpressionType.CubeNthCoordinate, |
| 71 | + sqlExpressionFactory.ApplyTypeMapping(cube, _cubeTypeMapping), |
| 72 | + ConvertToPostgresIndex(index), |
| 73 | + typeof(double), |
| 74 | + _doubleTypeMapping), |
| 75 | + |
| 76 | + nameof(NpgsqlCubeDbFunctionsExtensions.NthCoordinateKnn) when arguments is [var cube, var index] |
| 77 | + => new PgBinaryExpression( |
| 78 | + PgExpressionType.CubeNthCoordinateKnn, |
| 79 | + sqlExpressionFactory.ApplyTypeMapping(cube, _cubeTypeMapping), |
| 80 | + ConvertToPostgresIndex(index), |
| 81 | + typeof(double), |
| 82 | + _doubleTypeMapping), |
| 83 | + |
| 84 | + nameof(NpgsqlCubeDbFunctionsExtensions.Union) when arguments is [var cube1, var cube2] |
| 85 | + => sqlExpressionFactory.Function( |
| 86 | + "cube_union", |
| 87 | + [cube1, cube2], |
| 88 | + nullable: true, |
| 89 | + argumentsPropagateNullability: TrueArrays[2], |
| 90 | + typeof(NpgsqlCube), |
| 91 | + typeMappingSource.FindMapping(typeof(NpgsqlCube))), |
| 92 | + |
| 93 | + nameof(NpgsqlCubeDbFunctionsExtensions.Intersect) when arguments is [var cube1, var cube2] |
| 94 | + => sqlExpressionFactory.Function( |
| 95 | + "cube_inter", |
| 96 | + [cube1, cube2], |
| 97 | + nullable: true, |
| 98 | + argumentsPropagateNullability: TrueArrays[2], |
| 99 | + typeof(NpgsqlCube), |
| 100 | + typeMappingSource.FindMapping(typeof(NpgsqlCube))), |
| 101 | + |
| 102 | + nameof(NpgsqlCubeDbFunctionsExtensions.Enlarge) when arguments is [var cube1, var cube2, var dimension] |
| 103 | + => sqlExpressionFactory.Function( |
| 104 | + "cube_enlarge", |
| 105 | + [cube1, cube2, dimension], |
| 106 | + nullable: true, |
| 107 | + argumentsPropagateNullability: TrueArrays[3], |
| 108 | + typeof(NpgsqlCube), |
| 109 | + typeMappingSource.FindMapping(typeof(NpgsqlCube))), |
| 110 | + |
| 111 | + _ => null |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + /// <inheritdoc /> |
| 116 | + public virtual SqlExpression? Translate( |
| 117 | + SqlExpression? instance, |
| 118 | + MemberInfo member, |
| 119 | + Type returnType, |
| 120 | + IDiagnosticsLogger<DbLoggerCategory.Query> logger) |
| 121 | + { |
| 122 | + if (member.DeclaringType != typeof(NpgsqlCube)) |
| 123 | + { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + return member.Name switch |
| 128 | + { |
| 129 | + nameof(NpgsqlCube.Dimensions) |
| 130 | + => sqlExpressionFactory.Function( |
| 131 | + "cube_dim", |
| 132 | + [instance!], |
| 133 | + nullable: true, |
| 134 | + argumentsPropagateNullability: TrueArrays[1], |
| 135 | + typeof(int)), |
| 136 | + |
| 137 | + nameof(NpgsqlCube.IsPoint) |
| 138 | + => sqlExpressionFactory.Function( |
| 139 | + "cube_is_point", |
| 140 | + [instance!], |
| 141 | + nullable: true, |
| 142 | + argumentsPropagateNullability: TrueArrays[1], |
| 143 | + typeof(bool)), |
| 144 | + |
| 145 | + nameof(NpgsqlCube.LowerLeft) |
| 146 | + => throw new InvalidOperationException( |
| 147 | + $"The '{nameof(NpgsqlCube.LowerLeft)}' property cannot be translated to SQL. " + |
| 148 | + $"To access individual lower-left coordinates in queries, use indexer syntax (e.g., cube.LowerLeft[index]) instead."), |
| 149 | + |
| 150 | + nameof(NpgsqlCube.UpperRight) |
| 151 | + => throw new InvalidOperationException( |
| 152 | + $"The '{nameof(NpgsqlCube.UpperRight)}' property cannot be translated to SQL. " + |
| 153 | + $"To access individual upper-right coordinates in queries, use indexer syntax (e.g., cube.UpperRight[index]) instead."), |
| 154 | + |
| 155 | + _ => null |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Converts a zero-based index to one-based for PostgreSQL cube functions. |
| 161 | + /// For constant indexes, simplifies at translation time to avoid unnecessary addition in SQL. |
| 162 | + /// </summary> |
| 163 | + private SqlExpression ConvertToPostgresIndex(SqlExpression indexExpression) |
| 164 | + { |
| 165 | + var intTypeMapping = typeMappingSource.FindMapping(typeof(int)); |
| 166 | + |
| 167 | + return indexExpression is SqlConstantExpression { Value: int index } |
| 168 | + ? sqlExpressionFactory.Constant(index + 1, intTypeMapping) |
| 169 | + : sqlExpressionFactory.Add(indexExpression, sqlExpressionFactory.Constant(1, intTypeMapping)); |
| 170 | + } |
| 171 | +} |
0 commit comments