Skip to content

Commit f3da67f

Browse files
committed
imporved the decoding of numeric fields
1 parent bfa3b92 commit f3da67f

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

src/SciSharp.MySQL.Replication/SequenceReaderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ internal static string ReadString(ref this SequenceReader<byte> reader, Encoding
133133
}
134134

135135
/// <summary>
136-
/// Reads a fixed-length integer from the binary stream.
136+
/// Reads a fixed-length unsigned integer from the binary stream.
137137
/// </summary>
138138
/// <param name="reader">The sequence reader.</param>
139139
/// <param name="length">The number of bytes to read (1-4).</param>

src/SciSharp.MySQL.Replication/Types/Int24Type.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Buffers;
3+
using System.Buffers.Binary;
34

45
namespace SciSharp.MySQL.Replication.Types
56
{
@@ -19,7 +20,27 @@ class Int24Type : IMySQLDataType
1920
/// <returns>An integer representing the MySQL MEDIUMINT value.</returns>
2021
public object ReadValue(ref SequenceReader<byte> reader, ColumnMetadata columnMetadata)
2122
{
22-
return reader.ReadInteger(3);
23+
Span<byte> buffer = stackalloc byte[4];
24+
25+
reader.TryCopyTo(buffer.Slice(0, 3));
26+
reader.Advance(3);
27+
28+
buffer[3] = 0;
29+
30+
var signalByte = buffer[2];
31+
32+
if (!columnMetadata.IsUnsigned && (signalByte & 0x80) == 0x80) // Negative value
33+
{
34+
buffer[3] = 0xFF; // Set the sign bit for negative values
35+
}
36+
else
37+
{
38+
buffer[3] = 0x00; // Set the sign bit for positive values
39+
}
40+
41+
return columnMetadata.IsUnsigned
42+
? BinaryPrimitives.ReadUInt32LittleEndian(buffer)
43+
: BinaryPrimitives.ReadInt32LittleEndian(buffer);
2344
}
2445
}
2546
}

src/SciSharp.MySQL.Replication/Types/LongType.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading.Tasks;
33
using System.Collections.Generic;
44
using System.Buffers;
5+
using System.Buffers.Binary;
56

67
namespace SciSharp.MySQL.Replication.Types
78
{
@@ -21,7 +22,14 @@ class LongType : IMySQLDataType
2122
/// <returns>An integer representing the MySQL INT value.</returns>
2223
public object ReadValue(ref SequenceReader<byte> reader, ColumnMetadata columnMetadata)
2324
{
24-
return reader.ReadInteger(4);
25+
Span<byte> buffer = stackalloc byte[sizeof(int)];
26+
27+
reader.TryCopyTo(buffer);
28+
reader.Advance(sizeof(int));
29+
30+
return columnMetadata.IsUnsigned
31+
? BinaryPrimitives.ReadUInt32LittleEndian(buffer)
32+
: BinaryPrimitives.ReadInt32LittleEndian(buffer);
2533
}
2634
}
2735
}

src/SciSharp.MySQL.Replication/Types/ShortType.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading.Tasks;
33
using System.Collections.Generic;
44
using System.Buffers;
5+
using System.Buffers.Binary;
56

67
namespace SciSharp.MySQL.Replication.Types
78
{
@@ -21,7 +22,14 @@ class ShortType : IMySQLDataType
2122
/// <returns>A short value representing the MySQL SMALLINT value.</returns>
2223
public object ReadValue(ref SequenceReader<byte> reader, ColumnMetadata columnMetadata)
2324
{
24-
return (short)reader.ReadInteger(2);
25+
Span<byte> buffer = stackalloc byte[sizeof(short)];
26+
27+
reader.TryCopyTo(buffer);
28+
reader.Advance(sizeof(short));
29+
30+
return columnMetadata.IsUnsigned
31+
? (ushort)BinaryPrimitives.ReadUInt16LittleEndian(buffer)
32+
: (short)BinaryPrimitives.ReadInt16LittleEndian(buffer);
2533
}
2634
}
2735
}

src/SciSharp.MySQL.Replication/Types/TinyType.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ class TinyType : IMySQLDataType
2020
public object ReadValue(ref SequenceReader<byte> reader, ColumnMetadata columnMetadata)
2121
{
2222
reader.TryRead(out byte x);
23-
return (SByte)x;
23+
return columnMetadata.IsUnsigned
24+
? x
25+
: (sbyte)x;
2426
}
2527
}
2628
}

0 commit comments

Comments
 (0)