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
15 changes: 15 additions & 0 deletions EFCore.PG.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.PG.Plugins.Functiona
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.PG.NodaTime", "src\EFCore.PG.NodaTime\EFCore.PG.NodaTime.csproj", "{77F0608F-6D0C-481C-9108-D5176E2EAD69}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCore.PG.NTS", "src\EFCore.PG.NTS\EFCore.PG.NTS.csproj", "{D7106D61-C7CA-4005-B31F-43281BB397AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -79,6 +81,18 @@ Global
{77F0608F-6D0C-481C-9108-D5176E2EAD69}.Release|x64.Build.0 = Release|Any CPU
{77F0608F-6D0C-481C-9108-D5176E2EAD69}.Release|x86.ActiveCfg = Release|Any CPU
{77F0608F-6D0C-481C-9108-D5176E2EAD69}.Release|x86.Build.0 = Release|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Debug|x64.ActiveCfg = Debug|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Debug|x64.Build.0 = Debug|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Debug|x86.ActiveCfg = Debug|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Debug|x86.Build.0 = Debug|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Release|Any CPU.Build.0 = Release|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Release|x64.ActiveCfg = Release|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Release|x64.Build.0 = Release|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Release|x86.ActiveCfg = Release|Any CPU
{D7106D61-C7CA-4005-B31F-43281BB397AD}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -89,6 +103,7 @@ Global
{05A7D0B7-4AE1-4BC8-A1BE-2389F1593B2D} = {ED612DB1-AB32-4603-95E7-891BACA71C39}
{B78A7825-BE72-4509-B0AD-01EEC67A9624} = {ED612DB1-AB32-4603-95E7-891BACA71C39}
{77F0608F-6D0C-481C-9108-D5176E2EAD69} = {8537E50E-CF7F-49CB-B4EF-3E2A1B11F050}
{D7106D61-C7CA-4005-B31F-43281BB397AD} = {8537E50E-CF7F-49CB-B4EF-3E2A1B11F050}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F4EAAE6D-758C-4184-9D8C-7113384B61A8}
Expand Down
2 changes: 2 additions & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<add key="dotnet-core" value="https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json" />
<add key="extensions" value="https://dotnetfeed.blob.core.windows.net/aspnet-extensions/index.json" />
<add key="entityframeworkcore" value="https://dotnetfeed.blob.core.windows.net/aspnet-entityframeworkcore/index.json" />
<!-- For NTS 2.0 -->
<add key="NpgsqlUnstable" value="https://www.myget.org/F/npgsql-unstable/api/v3/index.json" />
<add key="NuGet.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
<ProjectReference Include="..\EFCore.PG\EFCore.PG.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Npgsql.NetTopologySuite" Version="4.0.4.1" />
<PackageReference Include="Npgsql.NetTopologySuite" Version="4.1.0-ci.2198" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GeoAPI.Geometries;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using NetTopologySuite.Geometries;
using Npgsql;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal;
Expand All @@ -22,8 +22,8 @@ public static class NpgsqlNetTopologySuiteDbContextOptionsBuilderExtensions
/// </returns>
public static NpgsqlDbContextOptionsBuilder UseNetTopologySuite(
[NotNull] this NpgsqlDbContextOptionsBuilder optionsBuilder,
ICoordinateSequenceFactory coordinateSequenceFactory = null,
IPrecisionModel precisionModel = null,
CoordinateSequenceFactory coordinateSequenceFactory = null,
PrecisionModel precisionModel = null,
Ordinates handleOrdinates = Ordinates.None,
bool geographyAsDefault = false)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Relational.Query.Pipeline;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Internal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,126 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using GeoAPI.Geometries;
using Microsoft.EntityFrameworkCore.Query.Expressions;
using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;
using System.Diagnostics;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
using NetTopologySuite.Geometries;

// ReSharper disable once CheckNamespace
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal
{
public class NpgsqlNetTopologySuiteMemberTranslatorPlugin : IMemberTranslatorPlugin
{
public virtual IEnumerable<IMemberTranslator> Translators { get; } = new IMemberTranslator[]
{
new NpgsqlGeometryMemberTranslator()
};
public NpgsqlNetTopologySuiteMemberTranslatorPlugin(
IRelationalTypeMappingSource typeMappingSource,
ISqlExpressionFactory sqlExpressionFactory)
=> Translators = new IMemberTranslator[]
{
new NpgsqlGeometryMemberTranslator(sqlExpressionFactory, typeMappingSource),
};

public virtual IEnumerable<IMemberTranslator> Translators { get; }
}

public class NpgsqlGeometryMemberTranslator : IMemberTranslator
{
static readonly CaseWhenClause[] _ogcGeometryTypeWhenThenList = new[]
readonly ISqlExpressionFactory _sqlExpressionFactory;
readonly IRelationalTypeMappingSource _typeMappingSource;
readonly CaseWhenClause[] _ogcGeometryTypeWhenThenList;

public NpgsqlGeometryMemberTranslator(ISqlExpressionFactory sqlExpressionFactory, IRelationalTypeMappingSource typeMappingSource)
{
new CaseWhenClause(Expression.Constant("ST_CircularString"), Expression.Constant(OgcGeometryType.CircularString)),
new CaseWhenClause(Expression.Constant("ST_CompoundCurve"), Expression.Constant(OgcGeometryType.CompoundCurve)),
new CaseWhenClause(Expression.Constant("ST_CurvePolygon"), Expression.Constant(OgcGeometryType.CurvePolygon)),
new CaseWhenClause(Expression.Constant("ST_GeometryCollection"), Expression.Constant(OgcGeometryType.GeometryCollection)),
new CaseWhenClause(Expression.Constant("ST_LineString"), Expression.Constant(OgcGeometryType.LineString)),
new CaseWhenClause(Expression.Constant("ST_MultiCurve"), Expression.Constant(OgcGeometryType.MultiCurve)),
new CaseWhenClause(Expression.Constant("ST_MultiLineString"), Expression.Constant(OgcGeometryType.MultiLineString)),
new CaseWhenClause(Expression.Constant("ST_MultiPoint"), Expression.Constant(OgcGeometryType.MultiPoint)),
new CaseWhenClause(Expression.Constant("ST_MultiPolygon"), Expression.Constant(OgcGeometryType.MultiPolygon)),
new CaseWhenClause(Expression.Constant("ST_MultiSurface"), Expression.Constant(OgcGeometryType.MultiSurface)),
new CaseWhenClause(Expression.Constant("ST_Point"), Expression.Constant(OgcGeometryType.Point)),
new CaseWhenClause(Expression.Constant("ST_Polygon"), Expression.Constant(OgcGeometryType.Polygon)),
new CaseWhenClause(Expression.Constant("ST_PolyhedralSurface"), Expression.Constant(OgcGeometryType.PolyhedralSurface)),
new CaseWhenClause(Expression.Constant("ST_Tin"), Expression.Constant(OgcGeometryType.TIN))
};
_sqlExpressionFactory = sqlExpressionFactory;
_typeMappingSource = typeMappingSource;

_ogcGeometryTypeWhenThenList = new[]
{
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_CircularString"), _sqlExpressionFactory.Constant(OgcGeometryType.CircularString)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_CompoundCurve"), _sqlExpressionFactory.Constant(OgcGeometryType.CompoundCurve)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_CurvePolygon"), _sqlExpressionFactory.Constant(OgcGeometryType.CurvePolygon)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_GeometryCollection"), _sqlExpressionFactory.Constant(OgcGeometryType.GeometryCollection)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_LineString"), _sqlExpressionFactory.Constant(OgcGeometryType.LineString)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiCurve"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiCurve)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiLineString"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiLineString)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiPoint"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiPoint)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiPolygon"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiPolygon)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_MultiSurface"), _sqlExpressionFactory.Constant(OgcGeometryType.MultiSurface)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_Point"), _sqlExpressionFactory.Constant(OgcGeometryType.Point)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_Polygon"), _sqlExpressionFactory.Constant(OgcGeometryType.Polygon)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_PolyhedralSurface"), _sqlExpressionFactory.Constant(OgcGeometryType.PolyhedralSurface)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_Tin"), _sqlExpressionFactory.Constant(OgcGeometryType.TIN))
};
}

public Expression Translate(MemberExpression e)
public SqlExpression Translate(SqlExpression instance, MemberInfo member, Type returnType)
{
var declaringType = e.Member.DeclaringType;
var declaringType = member.DeclaringType;

if (typeof(IPoint).IsAssignableFrom(declaringType))
if (!typeof(Geometry).IsAssignableFrom(declaringType))
return null;

var typeMapping = instance.TypeMapping;
Debug.Assert(typeMapping != null, "Instance must have typeMapping assigned.");
var storeType = instance.TypeMapping.StoreType;
var resultGeometryTypeMapping = typeof(Geometry).IsAssignableFrom(returnType)
? _typeMappingSource.FindMapping(returnType, storeType)
: null;

if (typeof(Point).IsAssignableFrom(declaringType))
{
switch (e.Member.Name)
var function = member.Name switch
{
case "X":
return new SqlFunctionExpression("ST_X", typeof(double), new[] { e.Expression });
case "Y":
return new SqlFunctionExpression("ST_Y", typeof(double), new[] { e.Expression });
case "Z":
return new SqlFunctionExpression("ST_Z", typeof(double), new[] { e.Expression });
case "M":
return new SqlFunctionExpression("ST_M", typeof(double), new[] { e.Expression });
}
nameof(Point.X) => "ST_X",
nameof(Point.Y) => "ST_Y",
nameof(Point.Z) => "ST_Z",
nameof(Point.M) => "ST_M",
_ => null
};

if (function != null)
return _sqlExpressionFactory.Function(function, new[] { instance }, typeof(double));
}

if (typeof(ILineString).IsAssignableFrom(declaringType))
if (typeof(LineString).IsAssignableFrom(declaringType))
{
switch (e.Member.Name)
{
case "Count":
return new SqlFunctionExpression("ST_NumPoints", typeof(int), new[] { e.Expression });
}
if (member.Name == "Count")
return _sqlExpressionFactory.Function("ST_NumPoints", new[] { instance }, typeof(int));
}

if (typeof(IGeometry).IsAssignableFrom(declaringType))
return member.Name switch
{
switch (e.Member.Name)
{
case "Area":
return new SqlFunctionExpression("ST_Area", typeof(double), new[] { e.Expression });
case "Boundary":
return new SqlFunctionExpression("ST_Boundary", typeof(IGeometry), new[] { e.Expression });
case "Centroid":
return new SqlFunctionExpression("ST_Centroid", typeof(Point), new[] { e.Expression });
case "Count":
return new SqlFunctionExpression("ST_NumGeometries", typeof(int), new[] { e.Expression });
case "Dimension":
return new SqlFunctionExpression("ST_Dimension", typeof(Dimension), new[] { e.Expression });
case "EndPoint":
return new SqlFunctionExpression("ST_EndPoint", typeof(Point), new[] { e.Expression });
case "Envelope":
return new SqlFunctionExpression("ST_Envelope", typeof(IGeometry), new[] { e.Expression });
case "ExteriorRing":
return new SqlFunctionExpression("ST_ExteriorRing", typeof(ILineString), new[] { e.Expression });
case "GeometryType":
return new SqlFunctionExpression("GeometryType", typeof(string), new[] { e.Expression });
case "IsClosed":
return new SqlFunctionExpression("ST_IsClosed", typeof(bool), new[] { e.Expression });
case "IsEmpty":
return new SqlFunctionExpression("ST_IsEmpty", typeof(bool), new[] { e.Expression });
case "IsRing":
return new SqlFunctionExpression("ST_IsRing", typeof(bool), new[] { e.Expression });
case "IsSimple":
return new SqlFunctionExpression("ST_IsSimple", typeof(bool), new[] { e.Expression });
case "IsValid":
return new SqlFunctionExpression("ST_IsValid", typeof(bool), new[] { e.Expression });
case "Length":
return new SqlFunctionExpression("ST_Length", typeof(double), new[] { e.Expression });
case "NumGeometries":
return new SqlFunctionExpression("ST_NumGeometries", typeof(int), new[] { e.Expression });
case "NumInteriorRings":
return new SqlFunctionExpression("ST_NumInteriorRings", typeof(int), new[] { e.Expression });
case "NumPoints":
return new SqlFunctionExpression("ST_NumPoints", typeof(int), new[] { e.Expression });
case "OgcGeometryType":
return new CaseExpression(
new SqlFunctionExpression("ST_GeometryType", typeof(string), new[] { e.Expression }),
_ogcGeometryTypeWhenThenList);
case "PointOnSurface":
case "InteriorPoint":
return new SqlFunctionExpression("ST_PointOnSurface", typeof(IGeometry), new[] { e.Expression });
case "SRID":
return new SqlFunctionExpression("ST_SRID", typeof(int), new[] { e.Expression });
case "StartPoint":
return new SqlFunctionExpression("ST_StartPoint", typeof(IPoint), new[] { e.Expression });
default:
return null;
}
}
nameof(Geometry.Area) => _sqlExpressionFactory.Function("ST_Area", new[] { instance }, typeof(double)),
nameof(Geometry.Boundary) => _sqlExpressionFactory.Function("ST_Boundary", new[] { instance }, typeof(Geometry), resultGeometryTypeMapping),
nameof(Geometry.Centroid) => _sqlExpressionFactory.Function("ST_Centroid", new[] { instance }, typeof(Point), resultGeometryTypeMapping),
nameof(GeometryCollection.Count) => _sqlExpressionFactory.Function("ST_NumGeometries", new[] { instance }, typeof(int)),
nameof(Geometry.Dimension) => _sqlExpressionFactory.Function("ST_Dimension", new[] { instance }, typeof(Dimension)),
nameof(LineString.EndPoint) => _sqlExpressionFactory.Function("ST_EndPoint", new[] { instance }, typeof(Point), resultGeometryTypeMapping),
nameof(Geometry.Envelope) => _sqlExpressionFactory.Function("ST_Envelope", new[] { instance }, typeof(Geometry), resultGeometryTypeMapping),
nameof(Polygon.ExteriorRing) => _sqlExpressionFactory.Function("ST_ExteriorRing", new[] { instance }, typeof(LineString), resultGeometryTypeMapping),
nameof(Geometry.GeometryType) => _sqlExpressionFactory.Function("GeometryType", new[] { instance }, typeof(string)),
nameof(LineString.IsClosed) => _sqlExpressionFactory.Function("ST_IsClosed", new[] { instance }, typeof(bool)),
nameof(Geometry.IsEmpty) => _sqlExpressionFactory.Function("ST_IsEmpty", new[] { instance }, typeof(bool)),
nameof(LineString.IsRing) => _sqlExpressionFactory.Function("ST_IsRing", new[] { instance }, typeof(bool)),
nameof(Geometry.IsSimple) => _sqlExpressionFactory.Function("ST_IsSimple", new[] { instance }, typeof(bool)),
nameof(Geometry.IsValid) => _sqlExpressionFactory.Function("ST_IsValid", new[] { instance }, typeof(bool)),
nameof(Geometry.Length) => _sqlExpressionFactory.Function("ST_Length", new[] { instance }, typeof(double)),
nameof(Geometry.NumGeometries) => _sqlExpressionFactory.Function("ST_NumGeometries", new[] { instance }, typeof(int)),
nameof(Polygon.NumInteriorRings) => _sqlExpressionFactory.Function("ST_NumInteriorRings", new[] { instance }, typeof(int)),
nameof(Geometry.NumPoints) => _sqlExpressionFactory.Function("ST_NumPoints", new[] { instance }, typeof(int)),
nameof(Geometry.PointOnSurface) => _sqlExpressionFactory.Function("ST_PointOnSurface", new[] { instance }, typeof(Geometry), resultGeometryTypeMapping),
nameof(Geometry.InteriorPoint) => _sqlExpressionFactory.Function("ST_PointOnSurface", new[] { instance }, typeof(Geometry), resultGeometryTypeMapping),
nameof(Geometry.SRID) => _sqlExpressionFactory.Function("ST_SRID", new[] { instance }, typeof(int)),
nameof(LineString.StartPoint) => _sqlExpressionFactory.Function("ST_StartPoint", new[] { instance }, typeof(Point), resultGeometryTypeMapping),

nameof(Geometry.OgcGeometryType) => _sqlExpressionFactory.Case(
_sqlExpressionFactory.Function("ST_GeometryType", new[] { instance }, typeof(string)),
_ogcGeometryTypeWhenThenList),

return null;
_ => (SqlExpression)null
};
}
}
}
Loading