|
| 1 | +using System; |
| 2 | +using Npgsql.Internal; |
| 3 | +using Npgsql.Internal.Postgres; |
| 4 | +using NpgsqlTypes; |
| 5 | + |
| 6 | +sealed class LegacyDateAndTimeResolverFactory : PgTypeInfoResolverFactory |
| 7 | +{ |
| 8 | + public override IPgTypeInfoResolver CreateResolver() => new Resolver(); |
| 9 | + public override IPgTypeInfoResolver CreateArrayResolver() => new ArrayResolver(); |
| 10 | + public override IPgTypeInfoResolver CreateRangeResolver() => new RangeResolver(); |
| 11 | + public override IPgTypeInfoResolver CreateRangeArrayResolver() => new RangeArrayResolver(); |
| 12 | + public override IPgTypeInfoResolver CreateMultirangeResolver() => new MultirangeResolver(); |
| 13 | + public override IPgTypeInfoResolver CreateMultirangeArrayResolver() => new MultirangeArrayResolver(); |
| 14 | + |
| 15 | + const string Date = "pg_catalog.date"; |
| 16 | + const string Time = "pg_catalog.time"; |
| 17 | + const string DateRange = "pg_catalog.daterange"; |
| 18 | + const string DateMultirange = "pg_catalog.datemultirange"; |
| 19 | + |
| 20 | + class Resolver : IPgTypeInfoResolver |
| 21 | + { |
| 22 | + TypeInfoMappingCollection? _mappings; |
| 23 | + protected TypeInfoMappingCollection Mappings => _mappings ??= AddMappings(new()); |
| 24 | + |
| 25 | + public PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options) |
| 26 | + => type == typeof(object) ? Mappings.Find(type, dataTypeName, options) : null; |
| 27 | + |
| 28 | + static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings) |
| 29 | + { |
| 30 | + mappings.AddStructType<DateTime>(Date, |
| 31 | + static (options, mapping, _) => options.GetTypeInfo(typeof(DateTime), new DataTypeName(mapping.DataTypeName))!, |
| 32 | + matchRequirement: MatchRequirement.DataTypeName); |
| 33 | + |
| 34 | + mappings.AddStructType<TimeSpan>(Time, |
| 35 | + static (options, mapping, _) => options.GetTypeInfo(typeof(TimeSpan), new DataTypeName(mapping.DataTypeName))!, |
| 36 | + isDefault: true); |
| 37 | + |
| 38 | + return mappings; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + sealed class ArrayResolver : Resolver, IPgTypeInfoResolver |
| 43 | + { |
| 44 | + TypeInfoMappingCollection? _mappings; |
| 45 | + new TypeInfoMappingCollection Mappings => _mappings ??= AddMappings(new(base.Mappings)); |
| 46 | + |
| 47 | + public new PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options) |
| 48 | + => type == typeof(object) ? Mappings.Find(type, dataTypeName, options) : null; |
| 49 | + |
| 50 | + static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings) |
| 51 | + { |
| 52 | + mappings.AddStructArrayType<DateTime>(Date); |
| 53 | + mappings.AddStructArrayType<TimeSpan>(Time); |
| 54 | + |
| 55 | + return mappings; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + class RangeResolver : IPgTypeInfoResolver |
| 60 | + { |
| 61 | + TypeInfoMappingCollection? _mappings; |
| 62 | + protected TypeInfoMappingCollection Mappings => _mappings ??= AddMappings(new()); |
| 63 | + |
| 64 | + public PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options) |
| 65 | + => type == typeof(object) ? Mappings.Find(type, dataTypeName, options) : null; |
| 66 | + |
| 67 | + static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings) |
| 68 | + { |
| 69 | + mappings.AddStructType<NpgsqlRange<DateTime>>(DateRange, |
| 70 | + static (options, mapping, _) => options.GetTypeInfo(typeof(NpgsqlRange<DateTime>), new DataTypeName(mapping.DataTypeName))!, |
| 71 | + matchRequirement: MatchRequirement.DataTypeName); |
| 72 | + |
| 73 | + return mappings; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + sealed class RangeArrayResolver : RangeResolver, IPgTypeInfoResolver |
| 78 | + { |
| 79 | + TypeInfoMappingCollection? _mappings; |
| 80 | + new TypeInfoMappingCollection Mappings => _mappings ??= AddMappings(new(base.Mappings)); |
| 81 | + |
| 82 | + public new PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options) |
| 83 | + => type == typeof(object) ? Mappings.Find(type, dataTypeName, options) : null; |
| 84 | + |
| 85 | + static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings) |
| 86 | + { |
| 87 | + mappings.AddStructArrayType<NpgsqlRange<DateTime>>(DateRange); |
| 88 | + |
| 89 | + return mappings; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + class MultirangeResolver : IPgTypeInfoResolver |
| 94 | + { |
| 95 | + TypeInfoMappingCollection? _mappings; |
| 96 | + protected TypeInfoMappingCollection Mappings => _mappings ??= AddMappings(new()); |
| 97 | + |
| 98 | + public PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options) |
| 99 | + => type == typeof(object) ? Mappings.Find(type, dataTypeName, options) : null; |
| 100 | + |
| 101 | + static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings) |
| 102 | + { |
| 103 | + mappings.AddType<NpgsqlRange<DateTime>[]>(DateMultirange, |
| 104 | + static (options, mapping, _) => options.GetTypeInfo(typeof(NpgsqlRange<DateTime>[]), new DataTypeName(mapping.DataTypeName))!, |
| 105 | + matchRequirement: MatchRequirement.DataTypeName); |
| 106 | + |
| 107 | + return mappings; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + sealed class MultirangeArrayResolver : MultirangeResolver, IPgTypeInfoResolver |
| 112 | + { |
| 113 | + TypeInfoMappingCollection? _mappings; |
| 114 | + new TypeInfoMappingCollection Mappings => _mappings ??= AddMappings(new(base.Mappings)); |
| 115 | + |
| 116 | + public new PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options) |
| 117 | + => type == typeof(object) ? Mappings.Find(type, dataTypeName, options) : null; |
| 118 | + |
| 119 | + static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings) |
| 120 | + { |
| 121 | + mappings.AddArrayType<NpgsqlRange<DateTime>[]>(DateMultirange); |
| 122 | + |
| 123 | + return mappings; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments