Skip to content
Merged
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
1 change: 1 addition & 0 deletions EFCore.PG.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=setweight/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=smallserial/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=strpos/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Subpath/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=subqueries/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=subquery/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=subscripted/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
using JetBrains.Annotations;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;
using NpgsqlTypes;

// ReSharper disable once CheckNamespace
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal
{
[UsedImplicitly]
public class NpgsqlGeometryTypeMapping<TGeometry> : RelationalGeometryTypeMapping<TGeometry, TGeometry>
public class NpgsqlGeometryTypeMapping<TGeometry> : RelationalGeometryTypeMapping<TGeometry, TGeometry>, INpgsqlTypeMapping
{
readonly bool _isGeography;

/// <inheritdoc />
public virtual NpgsqlDbType NpgsqlDbType
=> _isGeography ? NpgsqlDbType.Geography : NpgsqlDbType.Geometry;

public NpgsqlGeometryTypeMapping([NotNull] string storeType, bool isGeography) : base(converter: null, storeType)
=> _isGeography = isGeography;

Expand All @@ -28,7 +33,7 @@ protected override void ConfigureParameter(DbParameter parameter)
{
base.ConfigureParameter(parameter);

((NpgsqlParameter)parameter).NpgsqlDbType = _isGeography ? NpgsqlDbType.Geography : NpgsqlDbType.Geometry;
((NpgsqlParameter)parameter).NpgsqlDbType = NpgsqlDbType;
}

protected override string GenerateNonNullSqlLiteral(object value)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ public NpgsqlMemberTranslatorProvider(
AddTranslators(
new IMemberTranslator[] {
new NpgsqlArrayTranslator(typeMappingSource, sqlExpressionFactory, JsonPocoTranslator),
new NpgsqlStringMemberTranslator(sqlExpressionFactory),
new NpgsqlDateTimeMemberTranslator(sqlExpressionFactory),
new NpgsqlRangeTranslator(typeMappingSource, sqlExpressionFactory),
new NpgsqlJsonDomTranslator(typeMappingSource, sqlExpressionFactory),
new NpgsqlLTreeTranslator(typeMappingSource, sqlExpressionFactory),
JsonPocoTranslator,
new NpgsqlRangeTranslator(typeMappingSource, sqlExpressionFactory),
new NpgsqlStringMemberTranslator(sqlExpressionFactory),
new NpgsqlTimeSpanMemberTranslator(sqlExpressionFactory)
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Inte
{
public class NpgsqlMethodCallTranslatorProvider : RelationalMethodCallTranslatorProvider
{
public virtual NpgsqlLTreeTranslator LTreeTranslator { get; }

public NpgsqlMethodCallTranslatorProvider(
[NotNull] RelationalMethodCallTranslatorProviderDependencies dependencies,
[NotNull] IRelationalTypeMappingSource typeMappingSource,
Expand All @@ -17,6 +19,7 @@ public NpgsqlMethodCallTranslatorProvider(
var npgsqlSqlExpressionFactory = (NpgsqlSqlExpressionFactory)dependencies.SqlExpressionFactory;
var npgsqlTypeMappingSource = (NpgsqlTypeMappingSource)typeMappingSource;
var jsonTranslator = new NpgsqlJsonPocoTranslator(typeMappingSource, npgsqlSqlExpressionFactory);
LTreeTranslator = new NpgsqlLTreeTranslator(typeMappingSource, npgsqlSqlExpressionFactory);

AddTranslators(new IMethodCallTranslator[]
{
Expand All @@ -29,6 +32,7 @@ public NpgsqlMethodCallTranslatorProvider(
new NpgsqlJsonDomTranslator(typeMappingSource, npgsqlSqlExpressionFactory),
new NpgsqlJsonDbFunctionsTranslator(typeMappingSource, npgsqlSqlExpressionFactory),
new NpgsqlLikeTranslator(npgsqlSqlExpressionFactory),
LTreeTranslator,
new NpgsqlMathTranslator(typeMappingSource, npgsqlSqlExpressionFactory),
new NpgsqlNetworkTranslator(typeMappingSource, npgsqlSqlExpressionFactory),
new NpgsqlNewGuidTranslator(npgsqlSqlExpressionFactory, npgsqlOptions.PostgresVersion),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public enum PostgresExpressionType

JsonExists,
JsonExistsAny,
JsonExistsAll
JsonExistsAll,

LTreeMatches,
LTreeMatchesAny,
LTreeFirstAncestor,
LTreeFirstDescendent,
LTreeFirstMatches
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,66 @@ public class PostgresNewArrayExpression : SqlExpression
/// <summary>
/// Creates a new instance of the <see cref="PostgresNewArrayExpression" /> class.
/// </summary>
/// <param name="initializers">The initializers for the array.</param>
/// <param name="expressions">The values to initialize the elements of the new array.</param>
/// <param name="type">The <see cref="Type"/> of the expression.</param>
/// <param name="typeMapping">The <see cref="RelationalTypeMapping"/> associated with the expression.</param>
public PostgresNewArrayExpression(
[NotNull] IReadOnlyList<SqlExpression> initializers,
[NotNull] IReadOnlyList<SqlExpression> expressions,
[NotNull] Type type,
[CanBeNull] RelationalTypeMapping typeMapping)
: base(type, typeMapping)
{
Check.NotNull(initializers, nameof(initializers));
Check.NotNull(expressions, nameof(expressions));

if (!type.IsArrayOrGenericList())
throw new ArgumentException($"{nameof(PostgresNewArrayExpression)} must have an array type");

Initializers = initializers;
Expressions = expressions;
}

/// <summary>
/// The operator of this PostgreSQL binary operation.
/// </summary>
public virtual IReadOnlyList<SqlExpression> Initializers { get; }
public virtual IReadOnlyList<SqlExpression> Expressions { get; }

/// <inheritdoc />
protected override Expression VisitChildren(ExpressionVisitor visitor)
{
Check.NotNull(visitor, nameof(visitor));

List<SqlExpression> newInitializers = null;
for (var i = 0; i < Initializers.Count; i++)
List<SqlExpression> newExpressions = null;
for (var i = 0; i < Expressions.Count; i++)
{
var initializer = Initializers[i];
var newInitializer = (SqlExpression)visitor.Visit(initializer);
if (newInitializer != initializer && newInitializers is null)
var expression = Expressions[i];
var visitedExpression = (SqlExpression)visitor.Visit(expression);
if (visitedExpression != expression && newExpressions is null)
{
newInitializers = new List<SqlExpression>();
newExpressions = new List<SqlExpression>();
for (var j = 0; j < i; j++)
newInitializers.Add(newInitializer);
newExpressions.Add(visitedExpression);
}

if (newInitializers != null)
newInitializers.Add(newInitializer);
newExpressions?.Add(visitedExpression);
}

return newInitializers is null
return newExpressions is null
? this
: new PostgresNewArrayExpression(newInitializers, Type, TypeMapping);
: new PostgresNewArrayExpression(newExpressions, Type, TypeMapping);
}

/// <summary>
/// Creates a new expression that is like this one, but using the supplied children. If all of the children are the same, it will
/// return this expression.
/// </summary>
/// <param name="initializers">The initializers for the array.</param>
/// <param name="expressions">The values to initialize the elements of the new array.</param>
/// <returns>This expression if no children changed, or an expression with the updated children.</returns>
public virtual PostgresNewArrayExpression Update([NotNull] IReadOnlyList<SqlExpression> initializers)
public virtual PostgresNewArrayExpression Update([NotNull] IReadOnlyList<SqlExpression> expressions)
{
Check.NotNull(initializers, nameof(initializers));
Check.NotNull(expressions, nameof(expressions));

return initializers == Initializers
return expressions == Expressions
? this
: new PostgresNewArrayExpression(initializers, Type, TypeMapping);
: new PostgresNewArrayExpression(expressions, Type, TypeMapping);
}

/// <inheritdoc />
Expand All @@ -90,7 +89,7 @@ protected override void Print(ExpressionPrinter expressionPrinter)
expressionPrinter.Append("ARRAY[");

var first = true;
foreach (var initializer in Initializers)
foreach (var expression in Expressions)
{
if (!first)
{
Expand All @@ -99,7 +98,7 @@ protected override void Print(ExpressionPrinter expressionPrinter)

first = false;

expressionPrinter.Visit(initializer);
expressionPrinter.Visit(expression);
}

expressionPrinter.Append("]");
Expand All @@ -114,16 +113,16 @@ public override bool Equals(object obj)

bool Equals(PostgresNewArrayExpression postgresNewArrayExpression)
=> base.Equals(postgresNewArrayExpression)
&& Initializers.SequenceEqual(postgresNewArrayExpression.Initializers);
&& Expressions.SequenceEqual(postgresNewArrayExpression.Expressions);

/// <inheritdoc />
public override int GetHashCode()
{
var hash = new HashCode();

hash.Add(base.GetHashCode());
for (var i = 0; i < Initializers.Count; i++)
hash.Add(Initializers[i]);
for (var i = 0; i < Expressions.Count; i++)
hash.Add(Expressions[i]);

return hash.ToHashCode();
}
Expand Down
22 changes: 21 additions & 1 deletion src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
Expand Down Expand Up @@ -205,7 +206,7 @@ protected virtual Expression VisitPostgresNewArray([NotNull] PostgresNewArrayExp
{
Sql.Append("ARRAY[");
var first = true;
foreach (var initializer in postgresNewArrayExpression.Initializers)
foreach (var initializer in postgresNewArrayExpression.Expressions)
{
if (!first)
{
Expand Down Expand Up @@ -238,6 +239,9 @@ protected virtual Expression VisitPostgresBinary([NotNull] PostgresBinaryExpress
if (requiresBrackets)
Sql.Append(")");

Debug.Assert(binaryExpression.Left?.TypeMapping is not null);
Debug.Assert(binaryExpression.Right?.TypeMapping is not null);

Sql
.Append(" ")
.Append(binaryExpression.OperatorType switch
Expand Down Expand Up @@ -279,6 +283,22 @@ binaryExpression.Left.TypeMapping is NpgsqlCidrTypeMapping
PostgresExpressionType.JsonExistsAny => "?|",
PostgresExpressionType.JsonExistsAll => "?&",

PostgresExpressionType.LTreeMatches
when binaryExpression.Right.TypeMapping.StoreType == "lquery" ||
binaryExpression.Right.TypeMapping is NpgsqlArrayTypeMapping arrayMapping &&
arrayMapping.ElementMapping.StoreType == "lquery"
=> "~",
PostgresExpressionType.LTreeMatches
when binaryExpression.Right.TypeMapping.StoreType == "ltxtquery"
=> "@",
PostgresExpressionType.LTreeMatchesAny => "?",
PostgresExpressionType.LTreeFirstAncestor => "?@>",
PostgresExpressionType.LTreeFirstDescendent => "?<@",
PostgresExpressionType.LTreeFirstMatches
when binaryExpression.Right.TypeMapping.StoreType == "lquery" => "?~",
PostgresExpressionType.LTreeFirstMatches
when binaryExpression.Right.TypeMapping.StoreType == "ltxtquery" => "?@",

_ => throw new ArgumentOutOfRangeException(
$"Unhandled operator type: {binaryExpression.OperatorType}")
})
Expand Down
12 changes: 9 additions & 3 deletions src/EFCore.PG/Query/Internal/NpgsqlSqlNullabilityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,13 @@ protected virtual SqlExpression VisitBinary(
var left = Visit(binaryExpression.Left, allowOptimizedExpansion, out var leftNullable);
var right = Visit(binaryExpression.Right, allowOptimizedExpansion, out var rightNullable);

nullable = leftNullable || rightNullable;
nullable = binaryExpression.OperatorType switch
{
PostgresExpressionType.LTreeFirstAncestor => true,
PostgresExpressionType.LTreeFirstDescendent => true,
PostgresExpressionType.LTreeFirstMatches => true,
_ => leftNullable || rightNullable
};

return binaryExpression.Update(left, right);
}
Expand Down Expand Up @@ -244,9 +250,9 @@ protected virtual SqlExpression VisitNewArray(
Check.NotNull(newArrayExpression, nameof(newArrayExpression));

List<SqlExpression> newInitializers = null;
for (var i = 0; i < newArrayExpression.Initializers.Count; i++)
for (var i = 0; i < newArrayExpression.Expressions.Count; i++)
{
var initializer = newArrayExpression.Initializers[i];
var initializer = newArrayExpression.Expressions[i];
var newInitializer = Visit(initializer, allowOptimizedExpansion, out _);
if (newInitializer != initializer && newInitializers is null)
{
Expand Down
Loading