Skip to content

Commit fea6c56

Browse files
authored
Merge pull request #219 from bgrainger/csharp7
Use C# 7 language features.
2 parents 750188e + 6d30b76 commit fea6c56

26 files changed

+211
-394
lines changed

src/MySqlConnector/ByteArrayReader.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@ internal sealed class ByteArrayReader
66
{
77
public ByteArrayReader(byte[] buffer, int offset, int length)
88
{
9-
if (buffer == null)
10-
throw new ArgumentNullException(nameof(buffer));
11-
if (offset < 0)
12-
throw new ArgumentOutOfRangeException(nameof(offset));
13-
if (offset + length > buffer.Length)
14-
throw new ArgumentOutOfRangeException(nameof(length));
15-
16-
m_buffer = buffer;
17-
m_maxOffset = offset + length;
18-
m_offset = offset;
9+
m_buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
10+
m_offset = offset >= 0 ? offset : throw new ArgumentOutOfRangeException(nameof(offset));
11+
m_maxOffset = offset + length <= m_buffer.Length ? offset + length : throw new ArgumentOutOfRangeException(nameof(length));
1912
}
2013

2114
public ByteArrayReader(ArraySegment<byte> arraySegment)
@@ -25,13 +18,8 @@ public ByteArrayReader(ArraySegment<byte> arraySegment)
2518

2619
public int Offset
2720
{
28-
get { return m_offset; }
29-
set
30-
{
31-
if (value < 0 || value > m_maxOffset)
32-
throw new ArgumentOutOfRangeException(nameof(value), "value must be between 0 and {0}".FormatInvariant(m_maxOffset));
33-
m_offset = value;
34-
}
21+
get => m_offset;
22+
set => m_offset = value >= 0 && value <= m_maxOffset ? value : throw new ArgumentOutOfRangeException(nameof(value), "value must be between 0 and {0}".FormatInvariant(m_maxOffset));
3523
}
3624

3725
public byte ReadByte()

src/MySqlConnector/MySqlClient/Caches/CachedProcedure.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,12 @@ internal MySqlParameterCollection AlignParamsWithDb(MySqlParameterCollection par
6767
MySqlParameter alignParam;
6868
if (cachedParam.Direction == ParameterDirection.ReturnValue)
6969
{
70-
if (returnParam == null)
71-
throw new InvalidOperationException($"Attempt to call stored function {FullyQualified} without specifying a return parameter");
72-
alignParam = returnParam;
70+
alignParam = returnParam ?? throw new InvalidOperationException($"Attempt to call stored function {FullyQualified} without specifying a return parameter");
7371
}
7472
else
7573
{
7674
var index = parameterCollection.NormalizedIndexOf(cachedParam.Name);
77-
if (index == -1)
78-
throw new ArgumentException($"Parameter '{cachedParam.Name}' not found in the collection.");
79-
alignParam = parameterCollection[index];
75+
alignParam = index >= 0 ? parameterCollection[index] : throw new ArgumentException($"Parameter '{cachedParam.Name}' not found in the collection.");
8076
}
8177

8278
if (alignParam.Direction == default(ParameterDirection))

src/MySqlConnector/MySqlClient/CommandExecutors/TextCommandExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public virtual async Task<DbDataReader> ExecuteReaderAsync(string commandText, M
6363
await m_command.Connection.Session.SendAsync(payload, ioBehavior, cancellationToken).ConfigureAwait(false);
6464
return await MySqlDataReader.CreateAsync(m_command, behavior, ioBehavior, cancellationToken).ConfigureAwait(false);
6565
}
66-
catch (Exception ex) when (payload.ArraySegment.Count > 4194304 && (ex is SocketException || ex is IOException || ex is MySqlProtocolException))
66+
catch (Exception ex) when (payload.ArraySegment.Count > 4_194_304 && (ex is SocketException || ex is IOException || ex is MySqlProtocolException))
6767
{
6868
// the default MySQL Server value for max_allowed_packet (in MySQL 5.7) is 4MiB: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_allowed_packet
6969
// use "decimal megabytes" (to round up) when creating the exception message
70-
int megabytes = payload.ArraySegment.Count / 1000000;
70+
int megabytes = payload.ArraySegment.Count / 1_000_000;
7171
throw new MySqlException("Error submitting {0}MB packet; ensure 'max_allowed_packet' is greater than {0}MB.".FormatInvariant(megabytes), ex);
7272
}
7373
}

src/MySqlConnector/MySqlClient/ConnectionPool.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ public async Task<MySqlSession> GetSessionAsync(IOBehavior ioBehavior, Cancellat
2222

2323
try
2424
{
25-
MySqlSession session;
2625
// check for a pooled session
27-
if (m_sessions.TryDequeue(out session))
26+
if (m_sessions.TryDequeue(out var session))
2827
{
2928
if (session.PoolGeneration != m_generation || !await session.TryPingAsync(ioBehavior, cancellationToken).ConfigureAwait(false))
3029
{
@@ -91,8 +90,7 @@ public async Task ClearAsync(IOBehavior ioBehavior, CancellationToken cancellati
9190

9291
try
9392
{
94-
MySqlSession session;
95-
if (m_sessions.TryDequeue(out session))
93+
if (m_sessions.TryDequeue(out var session))
9694
{
9795
if (session.PoolGeneration != m_generation)
9896
{
@@ -122,8 +120,7 @@ public static ConnectionPool GetPool(ConnectionSettings cs)
122120

123121
var key = cs.ConnectionString;
124122

125-
ConnectionPool pool;
126-
if (!s_pools.TryGetValue(key, out pool))
123+
if (!s_pools.TryGetValue(key, out var pool))
127124
{
128125
pool = s_pools.GetOrAdd(cs.ConnectionString, newKey => new ConnectionPool(cs));
129126
}

src/MySqlConnector/MySqlClient/MySqlCommand.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,7 @@ private void VerifyValid()
181181
throw new MySqlException("There is already an open DataReader associated with this Connection which must be closed first.");
182182
}
183183

184-
internal void ReaderClosed()
185-
{
186-
var executor = m_commandExecutor as StoredProcedureCommandExecutor;
187-
executor?.SetParams();
188-
}
184+
internal void ReaderClosed() => (m_commandExecutor as StoredProcedureCommandExecutor)?.SetParams();
189185

190186
MySqlParameterCollection m_parameterCollection;
191187
CommandType m_commandType;

src/MySqlConnector/MySqlClient/MySqlConnection.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ namespace MySql.Data.MySqlClient
1414
public sealed class MySqlConnection : DbConnection
1515
{
1616
public MySqlConnection()
17+
: this("")
1718
{
18-
ConnectionString = "";
1919
}
2020

21-
public MySqlConnection(string connectionString)
22-
: this()
23-
{
24-
ConnectionString = connectionString;
25-
}
21+
public MySqlConnection(string connectionString) => ConnectionString = connectionString;
2622

2723
public new MySqlTransaction BeginTransaction() => (MySqlTransaction) base.BeginTransaction();
2824

@@ -143,10 +139,7 @@ private async Task OpenAsync(IOBehavior ioBehavior, CancellationToken cancellati
143139

144140
public override string ConnectionString
145141
{
146-
get
147-
{
148-
return m_connectionStringBuilder.GetConnectionString(!m_hasBeenOpened || m_connectionSettings.PersistSecurityInfo);
149-
}
142+
get => m_connectionStringBuilder.GetConnectionString(!m_hasBeenOpened || m_connectionSettings.PersistSecurityInfo);
150143
set
151144
{
152145
if (m_hasBeenOpened)
@@ -228,8 +221,7 @@ internal async Task<CachedProcedure> GetCachedProcedure(IOBehavior ioBehavior, s
228221
m_cachedProcedures = new Dictionary<string, CachedProcedure>();
229222

230223
var normalized = NormalizedSchema.MustNormalize(name, Database);
231-
CachedProcedure cachedProcedure;
232-
if (!m_cachedProcedures.TryGetValue(normalized.FullyQualified, out cachedProcedure))
224+
if (!m_cachedProcedures.TryGetValue(normalized.FullyQualified, out var cachedProcedure))
233225
{
234226
cachedProcedure = await CachedProcedure.FillAsync(ioBehavior, this, normalized.Schema, normalized.Component, cancellationToken).ConfigureAwait(false);
235227
m_cachedProcedures[normalized.FullyQualified] = cachedProcedure;

0 commit comments

Comments
 (0)