Skip to content

Commit 47b91a1

Browse files
committed
Use C# 8 switch expressions.
1 parent 5f25ebf commit 47b91a1

File tree

8 files changed

+76
-154
lines changed

8 files changed

+76
-154
lines changed

src/MySqlConnector.Logging.Microsoft.Extensions.Logging/MicrosoftExtensionsLoggingLoggerProvider.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,16 @@ public void Log(MySqlConnectorLogLevel level, string message, object[] args = nu
2424
m_logger.Log(GetLevel(level), 0, (message, args), exception, s_messageFormatter);
2525
}
2626

27-
private static LogLevel GetLevel(MySqlConnectorLogLevel level)
27+
private static LogLevel GetLevel(MySqlConnectorLogLevel level) => level switch
2828
{
29-
switch (level)
30-
{
31-
case MySqlConnectorLogLevel.Trace:
32-
return LogLevel.Trace;
33-
case MySqlConnectorLogLevel.Debug:
34-
return LogLevel.Debug;
35-
case MySqlConnectorLogLevel.Info:
36-
return LogLevel.Information;
37-
case MySqlConnectorLogLevel.Warn:
38-
return LogLevel.Warning;
39-
case MySqlConnectorLogLevel.Error:
40-
return LogLevel.Error;
41-
case MySqlConnectorLogLevel.Fatal:
42-
return LogLevel.Critical;
43-
default:
44-
throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'.");
45-
}
46-
}
29+
MySqlConnectorLogLevel.Trace => LogLevel.Trace,
30+
MySqlConnectorLogLevel.Debug => LogLevel.Debug,
31+
MySqlConnectorLogLevel.Info => LogLevel.Information,
32+
MySqlConnectorLogLevel.Warn => LogLevel.Warning,
33+
MySqlConnectorLogLevel.Error => LogLevel.Error,
34+
MySqlConnectorLogLevel.Fatal => LogLevel.Critical,
35+
_ => throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'.")
36+
};
4737

4838
static readonly Func<string, Exception, string> s_getMessage = (s, e) => s;
4939
static readonly Func<(string Message, object[] Args), Exception, string> s_messageFormatter = (s, e) => string.Format(CultureInfo.InvariantCulture, s.Message, s.Args);

src/MySqlConnector.Logging.NLog/NLogLoggerProvider.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,18 @@ public void Log(MySqlConnectorLogLevel level, string message, object[] args = nu
2525
}
2626
}
2727

28-
private static LogLevel GetLevel(MySqlConnectorLogLevel level)
28+
private static LogLevel GetLevel(MySqlConnectorLogLevel level) => level switch
2929
{
30-
switch (level)
31-
{
32-
case MySqlConnectorLogLevel.Trace:
33-
return LogLevel.Trace;
34-
case MySqlConnectorLogLevel.Debug:
35-
return LogLevel.Debug;
36-
case MySqlConnectorLogLevel.Info:
37-
return LogLevel.Info;
38-
case MySqlConnectorLogLevel.Warn:
39-
return LogLevel.Warn;
40-
case MySqlConnectorLogLevel.Error:
41-
return LogLevel.Error;
42-
case MySqlConnectorLogLevel.Fatal:
43-
return LogLevel.Fatal;
44-
default:
45-
throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'.");
46-
}
47-
}
30+
MySqlConnectorLogLevel.Trace => LogLevel.Trace,
31+
MySqlConnectorLogLevel.Debug => LogLevel.Debug,
32+
MySqlConnectorLogLevel.Info => LogLevel.Info,
33+
MySqlConnectorLogLevel.Warn => LogLevel.Warn,
34+
MySqlConnectorLogLevel.Error => LogLevel.Error,
35+
MySqlConnectorLogLevel.Fatal => LogLevel.Fatal,
36+
_ => throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'.")
37+
};
4838

49-
readonly Logger m_logger;
39+
readonly Logger m_logger;
5040
}
5141
}
5242
}

src/MySqlConnector.Logging.Serilog/SerilogLoggerProvider.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,16 @@ public void Log(MySqlConnectorLogLevel level, string message, object[] args = nu
3636
}
3737
}
3838

39-
private static LogEventLevel GetLevel(MySqlConnectorLogLevel level)
40-
{
41-
switch (level)
42-
{
43-
case MySqlConnectorLogLevel.Trace:
44-
return LogEventLevel.Verbose;
45-
case MySqlConnectorLogLevel.Debug:
46-
return LogEventLevel.Debug;
47-
case MySqlConnectorLogLevel.Info:
48-
return LogEventLevel.Information;
49-
case MySqlConnectorLogLevel.Warn:
50-
return LogEventLevel.Warning;
51-
case MySqlConnectorLogLevel.Error:
52-
return LogEventLevel.Error;
53-
case MySqlConnectorLogLevel.Fatal:
54-
return LogEventLevel.Fatal;
55-
default:
56-
throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'.");
57-
}
58-
}
39+
private static LogEventLevel GetLevel(MySqlConnectorLogLevel level) => level switch
40+
{
41+
MySqlConnectorLogLevel.Trace => LogEventLevel.Verbose,
42+
MySqlConnectorLogLevel.Debug => LogEventLevel.Debug,
43+
MySqlConnectorLogLevel.Info => LogEventLevel.Information,
44+
MySqlConnectorLogLevel.Warn => LogEventLevel.Warning,
45+
MySqlConnectorLogLevel.Error => LogEventLevel.Error,
46+
MySqlConnectorLogLevel.Fatal => LogEventLevel.Fatal,
47+
_ => throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'."),
48+
};
5949

6050
static readonly Regex tokenReplacer = new Regex(@"((\w+)?\s?(?:=|:)?\s?'?)\{(?:\d+)(\:\w+)?\}('?)", RegexOptions.Compiled);
6151

src/MySqlConnector.Logging.log4net/Log4netLoggerProvider.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,16 @@ public void Log(MySqlConnectorLogLevel level, string message, object[] args = nu
2727
m_logger.Log(s_loggerType, GetLevel(level), string.Format(CultureInfo.InvariantCulture, message, args), exception);
2828
}
2929

30-
private static Level GetLevel(MySqlConnectorLogLevel level)
30+
private static Level GetLevel(MySqlConnectorLogLevel level) => level switch
3131
{
32-
switch (level)
33-
{
34-
case MySqlConnectorLogLevel.Trace:
35-
return Level.Trace;
36-
case MySqlConnectorLogLevel.Debug:
37-
return Level.Debug;
38-
case MySqlConnectorLogLevel.Info:
39-
return Level.Info;
40-
case MySqlConnectorLogLevel.Warn:
41-
return Level.Warn;
42-
case MySqlConnectorLogLevel.Error:
43-
return Level.Error;
44-
case MySqlConnectorLogLevel.Fatal:
45-
return Level.Fatal;
46-
default:
47-
throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'.");
48-
}
49-
}
32+
MySqlConnectorLogLevel.Trace => Level.Trace,
33+
MySqlConnectorLogLevel.Debug => Level.Debug,
34+
MySqlConnectorLogLevel.Info => Level.Info,
35+
MySqlConnectorLogLevel.Warn => Level.Warn,
36+
MySqlConnectorLogLevel.Error => Level.Error,
37+
MySqlConnectorLogLevel.Fatal => Level.Fatal,
38+
_ => throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'.")
39+
};
5040

5141
readonly ILogger m_logger;
5242
}

src/MySqlConnector/Core/IMySqlCommand.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,15 @@ public static StatementPreparerOptions CreateStatementPreparerOptions(this IMySq
3636
if (command.CommandType == CommandType.StoredProcedure)
3737
statementPreparerOptions |= StatementPreparerOptions.AllowOutputParameters;
3838

39-
switch (connection.GuidFormat)
39+
statementPreparerOptions |= connection.GuidFormat switch
4040
{
41-
case MySqlGuidFormat.Char36:
42-
statementPreparerOptions |= StatementPreparerOptions.GuidFormatChar36;
43-
break;
44-
case MySqlGuidFormat.Char32:
45-
statementPreparerOptions |= StatementPreparerOptions.GuidFormatChar32;
46-
break;
47-
case MySqlGuidFormat.Binary16:
48-
statementPreparerOptions |= StatementPreparerOptions.GuidFormatBinary16;
49-
break;
50-
case MySqlGuidFormat.TimeSwapBinary16:
51-
statementPreparerOptions |= StatementPreparerOptions.GuidFormatTimeSwapBinary16;
52-
break;
53-
case MySqlGuidFormat.LittleEndianBinary16:
54-
statementPreparerOptions |= StatementPreparerOptions.GuidFormatLittleEndianBinary16;
55-
break;
56-
}
41+
MySqlGuidFormat.Char36 => StatementPreparerOptions.GuidFormatChar36,
42+
MySqlGuidFormat.Char32 => StatementPreparerOptions.GuidFormatChar32,
43+
MySqlGuidFormat.Binary16 => StatementPreparerOptions.GuidFormatBinary16,
44+
MySqlGuidFormat.TimeSwapBinary16 => StatementPreparerOptions.GuidFormatTimeSwapBinary16,
45+
MySqlGuidFormat.LittleEndianBinary16 => StatementPreparerOptions.GuidFormatLittleEndianBinary16,
46+
_ => StatementPreparerOptions.None
47+
};
5748

5849
return statementPreparerOptions;
5950
}

src/MySqlConnector/Core/StandardEnlistedTransaction.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,19 @@ public StandardEnlistedTransaction(Transaction transaction, MySqlConnection conn
1515

1616
protected override void OnStart()
1717
{
18-
string isolationLevel;
19-
switch (Transaction.IsolationLevel)
18+
var isolationLevel = Transaction.IsolationLevel switch
2019
{
21-
case IsolationLevel.Serializable:
22-
isolationLevel = "serializable";
23-
break;
24-
case IsolationLevel.ReadCommitted:
25-
isolationLevel = "read committed";
26-
break;
27-
case IsolationLevel.ReadUncommitted:
28-
isolationLevel = "read uncommitted";
29-
break;
30-
case IsolationLevel.Snapshot:
31-
case IsolationLevel.Chaos:
32-
throw new NotSupportedException("IsolationLevel.{0} is not supported.".FormatInvariant(Transaction.IsolationLevel));
33-
// "In terms of the SQL:1992 transaction isolation levels, the default InnoDB level is REPEATABLE READ." - http://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-model.html
34-
case IsolationLevel.Unspecified:
35-
case IsolationLevel.RepeatableRead:
36-
default:
37-
isolationLevel = "repeatable read";
38-
break;
39-
}
20+
IsolationLevel.Serializable => "serializable",
21+
IsolationLevel.ReadCommitted => "read committed",
22+
IsolationLevel.ReadUncommitted => "read uncommitted",
23+
IsolationLevel.RepeatableRead => "repeatable read",
24+
IsolationLevel.Snapshot => throw new NotSupportedException("IsolationLevel.{0} is not supported.".FormatInvariant(Transaction.IsolationLevel)),
25+
IsolationLevel.Chaos => throw new NotSupportedException("IsolationLevel.{0} is not supported.".FormatInvariant(Transaction.IsolationLevel)),
26+
27+
// "In terms of the SQL:1992 transaction isolation levels, the default InnoDB level is REPEATABLE READ." - http://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-model.html
28+
IsolationLevel.Unspecified => "repeatable read",
29+
_ => "repeatable read",
30+
};
4031

4132
using (var cmd = new MySqlCommand("set transaction isolation level " + isolationLevel + "; start transaction;", Connection))
4233
cmd.ExecuteNonQuery();

src/MySqlConnector/MySql.Data.MySqlClient/MySqlConnection.cs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,18 @@ private async ValueTask<MySqlTransaction> BeginDbTransactionAsync(IsolationLevel
5353
throw new InvalidOperationException("Cannot begin a transaction when already enlisted in a transaction.");
5454
#endif
5555

56-
string isolationLevelValue;
57-
switch (isolationLevel)
56+
string isolationLevelValue = isolationLevel switch
5857
{
59-
case IsolationLevel.ReadUncommitted:
60-
isolationLevelValue = "read uncommitted";
61-
break;
58+
IsolationLevel.ReadUncommitted => "read uncommitted",
59+
IsolationLevel.ReadCommitted => "read committed",
60+
IsolationLevel.RepeatableRead => "repeatable read",
61+
IsolationLevel.Serializable => "serializable",
6262

63-
case IsolationLevel.ReadCommitted:
64-
isolationLevelValue = "read committed";
65-
break;
63+
// "In terms of the SQL:1992 transaction isolation levels, the default InnoDB level is REPEATABLE READ." - http://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-model.html
64+
IsolationLevel.Unspecified => "repeatable read",
6665

67-
case IsolationLevel.Unspecified:
68-
// "In terms of the SQL:1992 transaction isolation levels, the default InnoDB level is REPEATABLE READ." - http://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-model.html
69-
case IsolationLevel.RepeatableRead:
70-
isolationLevelValue = "repeatable read";
71-
break;
72-
73-
case IsolationLevel.Serializable:
74-
isolationLevelValue = "serializable";
75-
break;
76-
77-
case IsolationLevel.Chaos:
78-
case IsolationLevel.Snapshot:
79-
default:
80-
throw new NotSupportedException("IsolationLevel.{0} is not supported.".FormatInvariant(isolationLevel));
81-
}
66+
_ => throw new NotSupportedException("IsolationLevel.{0} is not supported.".FormatInvariant(isolationLevel))
67+
};
8268

8369
using (var cmd = new MySqlCommand("set transaction isolation level " + isolationLevelValue + "; start transaction;", this))
8470
await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

src/MySqlConnector/Protocol/Serialization/ByteArrayReader.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,16 @@ public ReadOnlySpan<byte> ReadByteString(int length)
121121

122122
public ulong ReadLengthEncodedInteger()
123123
{
124-
byte encodedLength = m_buffer[m_offset++];
125-
switch (encodedLength)
124+
var encodedLength = m_buffer[m_offset++];
125+
return encodedLength switch
126126
{
127-
case 0xFB:
128-
throw new FormatException("Length-encoded integer cannot have 0xFB prefix byte.");
129-
case 0xFC:
130-
return ReadFixedLengthUInt32(2);
131-
case 0xFD:
132-
return ReadFixedLengthUInt32(3);
133-
case 0xFE:
134-
return ReadFixedLengthUInt64(8);
135-
case 0xFF:
136-
throw new FormatException("Length-encoded integer cannot have 0xFF prefix byte.");
137-
default:
138-
return encodedLength;
139-
}
127+
0xFB => throw new FormatException("Length-encoded integer cannot have 0xFB prefix byte."),
128+
0xFC => ReadFixedLengthUInt32(2),
129+
0xFD => ReadFixedLengthUInt32(3),
130+
0xFE => ReadFixedLengthUInt64(8),
131+
0xFF => throw new FormatException("Length-encoded integer cannot have 0xFF prefix byte."),
132+
_ => encodedLength
133+
};
140134
}
141135

142136
public int ReadLengthEncodedIntegerOrNull()

0 commit comments

Comments
 (0)