Skip to content

Commit

Permalink
Adds MAC address (macaddr and macaddr8) operators
Browse files Browse the repository at this point in the history
- Includes relational and bitwise operators for `macaddr` and `macaddr8`.
  - Mapped to `PhysicalAddress`.

- Includes functional tests covering new operators.

- TODO:
  - Add some basic info to the docs.

- BUG:
  - Tests failing for `macaddr8`:
```
System.FormatException : MAC addresses must have length 6 in PostgreSQL
  at Npgsql.TypeHandlers.NetworkHandlers.MacaddrHandler.ValidateAndGetLength(PhysicalAddress value, NpgsqlParameter parameter)
```
  • Loading branch information
austindrenski committed May 26, 2018
1 parent 8f69555 commit 472c031
Show file tree
Hide file tree
Showing 4 changed files with 429 additions and 37 deletions.
148 changes: 148 additions & 0 deletions src/EFCore.PG/Extensions/NpgsqlNetworkAddressExtensions.cs
Expand Up @@ -34,9 +34,14 @@ namespace Microsoft.EntityFrameworkCore
/// <summary>
/// Provides extension methods supporting PostgreSQL network address operator translation.
/// </summary>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/static/functions-net.html
/// </remarks>
[PublicAPI]
public static class NpgsqlNetworkAddressExtensions
{
#region RelationalOperators

/// <summary>
/// Determines whether an <see cref="IPAddress"/> is less than another <see cref="IPAddress"/>.
/// </summary>
Expand Down Expand Up @@ -65,6 +70,20 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static bool LessThan([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="PhysicalAddress"/> is less than another <see cref="PhysicalAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="macaddr">The left-hand macaddr.</param>
/// <param name="other">The right-hand macaddr.</param>
/// <returns>
/// True if the <see cref="PhysicalAddress"/> is less than the other <see cref="PhysicalAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool LessThan([CanBeNull] this DbFunctions _, PhysicalAddress macaddr, PhysicalAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> is less than or equal to another <see cref="IPAddress"/>.
/// </summary>
Expand Down Expand Up @@ -93,6 +112,20 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static bool LessThanOrEqual([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="PhysicalAddress"/> is less than or equal to another <see cref="PhysicalAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="macaddr">The left-hand macaddr.</param>
/// <param name="other">The right-hand macaddr.</param>
/// <returns>
/// True if the <see cref="PhysicalAddress"/> is less than or equal to the other <see cref="PhysicalAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool LessThanOrEqual([CanBeNull] this DbFunctions _, PhysicalAddress macaddr, PhysicalAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> is equal to another <see cref="IPAddress"/>.
/// </summary>
Expand Down Expand Up @@ -121,6 +154,20 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static bool Equal([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="PhysicalAddress"/> is equal to another <see cref="PhysicalAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="macaddr">The left-hand macaddr.</param>
/// <param name="other">The right-hand macaddr.</param>
/// <returns>
/// True if the <see cref="PhysicalAddress"/> is equal to the other <see cref="PhysicalAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Equal([CanBeNull] this DbFunctions _, PhysicalAddress macaddr, PhysicalAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> is greater than or equal to another <see cref="IPAddress"/>.
/// </summary>
Expand Down Expand Up @@ -149,6 +196,20 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static bool GreaterThanOrEqual([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="PhysicalAddress"/> is greater than or equal to another <see cref="PhysicalAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="macaddr">The left-hand macaddr.</param>
/// <param name="other">The right-hand macaddr.</param>
/// <returns>
/// True if the <see cref="PhysicalAddress"/> is greater than or equal to the other <see cref="PhysicalAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool GreaterThanOrEqual([CanBeNull] this DbFunctions _, PhysicalAddress macaddr, PhysicalAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> is greater than another <see cref="IPAddress"/>.
/// </summary>
Expand Down Expand Up @@ -177,6 +238,20 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static bool GreaterThan([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="PhysicalAddress"/> is greater than another <see cref="PhysicalAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="macaddr">The left-hand macaddr.</param>
/// <param name="other">The right-hand macaddr.</param>
/// <returns>
/// True if the <see cref="PhysicalAddress"/> is greater than the other <see cref="PhysicalAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool GreaterThan([CanBeNull] this DbFunctions _, PhysicalAddress macaddr, PhysicalAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> is not equal to another <see cref="IPAddress"/>.
/// </summary>
Expand Down Expand Up @@ -205,6 +280,24 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static bool NotEqual([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="PhysicalAddress"/> is not equal to another <see cref="PhysicalAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="macaddr">The left-hand macaddr.</param>
/// <param name="other">The right-hand macaddr.</param>
/// <returns>
/// True if the <see cref="PhysicalAddress"/> is not equal to the other <see cref="PhysicalAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool NotEqual([CanBeNull] this DbFunctions _, PhysicalAddress macaddr, PhysicalAddress other) => throw new ClientEvaluationNotSupportedException();

#endregion

#region ContainmentOperators

/// <summary>
/// Determines whether an <see cref="IPAddress"/> is contained within another <see cref="IPAddress"/>.
/// </summary>
Expand Down Expand Up @@ -345,6 +438,10 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static bool ContainsOrContainedBy([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

#endregion

#region BitwiseOperators

/// <summary>
/// Computes the bitwise NOT operation on an <see cref="IPAddress"/>.
/// </summary>
Expand All @@ -371,6 +468,19 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static (IPAddress Address, int Subnet) Not([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Computes the bitwise NOT operation on an <see cref="PhysicalAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="macaddr">The macaddr to negate.</param>
/// <returns>
/// The result of the bitwise NOT operation.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static PhysicalAddress Not([CanBeNull] this DbFunctions _, PhysicalAddress macaddr) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Computes the bitwise AND of two <see cref="IPAddress"/> instances.
/// </summary>
Expand Down Expand Up @@ -399,6 +509,20 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static (IPAddress Address, int Subnet) And([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Computes the bitwise AND of two <see cref="PhysicalAddress"/> instances.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="macaddr">The left-hand macaddr.</param>
/// <param name="other">The right-hand macaddr.</param>
/// <returns>
/// The result of the bitwise AND operation.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static PhysicalAddress And([CanBeNull] this DbFunctions _, PhysicalAddress macaddr, IPAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Computes the bitwise OR of two <see cref="IPAddress"/> instances.
/// </summary>
Expand Down Expand Up @@ -427,6 +551,24 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static (IPAddress Address, int Subnet) Or([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Computes the bitwise OR of two <see cref="PhysicalAddress"/> instances.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="macaddr">The left-hand macaddr.</param>
/// <param name="other">The right-hand macaddr.</param>
/// <returns>
/// The result of the bitwise OR operation.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static PhysicalAddress Or([CanBeNull] this DbFunctions _, PhysicalAddress macaddr, PhysicalAddress other) => throw new ClientEvaluationNotSupportedException();

#endregion

#region ArithmeticOperators

/// <summary>
/// Adds the <paramref name="value"/> to the <see cref="IPAddress"/>.
/// </summary>
Expand Down Expand Up @@ -511,6 +653,10 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static (IPAddress Address, int Subnet) Subtract([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

#endregion

#region Functions

/// <summary>
/// Returns the abbreviated display format as text.
/// </summary>
Expand Down Expand Up @@ -854,5 +1000,7 @@ public static class NpgsqlNetworkAddressExtensions
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static PhysicalAddress Set7BitMac8([CanBeNull] this DbFunctions _, PhysicalAddress macAddress) => throw new ClientEvaluationNotSupportedException();

#endregion
}
}
Expand Up @@ -34,7 +34,7 @@
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal
{
/// <summary>
/// Provides translation services for PostgreSQL network address (inet, cidr) operators.
/// Provides translation services for PostgreSQL network address (cidr, inet, macaddr, macaddr8) operators and functions.
/// </summary>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/static/functions-net.html
Expand Down
Expand Up @@ -7,6 +7,7 @@
<AssemblyOriginatorKeyFile>../../Npgsql.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<None Update="Northwind.sql">
Expand Down

0 comments on commit 472c031

Please sign in to comment.