Skip to content

Commit 08a5c54

Browse files
committed
Use new pattern syntax.
1 parent 8556147 commit 08a5c54

File tree

3 files changed

+62
-198
lines changed

3 files changed

+62
-198
lines changed

src/MySqlConnector/Core/BinaryRow.cs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,16 @@ protected override void GetDataOffsets(ReadOnlySpan<byte> data, int[] dataOffset
4545
else
4646
{
4747
var columnDefinition = ResultSet.ColumnDefinitions![column];
48-
int length;
49-
if (columnDefinition.ColumnType == ColumnType.Longlong || columnDefinition.ColumnType == ColumnType.Double)
50-
length = 8;
51-
else if (columnDefinition.ColumnType == ColumnType.Long || columnDefinition.ColumnType == ColumnType.Int24 || columnDefinition.ColumnType == ColumnType.Float)
52-
length = 4;
53-
else if (columnDefinition.ColumnType == ColumnType.Short || columnDefinition.ColumnType == ColumnType.Year)
54-
length = 2;
55-
else if (columnDefinition.ColumnType == ColumnType.Tiny)
56-
length = 1;
57-
else if (columnDefinition.ColumnType == ColumnType.Date || columnDefinition.ColumnType == ColumnType.DateTime || columnDefinition.ColumnType == ColumnType.Timestamp || columnDefinition.ColumnType == ColumnType.Time)
58-
length = reader.ReadByte();
59-
else if (columnDefinition.ColumnType == ColumnType.DateTime2 || columnDefinition.ColumnType == ColumnType.NewDate || columnDefinition.ColumnType == ColumnType.Timestamp2)
60-
throw new NotSupportedException("ColumnType {0} is not supported".FormatInvariant(columnDefinition.ColumnType));
61-
else
62-
length = checked((int) reader.ReadLengthEncodedInteger());
48+
var length = columnDefinition.ColumnType switch
49+
{
50+
ColumnType.Longlong or ColumnType.Double => 8,
51+
ColumnType.Long or ColumnType.Int24 or ColumnType.Float => 4,
52+
ColumnType.Short or ColumnType.Year => 2,
53+
ColumnType.Tiny => 1,
54+
ColumnType.Date or ColumnType.DateTime or ColumnType.Timestamp or ColumnType.Time => reader.ReadByte(),
55+
ColumnType.DateTime2 or ColumnType.NewDate or ColumnType.Timestamp2 => throw new NotSupportedException("ColumnType {0} is not supported".FormatInvariant(columnDefinition.ColumnType)),
56+
_ => checked((int) reader.ReadLengthEncodedInteger()),
57+
};
6358

6459
dataLengths[column] = length;
6560
dataOffsets[column] = reader.Offset;
@@ -72,31 +67,16 @@ protected override void GetDataOffsets(ReadOnlySpan<byte> data, int[] dataOffset
7267
protected override int GetInt32Core(ReadOnlySpan<byte> data, ColumnDefinitionPayload columnDefinition)
7368
{
7469
var isUnsigned = (columnDefinition.ColumnFlags & ColumnFlags.Unsigned) != 0;
75-
switch (columnDefinition.ColumnType)
70+
return columnDefinition.ColumnType switch
7671
{
77-
case ColumnType.Tiny:
78-
return isUnsigned ? (int) data[0] : (sbyte) data[0];
79-
80-
case ColumnType.Decimal:
81-
case ColumnType.NewDecimal:
82-
return Utf8Parser.TryParse(data, out decimal decimalValue, out int bytesConsumed) && bytesConsumed == data.Length ? checked((int) decimalValue) : throw new FormatException();
83-
84-
case ColumnType.Int24:
85-
case ColumnType.Long:
86-
return isUnsigned ? checked((int) MemoryMarshal.Read<uint>(data)) : MemoryMarshal.Read<int>(data);
87-
88-
case ColumnType.Longlong:
89-
return isUnsigned ? checked((int) MemoryMarshal.Read<ulong>(data)) : checked((int) MemoryMarshal.Read<long>(data));
90-
91-
case ColumnType.Short:
92-
return isUnsigned ? (int) MemoryMarshal.Read<ushort>(data) : MemoryMarshal.Read<short>(data);
93-
94-
case ColumnType.Year:
95-
return (int) MemoryMarshal.Read<short>(data);
96-
97-
default:
98-
throw new FormatException();
99-
}
72+
ColumnType.Tiny => isUnsigned ? (int) data[0] : (sbyte) data[0],
73+
ColumnType.Decimal or ColumnType.NewDecimal => Utf8Parser.TryParse(data, out decimal decimalValue, out int bytesConsumed) && bytesConsumed == data.Length ? checked((int) decimalValue) : throw new FormatException(),
74+
ColumnType.Int24 or ColumnType.Long => isUnsigned ? checked((int) MemoryMarshal.Read<uint>(data)) : MemoryMarshal.Read<int>(data),
75+
ColumnType.Longlong => isUnsigned ? checked((int) MemoryMarshal.Read<ulong>(data)) : checked((int) MemoryMarshal.Read<long>(data)),
76+
ColumnType.Short => isUnsigned ? (int) MemoryMarshal.Read<ushort>(data) : MemoryMarshal.Read<short>(data),
77+
ColumnType.Year => MemoryMarshal.Read<short>(data),
78+
_ => throw new FormatException(),
79+
};
10080
}
10181

10282
protected override object GetValueCore(ReadOnlySpan<byte> data, ColumnDefinitionPayload columnDefinition)

src/MySqlConnector/Core/TypeMapper.cs

Lines changed: 31 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -278,156 +278,38 @@ public static MySqlDbType ConvertToMySqlDbType(ColumnDefinitionPayload columnDef
278278

279279
public static ushort ConvertToColumnTypeAndFlags(MySqlDbType dbType, MySqlGuidFormat guidFormat)
280280
{
281-
var isUnsigned = false;
282-
ColumnType columnType;
283-
switch (dbType)
281+
var isUnsigned = dbType == MySqlDbType.UByte || dbType == MySqlDbType.UInt16 ||
282+
dbType == MySqlDbType.UInt24 || dbType == MySqlDbType.UInt32 || dbType == MySqlDbType.UInt64;
283+
var columnType = dbType switch
284284
{
285-
case MySqlDbType.Bool:
286-
case MySqlDbType.Byte:
287-
columnType = ColumnType.Tiny;
288-
break;
289-
290-
case MySqlDbType.UByte:
291-
columnType = ColumnType.Tiny;
292-
isUnsigned = true;
293-
break;
294-
295-
case MySqlDbType.Int16:
296-
columnType = ColumnType.Short;
297-
break;
298-
299-
case MySqlDbType.UInt16:
300-
columnType = ColumnType.Short;
301-
isUnsigned = true;
302-
break;
303-
304-
case MySqlDbType.Int24:
305-
columnType = ColumnType.Int24;
306-
break;
307-
308-
case MySqlDbType.UInt24:
309-
columnType = ColumnType.Int24;
310-
isUnsigned = true;
311-
break;
312-
313-
case MySqlDbType.Int32:
314-
columnType = ColumnType.Long;
315-
break;
316-
317-
case MySqlDbType.UInt32:
318-
columnType = ColumnType.Long;
319-
isUnsigned = true;
320-
break;
321-
322-
case MySqlDbType.Int64:
323-
columnType = ColumnType.Longlong;
324-
break;
325-
326-
case MySqlDbType.UInt64:
327-
columnType = ColumnType.Longlong;
328-
isUnsigned = true;
329-
break;
330-
331-
case MySqlDbType.Bit:
332-
columnType = ColumnType.Bit;
333-
break;
334-
335-
case MySqlDbType.Guid:
336-
if (guidFormat == MySqlGuidFormat.Char36 || guidFormat == MySqlGuidFormat.Char32)
337-
columnType = ColumnType.String;
338-
else
339-
columnType = ColumnType.Blob;
340-
break;
341-
342-
case MySqlDbType.Enum:
343-
case MySqlDbType.Set:
344-
columnType = ColumnType.String;
345-
break;
346-
347-
case MySqlDbType.Binary:
348-
case MySqlDbType.String:
349-
columnType = ColumnType.String;
350-
break;
351-
352-
case MySqlDbType.VarBinary:
353-
case MySqlDbType.VarChar:
354-
case MySqlDbType.VarString:
355-
columnType = ColumnType.VarString;
356-
break;
357-
358-
case MySqlDbType.TinyBlob:
359-
case MySqlDbType.TinyText:
360-
columnType = ColumnType.TinyBlob;
361-
break;
362-
363-
case MySqlDbType.Blob:
364-
case MySqlDbType.Text:
365-
columnType = ColumnType.Blob;
366-
break;
367-
368-
case MySqlDbType.MediumBlob:
369-
case MySqlDbType.MediumText:
370-
columnType = ColumnType.MediumBlob;
371-
break;
372-
373-
case MySqlDbType.LongBlob:
374-
case MySqlDbType.LongText:
375-
columnType = ColumnType.LongBlob;
376-
break;
377-
378-
case MySqlDbType.JSON:
379-
columnType = ColumnType.Json; // TODO: test
380-
break;
381-
382-
case MySqlDbType.Date:
383-
case MySqlDbType.Newdate:
384-
columnType = ColumnType.Date;
385-
break;
386-
387-
case MySqlDbType.DateTime:
388-
columnType = ColumnType.DateTime;
389-
break;
390-
391-
case MySqlDbType.Timestamp:
392-
columnType = ColumnType.Timestamp;
393-
break;
394-
395-
case MySqlDbType.Time:
396-
columnType = ColumnType.Time;
397-
break;
398-
399-
case MySqlDbType.Year:
400-
columnType = ColumnType.Year;
401-
break;
402-
403-
case MySqlDbType.Float:
404-
columnType = ColumnType.Float;
405-
break;
406-
407-
case MySqlDbType.Double:
408-
columnType = ColumnType.Double;
409-
break;
410-
411-
case MySqlDbType.Decimal:
412-
columnType = ColumnType.Decimal;
413-
break;
414-
415-
case MySqlDbType.NewDecimal:
416-
columnType = ColumnType.NewDecimal;
417-
break;
418-
419-
case MySqlDbType.Geometry:
420-
columnType = ColumnType.Geometry;
421-
break;
422-
423-
case MySqlDbType.Null:
424-
columnType = ColumnType.Null;
425-
break;
426-
427-
default:
428-
throw new NotImplementedException("ConvertToColumnTypeAndFlags for {0} is not implemented".FormatInvariant(dbType));
429-
}
430-
285+
MySqlDbType.Bool or MySqlDbType.Byte or MySqlDbType.UByte => ColumnType.Tiny,
286+
MySqlDbType.Int16 or MySqlDbType.UInt16 => ColumnType.Short,
287+
MySqlDbType.Int24 or MySqlDbType.UInt24 => ColumnType.Int24,
288+
MySqlDbType.Int32 or MySqlDbType.UInt32 => ColumnType.Long,
289+
MySqlDbType.Int64 or MySqlDbType.UInt64 => ColumnType.Longlong,
290+
MySqlDbType.Bit => ColumnType.Bit,
291+
MySqlDbType.Guid => (guidFormat == MySqlGuidFormat.Char36 || guidFormat == MySqlGuidFormat.Char32) ? ColumnType.String : ColumnType.Blob,
292+
MySqlDbType.Enum or MySqlDbType.Set => ColumnType.String,
293+
MySqlDbType.Binary or MySqlDbType.String => ColumnType.String,
294+
MySqlDbType.VarBinary or MySqlDbType.VarChar or MySqlDbType.VarString => ColumnType.VarString,
295+
MySqlDbType.TinyBlob or MySqlDbType.TinyText => ColumnType.TinyBlob,
296+
MySqlDbType.Blob or MySqlDbType.Text => ColumnType.Blob,
297+
MySqlDbType.MediumBlob or MySqlDbType.MediumText => ColumnType.MediumBlob,
298+
MySqlDbType.LongBlob or MySqlDbType.LongText => ColumnType.LongBlob,
299+
MySqlDbType.JSON => ColumnType.Json, // TODO: test
300+
MySqlDbType.Date or MySqlDbType.Newdate => ColumnType.Date,
301+
MySqlDbType.DateTime => ColumnType.DateTime,
302+
MySqlDbType.Timestamp => ColumnType.Timestamp,
303+
MySqlDbType.Time => ColumnType.Time,
304+
MySqlDbType.Year => ColumnType.Year,
305+
MySqlDbType.Float => ColumnType.Float,
306+
MySqlDbType.Double => ColumnType.Double,
307+
MySqlDbType.Decimal => ColumnType.Decimal,
308+
MySqlDbType.NewDecimal => ColumnType.NewDecimal,
309+
MySqlDbType.Geometry => ColumnType.Geometry,
310+
MySqlDbType.Null => ColumnType.Null,
311+
_ => throw new NotImplementedException("ConvertToColumnTypeAndFlags for {0} is not implemented".FormatInvariant(dbType)),
312+
};
431313
return (ushort) ((byte) columnType | (isUnsigned ? 0x8000 : 0));
432314
}
433315

src/MySqlConnector/Protocol/Serialization/ByteBufferWriter.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,23 +219,25 @@ internal static class ByteBufferWriterExtensions
219219
{
220220
public static void WriteLengthEncodedInteger(this ByteBufferWriter writer, ulong value)
221221
{
222-
if (value < 251)
222+
switch (value)
223223
{
224+
case < 251:
224225
writer.Write((byte) value);
225-
}
226-
else if (value < 65536)
227-
{
226+
break;
227+
228+
case < 65536:
228229
writer.Write((byte) 0xfc);
229230
writer.Write((ushort) value);
230-
}
231-
else if (value < 16777216)
232-
{
231+
break;
232+
233+
case < 16777216:
233234
writer.Write((uint) ((value << 8) | 0xfd));
234-
}
235-
else
236-
{
235+
break;
236+
237+
default:
237238
writer.Write((byte) 0xfe);
238239
writer.Write(value);
240+
break;
239241
}
240242
}
241243

0 commit comments

Comments
 (0)