Skip to content

Commit eedb08f

Browse files
committed
Use pattern matching.
1 parent 2a324d4 commit eedb08f

File tree

1 file changed

+130
-201
lines changed

1 file changed

+130
-201
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 130 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -41,82 +41,58 @@ public object GetValue(int ordinal)
4141
public bool GetBoolean(int ordinal)
4242
{
4343
var value = GetValue(ordinal);
44-
if (value is bool)
45-
return (bool) value;
46-
47-
if (value is sbyte)
48-
return (sbyte) value != 0;
49-
if (value is byte)
50-
return (byte) value != 0;
51-
if (value is short)
52-
return (short) value != 0;
53-
if (value is ushort)
54-
return (ushort) value != 0;
55-
if (value is int)
56-
return (int) value != 0;
57-
if (value is uint)
58-
return (uint) value != 0;
59-
if (value is long)
60-
return (long) value != 0;
61-
if (value is ulong)
62-
return (ulong) value != 0;
63-
if (value is decimal)
64-
return (decimal) value != 0;
65-
return (bool) value;
44+
return value switch
45+
{
46+
bool boolValue => boolValue,
47+
sbyte sbyteValue => sbyteValue != 0,
48+
byte byteValue => byteValue != 0,
49+
short shortValue => shortValue != 0,
50+
ushort ushortValue => ushortValue != 0,
51+
int intValue => intValue != 0,
52+
uint uintValue => uintValue != 0,
53+
long longValue => longValue != 0,
54+
ulong ulongValue => ulongValue != 0,
55+
decimal decimalValue => decimalValue != 0,
56+
_ => (bool) value,
57+
};
6658
}
6759

6860
public sbyte GetSByte(int ordinal)
6961
{
7062
var value = GetValue(ordinal);
71-
if (value is sbyte sbyteValue)
72-
return sbyteValue;
73-
74-
if (value is byte byteValue)
75-
return checked((sbyte) byteValue);
76-
if (value is short shortValue)
77-
return checked((sbyte) shortValue);
78-
if (value is ushort ushortValue)
79-
return checked((sbyte) ushortValue);
80-
if (value is int intValue)
81-
return checked((sbyte) intValue);
82-
if (value is uint uintValue)
83-
return checked((sbyte) uintValue);
84-
if (value is long longValue)
85-
return checked((sbyte) longValue);
86-
if (value is ulong ulongValue)
87-
return checked((sbyte) ulongValue);
88-
if (value is decimal decimalValue)
89-
return (sbyte) decimalValue;
90-
if (value is bool boolValue)
91-
return boolValue ? (sbyte) 1 : (sbyte) 0;
92-
return (sbyte) value;
63+
return value switch
64+
{
65+
sbyte sbyteValue => sbyteValue,
66+
byte byteValue => checked((sbyte) byteValue),
67+
short shortValue => checked((sbyte) shortValue),
68+
ushort ushortValue => checked((sbyte) ushortValue),
69+
int intValue => checked((sbyte) intValue),
70+
uint uintValue => checked((sbyte) uintValue),
71+
long longValue => checked((sbyte) longValue),
72+
ulong ulongValue => checked((sbyte) ulongValue),
73+
decimal decimalValue => (sbyte) decimalValue,
74+
bool boolValue => boolValue ? (sbyte) 1 : (sbyte) 0,
75+
_ => (sbyte) value,
76+
};
9377
}
9478

9579
public byte GetByte(int ordinal)
9680
{
9781
var value = GetValue(ordinal);
98-
if (value is byte byteValue)
99-
return byteValue;
100-
101-
if (value is sbyte sbyteValue)
102-
return checked((byte) sbyteValue);
103-
if (value is short shortValue)
104-
return checked((byte) shortValue);
105-
if (value is ushort ushortValue)
106-
return checked((byte) ushortValue);
107-
if (value is int intValue)
108-
return checked((byte) intValue);
109-
if (value is uint uintValue)
110-
return checked((byte) uintValue);
111-
if (value is long longValue)
112-
return checked((byte) longValue);
113-
if (value is ulong ulongValue)
114-
return checked((byte) ulongValue);
115-
if (value is decimal decimalValue)
116-
return (byte) decimalValue;
117-
if (value is bool boolValue)
118-
return boolValue ? (byte) 1 : (byte) 0;
119-
return (byte) value;
82+
return value switch
83+
{
84+
byte byteValue => byteValue,
85+
sbyte sbyteValue => checked((byte) sbyteValue),
86+
short shortValue => checked((byte) shortValue),
87+
ushort ushortValue => checked((byte) ushortValue),
88+
int intValue => checked((byte) intValue),
89+
uint uintValue => checked((byte) uintValue),
90+
long longValue => checked((byte) longValue),
91+
ulong ulongValue => checked((byte) ulongValue),
92+
decimal decimalValue => (byte) decimalValue,
93+
bool boolValue => boolValue ? (byte) 1 : (byte) 0,
94+
_ => (byte) value,
95+
};
12096
}
12197

12298
public long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)
@@ -178,28 +154,20 @@ public Guid GetGuid(int ordinal)
178154
public short GetInt16(int ordinal)
179155
{
180156
var value = GetValue(ordinal);
181-
if (value is short)
182-
return (short) value;
183-
184-
if (value is sbyte)
185-
return (sbyte) value;
186-
if (value is byte)
187-
return (byte) value;
188-
if (value is ushort)
189-
return checked((short) (ushort) value);
190-
if (value is int)
191-
return checked((short) (int) value);
192-
if (value is uint)
193-
return checked((short) (uint) value);
194-
if (value is long)
195-
return checked((short) (long) value);
196-
if (value is ulong)
197-
return checked((short) (ulong) value);
198-
if (value is decimal)
199-
return (short) (decimal) value;
200-
if (value is bool)
201-
return (bool) value ? (short) 1 : (short) 0;
202-
return (short) value;
157+
return value switch
158+
{
159+
short shortValue => shortValue,
160+
sbyte sbyteValue => sbyteValue,
161+
byte byteValue => byteValue,
162+
ushort ushortValue => checked((short) ushortValue),
163+
int intValue => checked((short) intValue),
164+
uint uintValue => checked((short) uintValue),
165+
long longValue => checked((short) longValue),
166+
ulong ulongValue => checked((short) ulongValue),
167+
decimal decimalValue => (short) decimalValue,
168+
bool boolValue => boolValue ? (short) 1 : (short) 0,
169+
_ => (short) value,
170+
};
203171
}
204172

205173
public int GetInt32(int ordinal)
@@ -242,109 +210,77 @@ and not ColumnType.Int24 and not ColumnType.Long and not ColumnType.Longlong
242210
public long GetInt64(int ordinal)
243211
{
244212
var value = GetValue(ordinal);
245-
if (value is long)
246-
return (long) value;
247-
248-
if (value is sbyte)
249-
return (sbyte) value;
250-
if (value is byte)
251-
return (byte) value;
252-
if (value is short)
253-
return (short) value;
254-
if (value is ushort)
255-
return (ushort) value;
256-
if (value is int)
257-
return (int) value;
258-
if (value is uint)
259-
return (uint) value;
260-
if (value is ulong)
261-
return checked((long) (ulong) value);
262-
if (value is decimal)
263-
return (long) (decimal) value;
264-
if (value is bool)
265-
return (bool) value ? 1 : 0;
266-
return (long) value;
213+
return value switch
214+
{
215+
long longValue => longValue,
216+
sbyte sbyteValue => sbyteValue,
217+
byte byteValue => byteValue,
218+
short shortValue => shortValue,
219+
ushort ushortValue => ushortValue,
220+
int intValue => intValue,
221+
uint uintValue => uintValue,
222+
ulong ulongValue => checked((long) ulongValue),
223+
decimal decimalValue => (long) decimalValue,
224+
bool boolValue => boolValue ? 1 : 0,
225+
_ => (long) value,
226+
};
267227
}
268228

269229
public ushort GetUInt16(int ordinal)
270230
{
271231
var value = GetValue(ordinal);
272-
if (value is ushort)
273-
return (ushort) value;
274-
275-
if (value is sbyte)
276-
return checked((ushort) (sbyte) value);
277-
if (value is byte)
278-
return (byte) value;
279-
if (value is short)
280-
return checked((ushort) (short) value);
281-
if (value is int)
282-
return checked((ushort) (int) value);
283-
if (value is uint)
284-
return checked((ushort) (uint) value);
285-
if (value is long)
286-
return checked((ushort) (long) value);
287-
if (value is ulong)
288-
return checked((ushort) (ulong) value);
289-
if (value is decimal)
290-
return (ushort) (decimal) value;
291-
if (value is bool)
292-
return (bool) value ? (ushort) 1 : (ushort) 0;
293-
return (ushort) value;
232+
return value switch
233+
{
234+
ushort ushortValue => ushortValue,
235+
sbyte sbyteValue => checked((ushort) sbyteValue),
236+
byte byteValue => byteValue,
237+
short shortValue => checked((ushort) shortValue),
238+
int intValue => checked((ushort) intValue),
239+
uint uintValue => checked((ushort) uintValue),
240+
long longValue => checked((ushort) longValue),
241+
ulong ulongValue => checked((ushort) ulongValue),
242+
decimal decimalValue => (ushort) decimalValue,
243+
bool boolValue => boolValue ? (ushort) 1 : (ushort) 0,
244+
_ => (ushort) value,
245+
};
294246
}
295247

296248
public uint GetUInt32(int ordinal)
297249
{
298250
var value = GetValue(ordinal);
299-
if (value is uint)
300-
return (uint) value;
301-
302-
if (value is sbyte)
303-
return checked((uint) (sbyte) value);
304-
if (value is byte)
305-
return (byte) value;
306-
if (value is short)
307-
return checked((uint) (short) value);
308-
if (value is ushort)
309-
return (ushort) value;
310-
if (value is int)
311-
return checked((uint) (int) value);
312-
if (value is long)
313-
return checked((uint) (long) value);
314-
if (value is ulong)
315-
return checked((uint) (ulong) value);
316-
if (value is decimal)
317-
return (uint) (decimal) value;
318-
if (value is bool)
319-
return (bool) value ? 1u : 0;
320-
return (uint) value;
251+
return value switch
252+
{
253+
uint uintValue => uintValue,
254+
sbyte sbyteValue => checked((uint) sbyteValue),
255+
byte byteValue => byteValue,
256+
short shortValue => checked((uint) shortValue),
257+
ushort ushortValue => ushortValue,
258+
int intValue => checked((uint) intValue),
259+
long longValue => checked((uint) longValue),
260+
ulong ulongValue => checked((uint) ulongValue),
261+
decimal decimalValue => (uint) decimalValue,
262+
bool boolValue => boolValue ? 1u : 0,
263+
_ => (uint) value,
264+
};
321265
}
322266

323267
public ulong GetUInt64(int ordinal)
324268
{
325269
var value = GetValue(ordinal);
326-
if (value is ulong)
327-
return (ulong) value;
328-
329-
if (value is sbyte)
330-
return checked((ulong) (sbyte) value);
331-
if (value is byte)
332-
return (byte) value;
333-
if (value is short)
334-
return checked((ulong) (short) value);
335-
if (value is ushort)
336-
return (ushort) value;
337-
if (value is int)
338-
return checked((ulong) (int) value);
339-
if (value is uint)
340-
return (uint) value;
341-
if (value is long)
342-
return checked((ulong) (long) value);
343-
if (value is decimal)
344-
return (ulong) (decimal) value;
345-
if (value is bool)
346-
return (bool) value ? 1ul : 0;
347-
return (ulong) value;
270+
return value switch
271+
{
272+
ulong ulongValue => ulongValue,
273+
sbyte sbyteValue => checked((ulong) sbyteValue),
274+
byte byteValue => byteValue,
275+
short shortValue => checked((ulong) shortValue),
276+
ushort ushortValue => ushortValue,
277+
int intValue => checked((ulong) intValue),
278+
uint uintValue => uintValue,
279+
long longValue => checked((ulong) longValue),
280+
decimal decimalValue => (ulong) decimalValue,
281+
bool boolValue => boolValue ? 1ul : 0,
282+
_ => (ulong) value,
283+
};
348284
}
349285

350286
public DateTime GetDateTime(int ordinal)
@@ -380,44 +316,37 @@ public Stream GetStream(int ordinal)
380316
public decimal GetDecimal(int ordinal)
381317
{
382318
var value = GetValue(ordinal);
383-
if (value is decimal) // happy flow
384-
return (decimal) value;
385-
386-
if (value is double doubleValue)
387-
return (decimal) doubleValue;
388-
389-
if (value is float floatValue)
390-
return (decimal) floatValue;
391-
392-
return (decimal) value;
319+
return value switch
320+
{
321+
decimal decimalValue => decimalValue,
322+
double doubleValue => (decimal) doubleValue,
323+
float floatValue => (decimal) floatValue,
324+
_ => (decimal) value,
325+
};
393326
}
394327

395328
public double GetDouble(int ordinal)
396329
{
397330
var value = GetValue(ordinal);
398-
if (value is double) // happy flow
399-
return (double) value;
400-
401-
if (value is float floatValue)
402-
return floatValue;
403-
404-
if (value is decimal decimalValue)
405-
return (double) decimalValue;
406-
407-
return (double) value;
331+
return value switch
332+
{
333+
double doubleValue => doubleValue,
334+
float floatValue => floatValue,
335+
decimal decimalValue => (double) decimalValue,
336+
_ => (double) value,
337+
};
408338
}
409339

410340
public float GetFloat(int ordinal)
411341
{
412-
var value = GetValue(ordinal);
413-
if (value is float) // happy flow
414-
return (float) value;
415-
416342
// Loss of precision is expected, significant loss of information is not.
417343
// Use explicit range checks to guard against that.
344+
var value = GetValue(ordinal);
418345
return value switch
419346
{
420-
double doubleValue => doubleValue is >= float.MinValue and <= float.MaxValue ? (float) doubleValue : throw new InvalidCastException("The value cannot be safely cast to Single."),
347+
float floatValue => floatValue,
348+
double doubleValue when doubleValue is >= float.MinValue and <= float.MaxValue => (float) doubleValue,
349+
double _ => throw new InvalidCastException("The value cannot be safely cast to Single."),
421350
decimal decimalValue => (float) decimalValue,
422351
_ => (float) value,
423352
};

0 commit comments

Comments
 (0)