Skip to content
Open

NTS #110

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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\EntityFrameworkCore.ClickHouse.NTS\EntityFrameworkCore.ClickHouse.NTS.csproj" />
<ProjectReference Include="..\EntityFrameworkCore.ClickHouse\EntityFrameworkCore.ClickHouse.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ClickHouseMigrationsSqlGeneratorTest : MigrationsSqlGeneratorTestBa
public ClickHouseMigrationsSqlGeneratorTest()
: base(
ClickHouseTestHelpers.Instance,
new ServiceCollection()/*.AddEntityFrameworkClickHouseNetTopologySuite()*/,
new ServiceCollection().AddEntityFrameworkClickHouseNetTopologySuite(),
ClickHouseTestHelpers.Instance.AddProviderOptions(
((IRelationalDbContextOptionsBuilderInfrastructure)
new ClickHouseDbContextOptionsBuilder(new DbContextOptionsBuilder())/*.UseNetTopologySuite()*/)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using ClickHouse.EntityFrameworkCore.Infrastructure;
using EntityFrameworkCore.ClickHouse.FunctionalTests.TestUtilities;
using EntityFrameworkCore.ClickHouse.NTS.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.TestModels.SpatialModel;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;

namespace EntityFrameworkCore.ClickHouse.FunctionalTests.Query;

public class SpatialQueryClickHouseFixture : SpatialQueryRelationalFixture
{
protected override ITestStoreFactory TestStoreFactory
=> ClickHouseTestStoreFactory.Instance;

protected override IServiceCollection AddServices(IServiceCollection serviceCollection)
=> base.AddServices(serviceCollection)
.AddEntityFrameworkClickHouseNetTopologySuite();

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
{
var optionsBuilder = base.AddOptions(builder);
new ClickHouseDbContextOptionsBuilder(optionsBuilder).UseNetTopologySuite();

return optionsBuilder;
}

protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
base.OnModelCreating(modelBuilder, context);

modelBuilder.Entity<LineStringEntity>().Property(e => e.LineString).HasColumnType("Geometry");
modelBuilder.Entity<MultiLineStringEntity>().Property(e => e.MultiLineString).HasColumnType("Geometry");
modelBuilder.Entity<PointEntity>(
x =>
{
x.Ignore(e => e.Geometry);
x.Property(e => e.Point).HasColumnType("Point").IsRequired();
x.Property(e => e.PointZ).HasColumnType("Point").IsRequired();
x.Property(e => e.PointM).HasColumnType("Point").IsRequired();
x.Property(e => e.PointZM).HasColumnType("Point").IsRequired();
});
modelBuilder.Entity<PolygonEntity>().Property(e => e.Polygon).HasColumnType("Geometry");
modelBuilder.Entity<GeoPointEntity>().Property(e => e.Location).HasColumnType("Point");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore.Query;
using Xunit.Abstractions;

namespace EntityFrameworkCore.ClickHouse.FunctionalTests.Query;

public class SpatialQueryClickHouseTest : SpatialQueryRelationalTestBase<SpatialQueryClickHouseFixture>
{
public SpatialQueryClickHouseTest(SpatialQueryClickHouseFixture fixture, ITestOutputHelper testOutputHelper)
: base(fixture)
{
Fixture.TestSqlLoggerFactory.Clear();
Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using EntityFrameworkCore.ClickHouse.NTS.Scaffolding.Internal;
using EntityFrameworkCore.ClickHouse.NTS.Storage.Internal;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NetTopologySuite;

namespace EntityFrameworkCore.ClickHouse.NTS.Design.Internal;

public class ClickHouseNetTopologySuiteDesignTimeServices : IDesignTimeServices
{
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
=> serviceCollection
.AddSingleton<IRelationalTypeMappingSourcePlugin, ClickHouseNetTopologySuiteTypeMappingSourcePlugin>()
.AddSingleton<IProviderCodeGeneratorPlugin, ClickHouseNetTopologySuiteCodeGeneratorPlugin>()
.TryAddSingleton(NtsGeometryServices.Instance);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\EntityFrameworkCore.ClickHouse\EntityFrameworkCore.ClickHouse.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NetTopologySuite" Version="2.6.0" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\EntityFrameworkCore.ClickHouse\Extensions\ClickHouseDbParameterExtensions.cs">
<Link>Extensions\ClickHouseDbParameterExtensions.cs</Link>
</Compile>
<Compile Include="..\EntityFrameworkCore.ClickHouse\Extensions\StringExtensions.cs">
<Link>Extensions\StringExtensions.cs</Link>
</Compile>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using ClickHouse.EntityFrameworkCore.Infrastructure;
using EntityFrameworkCore.ClickHouse.NTS.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace EntityFrameworkCore.ClickHouse.NTS.Extensions;

public static class ClickHouseNetTopologySuiteDbContextOptionsBuilderExtensions
{
public static ClickHouseDbContextOptionsBuilder UseNetTopologySuite(
this ClickHouseDbContextOptionsBuilder optionsBuilder)
{
var coreOptionsBuilder = ((IRelationalDbContextOptionsBuilderInfrastructure)optionsBuilder).OptionsBuilder;

var extension = coreOptionsBuilder.Options.FindExtension<ClickHouseNetTopologySuiteOptionsExtension>()
?? new ClickHouseNetTopologySuiteOptionsExtension();

((IDbContextOptionsBuilderInfrastructure)coreOptionsBuilder).AddOrUpdateExtension(extension);

return optionsBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using EntityFrameworkCore.ClickHouse.NTS.Query.Internal;
using EntityFrameworkCore.ClickHouse.NTS.Storage.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NetTopologySuite;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection;

public static class ClickHouseNetTopologySuiteServiceCollectionExtensions
{
public static IServiceCollection AddEntityFrameworkClickHouseNetTopologySuite(
this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton(NtsGeometryServices.Instance);

new EntityFrameworkRelationalServicesBuilder(serviceCollection)
.TryAdd<IRelationalTypeMappingSourcePlugin, ClickHouseNetTopologySuiteTypeMappingSourcePlugin>()
.TryAdd<IMethodCallTranslatorPlugin, ClickHouseNetTopologySuiteMethodCallTranslatorPlugin>()
.TryAdd<IAggregateMethodCallTranslatorPlugin, ClickHouseNetTopologySuiteAggregateMethodCallTranslatorPlugin>()
.TryAdd<IMemberTranslatorPlugin, ClickHouseNetTopologySuiteMemberTranslatorPlugin>()
/*.TryAdd<IEvaluatableExpressionFilterPlugin, ClickHouseNetTopologySuiteEvaluatableExpressionFilterPlugin>()*/;

return serviceCollection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using NetTopologySuite.Geometries;
using System.Runtime.InteropServices.Swift;

namespace EntityFrameworkCore.ClickHouse.NTS.Extensions;

internal static class GeometryExtensions
{
public static object ToClickHouse(this Geometry geometry)
{
return geometry.OgcGeometryType switch
{
OgcGeometryType.Point => ToClickHouse((Point)geometry),
OgcGeometryType.LineString => ToClickHouse((LineString)geometry),
OgcGeometryType.Polygon => throw new NotImplementedException(),
OgcGeometryType.MultiPoint => throw new NotImplementedException(),
OgcGeometryType.MultiLineString => ToClickHouse((MultiLineString)geometry),
OgcGeometryType.MultiPolygon => throw new NotImplementedException(),
OgcGeometryType.GeometryCollection => throw new NotImplementedException(),
OgcGeometryType.CircularString => throw new NotImplementedException(),
OgcGeometryType.CompoundCurve => throw new NotImplementedException(),
OgcGeometryType.CurvePolygon => throw new NotImplementedException(),
OgcGeometryType.MultiCurve => throw new NotImplementedException(),
OgcGeometryType.MultiSurface => throw new NotImplementedException(),
OgcGeometryType.Curve => throw new NotImplementedException(),
OgcGeometryType.Surface => throw new NotImplementedException(),
OgcGeometryType.PolyhedralSurface => throw new NotImplementedException(),
OgcGeometryType.TIN => throw new NotImplementedException(),
_ => throw new ArgumentOutOfRangeException()
};
}

public static Tuple<double, double> ToClickHouse(Coordinate coordinate) => new(coordinate.X, coordinate.Y);

public static Tuple<double, double> ToClickHouse(Point point) => new(point.X, point.Y);

public static Tuple<double, double>[] ToClickHouse(LineString lineString) => lineString.Coordinates.Select(ToClickHouse).ToArray();

public static Tuple<double, double>[][] ToClickHouse(MultiLineString multiLineString)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using EntityFrameworkCore.ClickHouse.NTS.Extensions;
using EntityFrameworkCore.ClickHouse.NTS.Storage.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;

namespace EntityFrameworkCore.ClickHouse.NTS.Infrastructure.Internal;

public class ClickHouseNetTopologySuiteOptionsExtension : IDbContextOptionsExtension
{
private DbContextOptionsExtensionInfo? _info;

public virtual void ApplyServices(IServiceCollection services)
=> services.AddEntityFrameworkClickHouseNetTopologySuite();

public void Validate(IDbContextOptions options)
{
var internalServiceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider;

if (internalServiceProvider != null)
{
using var scope = internalServiceProvider.CreateScope();
var plugins = scope.ServiceProvider.GetService<IEnumerable<IRelationalTypeMappingSourcePlugin>>();
if (plugins?.Any(s => s is ClickHouseNetTopologySuiteTypeMappingSourcePlugin) != true)
{
throw new InvalidOperationException("UseNetTopologySuite requires AddEntityFrameworkClickHouseNetTopologySuite to be called on the internal service provider used.");
}
}
}

public DbContextOptionsExtensionInfo Info => _info ??= new ExtensionInfo(this);

private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
{
public ExtensionInfo(IDbContextOptionsExtension extension)
: base(extension)
{
}

private new ClickHouseNetTopologySuiteOptionsExtension Extension
=> (ClickHouseNetTopologySuiteOptionsExtension)base.Extension;

public override bool IsDatabaseProvider
=> false;

public override int GetServiceProviderHashCode()
=> 0;

public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other)
=> other is ExtensionInfo;

public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
=> debugInfo["ClickHouse:" + nameof(ClickHouseNetTopologySuiteDbContextOptionsBuilderExtensions.UseNetTopologySuite)] = "1";

public override string LogFragment
=> "using NetTopologySuite ";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using System.Reflection;

namespace EntityFrameworkCore.ClickHouse.NTS.Query.Internal;

public class ClickHouseGeometryCollectionMemberTranslator : IMemberTranslator
{
public SqlExpression? Translate(SqlExpression? instance, MemberInfo member, Type returnType, IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using System.Reflection;

namespace EntityFrameworkCore.ClickHouse.NTS.Query.Internal;

public class ClickHouseGeometryCollectionMethodTranslator : IMethodCallTranslator
{
public SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using System.Reflection;

namespace EntityFrameworkCore.ClickHouse.NTS.Query.Internal;

public class ClickHouseGeometryMemberTranslator : IMemberTranslator
{
public SqlExpression? Translate(SqlExpression? instance, MemberInfo member, Type returnType, IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using System.Reflection;

namespace EntityFrameworkCore.ClickHouse.NTS.Query.Internal;

public class ClickHouseGeometryMethodTranslator : IMethodCallTranslator
{
public SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using System.Reflection;

namespace EntityFrameworkCore.ClickHouse.NTS.Query.Internal;

public class ClickHouseLineStringMemberTranslator : IMemberTranslator
{
public SqlExpression? Translate(
SqlExpression? instance,
MemberInfo member,
Type returnType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using System.Reflection;

namespace EntityFrameworkCore.ClickHouse.NTS.Query.Internal;

public class ClickHouseLineStringMethodTranslator : IMethodCallTranslator
{
public SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
return null;
}
}
Loading
Loading