forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Some work on date/time and network converters #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Npgsql/Internal/Converters/Networking/IPAddressConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
|
|
||
| namespace Npgsql.Internal.Converters; | ||
|
|
||
| sealed class IPAddressConverter : PgBufferedConverter<IPAddress> | ||
| { | ||
| public override Size GetSize(SizeContext context, IPAddress value, ref object? writeState) | ||
| => NpgsqlInetConverter.DoGetSize(context, value, ref writeState); | ||
|
|
||
| protected override IPAddress ReadCore(PgReader reader) | ||
| => NpgsqlInetConverter.DoReadCore(reader, shouldBeCidr: false).Address; | ||
|
|
||
| protected override void WriteCore(PgWriter writer, IPAddress value) | ||
| => NpgsqlInetConverter.DoWriteCore( | ||
| writer, | ||
| (value, (byte)(value.AddressFamily == AddressFamily.InterNetwork ? 32 : 128)), | ||
| isCidr: false); | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/Npgsql/Internal/Converters/Networking/MacaddrConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using System.Diagnostics; | ||
| using System.Net.NetworkInformation; | ||
|
|
||
| namespace Npgsql.Internal.Converters; | ||
|
|
||
| sealed class MacaddrConverter : PgBufferedConverter<PhysicalAddress> | ||
| { | ||
| public override Size GetSize(SizeContext context, PhysicalAddress value, ref object? writeState) | ||
| => value.GetAddressBytes().Length; | ||
|
|
||
| protected override PhysicalAddress ReadCore(PgReader reader) | ||
| { | ||
| var len = reader.Current.Size.Value; | ||
| Debug.Assert(len is 6 or 8); | ||
|
|
||
| var bytes = new byte[len]; | ||
| reader.CopyTo(bytes); | ||
| return new PhysicalAddress(bytes); | ||
| } | ||
|
|
||
| protected override void WriteCore(PgWriter writer, PhysicalAddress value) | ||
| { | ||
| var bytes = value.GetAddressBytes(); | ||
| writer.WriteRaw(bytes); | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
src/Npgsql/Internal/Converters/Networking/NpgsqlCidrConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System; | ||
| using System.Buffers; | ||
| using System.Diagnostics; | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
| using NpgsqlTypes; | ||
|
|
||
| namespace Npgsql.Internal.Converters; | ||
|
|
||
| sealed class NpgsqlCidrConverter : PgBufferedConverter<NpgsqlCidr> | ||
| { | ||
| public override Size GetSize(SizeContext context, NpgsqlCidr value, ref object? writeState) | ||
| => NpgsqlInetConverter.DoGetSize(context, value.Address, ref writeState); | ||
|
|
||
| protected override NpgsqlCidr ReadCore(PgReader reader) | ||
| { | ||
| var (ip, netmask) = NpgsqlInetConverter.DoReadCore(reader, shouldBeCidr: true); | ||
| return new(ip, netmask); | ||
| } | ||
|
|
||
| protected override void WriteCore(PgWriter writer, NpgsqlCidr value) | ||
| => NpgsqlInetConverter.DoWriteCore(writer, (value.Address, value.Netmask), isCidr: false); | ||
| } |
66 changes: 66 additions & 0 deletions
66
src/Npgsql/Internal/Converters/Networking/NpgsqlInetConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using System; | ||
| using System.Buffers; | ||
| using System.Diagnostics; | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
| using NpgsqlTypes; | ||
|
|
||
| namespace Npgsql.Internal.Converters; | ||
|
|
||
| sealed class NpgsqlInetConverter : PgBufferedConverter<NpgsqlInet> | ||
| { | ||
| const byte IPv4 = 2; | ||
| const byte IPv6 = 3; | ||
|
|
||
| public override Size GetSize(SizeContext context, NpgsqlInet value, ref object? writeState) | ||
| => DoGetSize(context, value.Address, ref writeState); | ||
|
|
||
| internal static Size DoGetSize(SizeContext context, IPAddress ipAddress, ref object? writeState) | ||
| => ipAddress.AddressFamily switch | ||
| { | ||
| AddressFamily.InterNetwork => 8, | ||
| AddressFamily.InterNetworkV6 => 20, | ||
| _ => throw new InvalidCastException( | ||
| $"Can't handle IPAddress with AddressFamily {ipAddress.AddressFamily}, only InterNetwork or InterNetworkV6!") | ||
| }; | ||
|
|
||
| protected override NpgsqlInet ReadCore(PgReader reader) | ||
| { | ||
| var (ip, netmask) = DoReadCore(reader, shouldBeCidr: false); | ||
| return new(ip, netmask); | ||
| } | ||
|
|
||
| internal static (IPAddress Address, byte Netmask) DoReadCore(PgReader reader, bool shouldBeCidr) | ||
| { | ||
| _ = reader.ReadByte(); // addressFamily | ||
| var mask = reader.ReadByte(); // mask | ||
|
|
||
| var isCidr = reader.ReadByte() == 1; | ||
| Debug.Assert(isCidr == shouldBeCidr); | ||
|
|
||
| var numBytes = reader.ReadByte(); | ||
| Span<byte> bytes = stackalloc byte[numBytes]; | ||
| reader.CopyTo(bytes); | ||
| return (new IPAddress(bytes), mask); | ||
| } | ||
|
|
||
| protected override void WriteCore(PgWriter writer, NpgsqlInet value) | ||
| => DoWriteCore(writer, (value.Address, value.Netmask), isCidr: false); | ||
|
|
||
| internal static void DoWriteCore(PgWriter writer, (IPAddress Address, byte Netmask) value, bool isCidr) | ||
| { | ||
| writer.WriteByte(value.Address.AddressFamily switch | ||
| { | ||
| AddressFamily.InterNetwork => IPv4, | ||
| AddressFamily.InterNetworkV6 => IPv6, | ||
| _ => throw new InvalidCastException( | ||
| $"Can't handle IPAddress with AddressFamily {value.Address.AddressFamily}, only InterNetwork or InterNetworkV6!") | ||
| }); | ||
|
|
||
| writer.WriteByte(value.Netmask); | ||
| writer.WriteByte((byte)(isCidr ? 1 : 0)); // Ignored on server side | ||
| var bytes = value.Address.GetAddressBytes(); | ||
| writer.WriteByte((byte)bytes.Length); | ||
| writer.WriteRaw(bytes); | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
src/Npgsql/Internal/Converters/Temporal/DateConverters.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| using System; | ||
| using Npgsql.Properties; | ||
|
|
||
| namespace Npgsql.Internal.Converters; | ||
|
|
||
| sealed class DateTimeDateConverter : PgBufferedConverter<DateTime> | ||
| { | ||
| readonly bool _dateTimeInfinityConversions; | ||
|
|
||
| static readonly DateTime BaseValue = new(2000, 1, 1, 0, 0, 0); | ||
|
|
||
| public DateTimeDateConverter(bool dateTimeInfinityConversions) | ||
| => _dateTimeInfinityConversions = dateTimeInfinityConversions; | ||
|
|
||
| public override bool CanConvert(DataFormat format, out BufferingRequirement bufferingRequirement) | ||
| { | ||
| bufferingRequirement = BufferingRequirement.FixedSize; | ||
| return base.CanConvert(format, out _); | ||
| } | ||
|
|
||
| public override Size GetSize(SizeContext context, DateTime value, ref object? writeState) | ||
| => sizeof(int); | ||
|
|
||
| protected override DateTime ReadCore(PgReader reader) | ||
| => reader.ReadInt32() switch | ||
| { | ||
| int.MaxValue => !_dateTimeInfinityConversions | ||
| ? throw new InvalidCastException(NpgsqlStrings.CannotReadInfinityValue) | ||
| : DateTime.MaxValue, | ||
| int.MinValue => !_dateTimeInfinityConversions | ||
| ? throw new InvalidCastException(NpgsqlStrings.CannotReadInfinityValue) | ||
| : DateTime.MinValue, | ||
| var value => BaseValue + TimeSpan.FromDays(value) | ||
| }; | ||
|
|
||
| protected override void WriteCore(PgWriter writer, DateTime value) | ||
| { | ||
| if (_dateTimeInfinityConversions) | ||
| { | ||
| if (value == DateTime.MaxValue) | ||
| { | ||
| writer.WriteInt32(int.MaxValue); | ||
| return; | ||
| } | ||
|
|
||
| if (value == DateTime.MinValue) | ||
| { | ||
| writer.WriteInt32(int.MinValue); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| writer.WriteInt32((value.Date - BaseValue).Days); | ||
| } | ||
| } | ||
|
|
||
| #if NET6_0_OR_GREATER | ||
| sealed class DateOnlyDateConverter : PgBufferedConverter<DateOnly> | ||
| { | ||
| readonly bool _dateTimeInfinityConversions; | ||
|
|
||
| static readonly DateOnly BaseValue = new(2000, 1, 1); | ||
|
|
||
| public DateOnlyDateConverter(bool dateTimeInfinityConversions) | ||
| => _dateTimeInfinityConversions = dateTimeInfinityConversions; | ||
|
|
||
| protected override DateOnly ReadCore(PgReader reader) | ||
| => reader.ReadInt32() switch | ||
| { | ||
| int.MaxValue => !_dateTimeInfinityConversions | ||
| ? throw new InvalidCastException(NpgsqlStrings.CannotReadInfinityValue) | ||
| : DateOnly.MaxValue, | ||
| int.MinValue => !_dateTimeInfinityConversions | ||
| ? throw new InvalidCastException(NpgsqlStrings.CannotReadInfinityValue) | ||
| : DateOnly.MinValue, | ||
| var value => BaseValue.AddDays(value) | ||
| }; | ||
|
|
||
| public override Size GetSize(SizeContext context, DateOnly value, ref object? writeState) | ||
| => sizeof(int); | ||
|
|
||
| protected override void WriteCore(PgWriter writer, DateOnly value) | ||
| { | ||
| if (_dateTimeInfinityConversions) | ||
| { | ||
| if (value == DateOnly.MaxValue) | ||
| { | ||
| writer.WriteInt32(int.MaxValue); | ||
| return; | ||
| } | ||
|
|
||
| if (value == DateOnly.MinValue) | ||
| { | ||
| writer.WriteInt32(int.MinValue); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| writer.WriteInt32(value.DayNumber - BaseValue.DayNumber); | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.