Use SDK 5.0.0-preview2 and fix nullability issues · npgsql/npgsql@a2672ae · GitHub
Skip to content

Commit a2672ae

Browse files
committed
Use SDK 5.0.0-preview2 and fix nullability issues
Important to prepare for multiplexing
1 parent ba4285c commit a2672ae

9 files changed

Lines changed: 62 additions & 30 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 5 additions & 0 deletions

‎global.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "3.1.100"
3+
"version": "5.0.100-preview.2.20176.6"
44
}
55
}

‎src/Npgsql/BackendMessages/RowDescriptionMessage.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ sealed class InsensitiveComparer : IEqualityComparer<string>
122122

123123
// We should really have CompareOptions.IgnoreKanaType here, but see
124124
// https://github.com/dotnet/corefx/issues/12518#issuecomment-389658716
125-
public bool Equals(string x, string y)
125+
public bool Equals(string? x, string? y)
126126
=> CompareInfo.Compare(x, y, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType) == 0;
127127

128128
public int GetHashCode(string o)

‎src/Npgsql/NpgsqlBinaryExporter.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ async ValueTask<T> DoRead<T>(NpgsqlTypeHandler handler, bool async)
262262
#pragma warning disable CS8653 // A default expression introduces a null value when 'T' is a non-nullable reference type.
263263
// When T is a Nullable<T>, we support returning null
264264
if (NullableHandler<T>.Exists)
265-
return default;
265+
return default!;
266266
#pragma warning restore CS8653
267267
throw new InvalidCastException("Column is null");
268268
}

‎src/Npgsql/NpgsqlTypes/NpgsqlRange.cs‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ namespace NpgsqlTypes
139139
/// </summary>
140140
/// <param name="lowerBound">The lower bound of the range.</param>
141141
/// <param name="upperBound">The upper bound of the range.</param>
142-
public NpgsqlRange(T lowerBound, T upperBound)
142+
public NpgsqlRange([AllowNull] T lowerBound, [AllowNull] T upperBound)
143143
: this(lowerBound, true, false, upperBound, true, false) { }
144144

145145
/// <summary>
@@ -149,7 +149,9 @@ public NpgsqlRange(T lowerBound, T upperBound)
149149
/// <param name="lowerBoundIsInclusive">True if the lower bound is is part of the range (i.e. inclusive); otherwise, false.</param>
150150
/// <param name="upperBound">The upper bound of the range.</param>
151151
/// <param name="upperBoundIsInclusive">True if the upper bound is part of the range (i.e. inclusive); otherwise, false.</param>
152-
public NpgsqlRange(T lowerBound, bool lowerBoundIsInclusive, T upperBound, bool upperBoundIsInclusive)
152+
public NpgsqlRange(
153+
[AllowNull] T lowerBound, bool lowerBoundIsInclusive,
154+
[AllowNull] T upperBound, bool upperBoundIsInclusive)
153155
: this(lowerBound, lowerBoundIsInclusive, false, upperBound, upperBoundIsInclusive, false) { }
154156

155157
/// <summary>
@@ -161,8 +163,9 @@ public NpgsqlRange(T lowerBound, bool lowerBoundIsInclusive, T upperBound, bool
161163
/// <param name="upperBound">The upper bound of the range.</param>
162164
/// <param name="upperBoundIsInclusive">True if the upper bound is part of the range (i.e. inclusive); otherwise, false.</param>
163165
/// <param name="upperBoundInfinite">True if the upper bound is indefinite (i.e. infinite or unbounded); otherwise, false.</param>
164-
public NpgsqlRange(T lowerBound, bool lowerBoundIsInclusive, bool lowerBoundInfinite,
165-
T upperBound, bool upperBoundIsInclusive, bool upperBoundInfinite)
166+
public NpgsqlRange(
167+
[AllowNull] T lowerBound, bool lowerBoundIsInclusive, bool lowerBoundInfinite,
168+
[AllowNull] T upperBound, bool upperBoundIsInclusive, bool upperBoundInfinite)
166169
: this(
167170
lowerBound,
168171
upperBound,
@@ -204,7 +207,7 @@ internal NpgsqlRange([AllowNull] T lowerBound, [AllowNull] T upperBound, RangeFl
204207
/// <returns>
205208
/// True if the range is implicitly empty; otherwise, false.
206209
/// </returns>
207-
static bool IsEmptyRange(T lowerBound, T upperBound, RangeFlags flags)
210+
static bool IsEmptyRange([AllowNull] T lowerBound, [AllowNull] T upperBound, RangeFlags flags)
208211
{
209212
// ---------------------------------------------------------------------------------
210213
// We only want to check for those conditions that are unambiguously erroneous:
@@ -227,15 +230,16 @@ static bool IsEmptyRange(T lowerBound, T upperBound, RangeFlags flags)
227230
if ((flags & RangeFlags.Inclusive) == RangeFlags.Inclusive)
228231
return false;
229232

233+
if (lowerBound is null || upperBound is null)
234+
return false;
235+
230236
if (!HasEquatableBounds)
231237
return lowerBound?.Equals(upperBound) ?? false;
232238

233-
var lower = (IEquatable<T>?)lowerBound;
234-
var upper = (IEquatable<T>?)upperBound;
239+
var lower = (IEquatable<T>)lowerBound;
240+
var upper = (IEquatable<T>)upperBound;
235241

236-
return lower != null && !lower.Equals(default!) &&
237-
upper != null && !upper.Equals(default!) &&
238-
lower.Equals(upperBound);
242+
return !lower.Equals(default!) && !upper.Equals(default!) && lower.Equals(upperBound);
239243
}
240244

241245
/// <summary>
@@ -303,13 +307,22 @@ public bool Equals(NpgsqlRange<T> other)
303307
return false;
304308

305309
if (HasEquatableBounds)
306-
return
307-
(LowerBound == null ? other.LowerBound == null : ((IEquatable<T>)LowerBound).Equals(other.LowerBound)) &&
308-
(UpperBound == null ? other.UpperBound == null : ((IEquatable<T>)UpperBound).Equals(other.UpperBound));
310+
{
311+
var lowerEqual = LowerBound is null
312+
? other.LowerBound is null
313+
: !(other.LowerBound is null) && ((IEquatable<T>)LowerBound).Equals(other.LowerBound);
314+
315+
if (!lowerEqual)
316+
return false;
317+
318+
return UpperBound is null
319+
? other.UpperBound is null
320+
: !(other.UpperBound is null) && ((IEquatable<T>)UpperBound).Equals(other.UpperBound);
321+
}
309322

310323
return
311-
(LowerBound?.Equals(other.LowerBound) ?? other.LowerBound == null) &&
312-
(UpperBound?.Equals(other.UpperBound) ?? other.UpperBound == null);
324+
(LowerBound?.Equals(other.LowerBound) ?? other.LowerBound is null) &&
325+
(UpperBound?.Equals(other.UpperBound) ?? other.UpperBound is null);
313326
}
314327

315328
/// <inheritdoc />

‎src/Npgsql/TypeHandlers/ArrayHandler.cs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,19 @@ int ValidateAndGetLengthGeneric(ICollection<TElement> value, ref NpgsqlLengthCac
321321
NpgsqlLengthCache? elemLengthCache = lengthCache;
322322

323323
foreach (var element in value)
324-
if (element != null && typeof(TElement) != typeof(DBNull))
325-
try
326-
{
327-
len += ElementHandler.ValidateAndGetLength(element, ref elemLengthCache, null);
328-
}
329-
catch (Exception e)
330-
{
331-
throw MixedTypesOrJaggedArrayException(e);
332-
}
324+
{
325+
if (element is null || typeof(TElement) == typeof(DBNull))
326+
continue;
327+
328+
try
329+
{
330+
len += ElementHandler.ValidateAndGetLength(element, ref elemLengthCache, null);
331+
}
332+
catch (Exception e)
333+
{
334+
throw MixedTypesOrJaggedArrayException(e);
335+
}
336+
}
333337

334338
lengthCache.Lengths[pos] = len;
335339
return len;

‎src/Npgsql/TypeHandlers/RangeHandler.cs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,18 @@ public int ValidateAndGetLength(NpgsqlRange<TElement> value, ref NpgsqlLengthCac
123123
if (!value.IsEmpty)
124124
{
125125
if (!value.LowerBoundInfinite)
126-
totalLen += 4 + _elementHandler.ValidateAndGetLength(value.LowerBound, ref lengthCache, null);
126+
{
127+
totalLen += 4;
128+
if (!(value.LowerBound is null) && typeof(TElement) != typeof(DBNull))
129+
totalLen += _elementHandler.ValidateAndGetLength(value.LowerBound, ref lengthCache, null);
130+
}
127131

128132
if (!value.UpperBoundInfinite)
129-
totalLen += 4 + _elementHandler.ValidateAndGetLength(value.UpperBound, ref lengthCache, null);
133+
{
134+
totalLen += 4;
135+
if (!(value.UpperBound is null) && typeof(TElement) != typeof(DBNull))
136+
totalLen += _elementHandler.ValidateAndGetLength(value.UpperBound, ref lengthCache, null);
137+
}
130138
}
131139

132140
// If we're traversing an already-populated length cache, rewind to first element slot so that

‎test/Directory.Build.props‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<!-- Build configuration -->
55
<PropertyGroup>
6-
<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
6+
<TargetFrameworks>net461;netcoreapp5.0</TargetFrameworks>
77
<IsPackable>false</IsPackable>
88
</PropertyGroup>
99

‎test/Npgsql.Tests/TestUtil.cs‎

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)