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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<EFCoreVersion>10.0.0-rc.2.25502.106</EFCoreVersion>
<MicrosoftExtensionsVersion>10.0.0-rc.2.25502.106</MicrosoftExtensionsVersion>
<NpgsqlVersion>9.0.3</NpgsqlVersion>
<NpgsqlVersion>10.0.0-rc.1</NpgsqlVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,21 @@ public static int Subtract(this DbFunctions _, NpgsqlInet inet, NpgsqlInet other
public static string Abbreviate(this DbFunctions _, NpgsqlInet inet)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Abbreviate)));

/// <summary>
/// Returns the abbreviated display format as text.
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="cidr">The cidr to abbreviate.</param>
/// <returns>
/// The abbreviated display format as text.
/// </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 string Abbreviate(this DbFunctions _, IPNetwork cidr)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Abbreviate)));

#pragma warning disable CS0618 // NpgsqlCidr is obsolete, replaced by .NET IPNetwork
/// <summary>
/// Returns the abbreviated display format as text.
/// </summary>
Expand All @@ -385,6 +400,7 @@ public static string Abbreviate(this DbFunctions _, NpgsqlInet inet)
/// </exception>
public static string Abbreviate(this DbFunctions _, NpgsqlCidr cidr)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Abbreviate)));
#pragma warning restore CS0618

/// <summary>
/// Returns the broadcast address for a network.
Expand Down Expand Up @@ -481,7 +497,7 @@ public static NpgsqlInet Netmask(this DbFunctions _, NpgsqlInet inet)
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static NpgsqlCidr Network(this DbFunctions _, NpgsqlInet inet)
public static IPNetwork Network(this DbFunctions _, NpgsqlInet inet)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Network)));

/// <summary>
Expand All @@ -499,6 +515,22 @@ public static NpgsqlCidr Network(this DbFunctions _, NpgsqlInet inet)
public static NpgsqlInet SetMaskLength(this DbFunctions _, NpgsqlInet inet, int length)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(SetMaskLength)));

/// <summary>
/// Sets the length of the subnet mask.
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="cidr">The cidr to modify.</param>
/// <param name="length">The subnet mask length to set.</param>
/// <returns>
/// The network with a subnet mask of the specified length.
/// </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 IPNetwork SetMaskLength(this DbFunctions _, IPNetwork cidr, int length)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(SetMaskLength)));

#pragma warning disable CS0618 // NpgsqlCidr is obsolete, replaced by .NET IPNetwork
/// <summary>
/// Sets the length of the subnet mask.
/// </summary>
Expand All @@ -513,6 +545,7 @@ public static NpgsqlInet SetMaskLength(this DbFunctions _, NpgsqlInet inet, int
/// </exception>
public static NpgsqlCidr SetMaskLength(this DbFunctions _, NpgsqlCidr cidr, int length)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(SetMaskLength)));
#pragma warning restore CS0618

/// <summary>
/// Extracts the IP address and subnet mask as text.
Expand Down Expand Up @@ -555,7 +588,7 @@ public static bool SameFamily(this DbFunctions _, NpgsqlInet inet, NpgsqlInet ot
/// <exception cref="NotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static NpgsqlCidr Merge(this DbFunctions _, NpgsqlInet inet, NpgsqlInet other)
public static IPNetwork Merge(this DbFunctions _, NpgsqlInet inet, NpgsqlInet other)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Merge)));

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ public NpgsqlNetworkTranslator(
return TranslateInetExtensionMethod(method, arguments);
}

if (paramType == typeof(NpgsqlCidr))
#pragma warning disable CS0618 // NpgsqlCidr is obsolete, replaced by .NET IPNetwork
if (paramType == typeof(IPNetwork) || paramType == typeof(NpgsqlCidr))
{
return TranslateCidrExtensionMethod(method, arguments);
}
#pragma warning restore CS0618

if (paramType == typeof(PhysicalAddress))
{
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@ protected virtual Expression VisitPgBinary(PgBinaryExpression binaryExpression)
binaryExpression.OperatorType switch
{
PgExpressionType.Contains
when binaryExpression.Left.TypeMapping is NpgsqlInetTypeMapping or NpgsqlCidrTypeMapping
when binaryExpression.Left.TypeMapping is NpgsqlInetTypeMapping or NpgsqlCidrTypeMapping or NpgsqlLegacyCidrTypeMapping
=> ">>",

PgExpressionType.ContainedBy
when binaryExpression.Left.TypeMapping is NpgsqlInetTypeMapping or NpgsqlCidrTypeMapping
when binaryExpression.Left.TypeMapping is NpgsqlInetTypeMapping or NpgsqlCidrTypeMapping or NpgsqlLegacyCidrTypeMapping
=> "<<",

PgExpressionType.Contains => "@>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
// implicit conversion operator to NpgsqlInet. So remove that cast as well.
case ExpressionType.Convert
when unaryExpression.Type == typeof(NpgsqlInet)
&& (unaryExpression.Operand.Type == typeof(IPAddress) || unaryExpression.Operand.Type == typeof(NpgsqlCidr)):
&& (unaryExpression.Operand.Type == typeof(IPAddress)
|| unaryExpression.Operand.Type == typeof(IPNetwork)
#pragma warning disable CS0618 // NpgsqlCidr is obsolete, replaced by .NET IPNetwork
|| unaryExpression.Operand.Type == typeof(NpgsqlCidr)):
#pragma warning restore CS0618
return Visit(unaryExpression.Operand);
}

Expand Down
127 changes: 127 additions & 0 deletions src/EFCore.PG/Storage/Internal/Mapping/NpgsqlCidrLegacyTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System.Net;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.Json;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;

#pragma warning disable CS0618 // NpgsqlCidr is obsolete, replaced by .NET IPNetwork

/// <summary>
/// The type mapping for the PostgreSQL cidr type.
/// </summary>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/static/datatype-net-types.html#DATATYPE-CIDR
/// </remarks>
public class NpgsqlLegacyCidrTypeMapping : NpgsqlTypeMapping
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static NpgsqlLegacyCidrTypeMapping Default { get; } = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public NpgsqlLegacyCidrTypeMapping()
: base("cidr", typeof(NpgsqlCidr), NpgsqlDbType.Cidr, JsonCidrLegacyReaderWriter.Instance)
{
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected NpgsqlLegacyCidrTypeMapping(RelationalTypeMappingParameters parameters)
: base(parameters, NpgsqlDbType.Cidr)
{
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new NpgsqlLegacyCidrTypeMapping(parameters);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string GenerateNonNullSqlLiteral(object value)
{
var cidr = (NpgsqlCidr)value;
return $"CIDR '{cidr.Address}/{cidr.Netmask}'";
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override Expression GenerateCodeLiteral(object value)
{
var cidr = (NpgsqlCidr)value;
return Expression.New(
NpgsqlCidrConstructor,
Expression.Call(ParseMethod, Expression.Constant(cidr.Address.ToString())),
Expression.Constant(cidr.Netmask));
}

private static readonly MethodInfo ParseMethod = typeof(IPAddress).GetMethod("Parse", [typeof(string)])!;

private static readonly ConstructorInfo NpgsqlCidrConstructor =
typeof(NpgsqlCidr).GetConstructor([typeof(IPAddress), typeof(byte)])!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class JsonCidrLegacyReaderWriter : JsonValueReaderWriter<NpgsqlCidr>
{
private static readonly PropertyInfo InstanceProperty = typeof(JsonCidrLegacyReaderWriter).GetProperty(nameof(Instance))!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static JsonCidrLegacyReaderWriter Instance { get; } = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override NpgsqlCidr FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> new(manager.CurrentReader.GetString()!);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, NpgsqlCidr value)
=> writer.WriteStringValue(value.ToString());

/// <inheritdoc />
public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty);
}
}
24 changes: 12 additions & 12 deletions src/EFCore.PG/Storage/Internal/Mapping/NpgsqlCidrTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class NpgsqlCidrTypeMapping : NpgsqlTypeMapping
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public NpgsqlCidrTypeMapping()
: base("cidr", typeof(NpgsqlCidr), NpgsqlDbType.Cidr, JsonCidrReaderWriter.Instance)
: base("cidr", typeof(IPNetwork), NpgsqlDbType.Cidr, JsonCidrReaderWriter.Instance)
{
}

Expand Down Expand Up @@ -59,8 +59,8 @@ protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters p
/// </summary>
protected override string GenerateNonNullSqlLiteral(object value)
{
var cidr = (NpgsqlCidr)value;
return $"CIDR '{cidr.Address}/{cidr.Netmask}'";
var ipNetwork = (IPNetwork)value;
return $"CIDR '{ipNetwork.BaseAddress}/{ipNetwork.PrefixLength}'";
}

/// <summary>
Expand All @@ -71,25 +71,25 @@ protected override string GenerateNonNullSqlLiteral(object value)
/// </summary>
public override Expression GenerateCodeLiteral(object value)
{
var cidr = (NpgsqlCidr)value;
var cidr = (IPNetwork)value;
return Expression.New(
NpgsqlCidrConstructor,
Expression.Call(ParseMethod, Expression.Constant(cidr.Address.ToString())),
Expression.Constant(cidr.Netmask));
Expression.Call(ParseMethod, Expression.Constant(cidr.BaseAddress.ToString())),
Expression.Constant(cidr.PrefixLength));
}

private static readonly MethodInfo ParseMethod = typeof(IPAddress).GetMethod("Parse", [typeof(string)])!;

private static readonly ConstructorInfo NpgsqlCidrConstructor =
typeof(NpgsqlCidr).GetConstructor([typeof(IPAddress), typeof(byte)])!;
typeof(IPNetwork).GetConstructor([typeof(IPAddress), typeof(int)])!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class JsonCidrReaderWriter : JsonValueReaderWriter<NpgsqlCidr>
public sealed class JsonCidrReaderWriter : JsonValueReaderWriter<IPNetwork>
{
private static readonly PropertyInfo InstanceProperty = typeof(JsonCidrReaderWriter).GetProperty(nameof(Instance))!;

Expand All @@ -107,17 +107,17 @@ public sealed class JsonCidrReaderWriter : JsonValueReaderWriter<NpgsqlCidr>
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override NpgsqlCidr FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> new(manager.CurrentReader.GetString()!);
public override IPNetwork FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> IPNetwork.Parse(manager.CurrentReader.GetString()!);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, NpgsqlCidr value)
=> writer.WriteStringValue(value.ToString());
public override void ToJsonTyped(Utf8JsonWriter writer, IPNetwork ipNetwork)
=> writer.WriteStringValue(ipNetwork.ToString());

/// <inheritdoc />
public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty);
Expand Down
Loading