Skip to content

Commit

Permalink
Update namespaces and overloads
Browse files Browse the repository at this point in the history
- Updated namespaces in extensions to match style.
- Changed network extensions to use DbFunctions per npgsql#407.
- Added XML docs to indicate these extensionss throw outside of EF Core.
  • Loading branch information
austindrenski committed Jul 22, 2018
1 parent c99f002 commit e3f9c1f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 42 deletions.
36 changes: 36 additions & 0 deletions src/EFCore.PG/Extensions/NpgsqlRangeExtensions.cs
Expand Up @@ -43,6 +43,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// <value>true</value> if the range contains the specified value; otherwise, <value>false</value>.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Contains<T>(this NpgsqlRange<T> range, T value) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -54,6 +57,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// <value>true</value> if the range contains the specified range; otherwise, <value>false</value>.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Contains<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -65,6 +71,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// <value>true</value> if the range contains the specified range; otherwise, <value>false</value>.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool ContainedBy<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => b.Contains(a);

/// <summary>
Expand All @@ -76,6 +85,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// <value>true</value> if the ranges overlap (share points in common); otherwise, <value>false</value>.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Overlaps<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -87,6 +99,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// <value>true</value> if the first range is strictly to the left of the second; otherwise, <value>false</value>.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool IsStrictlyLeftOf<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -98,6 +113,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// <value>true</value> if the first range is strictly to the right of the second; otherwise, <value>false</value>.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool IsStrictlyRightOf<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -109,6 +127,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// <value>true</value> if the first range does not extend to the left of the second; otherwise, <value>false</value>.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool DoesNotExtendLeftOf<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -120,6 +141,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// <value>true</value> if the first range does not extend to the right of the second; otherwise, <value>false</value>.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool DoesNotExtendRightOf<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -131,6 +155,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// <value>true</value> if the ranges are adjacent; otherwise, <value>false</value>.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool IsAdjacentTo<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -142,6 +169,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// The unique elements that appear in either range.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static NpgsqlRange<T> Union<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -153,6 +183,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// The elements that appear in both ranges.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static NpgsqlRange<T> Intersect<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();

/// <summary>
Expand All @@ -164,6 +197,9 @@ public static class NpgsqlRangeExtensions
/// <returns>
/// The elements that appear in the first range, but not the second range.
/// </returns>
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static NpgsqlRange<T> Except<T>(this NpgsqlRange<T> a, NpgsqlRange<T> b) where T : IComparable<T> => throw new NotSupportedException();
}
}
28 changes: 23 additions & 5 deletions src/EFCore.PG/NpgsqlNetworkAddressExtensions.cs
Expand Up @@ -25,9 +25,11 @@

using System;
using System.Net;
using JetBrains.Annotations;
using NpgsqlTypes;

namespace Npgsql.EntityFrameworkCore.PostgreSQL
// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
{
/// <summary>
/// Provides extension methods supporting PostgreSQL network address operator translation.
Expand All @@ -37,41 +39,57 @@ public static class NpgsqlNetworkAddressExtensions
/// <summary>
/// Determines whether an <see cref="IPAddress"/> contains another <see cref="IPAddress"/>.
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="ipAddress">The IP address to search.</param>
/// <param name="other">The IP address to locate.</param>
/// <returns>
/// <value>true</value> if the <see cref="IPAddress"/> contains the other <see cref="IPAddress"/>; otherwise, <value>false</value>.
/// </returns>
public static bool Contains(this IPAddress ipAddress, IPAddress other) => throw new NotImplementedException();
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Contains([CanBeNull] this DbFunctions _, [CanBeNull] IPAddress ipAddress, [CanBeNull] IPAddress other) => throw new NotSupportedException();

/// <summary>
/// Determines whether an <see cref="NpgsqlInet"/> contains another <see cref="NpgsqlInet"/>.
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="inet">The inet to search.</param>
/// <param name="other">The inet to locate.</param>
/// <returns>
/// <value>true</value> if the <see cref="NpgsqlInet"/> contains the other <see cref="NpgsqlInet"/>; otherwise, <value>false</value>.
/// </returns>
public static bool Contains(this NpgsqlInet inet, NpgsqlInet other) => throw new NotImplementedException();
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Contains([CanBeNull] this DbFunctions _, NpgsqlInet inet, NpgsqlInet other) => throw new NotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> contains or is equal to another <see cref="IPAddress"/>.
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="ipAddress">The IP address to search.</param>
/// <param name="other">The IP address to locate.</param>
/// <returns>
/// <value>true</value> if the <see cref="IPAddress"/> contains or is equal to the other <see cref="IPAddress"/>; otherwise, <value>false</value>.
/// </returns>
public static bool ContainsOrEquals(this IPAddress ipAddress, IPAddress other) => throw new NotImplementedException();
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool ContainsOrEquals([CanBeNull] this DbFunctions _, [CanBeNull] IPAddress ipAddress, [CanBeNull] IPAddress other) => throw new NotSupportedException();

/// <summary>
/// Determines whether an <see cref="NpgsqlInet"/> contains or is equal to another <see cref="NpgsqlInet"/>.
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="inet">The inet to search.</param>
/// <param name="other">The inet to locate.</param>
/// <returns>
/// <value>true</value> if the <see cref="NpgsqlInet"/> contains or is equal to the other <see cref="NpgsqlInet"/>; otherwise, <value>false</value>.
/// </returns>
public static bool ContainsOrEquals(this NpgsqlInet inet, NpgsqlInet other) => throw new NotImplementedException();
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool ContainsOrEquals([CanBeNull] this DbFunctions _, NpgsqlInet inet, NpgsqlInet other) => throw new NotSupportedException();
}
}
Expand Up @@ -36,7 +36,28 @@ public NetworkAddressQueryNpgsqlTest(NetworkAddressQueryNpgsqlFixture fixture)
#region Tests

/// <summary>
/// Tests translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(IPAddress,IPAddress)"/>.
/// Demonstrates parameter duplication.
/// </summary>
[Fact(Skip = nameof(NetworkAddressQueryNpgsqlTest))]
public void Demonstrate_ValueTypeParametersAreDuplicated()
{
using (NetContext context = Fixture.CreateContext())
{
NpgsqlInet npgsqlInet = new IPAddress(0);

bool[] _ =
context.NetTestEntities
.Where(x => EF.Functions.ContainsOrEquals(x.CidrMappedToNpgsqlInet, npgsqlInet))
.Select(x => x.CidrMappedToNpgsqlInet.Equals(npgsqlInet))
.ToArray();

AssertContainsSql("SELECT x.\"CidrMappedToNpgsqlInet\" = @__npgsqlInet_0");
AssertContainsSql("WHERE x.\"CidrMappedToNpgsqlInet\" >>= @__npgsqlInet_0");
}
}

/// <summary>
/// Tests translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(DbFunctions,IPAddress,IPAddress)"/>.
/// </summary>
[Fact]
public void IPAddressContainsIPAddress()
Expand All @@ -47,15 +68,15 @@ public void IPAddressContainsIPAddress()

NetTestEntity[] _ =
context.NetTestEntities
.Where(x => x.InetMappedToIPAddress.Contains(address))
.Where(x => EF.Functions.Contains(x.InetMappedToIPAddress, address))
.ToArray();

AssertContainsSql("WHERE x.\"InetMappedToIPAddress\" >> @__address_0");
}
}

/// <summary>
/// Tests translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(NpgsqlInet,NpgsqlInet)"/>.
/// Tests translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(DbFunctions,NpgsqlInet,NpgsqlInet)"/>.
/// </summary>
[Fact]
public void NpgsqlInetContainsNpgsqlInet()
Expand All @@ -66,15 +87,15 @@ public void NpgsqlInetContainsNpgsqlInet()

NetTestEntity[] _ =
context.NetTestEntities
.Where(x => x.CidrMappedToNpgsqlInet.Contains(npgsqlInet))
.Where(x => EF.Functions.Contains(x.CidrMappedToNpgsqlInet, npgsqlInet))
.ToArray();

AssertContainsSql("WHERE x.\"CidrMappedToNpgsqlInet\" >> @__npgsqlInet_0");
}
}

/// <summary>
/// Tests inverse translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(IPAddress,IPAddress)"/>.
/// Tests inverse translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(DbFunctions,IPAddress,IPAddress)"/>.
/// </summary>
[Fact]
public void IPAddressDoesNotContainsIPAddress()
Expand All @@ -85,15 +106,15 @@ public void IPAddressDoesNotContainsIPAddress()

NetTestEntity[] _ =
context.NetTestEntities
.Where(x => !x.InetMappedToIPAddress.Contains(address))
.Where(x => !EF.Functions.Contains(x.InetMappedToIPAddress, address))
.ToArray();

AssertContainsSql("WHERE NOT (x.\"InetMappedToIPAddress\" >> @__address_0 = TRUE)");
}
}

/// <summary>
/// Tests inverse translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(NpgsqlInet,NpgsqlInet)"/>.
/// Tests inverse translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(DbFunctions,NpgsqlInet,NpgsqlInet)"/>.
/// </summary>
[Fact]
public void NpgsqlInetDoesNotContainNpgsqlInet()
Expand All @@ -104,15 +125,15 @@ public void NpgsqlInetDoesNotContainNpgsqlInet()

NetTestEntity[] _ =
context.NetTestEntities
.Where(x => !x.CidrMappedToNpgsqlInet.Contains(npgsqlInet))
.Where(x => !EF.Functions.Contains(x.CidrMappedToNpgsqlInet, npgsqlInet))
.ToArray();

AssertContainsSql("WHERE NOT (x.\"CidrMappedToNpgsqlInet\" >> @__npgsqlInet_0 = TRUE)");
}
}

/// <summary>
/// Tests translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(IPAddress,IPAddress)"/>.
/// Tests translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(DbFunctions,IPAddress,IPAddress)"/>.
/// </summary>
[Fact]
public void IPAddressContainOrEqualIPAddress()
Expand All @@ -123,15 +144,15 @@ public void IPAddressContainOrEqualIPAddress()

NetTestEntity[] _ =
context.NetTestEntities
.Where(x => x.InetMappedToIPAddress.ContainsOrEquals(address))
.Where(x => EF.Functions.ContainsOrEquals(x.InetMappedToIPAddress, address))
.ToArray();

AssertContainsSql("WHERE x.\"InetMappedToIPAddress\" >>= @__address_0");
}
}

/// <summary>
/// Tests translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(NpgsqlInet,NpgsqlInet)"/>.
/// Tests translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(DbFunctions,NpgsqlInet,NpgsqlInet)"/>.
/// </summary>
[Fact]
public void NpgsqlInetContainsOrEqualsNpgsqlInet()
Expand All @@ -142,15 +163,15 @@ public void NpgsqlInetContainsOrEqualsNpgsqlInet()

NetTestEntity[] _ =
context.NetTestEntities
.Where(x => x.CidrMappedToNpgsqlInet.ContainsOrEquals(npgsqlInet))
.Where(x => EF.Functions.ContainsOrEquals(x.CidrMappedToNpgsqlInet, npgsqlInet))
.ToArray();

AssertContainsSql("WHERE x.\"CidrMappedToNpgsqlInet\" >>= @__npgsqlInet_0");
}
}

/// <summary>
/// Tests inverse translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(IPAddress,IPAddress)"/>.
/// Tests inverse translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(DbFunctions,IPAddress,IPAddress)"/>.
/// </summary>
[Fact]
public void IPAddressDoesNotContainOrEqualIPAddress()
Expand All @@ -161,15 +182,15 @@ public void IPAddressDoesNotContainOrEqualIPAddress()

NetTestEntity[] _ =
context.NetTestEntities
.Where(x => !x.InetMappedToIPAddress.ContainsOrEquals(address))
.Where(x => !EF.Functions.ContainsOrEquals(x.InetMappedToIPAddress, address))
.ToArray();

AssertContainsSql("WHERE NOT (x.\"InetMappedToIPAddress\" >>= @__address_0 = TRUE)");
}
}

/// <summary>
/// Tests inverse translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(NpgsqlInet,NpgsqlInet)"/>.
/// Tests inverse translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(DbFunctions,NpgsqlInet,NpgsqlInet)"/>.
/// </summary>
[Fact]
public void NpgsqlInetDoesNotContainOrEqualNpgsqlInet()
Expand All @@ -180,34 +201,13 @@ public void NpgsqlInetDoesNotContainOrEqualNpgsqlInet()

NetTestEntity[] _ =
context.NetTestEntities
.Where(x => !x.CidrMappedToNpgsqlInet.ContainsOrEquals(npgsqlInet))
.Where(x => !EF.Functions.ContainsOrEquals(x.CidrMappedToNpgsqlInet, npgsqlInet))
.ToArray();

AssertContainsSql("WHERE NOT (x.\"CidrMappedToNpgsqlInet\" >>= @__npgsqlInet_0 = TRUE)");
}
}

/// <summary>
/// Tests inverse translation for <see cref="NpgsqlNetworkAddressExtensions.Contains(NpgsqlInet,NpgsqlInet)"/>.
/// </summary>
[Fact]
public void Demonstrate_ValueTypeParametersAreDuplicated()
{
using (NetContext context = Fixture.CreateContext())
{
NpgsqlInet npgsqlInet = new IPAddress(0);

bool[] _ =
context.NetTestEntities
.Where(x => x.CidrMappedToNpgsqlInet.ContainsOrEquals(npgsqlInet))
.Select(x => x.CidrMappedToNpgsqlInet.Equals(npgsqlInet))
.ToArray();

AssertContainsSql("SELECT x.\"CidrMappedToNpgsqlInet\" = @__npgsqlInet_0");
AssertContainsSql("WHERE x.\"CidrMappedToNpgsqlInet\" >>= @__npgsqlInet_0");
}
}

#endregion

#region Fixtures
Expand Down

0 comments on commit e3f9c1f

Please sign in to comment.