Skip to content

Commit 2de0e04

Browse files
committed
Add nullable annotations.
1 parent ac126ab commit 2de0e04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+105
-127
lines changed

src/MySqlConnector/Core/CachedParameter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Data;
43
using MySql.Data.MySqlClient;
@@ -7,7 +6,7 @@ namespace MySqlConnector.Core
76
{
87
internal sealed class CachedParameter
98
{
10-
public CachedParameter(int ordinalPosition, string mode, string name, string dataType, bool unsigned, int length)
9+
public CachedParameter(int ordinalPosition, string? mode, string? name, string dataType, bool unsigned, int length)
1110
{
1211
Position = ordinalPosition;
1312
if (Position == 0)
@@ -24,7 +23,7 @@ public CachedParameter(int ordinalPosition, string mode, string name, string dat
2423

2524
public int Position { get; }
2625
public ParameterDirection Direction { get; }
27-
public string Name { get; }
26+
public string? Name { get; }
2827
public MySqlDbType MySqlDbType { get; }
2928
}
3029
}

src/MySqlConnector/Core/CachedProcedure.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Collections.Generic;
43
using System.Data;
@@ -17,7 +16,7 @@ namespace MySqlConnector.Core
1716
{
1817
internal sealed class CachedProcedure
1918
{
20-
public static async Task<CachedProcedure> FillAsync(IOBehavior ioBehavior, MySqlConnection connection, string schema, string component, CancellationToken cancellationToken)
19+
public static async Task<CachedProcedure?> FillAsync(IOBehavior ioBehavior, MySqlConnection connection, string schema, string component, CancellationToken cancellationToken)
2120
{
2221
// try to use mysql.proc first, as it is much faster
2322
if (connection.Session.ServerVersion.Version < ServerVersions.RemovesMySqlProcTable && !connection.Session.ProcAccessDenied)
@@ -117,7 +116,7 @@ private CachedProcedure(string schema, string component, IReadOnlyList<CachedPar
117116
Parameters = parameters;
118117
}
119118

120-
internal MySqlParameterCollection AlignParamsWithDb(MySqlParameterCollection parameterCollection)
119+
internal MySqlParameterCollection AlignParamsWithDb(MySqlParameterCollection? parameterCollection)
121120
{
122121
var alignedParams = new MySqlParameterCollection();
123122
var returnParam = parameterCollection?.FirstOrDefault(x => x.Direction == ParameterDirection.ReturnValue);
@@ -132,7 +131,7 @@ internal MySqlParameterCollection AlignParamsWithDb(MySqlParameterCollection par
132131
else
133132
{
134133
var index = parameterCollection?.NormalizedIndexOf(cachedParam.Name) ?? -1;
135-
alignParam = index >= 0 ? parameterCollection[index] : throw new ArgumentException($"Parameter '{cachedParam.Name}' not found in the collection.");
134+
alignParam = index >= 0 ? parameterCollection![index] : throw new ArgumentException($"Parameter '{cachedParam.Name}' not found in the collection.");
136135
}
137136

138137
if (!alignParam.HasSetDirection)
@@ -252,7 +251,7 @@ internal static string ParseDataType(string sql, out bool unsigned, out int leng
252251
return sql;
253252
}
254253

255-
private static CachedParameter CreateCachedParameter(int ordinal, string direction, string name, string dataType, bool unsigned, int length, string originalSql)
254+
private static CachedParameter CreateCachedParameter(int ordinal, string? direction, string? name, string dataType, bool unsigned, int length, string originalSql)
256255
{
257256
try
258257
{

src/MySqlConnector/Core/ColumnTypeMetadata.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using MySql.Data.MySqlClient;
32

43
namespace MySqlConnector.Core
@@ -7,7 +6,7 @@ internal sealed class ColumnTypeMetadata
76
{
87
public static string CreateLookupKey(string columnTypeName, bool isUnsigned, int length) => $"{columnTypeName}|{(isUnsigned ? "u" : "s")}|{length}";
98

10-
public ColumnTypeMetadata(string dataTypeName, DbTypeMapping dbTypeMapping, MySqlDbType mySqlDbType, bool isUnsigned = false, bool binary = false, int length = 0, string simpleDataTypeName = null, string createFormat = null, long columnSize = 0)
9+
public ColumnTypeMetadata(string dataTypeName, DbTypeMapping dbTypeMapping, MySqlDbType mySqlDbType, bool isUnsigned = false, bool binary = false, int length = 0, string? simpleDataTypeName = null, string? createFormat = null, long columnSize = 0)
1110
{
1211
DataTypeName = dataTypeName;
1312
SimpleDataTypeName = simpleDataTypeName ?? dataTypeName;

src/MySqlConnector/Core/CommandListPosition.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System.Collections.Generic;
32

43
namespace MySqlConnector.Core

src/MySqlConnector/Core/ConnectionPool.cs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Collections.Concurrent;
43
using System.Collections.Generic;
@@ -44,15 +43,15 @@ public async ValueTask<ServerSession> GetSessionAsync(MySqlConnection connection
4443
else
4544
m_sessionSemaphore.Wait(cancellationToken);
4645

47-
ServerSession session = null;
46+
ServerSession? session = null;
4847
try
4948
{
5049
// check for a waiting session
5150
lock (m_sessions)
5251
{
5352
if (m_sessions.Count > 0)
5453
{
55-
session = m_sessions.First.Value;
54+
session = m_sessions.First!.Value;
5655
m_sessions.RemoveFirst();
5756
}
5857
}
@@ -212,12 +211,12 @@ public async Task ReapAsync(IOBehavior ioBehavior, CancellationToken cancellatio
212211
/// object is shared between multiple threads and is only safe to use after taking a <c>lock</c> on the
213212
/// object itself.
214213
/// </summary>
215-
public Dictionary<string, CachedProcedure> GetProcedureCache()
214+
public Dictionary<string, CachedProcedure?> GetProcedureCache()
216215
{
217216
var procedureCache = m_procedureCache;
218217
if (procedureCache is null)
219218
{
220-
var newProcedureCache = new Dictionary<string, CachedProcedure>();
219+
var newProcedureCache = new Dictionary<string, CachedProcedure?>();
221220
procedureCache = Interlocked.CompareExchange(ref m_procedureCache, newProcedureCache, null) ?? newProcedureCache;
222221
}
223222
return procedureCache;
@@ -282,12 +281,12 @@ private async Task CleanPoolAsync(IOBehavior ioBehavior, Func<ServerSession, boo
282281
try
283282
{
284283
// check for a waiting session
285-
ServerSession session = null;
284+
ServerSession? session = null;
286285
lock (m_sessions)
287286
{
288287
if (m_sessions.Count > 0)
289288
{
290-
session = m_sessions.Last.Value;
289+
session = m_sessions.Last!.Value;
291290
m_sessions.RemoveLast();
292291
}
293292
}
@@ -361,7 +360,7 @@ private async Task CreateMinimumPooledSessions(IOBehavior ioBehavior, Cancellati
361360
}
362361
}
363362

364-
public static ConnectionPool GetPool(string connectionString)
363+
public static ConnectionPool? GetPool(string connectionString)
365364
{
366365
// check single-entry MRU cache for this exact connection string; most applications have just one
367366
// connection string and will get a cache hit here
@@ -391,7 +390,7 @@ public static ConnectionPool GetPool(string connectionString)
391390
{
392391
// try to set the pool for the connection string to the canonical pool; if someone else
393392
// beats us to it, just use the existing value
394-
pool = s_pools.GetOrAdd(connectionString, pool);
393+
pool = s_pools.GetOrAdd(connectionString, pool)!;
395394
s_mruCache = new ConnectionStringPool(connectionString, pool);
396395
return pool;
397396
}
@@ -445,17 +444,17 @@ private ConnectionPool(ConnectionSettings cs)
445444
m_sessionSemaphore = new SemaphoreSlim(cs.MaximumPoolSize);
446445
m_sessions = new LinkedList<ServerSession>();
447446
m_leasedSessions = new Dictionary<string, ServerSession>();
448-
if (cs.LoadBalance == MySqlLoadBalance.LeastConnections)
447+
if (cs.ConnectionProtocol == MySqlConnectionProtocol.Sockets && cs.LoadBalance == MySqlLoadBalance.LeastConnections)
449448
{
450449
m_hostSessions = new Dictionary<string, int>();
451-
foreach (var hostName in cs.HostNames)
450+
foreach (var hostName in cs.HostNames!)
452451
m_hostSessions[hostName] = 0;
453452
}
454453

455454
m_loadBalancer = cs.ConnectionProtocol != MySqlConnectionProtocol.Sockets ? null :
456-
cs.HostNames.Count == 1 || cs.LoadBalance == MySqlLoadBalance.FailOver ? FailOverLoadBalancer.Instance :
455+
cs.HostNames!.Count == 1 || cs.LoadBalance == MySqlLoadBalance.FailOver ? FailOverLoadBalancer.Instance :
457456
cs.LoadBalance == MySqlLoadBalance.Random ? RandomLoadBalancer.Instance :
458-
cs.LoadBalance == MySqlLoadBalance.LeastConnections ? new LeastConnectionsLoadBalancer(this) :
457+
cs.LoadBalance == MySqlLoadBalance.LeastConnections ? new LeastConnectionsLoadBalancer(m_hostSessions!) :
459458
(ILoadBalancer) new RoundRobinLoadBalancer();
460459

461460
Id = Interlocked.Increment(ref s_poolId);
@@ -500,27 +499,27 @@ private void AdjustHostConnectionCount(ServerSession session, int delta)
500499

501500
private sealed class LeastConnectionsLoadBalancer : ILoadBalancer
502501
{
503-
public LeastConnectionsLoadBalancer(ConnectionPool pool) => m_pool = pool;
502+
public LeastConnectionsLoadBalancer(Dictionary<string, int> hostSessions) => m_hostSessions = hostSessions;
504503

505504
public IEnumerable<string> LoadBalance(IReadOnlyList<string> hosts)
506505
{
507-
lock (m_pool.m_hostSessions)
508-
return m_pool.m_hostSessions.OrderBy(x => x.Value).Select(x => x.Key).ToList();
506+
lock (m_hostSessions)
507+
return m_hostSessions.OrderBy(x => x.Value).Select(x => x.Key).ToList();
509508
}
510509

511-
readonly ConnectionPool m_pool;
510+
readonly Dictionary<string, int> m_hostSessions;
512511
}
513512

514513
private sealed class ConnectionStringPool
515514
{
516-
public ConnectionStringPool(string connectionString, ConnectionPool pool)
515+
public ConnectionStringPool(string connectionString, ConnectionPool? pool)
517516
{
518517
ConnectionString = connectionString;
519518
Pool = pool;
520519
}
521520

522521
public string ConnectionString { get; }
523-
public ConnectionPool Pool { get; }
522+
public ConnectionPool? Pool { get; }
524523
}
525524

526525
#if !NETSTANDARD1_3
@@ -530,11 +529,11 @@ static ConnectionPool()
530529
AppDomain.CurrentDomain.ProcessExit += OnAppDomainShutDown;
531530
}
532531

533-
static void OnAppDomainShutDown(object sender, EventArgs e) => ClearPoolsAsync(IOBehavior.Synchronous, CancellationToken.None).GetAwaiter().GetResult();
532+
static void OnAppDomainShutDown(object? sender, EventArgs e) => ClearPoolsAsync(IOBehavior.Synchronous, CancellationToken.None).GetAwaiter().GetResult();
534533
#endif
535534

536535
static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(ConnectionPool));
537-
static readonly ConcurrentDictionary<string, ConnectionPool> s_pools = new ConcurrentDictionary<string, ConnectionPool>();
536+
static readonly ConcurrentDictionary<string, ConnectionPool?> s_pools = new ConcurrentDictionary<string, ConnectionPool?>();
538537

539538
static int s_poolId;
540539
static ConnectionStringPool s_mruCache;
@@ -544,12 +543,12 @@ static ConnectionPool()
544543
readonly SemaphoreSlim m_sessionSemaphore;
545544
readonly LinkedList<ServerSession> m_sessions;
546545
readonly Dictionary<string, ServerSession> m_leasedSessions;
547-
readonly ILoadBalancer m_loadBalancer;
548-
readonly Dictionary<string, int> m_hostSessions;
546+
readonly ILoadBalancer? m_loadBalancer;
547+
readonly Dictionary<string, int>? m_hostSessions;
549548
readonly object[] m_logArguments;
550-
Task m_reaperTask;
549+
Task? m_reaperTask;
551550
uint m_lastRecoveryTime;
552551
int m_lastSessionId;
553-
Dictionary<string, CachedProcedure> m_procedureCache;
552+
Dictionary<string, CachedProcedure?>? m_procedureCache;
554553
}
555554
}

src/MySqlConnector/Core/ConnectionSettings.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Collections.Generic;
43
using System.IO;
@@ -16,13 +15,17 @@ public ConnectionSettings(MySqlConnectionStringBuilder csb)
1615

1716
if (csb.ConnectionProtocol == MySqlConnectionProtocol.UnixSocket || (!Utility.IsWindows() && (csb.Server.StartsWith("/", StringComparison.Ordinal) || csb.Server.StartsWith("./", StringComparison.Ordinal))))
1817
{
18+
if (csb.LoadBalance != MySqlLoadBalance.RoundRobin)
19+
throw new NotSupportedException("LoadBalance not supported when ConnectionProtocol=UnixSocket");
1920
if (!File.Exists(csb.Server))
2021
throw new MySqlException("Cannot find Unix Socket at " + csb.Server);
2122
ConnectionProtocol = MySqlConnectionProtocol.UnixSocket;
2223
UnixSocket = Path.GetFullPath(csb.Server);
2324
}
2425
else if (csb.ConnectionProtocol == MySqlConnectionProtocol.NamedPipe)
2526
{
27+
if (csb.LoadBalance != MySqlLoadBalance.RoundRobin)
28+
throw new NotSupportedException("LoadBalance not supported when ConnectionProtocol=NamedPipe");
2629
ConnectionProtocol = MySqlConnectionProtocol.NamedPipe;
2730
HostNames = (csb.Server == "." || string.Equals(csb.Server, "localhost", StringComparison.OrdinalIgnoreCase)) ? s_localhostPipeServer : new[] { csb.Server };
2831
PipeName = csb.PipeName;
@@ -119,11 +122,11 @@ private static MySqlGuidFormat GetEffectiveGuidFormat(MySqlGuidFormat guidFormat
119122
// Base Options
120123
public string ConnectionString { get; }
121124
public MySqlConnectionProtocol ConnectionProtocol { get; }
122-
public IReadOnlyList<string> HostNames { get; }
125+
public IReadOnlyList<string>? HostNames { get; }
123126
public MySqlLoadBalance LoadBalance { get; }
124127
public int Port { get; }
125-
public string PipeName { get; }
126-
public string UnixSocket { get; }
128+
public string? PipeName { get; }
129+
public string? UnixSocket { get; }
127130
public string UserID { get; }
128131
public string Password { get; }
129132
public string Database { get; }
@@ -172,7 +175,7 @@ private static MySqlGuidFormat GetEffectiveGuidFormat(MySqlGuidFormat guidFormat
172175
public bool UseCompression { get; }
173176
public bool UseXaTransactions { get; }
174177

175-
public byte[] ConnectionAttributes { get; set; }
178+
public byte[]? ConnectionAttributes { get; set; }
176179

177180
// Helper Functions
178181
int? m_connectionTimeoutMilliseconds;

src/MySqlConnector/Core/DbTypeMapping.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
#nullable disable
21
using System;
32
using System.Data;
43

54
namespace MySqlConnector.Core
65
{
76
internal sealed class DbTypeMapping
87
{
9-
public DbTypeMapping(Type clrType, DbType[] dbTypes, Func<object, object> convert = null)
8+
public DbTypeMapping(Type clrType, DbType[] dbTypes, Func<object, object>? convert = null)
109
{
1110
ClrType = clrType;
1211
DbTypes = dbTypes;
@@ -20,9 +19,9 @@ public object DoConversion(object obj)
2019
{
2120
if (obj.GetType() == ClrType)
2221
return obj;
23-
return m_convert is null ? Convert.ChangeType(obj, ClrType) : m_convert(obj);
22+
return m_convert is null ? Convert.ChangeType(obj, ClrType)! : m_convert(obj);
2423
}
2524

26-
readonly Func<object, object> m_convert;
25+
readonly Func<object, object>? m_convert;
2726
}
2827
}

src/MySqlConnector/Core/EnlistedTransactionBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
#if !NETSTANDARD1_3
32
using System;
43
using System.Transactions;
@@ -19,7 +18,7 @@ internal abstract class EnlistedTransactionBase : IEnlistmentNotification
1918
public void Start()
2019
{
2120
OnStart();
22-
Transaction.EnlistVolatile(this, EnlistmentOptions.None);
21+
Transaction!.EnlistVolatile(this, EnlistmentOptions.None);
2322
}
2423

2524
void IEnlistmentNotification.Prepare(PreparingEnlistment enlistment)
@@ -33,15 +32,13 @@ void IEnlistmentNotification.Commit(Enlistment enlistment)
3332
OnCommit(enlistment);
3433
enlistment.Done();
3534
Connection.UnenlistTransaction();
36-
Transaction = null;
3735
}
3836

3937
void IEnlistmentNotification.Rollback(Enlistment enlistment)
4038
{
4139
OnRollback(enlistment);
4240
enlistment.Done();
4341
Connection.UnenlistTransaction();
44-
Transaction = null;
4542
}
4643

4744
public void InDoubt(Enlistment enlistment) => throw new NotImplementedException();

src/MySqlConnector/Core/ICancellableCommand.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Threading;
43
using MySql.Data.MySqlClient;
@@ -14,8 +13,8 @@ internal interface ICancellableCommand
1413
int CommandId { get; }
1514
int CommandTimeout { get; }
1615
int CancelAttemptCount { get; set; }
17-
MySqlConnection Connection { get; }
18-
IDisposable RegisterCancel(CancellationToken cancellationToken);
16+
MySqlConnection? Connection { get; }
17+
IDisposable? RegisterCancel(CancellationToken cancellationToken);
1918
}
2019

2120
internal static class ICancellableCommandExtensions

src/MySqlConnector/Core/ILoadBalancer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Collections.Generic;
43

0 commit comments

Comments
 (0)