|
| 1 | +using System.Net; |
| 2 | +using System.Text.Json; |
| 3 | +using Microsoft.EntityFrameworkCore.Storage.Json; |
| 4 | + |
| 5 | +namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping; |
| 6 | + |
| 7 | +#pragma warning disable CS0618 // NpgsqlCidr is obsolete, replaced by .NET IPNetwork |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// The type mapping for the PostgreSQL cidr type. |
| 11 | +/// </summary> |
| 12 | +/// <remarks> |
| 13 | +/// See: https://www.postgresql.org/docs/current/static/datatype-net-types.html#DATATYPE-CIDR |
| 14 | +/// </remarks> |
| 15 | +public class NpgsqlLegacyCidrTypeMapping : NpgsqlTypeMapping |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 19 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 20 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 21 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 22 | + /// </summary> |
| 23 | + public static NpgsqlLegacyCidrTypeMapping Default { get; } = new(); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 27 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 28 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 29 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 30 | + /// </summary> |
| 31 | + public NpgsqlLegacyCidrTypeMapping() |
| 32 | + : base("cidr", typeof(NpgsqlCidr), NpgsqlDbType.Cidr, JsonCidrLegacyReaderWriter.Instance) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 38 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 39 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 40 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 41 | + /// </summary> |
| 42 | + protected NpgsqlLegacyCidrTypeMapping(RelationalTypeMappingParameters parameters) |
| 43 | + : base(parameters, NpgsqlDbType.Cidr) |
| 44 | + { |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 49 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 50 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 51 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 52 | + /// </summary> |
| 53 | + protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters) |
| 54 | + => new NpgsqlLegacyCidrTypeMapping(parameters); |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 58 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 59 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 60 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 61 | + /// </summary> |
| 62 | + protected override string GenerateNonNullSqlLiteral(object value) |
| 63 | + { |
| 64 | + var cidr = (NpgsqlCidr)value; |
| 65 | + return $"CIDR '{cidr.Address}/{cidr.Netmask}'"; |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 70 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 71 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 72 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 73 | + /// </summary> |
| 74 | + public override Expression GenerateCodeLiteral(object value) |
| 75 | + { |
| 76 | + var cidr = (NpgsqlCidr)value; |
| 77 | + return Expression.New( |
| 78 | + NpgsqlCidrConstructor, |
| 79 | + Expression.Call(ParseMethod, Expression.Constant(cidr.Address.ToString())), |
| 80 | + Expression.Constant(cidr.Netmask)); |
| 81 | + } |
| 82 | + |
| 83 | + private static readonly MethodInfo ParseMethod = typeof(IPAddress).GetMethod("Parse", [typeof(string)])!; |
| 84 | + |
| 85 | + private static readonly ConstructorInfo NpgsqlCidrConstructor = |
| 86 | + typeof(NpgsqlCidr).GetConstructor([typeof(IPAddress), typeof(byte)])!; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 90 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 91 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 92 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 93 | + /// </summary> |
| 94 | + public sealed class JsonCidrLegacyReaderWriter : JsonValueReaderWriter<NpgsqlCidr> |
| 95 | + { |
| 96 | + private static readonly PropertyInfo InstanceProperty = typeof(JsonCidrLegacyReaderWriter).GetProperty(nameof(Instance))!; |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 100 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 101 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 102 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 103 | + /// </summary> |
| 104 | + public static JsonCidrLegacyReaderWriter Instance { get; } = new(); |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 108 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 109 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 110 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 111 | + /// </summary> |
| 112 | + public override NpgsqlCidr FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null) |
| 113 | + => new(manager.CurrentReader.GetString()!); |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 117 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 118 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 119 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 120 | + /// </summary> |
| 121 | + public override void ToJsonTyped(Utf8JsonWriter writer, NpgsqlCidr value) |
| 122 | + => writer.WriteStringValue(value.ToString()); |
| 123 | + |
| 124 | + /// <inheritdoc /> |
| 125 | + public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty); |
| 126 | + } |
| 127 | +} |
0 commit comments