Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlServer Spatial: Enable calling methods on parameterized values #16097

Merged
merged 2 commits into from
Jul 9, 2019
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
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<SQLitePCLRawCorePackageVersion>2.0.0-pre20190628101813</SQLitePCLRawCorePackageVersion>
<StyleCopAnalyzersPackageVersion>1.1.1-beta.61</StyleCopAnalyzersPackageVersion>
<BenchmarkDotNetPackageVersion>0.11.3</BenchmarkDotNetPackageVersion>
<MicrosoftDataSqlClientPackageVersion>1.0.19128.1-Preview</MicrosoftDataSqlClientPackageVersion>
<MicrosoftDataSqlClientPackageVersion>1.0.19189.1-Preview</MicrosoftDataSqlClientPackageVersion>
</PropertyGroup>
<PropertyGroup Label="Dependencies from aspnet/Extensions">
<MicrosoftExtensionsCachingMemoryPackageVersion>3.0.0-preview8.19359.2</MicrosoftExtensionsCachingMemoryPackageVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
using JetBrains.Annotations;
using Microsoft.Data.SqlClient; // Note: Hard reference to SqlClient here.
using Microsoft.EntityFrameworkCore.SqlServer.Storage.ValueConversion.Internal;
Expand All @@ -29,6 +32,9 @@ public class SqlServerGeometryTypeMapping<TGeometry> : RelationalGeometryTypeMap
private static readonly MethodInfo _getSqlBytes
= typeof(SqlDataReader).GetRuntimeMethod(nameof(SqlDataReader.GetSqlBytes), new[] { typeof(int) });

private static Action<DbParameter, SqlDbType> _sqlDbTypeSetter;
private static Action<DbParameter, string> _udtTypeNameSetter;

private readonly bool _isGeography;

/// <summary>
Expand Down Expand Up @@ -149,10 +155,17 @@ protected override Type WKTReaderType
/// </summary>
protected override void ConfigureParameter(DbParameter parameter)
{
var type = parameter.GetType();
LazyInitializer.EnsureInitialized(ref _sqlDbTypeSetter, () => CreateSqlDbTypeAccessor(type));
LazyInitializer.EnsureInitialized(ref _udtTypeNameSetter, () => CreateUdtTypeNameAccessor(type));

if (parameter.Value == DBNull.Value)
{
parameter.Value = SqlBytes.Null;
}

_sqlDbTypeSetter(parameter, SqlDbType.Udt);
_udtTypeNameSetter(parameter, _isGeography ? "geography" : "geometry");
}

private static SqlServerBytesReader CreateReader(NtsGeometryServices services, bool isGeography)
Expand All @@ -169,5 +182,33 @@ private static SqlServerBytesWriter CreateWriter(bool isGeography)

private static bool IsGeography(string storeType)
=> string.Equals(storeType, "geography", StringComparison.OrdinalIgnoreCase);

private static Action<DbParameter, SqlDbType> CreateSqlDbTypeAccessor(Type paramType)
{
var paramParam = Expression.Parameter(typeof(DbParameter), "parameter");
var valueParam = Expression.Parameter(typeof(SqlDbType), "value");

return Expression.Lambda<Action<DbParameter, SqlDbType>>(
Expression.Call(
Expression.Convert(paramParam, paramType),
paramType.GetProperty("SqlDbType").SetMethod,
valueParam),
paramParam,
valueParam).Compile();
}

private static Action<DbParameter, string> CreateUdtTypeNameAccessor(Type paramType)
{
var paramParam = Expression.Parameter(typeof(DbParameter), "parameter");
var valueParam = Expression.Parameter(typeof(string), "value");

return Expression.Lambda<Action<DbParameter, string>>(
Expression.Call(
Expression.Convert(paramParam, paramType),
paramType.GetProperty("UdtTypeName").SetMethod,
valueParam),
paramParam,
valueParam).Compile();
}
}
}
18 changes: 18 additions & 0 deletions test/EFCore.Specification.Tests/Query/SpatialQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,24 @@ public virtual Task Distance_on_converted_geometry_type(bool isAsync)
elementAsserter: (e, a) => { Assert.Equal(e.Id, a.Id); });
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Distance_on_converted_geometry_type_lhs(bool isAsync)
{
var point = new GeoPoint(1, 0);

return AssertQuery<GeoPointEntity>(
isAsync,
es => es.Select(
e => new
{
e.Id,
Distance = point.Distance(e.Location)
}),
elementSorter: e => e.Id,
elementAsserter: (e, a) => { Assert.Equal(e.Id, a.Id); });
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Distance_on_converted_geometry_type_constant(bool isAsync)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,23 @@ public override async Task Distance_on_converted_geometry_type(bool isAsync)
await base.Distance_on_converted_geometry_type(isAsync);

AssertSql(
@"@__point_0='0xE6100000010C000000000000F03F0000000000000000' (Nullable = false) (Size = 22) (DbType = Binary)
@"@__point_0='0xE6100000010C000000000000F03F0000000000000000' (Nullable = false) (Size = 22) (DbType = Object)

SELECT [g].[Id], [g].[Location].STDistance(@__point_0) AS [Distance]
FROM [GeoPointEntity] AS [g]");
}

public override async Task Distance_on_converted_geometry_type_lhs(bool isAsync)
{
await base.Distance_on_converted_geometry_type_lhs(isAsync);

AssertSql(
@"@__point_0='0xE6100000010C000000000000F03F0000000000000000' (Nullable = false) (Size = 22) (DbType = Object)

SELECT [g].[Id], @__point_0.STDistance([g].[Location]) AS [Distance]
FROM [GeoPointEntity] AS [g]");
Copy link
Contributor Author

@bricelam bricelam Jun 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 @Wraith2 you rock! (no System.Data.SqlTypes)

}

[ConditionalTheory(Skip = "Mixing SRIDs not supported")]
public override Task Distance_on_converted_geometry_type_constant(bool isAsync)
{
Expand Down Expand Up @@ -276,7 +287,7 @@ public override async Task EqualsTopologically(bool isAsync)

// issue #15994
// AssertSql(
// @"@__point_0='0xE6100000010C00000000000000000000000000000000' (Size = 22) (DbType = Binary)
// @"@__point_0='0xE6100000010C00000000000000000000000000000000' (Size = 22) (DbType = Object)

//SELECT [e].[Id], [e].[Point].STEquals(@__point_0) AS [EqualsTopologically]
//FROM [PointEntity] AS [e]");
Expand Down Expand Up @@ -344,7 +355,7 @@ public override async Task Intersection(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0xE610000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0xE610000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STIntersection(@__polygon_0) AS [Intersection]
//FROM [PolygonEntity] AS [e]");
Expand All @@ -356,7 +367,7 @@ public override async Task Intersects(bool isAsync)

// issue #15994
// AssertSql(
// @"@__lineString_0='0xE61000000114000000000000E0BF000000000000E03F000000000000E03F0000...' (Size = 38) (DbType = Binary)
// @"@__lineString_0='0xE61000000114000000000000E0BF000000000000E03F000000000000E03F0000...' (Size = 38) (DbType = Object)

//SELECT [e].[Id], [e].[LineString].STIntersects(@__lineString_0) AS [Intersects]
//FROM [LineStringEntity] AS [e]");
Expand Down Expand Up @@ -415,7 +426,7 @@ public override async Task IsWithinDistance(bool isAsync)
await base.IsWithinDistance(isAsync);

AssertSql(
@"@__point_0='0xE6100000010C000000000000F03F0000000000000000' (Size = 22) (DbType = Binary)
@"@__point_0='0xE6100000010C000000000000F03F0000000000000000' (Size = 22) (DbType = Object)

SELECT [p].[Id], CASE
WHEN [p].[Point] IS NULL THEN NULL
Expand Down Expand Up @@ -509,7 +520,7 @@ public override async Task Overlaps(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0xE610000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0xE610000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STOverlaps(@__polygon_0) AS [Overlaps]
//FROM [PolygonEntity] AS [e]");
Expand Down Expand Up @@ -566,7 +577,7 @@ public override async Task SymmetricDifference(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0xE610000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0xE610000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STSymDifference(@__polygon_0) AS [SymmetricDifference]
//FROM [PolygonEntity] AS [e]");
Expand Down Expand Up @@ -604,7 +615,7 @@ public override async Task Union(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0xE610000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0xE610000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STUnion(@__polygon_0) AS [Union]
//FROM [PolygonEntity] AS [e]");
Expand All @@ -622,7 +633,7 @@ public override async Task Within(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0xE6100000010405000000000000000000F0BF000000000000F0BF000000000000...' (Size = 112) (DbType = Binary)
// @"@__polygon_0='0xE6100000010405000000000000000000F0BF000000000000F0BF000000000000...' (Size = 112) (DbType = Object)

//SELECT [e].[Id], [e].[Point].STWithin(@__polygon_0) AS [Within]
//FROM [PointEntity] AS [e]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public override async Task Contains(bool isAsync)

// issue #15994
// AssertSql(
// @"@__point_0='0x00000000010C000000000000D03F000000000000D03F' (Size = 22) (DbType = Binary)
// @"@__point_0='0x00000000010C000000000000D03F000000000000D03F' (Size = 22) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STContains(@__point_0) AS [Contains]
//FROM [PolygonEntity] AS [e]");
Expand Down Expand Up @@ -159,7 +159,7 @@ public override async Task Crosses(bool isAsync)

// issue #15994
// AssertSql(
// @"@__lineString_0='0x000000000114000000000000E03F000000000000E0BF000000000000E03F0000...' (Size = 38) (DbType = Binary)
// @"@__lineString_0='0x000000000114000000000000E03F000000000000E0BF000000000000E03F0000...' (Size = 38) (DbType = Object)

//SELECT [e].[Id], [e].[LineString].STCrosses(@__lineString_0) AS [Crosses]
//FROM [LineStringEntity] AS [e]");
Expand All @@ -171,7 +171,7 @@ public override async Task Difference(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STDifference(@__polygon_0) AS [Difference]
//FROM [PolygonEntity] AS [e]");
Expand All @@ -183,12 +183,24 @@ public override async Task Distance_on_converted_geometry_type(bool isAsync)

// issue #15994
// AssertSql(
// @"@__point_0='0x00000000010C000000000000F03F0000000000000000' (Nullable = false) (Size = 22) (DbType = Binary)
// @"@__point_0='0x00000000010C000000000000F03F0000000000000000' (Nullable = false) (Size = 22) (DbType = Object)

//SELECT [e].[Id], [e].[Location].STDistance(@__point_0) AS [Distance]
//FROM [GeoPointEntity] AS [e]");
}

public override async Task Distance_on_converted_geometry_type_lhs(bool isAsync)
{
await base.Distance_on_converted_geometry_type_lhs(isAsync);

// issue #15994
// AssertSql(
// @"@__point_0='0x00000000010C000000000000F03F0000000000000000' (Nullable = false) (Size = 22) (DbType = Object)

//SELECT [e].[Id], @__point_0.STDistance([e].[Location]) AS [Distance]
//FROM [GeoPointEntity] AS [e]");
}

public override async Task Distance_on_converted_geometry_type_constant(bool isAsync)
{
await base.Distance_on_converted_geometry_type_constant(isAsync);
Expand Down Expand Up @@ -265,7 +277,7 @@ public override async Task Disjoint(bool isAsync)

// issue #15994
// AssertSql(
// @"@__point_0='0x00000000010C000000000000F03F000000000000F03F' (Size = 22) (DbType = Binary)
// @"@__point_0='0x00000000010C000000000000F03F000000000000F03F' (Size = 22) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STDisjoint(@__point_0) AS [Disjoint]
//FROM [PolygonEntity] AS [e]");
Expand All @@ -277,7 +289,7 @@ public override async Task Distance(bool isAsync)

// issue #15994
// AssertSql(
// @"@__point_0='0x00000000010C0000000000000000000000000000F03F' (Size = 22) (DbType = Binary)
// @"@__point_0='0x00000000010C0000000000000000000000000000F03F' (Size = 22) (DbType = Object)

//SELECT [e].[Id], [e].[Point].STDistance(@__point_0) AS [Distance]
//FROM [PointEntity] AS [e]");
Expand All @@ -289,7 +301,7 @@ public override async Task Distance_geometry(bool isAsync)

// issue #15994
// AssertSql(
// @"@__point_0='0x00000000010C0000000000000000000000000000F03F' (Size = 22) (DbType = Binary)
// @"@__point_0='0x00000000010C0000000000000000000000000000F03F' (Size = 22) (DbType = Object)

//SELECT [e].[Id], [e].[Geometry].STDistance(@__point_0) AS [Distance]
//FROM [PointEntity] AS [e]");
Expand Down Expand Up @@ -319,7 +331,7 @@ public override async Task EqualsTopologically(bool isAsync)

// issue #15994
// AssertSql(
// @"@__point_0='0x00000000010C00000000000000000000000000000000' (Size = 22) (DbType = Binary)
// @"@__point_0='0x00000000010C00000000000000000000000000000000' (Size = 22) (DbType = Object)

//SELECT [e].[Id], [e].[Point].STEquals(@__point_0) AS [EqualsTopologically]
//FROM [PointEntity] AS [e]");
Expand Down Expand Up @@ -390,7 +402,7 @@ public override async Task Intersection(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STIntersection(@__polygon_0) AS [Intersection]
//FROM [PolygonEntity] AS [e]");
Expand All @@ -402,7 +414,7 @@ public override async Task Intersects(bool isAsync)

// issue #15994
// AssertSql(
// @"@__lineString_0='0x000000000114000000000000E03F000000000000E0BF000000000000E03F0000...' (Size = 38) (DbType = Binary)
// @"@__lineString_0='0x000000000114000000000000E03F000000000000E0BF000000000000E03F0000...' (Size = 38) (DbType = Object)

//SELECT [e].[Id], [e].[LineString].STIntersects(@__lineString_0) AS [Intersects]
//FROM [LineStringEntity] AS [e]");
Expand Down Expand Up @@ -468,7 +480,7 @@ public override async Task IsWithinDistance(bool isAsync)

// issue #15994
// AssertSql(
// @"@__point_0='0x00000000010C0000000000000000000000000000F03F' (Size = 22) (DbType = Binary)
// @"@__point_0='0x00000000010C0000000000000000000000000000F03F' (Size = 22) (DbType = Object)

//SELECT [e].[Id], CASE
// WHEN [e].[Point].STDistance(@__point_0) <= 1.0E0
Expand Down Expand Up @@ -558,7 +570,7 @@ public override async Task Overlaps(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STOverlaps(@__polygon_0) AS [Overlaps]
//FROM [PolygonEntity] AS [e]");
Expand All @@ -579,7 +591,7 @@ public override async Task Relate(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STRelate(@__polygon_0, N'212111212') AS [Relate]
//FROM [PolygonEntity] AS [e]");
Expand Down Expand Up @@ -624,7 +636,7 @@ public override async Task SymmetricDifference(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STSymDifference(@__polygon_0) AS [SymmetricDifference]
//FROM [PolygonEntity] AS [e]");
Expand Down Expand Up @@ -656,7 +668,7 @@ public override async Task Touches(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0x000000000104040000000000000000000000000000000000F03F000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0x000000000104040000000000000000000000000000000000F03F000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STTouches(@__polygon_0) AS [Touches]
//FROM [PolygonEntity] AS [e]");
Expand All @@ -668,7 +680,7 @@ public override async Task Union(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Binary)
// @"@__polygon_0='0x0000000001040400000000000000000000000000000000000000000000000000...' (Size = 96) (DbType = Object)

//SELECT [e].[Id], [e].[Polygon].STUnion(@__polygon_0) AS [Union]
//FROM [PolygonEntity] AS [e]");
Expand All @@ -686,7 +698,7 @@ public override async Task Within(bool isAsync)

// issue #15994
// AssertSql(
// @"@__polygon_0='0x00000000010405000000000000000000F0BF000000000000F0BF000000000000...' (Size = 112) (DbType = Binary)
// @"@__polygon_0='0x00000000010405000000000000000000F0BF000000000000F0BF000000000000...' (Size = 112) (DbType = Object)

//SELECT [e].[Id], [e].[Point].STWithin(@__polygon_0) AS [Within]
//FROM [PointEntity] AS [e]");
Expand Down
11 changes: 11 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/Query/SpatialQuerySqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ public override async Task Distance_on_converted_geometry_type(bool isAsync)
FROM ""GeoPointEntity"" AS ""g""");
}

public override async Task Distance_on_converted_geometry_type_lhs(bool isAsync)
{
await base.Distance_on_converted_geometry_type_lhs(isAsync);

AssertSql(
@"@__point_0='0x0001000000000000000000000000000000000000F03F00000000000000000000...' (Nullable = false) (Size = 60) (DbType = String)

SELECT ""g"".""Id"", Distance(@__point_0, ""g"".""Location"") AS ""Distance""
FROM ""GeoPointEntity"" AS ""g""");
}

public override async Task Distance_on_converted_geometry_type_constant(bool isAsync)
{
await base.Distance_on_converted_geometry_type_constant(isAsync);
Expand Down