Skip to content

Commit 8d0d348

Browse files
committed
Use type patterns.
1 parent 27809b8 commit 8d0d348

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

src/MySqlConnector/MySqlClient/MySqlConnectionStringBuilder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,19 +397,19 @@ public override object GetObject(MySqlConnectionStringBuilder builder)
397397

398398
private static T ChangeType(object objectValue)
399399
{
400-
if (typeof(T) == typeof(bool) && objectValue is string)
400+
if (typeof(T) == typeof(bool) && objectValue is string booleanString)
401401
{
402-
if (string.Equals((string) objectValue, "yes", StringComparison.OrdinalIgnoreCase))
402+
if (string.Equals(booleanString, "yes", StringComparison.OrdinalIgnoreCase))
403403
return (T) (object) true;
404-
if (string.Equals((string) objectValue, "no", StringComparison.OrdinalIgnoreCase))
404+
if (string.Equals(booleanString, "no", StringComparison.OrdinalIgnoreCase))
405405
return (T) (object) false;
406406
}
407407

408-
if (typeof(T) == typeof(MySqlSslMode) && objectValue is string)
408+
if (typeof(T) == typeof(MySqlSslMode) && objectValue is string sslModeString)
409409
{
410410
foreach (var val in Enum.GetValues(typeof(T)))
411411
{
412-
if (string.Equals((string) objectValue, val.ToString(), StringComparison.OrdinalIgnoreCase))
412+
if (string.Equals(sslModeString, val.ToString(), StringComparison.OrdinalIgnoreCase))
413413
return (T) val;
414414
}
415415
throw new InvalidOperationException("Value '{0}' not supported for option '{1}'.".FormatInvariant(objectValue, typeof(T).Name));

src/MySqlConnector/MySqlClient/MySqlParameter.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,30 @@ internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions opti
8383
{
8484
writer.WriteUtf8("NULL");
8585
}
86-
else if (Value is string)
86+
else if (Value is string stringValue)
8787
{
8888
writer.Write((byte) '\'');
89-
writer.WriteUtf8(((string) Value).Replace("\\", "\\\\").Replace("'", "\\'"));
89+
writer.WriteUtf8(stringValue.Replace("\\", "\\\\").Replace("'", "\\'"));
9090
writer.Write((byte) '\'');
9191
}
9292
else if (Value is byte || Value is sbyte || Value is short || Value is int || Value is long || Value is ushort || Value is uint || Value is ulong || Value is decimal)
9393
{
9494
writer.WriteUtf8("{0}".FormatInvariant(Value));
9595
}
96-
else if (Value is byte[])
96+
else if (Value is byte[] byteArrayValue)
9797
{
9898
writer.WriteUtf8("_binary'");
99-
foreach (var by in (byte[]) Value)
99+
foreach (var by in byteArrayValue)
100100
{
101101
if (by == 0x27 || by == 0x5C)
102102
writer.Write((byte) 0x5C);
103103
writer.Write(by);
104104
}
105105
writer.Write((byte) '\'');
106106
}
107-
else if (Value is bool)
107+
else if (Value is bool boolValue)
108108
{
109-
writer.WriteUtf8(((bool) Value) ? "true" : "false");
109+
writer.WriteUtf8(boolValue ? "true" : "false");
110110
}
111111
else if (Value is float || Value is double)
112112
{
@@ -116,33 +116,32 @@ internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions opti
116116
{
117117
writer.WriteUtf8("timestamp '{0:yyyy'-'MM'-'dd' 'HH':'mm':'ss'.'ffffff}'".FormatInvariant(Value));
118118
}
119-
else if (Value is DateTimeOffset)
119+
else if (Value is DateTimeOffset dateTimeOffsetValue)
120120
{
121121
// store as UTC as it will be read as such when deserialized from a timespan column
122-
writer.WriteUtf8("timestamp '{0:yyyy'-'MM'-'dd' 'HH':'mm':'ss'.'ffffff}'".FormatInvariant(((DateTimeOffset)Value).UtcDateTime));
122+
writer.WriteUtf8("timestamp '{0:yyyy'-'MM'-'dd' 'HH':'mm':'ss'.'ffffff}'".FormatInvariant(dateTimeOffsetValue.UtcDateTime));
123123
}
124-
else if (Value is TimeSpan)
124+
else if (Value is TimeSpan ts)
125125
{
126126
writer.WriteUtf8("time '");
127-
var ts = (TimeSpan) Value;
128127
if (ts.Ticks < 0)
129128
{
130129
writer.Write((byte) '-');
131130
ts = TimeSpan.FromTicks(-ts.Ticks);
132131
}
133132
writer.WriteUtf8("{0}:{1:mm':'ss'.'ffffff}'".FormatInvariant(ts.Days * 24 + ts.Hours, ts));
134133
}
135-
else if (Value is Guid)
134+
else if (Value is Guid guidValue)
136135
{
137136
if ((options & StatementPreparerOptions.OldGuids) != 0)
138137
{
139138
writer.WriteUtf8("_binary'");
140-
writer.Write(((Guid) Value).ToByteArray());
139+
writer.Write(guidValue.ToByteArray());
141140
writer.Write((byte) '\'');
142141
}
143142
else
144143
{
145-
writer.WriteUtf8("'{0:D}'".FormatInvariant(Value));
144+
writer.WriteUtf8("'{0:D}'".FormatInvariant(guidValue));
146145
}
147146
}
148147
else if (DbType == DbType.Int16)

src/MySqlConnector/MySqlClient/Results/Row.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,13 @@ public char GetChar(int ordinal)
133133
public Guid GetGuid(int ordinal)
134134
{
135135
var value = GetValue(ordinal);
136-
if (value is Guid)
137-
return (Guid) value;
136+
if (value is Guid guid)
137+
return guid;
138138

139-
if (Guid.TryParse(value as string, out var guid))
139+
if (value is string stringValue && Guid.TryParse(stringValue, out guid))
140140
return guid;
141141

142-
byte[] bytes = value as byte[];
143-
if (bytes != null && bytes.Length == 16)
142+
if (value is byte[] bytes && bytes.Length == 16)
144143
return new Guid(bytes);
145144

146145
throw new MySqlException("The value could not be converted to a GUID: {0}".FormatInvariant(value));
@@ -244,9 +243,7 @@ public decimal GetDecimal(int ordinal)
244243
public double GetDouble(int ordinal)
245244
{
246245
object value = GetValue(ordinal);
247-
if (value is float)
248-
return (float) value;
249-
return (double) value;
246+
return value is float floatValue ? floatValue : (double) value;
250247
}
251248

252249
public float GetFloat(int ordinal)

0 commit comments

Comments
 (0)